Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class ShaderMaterial
- Object
- 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 types
enum ShaderMaterial.Hook | An input of the engine’s shader that a graph can replace. |
Fields
public static final int MAX_UNIFORMS = 16 | The most uniforms one material can declare. |
public static final int MAX_TEXTURES = 4 | The most textures one material can declare. |
public static final String INSTANCE = "cn1Instance" | The uniform GraphicsDevice.drawInstanced writes before each copy: x is the copy’s index, y a stable random number in 0 to 1 for it. |
public static final String INSTANCE_COLOR = "cn1InstanceColor" | The uniform holding the colour GraphicsDevice.drawInstanced gives each copy, white when it gives none. |
Constructors
public ShaderMaterial() |
Methods
Inherited methods
Field details
MAX_UNIFORMS
public static final int MAX_UNIFORMS = 16The most uniforms one material can declare.
MAX_TEXTURES
public static final int MAX_TEXTURES = 4The most textures one material can declare.
INSTANCE
public static final String INSTANCE = "cn1Instance"The uniform
GraphicsDevice.drawInstanced writes before each copy:
x is the copy’s index, y a stable random number in 0 to 1 for it.INSTANCE_COLOR
public static final String INSTANCE_COLOR = "cn1InstanceColor"The uniform holding the colour
GraphicsDevice.drawInstanced gives
each copy, white when it gives none.Constructor details
ShaderMaterial
public ShaderMaterial()Method details
set
public ShaderMaterial set(ShaderMaterial.Hook hook, ShaderNode graph)Sets the graph for a hook, or null to use the engine’s own input.
Parameters
hookShaderMaterial.Hook- the input to replace
graphShaderNode- a node of the hook’s type, or null
Returns
this material
get
public ShaderNode get(ShaderMaterial.Hook hook)Returns the graph set for a hook.
Parameters
hookShaderMaterial.Hook- the input
Returns
the graph, or null
declareUniform
public ShaderMaterial declareUniform(String name, ShaderType type)Declares a uniform the graphs read with
ShaderNode.uniform.
Declaring a name twice with the same type does nothing.Parameters
nameString- the uniform name
typeShaderType- its type
Returns
this material
setUniform
public ShaderMaterial setUniform(String name, float... values)Sets a uniform’s value. Pass as many floats as its type has
components; missing ones are zero.
Parameters
nameString- a declared uniform
valuesfloat...- its components
Returns
this material
setColorUniform
public ShaderMaterial setColorUniform(String name, int rgb)Sets a
VEC3 uniform from a packed 0xRRGGBB colour.Parameters
nameString- a declared uniform
rgbint- the colour
Returns
this material
declareTexture
public ShaderMaterial declareTexture(String name, Texture texture)Declares a texture the graphs read with
ShaderNode.sample.Parameters
nameString- the texture name
textureTexture- the texture, or null to bind it later with
setTexture
Returns
this material
setTexture
public ShaderMaterial setTexture(String name, Texture texture)Binds a declared texture.
Parameters
nameString- the declared name
textureTexture- the texture
Returns
this material
getUniformSlot
public int getUniformSlot(String name)Returns the slot of a uniform: its four floats start at
slot * 4 in
getUniformValues. For backends.Parameters
nameString- the uniform name
Returns
the slot, or -1 if it is not declared
getUniformType
public ShaderType getUniformType(String name)Returns the type of a declared uniform.
Parameters
nameString- 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() * 4
floats of it as one vec4 array. Do not modify it.Returns
the shared value array
getUniformName
public String getUniformName(int slot)Returns the name of the uniform in a slot.
Parameters
slotint- 0 to
getUniformCount() - 1
Returns
the name
getUniformType
public ShaderType getUniformType(int slot)Returns the type of the uniform in a slot.
Parameters
slotint- 0 to
getUniformCount() - 1
Returns
the type
getTextureName
public String getTextureName(int slot)Returns the name of the texture in a slot.
Parameters
slotint- 0 to
getTextureCount() - 1
Returns
the name
getTextureSlot
public int getTextureSlot(String name)Returns the slot of a texture, its index among the material’s custom
textures. For backends.
Parameters
nameString- 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
public Texture getTexture(int slot)Returns the texture bound to a slot.
Parameters
slotint- 0 to
getTextureCount() - 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
public String 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