Class ShaderMaterial
Custom shading for a com.codename1.gpu.Material: a set of ShaderNode
graphs, each replacing one input of the engine's own shader. What a hook
does not replace keeps working as before, so a custom surface is still lit,
shadowed, fogged and tone mapped like every other.
import com.codename1.gpu.Material;
import com.codename1.gpu.shader.*;
Material material = new Material(Material.Type.PBR);
ShaderMaterial waves = new ShaderMaterial()
.declareUniform("amp", ShaderType.FLOAT)
.set(ShaderMaterial.Hook.VERTEX_OFFSET, ShaderNode.vec(ShaderNode.f(0f),
ShaderNode.sin(ShaderNode.position().x().add(ShaderNode.time()))
.mul(ShaderNode.uniform("amp", ShaderType.FLOAT)), ShaderNode.f(0f)))
.setUniform("amp", 0.3f);
material.setShader(waves);
Uniform values and textures are per material and may change every frame without recompiling anything. Changing a graph, or declaring a uniform or texture, makes the backend build a new program the next time the material is drawn, so do that once, not per frame.
Limits, the same on every backend: 16 uniforms and 4 textures per material.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAn input of the engine's shader that a graph can replace. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe uniformGraphicsDevice.drawInstancedwrites before each copy: x is the copy's index, y a stable random number in 0 to 1 for it.static final StringThe uniform holding the colourGraphicsDevice.drawInstancedgives each copy, white when it gives none.static final intThe most textures one material can declare.static final intThe most uniforms one material can declare. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondeclareTexture(String name, Texture texture) Declares a texture the graphs read withShaderNode.sample.declareUniform(String name, ShaderType type) Declares a uniform the graphs read withShaderNode.uniform.get(ShaderMaterial.Hook hook) Returns the graph set for a hook.Returns a string identifying the generated program: equal for two materials whose graphs, uniforms and textures produce the same source.getTexture(int slot) Returns the texture bound to a slot.intReturns the number of declared textures.getTextureName(int slot) Returns the name of the texture in a slot.intgetTextureSlot(String name) Returns the slot of a texture, its index among the material's custom textures.intReturns the number of declared uniforms.getUniformName(int slot) Returns the name of the uniform in a slot.intgetUniformSlot(String name) Returns the slot of a uniform: its four floats start atslot * 4ingetUniformValues.getUniformType(int slot) Returns the type of the uniform in a slot.getUniformType(String name) Returns the type of a declared uniform.float[]Returns the uniform values packed four floats per slot, in declaration order.intReturns a number that changes whenever the generated program would.set(ShaderMaterial.Hook hook, ShaderNode graph) Sets the graph for a hook, or null to use the engine's own input.setColorUniform(String name, int rgb) Sets aVEC3uniform from a packed0xRRGGBBcolour.setTexture(String name, Texture texture) Binds a declared texture.setUniform(String name, float... values) Sets a uniform's value.
-
Field Details
-
MAX_UNIFORMS
public static final int MAX_UNIFORMSThe most uniforms one material can declare.- See Also:
-
MAX_TEXTURES
public static final int MAX_TEXTURESThe most textures one material can declare.- See Also:
-
INSTANCE
The uniformGraphicsDevice.drawInstancedwrites before each copy: x is the copy's index, y a stable random number in 0 to 1 for it.- See Also:
-
INSTANCE_COLOR
The uniform holding the colourGraphicsDevice.drawInstancedgives each copy, white when it gives none.- See Also:
-
-
Constructor Details
-
ShaderMaterial
public ShaderMaterial()
-
-
Method Details
-
set
Sets the graph for a hook, or null to use the engine's own input.
Parameters
-
hook: the input to replace -
graph: a node of the hook's type, or null
Returns
this material
-
-
get
Returns the graph set for a hook.
Parameters
hook: the input
Returns
the graph, or null
-
declareUniform
Declares a uniform the graphs read with
ShaderNode.uniform. Declaring a name twice with the same type does nothing.Parameters
-
name: the uniform name -
type: its type
Returns
this material
-
-
setUniform
Sets a uniform's value. Pass as many floats as its type has components; missing ones are zero.
Parameters
-
name: a declared uniform -
values: its components
Returns
this material
-
-
setColorUniform
Sets a
VEC3uniform from a packed0xRRGGBBcolour.Parameters
-
name: a declared uniform -
rgb: the colour
Returns
this material
-
-
declareTexture
Declares a texture the graphs read with
ShaderNode.sample.Parameters
-
name: the texture name -
texture: the texture, or null to bind it later withsetTexture
Returns
this material
-
-
setTexture
Binds a declared texture.
Parameters
-
name: the declared name -
texture: the texture
Returns
this material
-
-
getUniformSlot
Returns the slot of a uniform: its four floats start at
slot * 4ingetUniformValues. For backends.Parameters
name: the uniform name
Returns
the slot, or -1 if it is not declared
-
getUniformType
Returns the type of a declared uniform.
Parameters
name: the uniform name
Returns
its type, or null if it is not declared
-
getUniformCount
public int getUniformCount()Returns the number of declared uniforms.
Returns
the count
-
getUniformValues
public float[] getUniformValues()Returns the uniform values packed four floats per slot, in declaration order. For backends, which upload
getUniformCount() * 4floats of it as one vec4 array. Do not modify it.Returns
the shared value array
-
getUniformName
Returns the name of the uniform in a slot.
Parameters
slot: 0 togetUniformCount() - 1
Returns
the name
-
getUniformType
Returns the type of the uniform in a slot.
Parameters
slot: 0 togetUniformCount() - 1
Returns
the type
-
getTextureName
Returns the name of the texture in a slot.
Parameters
slot: 0 togetTextureCount() - 1
Returns
the name
-
getTextureSlot
Returns the slot of a texture, its index among the material's custom textures. For backends.
Parameters
name: the texture name
Returns
the slot, or -1 if it is not declared
-
getTextureCount
public int getTextureCount()Returns the number of declared textures.
Returns
the count
-
getTexture
Returns the texture bound to a slot.
Parameters
slot: 0 togetTextureCount() - 1
Returns
the texture, or null when none is bound
-
getVersion
public int getVersion()Returns a number that changes whenever the generated program would.
Returns
the version
-
getShaderKey
Returns a string identifying the generated program: equal for two materials whose graphs, uniforms and textures produce the same source. Backends add it to their program cache key.
Returns
the key
-