Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class ShaderMaterial

  1. Object
  2. 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.HookAn input of the engine’s shader that a graph can replace.

Fields

public static final int MAX_UNIFORMS = 16The most uniforms one material can declare.
public static final int MAX_TEXTURES = 4The 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

public ShaderMaterial set(ShaderMaterial.Hook hook, ShaderNode graph)Sets the graph for a hook, or null to use the engine’s own input.
public ShaderNode get(ShaderMaterial.Hook hook)Returns the graph set for a hook.
public ShaderMaterial declareUniform(String name, ShaderType type)Declares a uniform the graphs read with ShaderNode.uniform.
public ShaderMaterial setUniform(String name, float... values)Sets a uniform’s value.
public ShaderMaterial setColorUniform(String name, int rgb)Sets a VEC3 uniform from a packed 0xRRGGBB colour.
public ShaderMaterial declareTexture(String name, Texture texture)Declares a texture the graphs read with ShaderNode.sample.
public ShaderMaterial setTexture(String name, Texture texture)Binds a declared texture.
public int getUniformSlot(String name)Returns the slot of a uniform: its four floats start at slot * 4 in getUniformValues.
public ShaderType getUniformType(String name)Returns the type of a declared uniform.
public int getUniformCount()Returns the number of declared uniforms.
public float[] getUniformValues()Returns the uniform values packed four floats per slot, in declaration order.
public String getUniformName(int slot)Returns the name of the uniform in a slot.
public ShaderType getUniformType(int slot)Returns the type of the uniform in a slot.
public String getTextureName(int slot)Returns the name of the texture in a slot.
public int getTextureSlot(String name)Returns the slot of a texture, its index among the material’s custom textures.
public int getTextureCount()Returns the number of declared textures.
public Texture getTexture(int slot)Returns the texture bound to a slot.
public int getVersion()Returns a number that changes whenever the generated program would.
public String getShaderKey()Returns a string identifying the generated program: equal for two materials whose graphs, uniforms and textures produce the same source.

Inherited methods

Field details

MAX_UNIFORMS

public static final int MAX_UNIFORMS = 16
The most uniforms one material can declare.

MAX_TEXTURES

public static final int MAX_TEXTURES = 4
The 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

hook ShaderMaterial.Hook
the input to replace
graph ShaderNode
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

hook ShaderMaterial.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

name String
the uniform name
type ShaderType
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

name String
a declared uniform
values float...
its components

Returns

this material

setColorUniform

public ShaderMaterial setColorUniform(String name, int rgb)
Sets a VEC3 uniform from a packed 0xRRGGBB colour.

Parameters

name String
a declared uniform
rgb int
the colour

Returns

this material

declareTexture

public ShaderMaterial declareTexture(String name, Texture texture)
Declares a texture the graphs read with ShaderNode.sample.

Parameters

name String
the texture name
texture Texture
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

name String
the declared name
texture Texture
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

name String
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

name String
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

slot int
0 to getUniformCount() - 1

Returns

the name

getUniformType

public ShaderType getUniformType(int slot)
Returns the type of the uniform in a slot.

Parameters

slot int
0 to getUniformCount() - 1

Returns

the type

getTextureName

public String getTextureName(int slot)
Returns the name of the texture in a slot.

Parameters

slot int
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

name String
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

slot int
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