Class ConstantVariableShader

A shader part that generates constant variable functions for both vertex and pixel shaders. This class creates GLSL/WGSL functions that output constant values of specified composition and component types.

const constantShader = new ConstantVariableShader(
'getConstantColor',
CompositionType.Vec3,
ComponentType.Float
);
constantShader.setConstantValue(Vector3.fromCopyArray([1.0, 0.5, 0.0]));

Hierarchy (view full)

Constructors

Properties

__webglResourceRepository?: WebGLResourceRepository = ...
__instance: CommonShaderPart

Accessors

  • get attributeCompositions(): CompositionTypeEnum[]
  • Returns the attribute compositions required by this shader part. Since this is a constant variable shader that doesn't use any vertex attributes, it returns an empty array.

    Returns CompositionTypeEnum[]

    An empty array as no attribute compositions are needed for constant values.

  • get attributeNames(): AttributeNames
  • Returns the attribute names required by this shader part. Since this is a constant variable shader that doesn't use any vertex attributes, it returns an empty array.

    Returns AttributeNames

    An empty array as no vertex attributes are needed for constant values.

  • get attributeSemantics(): VertexAttributeEnum[]
  • Returns the vertex attribute semantics required by this shader part. Since this is a constant variable shader that doesn't use any vertex attributes, it returns an empty array.

    Returns VertexAttributeEnum[]

    An empty array as no vertex attribute semantics are needed for constant values.

  • get pixelShaderDefinitions(): string
  • Generates the pixel shader function definition that outputs the constant value. This is identical to the vertex shader definition as constant values are the same regardless of shader stage.

    Returns string

    The GLSL or WGSL function definition string for the pixel shader, depending on the current process approach (WebGL or WebGPU).

    Generated GLSL:

    void getConstantColor(out vec3 outValue) {
      outValue = vec3(1.0, 0.5, 0.0);
    }
    

    Generated WGSL:

    fn getConstantColor(outValue: ptr>) {
      *outValue = vec3(1.0, 0.5, 0.0);
    }
    
  • get vertexShaderDefinitions(): string
  • Generates the vertex shader function definition that outputs the constant value. The generated function takes an output parameter and assigns the constant value to it.

    Returns string

    The GLSL or WGSL function definition string for the vertex shader, depending on the current process approach (WebGL or WebGPU).

    Generated GLSL:

    void getConstantColor(out vec3 outValue) {
      outValue = vec3(1.0, 0.5, 0.0);
    }
    

    Generated WGSL:

    fn getConstantColor(outValue: ptr>) {
      *outValue = vec3(1.0, 0.5, 0.0);
    }
    

Methods

  • Sets the constant value that will be output by the generated shader function. The value is converted to the appropriate GLSL/WGSL string representation based on the component type.

    Parameters

    • value: IVector

      The vector value to be used as the constant. The number of components used depends on the composition type specified in the constructor.

    Returns void

    // For a Vec3 float constant
    shader.setConstantValue(Vector3.fromCopyArray([1.0, 0.5, 0.0]));

    // For a scalar int constant
    shader.setConstantValue(Scalar.fromCopyNumber(42));

    // For a boolean constant
    shader.setConstantValue(Scalar.fromCopyNumber(1)); // true
  • Generates variable assignment statement with proper type declaration. Creates appropriate syntax for both WebGL (GLSL) and WebGPU (WGSL) based on the current process approach.

    Parameters

    • varName: string

      The name of the variable to declare

    • inputSocket: Socket<string, CompositionTypeEnum, ComponentTypeEnum, SocketDefaultValue>

      The socket containing type and default value information

    Returns string

    The variable assignment statement string

  • Generates varying variable assignment statement for vertex shaders. Creates code to write varying variables that will be passed to fragment shader.

    Parameters

    • inputNode: AbstractShaderNode

      The shader node that provides the varying variable

    • varNames: string[]

      Array of variable names to assign

    • j: number

      Index of the current variable in the varNames array

    Returns string

    The varying variable assignment statement string for vertex shader

  • Generates the main function beginning code for vertex or fragment shaders. Handles differences between WebGL (GLSL) and WebGPU (WGSL) shader languages.

    Parameters

    • isVertexStage: boolean

      True if generating code for vertex shader, false for fragment shader

    Returns string

    The shader code string for the main function beginning

  • Generates the main function ending code for vertex or fragment shaders. Handles differences between WebGL (GLSL) and WebGPU (WGSL) shader languages.

    Parameters

    • isVertexStage: boolean

      True if generating code for vertex shader, false for fragment shader

    Returns "\n return output;\n}\n" | "\n return rt0;\n}\n" | "\n}\n "

    The shader code string for the main function ending

  • Generates fragment/pixel shader prerequisites including definitions and varying variable declarations. Creates appropriate code for both WebGL (GLSL) and WebGPU (WGSL) based on the current process approach.

    Parameters

    • shaderNodes: AbstractShaderNode[]

      Array of shader nodes used to generate varying variables for WebGPU

    Returns string

    The complete fragment shader prerequisites code string

  • Generates vertex shader prerequisites including definitions, vertex inputs, and uniform declarations. Creates appropriate code for both WebGL (GLSL) and WebGPU (WGSL) based on the current process approach.

    Parameters

    • shaderNodes: AbstractShaderNode[]

      Array of shader nodes used to generate varying variables for WebGPU

    Returns string

    The complete vertex shader prerequisites code string