Class ShaderMaterial

java.lang.Object
com.codename1.gpu.shader.ShaderMaterial

public final class ShaderMaterial extends Object

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.

  • Field Details

    • MAX_UNIFORMS

      public static final int MAX_UNIFORMS
      The most uniforms one material can declare.
      See Also:
    • MAX_TEXTURES

      public static final int MAX_TEXTURES
      The most textures one material can declare.
      See Also:
    • INSTANCE

      public static final String INSTANCE
      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.
      See Also:
    • INSTANCE_COLOR

      public static final String INSTANCE_COLOR
      The uniform holding the colour GraphicsDevice.drawInstanced gives each copy, white when it gives none.
      See Also:
  • 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: the input to replace

      • graph: 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: 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: the uniform name

      • type: 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: a declared uniform

      • values: its components

      Returns

      this material

    • setColorUniform

      public ShaderMaterial setColorUniform(String name, int rgb)

      Sets a VEC3 uniform from a packed 0xRRGGBB colour.

      Parameters
      • name: a declared uniform

      • rgb: the colour

      Returns

      this material

    • declareTexture

      public ShaderMaterial declareTexture(String name, Texture texture)

      Declares a texture the graphs read with ShaderNode.sample.

      Parameters
      • name: the texture name

      • 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: the declared name

      • 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: 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: 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: 0 to getUniformCount() - 1
      Returns

      the name

    • getUniformType

      public ShaderType getUniformType(int slot)

      Returns the type of the uniform in a slot.

      Parameters
      • slot: 0 to getUniformCount() - 1
      Returns

      the type

    • getTextureName

      public String getTextureName(int slot)

      Returns the name of the texture in a slot.

      Parameters
      • slot: 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: 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: 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