Class ShaderEvaluator
Evaluates ShaderNode graphs on the CPU, with the same results the GPU
computes from the same graph. Uses:
- Gameplay that follows a shader. A boat that rides the waves a vertex offset draws evaluates the same graph at its own position, so it floats on the water the player sees rather than on an approximation.
- The software renderer, which has no shader compiler.
- Tests of a graph's values.
import com.codename1.gpu.shader.*;
import com.codename1.gpu.shaders.Water;
ShaderMaterial water = Water.ocean();
float now = 2.5f, boatX = 3f, boatZ = -4f;
ShaderEvaluator ev = new ShaderEvaluator(water)
.setInput("time", now)
.setInput("position", boatX, 0f, boatZ);
float lift = ev.evaluate(water.get(ShaderMaterial.Hook.VERTEX_OFFSET))[1];
Inputs are the names of the ShaderNode built ins (position, normal,
uv, worldPos, worldNormal, viewDir, cameraPos, sunDir,
sunColor, time, baseColor); an input never set reads as zero.
Uniforms are read from the material at each evaluation, so a changed
value needs nothing else. A graph is compiled to a flat program the first
time it is evaluated and kept; evaluating it again allocates nothing
except through evaluate(ShaderNode), which returns a new array.
An evaluator is not thread safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReads a material texture for the evaluator. -
Constructor Summary
ConstructorsConstructorDescriptionShaderEvaluator(ShaderMaterial material) Creates an evaluator for the graphs of a material. -
Method Summary
Modifier and TypeMethodDescriptionfloat[]evaluate(ShaderNode node) Evaluates a graph.voidevaluate(ShaderNode node, float[] out) Evaluates a graph into an array, without allocating.floatevaluateFloat(ShaderNode node) Evaluates aFLOATgraph.Sets a built-in input.Sets what reads the material's textures.
-
Constructor Details
-
ShaderEvaluator
Creates an evaluator for the graphs of a material.
Parameters
material: the material whose uniforms and textures the graphs read, or null when they read none
-
-
Method Details
-
setInput
Sets a built-in input.
Parameters
-
name: the built-in's name, such asworldPos -
values: its components; missing ones are zero
Returns
this evaluator
-
-
setTextureSampler
Sets what reads the material's textures. Without one every texture reads as opaque white.
Parameters
sampler: the sampler, or null
Returns
this evaluator
-
evaluate
Evaluates a graph.
Parameters
node: the graph
Returns
a new array of four floats; components past the node's type are zero
-
evaluate
Evaluates a graph into an array, without allocating.
Parameters
-
node: the graph -
out: receives the value; at least as long as the node's type, and up to four floats are written
-
-
evaluateFloat
Evaluates a
FLOATgraph.Parameters
node: the graph
Returns
its value
-