Engine API Reference - v2.20.0-beta.0
    Preparing search index...

    Class ShaderUtils

    Utility class for creating shaders. Provides a higher-level API over the Shader constructor, handling cross-API concerns such as GLSL/WGSL selection and translation, shader caching, include and define resolution, and attaching commonly used extensions and precision qualifiers.

    Index

    Methods

    Methods

    • Creates a shader. When the active graphics device is WebGL, the provided GLSL vertex and fragment source code is used. For WebGPU, if WGSL vertex and fragment source code is supplied, it is used directly; otherwise, the system automatically translates the provided GLSL code into WGSL. In the case of GLSL shaders, additional blocks are appended to both the vertex and fragment source code to support extended features and maintain compatibility. These additions include the shader version declaration, precision qualifiers, and commonly used extensions, and therefore should be excluded from the user-supplied GLSL source. Note: The shader has access to all registered shader chunks via the #include directive. Any provided includes will be applied as overrides on top of those.

      Parameters

      • device: GraphicsDevice

        The graphics device.

      • options: {
            attributes: {};
            fragmentChunk?: string;
            fragmentDefines?: Map<string, string>;
            fragmentGLSL?: string;
            fragmentIncludes?: Map<string, string>;
            fragmentOutputTypes?: string | string[];
            fragmentWGSL?: string;
            uniqueName: string;
            useTransformFeedback?: boolean;
            vertexChunk?: string;
            vertexDefines?: Map<string, string>;
            vertexGLSL?: string;
            vertexIncludes?: Map<string, string>;
            vertexWGSL?: string;
        }

        Object for passing optional arguments.

        • attributes: {}

          Object detailing the mapping of vertex shader attribute names to semantics SEMANTIC_*. This enables the engine to match vertex buffer data to the shader attributes.

        • OptionalfragmentChunk?: string

          The name of the fragment shader chunk to use.

        • OptionalfragmentDefines?: Map<string, string>

          A map containing key-value pairs of define names and their values. These are used for resolving #ifdef style of directives in the fragment code.

        • OptionalfragmentGLSL?: string

          The fragment shader code in GLSL. Ignored if fragmentChunk is provided.

        • OptionalfragmentIncludes?: Map<string, string>

          A map containing key-value pairs of include names and their content. These are used for resolving #include directives in the fragment shader source.

        • OptionalfragmentOutputTypes?: string | string[]

          Fragment shader output types, which default to vec4. Passing a string will set the output type for all color attachments. Passing an array will set the output type for each color attachment.

        • OptionalfragmentWGSL?: string

          The fragment shader code in WGSL. Ignored if fragmentChunk is provided.

        • uniqueName: string

          Unique name for the shader. If a shader with this name already exists, it will be returned instead of a new shader instance.

        • OptionaluseTransformFeedback?: boolean

          Whether to use transform feedback. Defaults to false. Only supported by WebGL.

        • OptionalvertexChunk?: string

          The name of the vertex shader chunk to use.

        • OptionalvertexDefines?: Map<string, string>

          A map containing key-value pairs of define names and their values. These are used for resolving #ifdef style of directives in the vertex code.

        • OptionalvertexGLSL?: string

          The vertex shader code in GLSL. Ignored if vertexChunk is provided.

        • OptionalvertexIncludes?: Map<string, string>

          A map containing key-value pairs of include names and their content. These are used for resolving #include directives in the vertex shader source.

        • OptionalvertexWGSL?: string

          The vertex shader code in WGSL. Ignored if vertexChunk is provided.

      Returns Shader

      The newly created shader.