# ShaderUtils

Class · category: Graphics

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/scene/shader-lib/shader-utils.js#L43

Utility class for creating shaders. Provides a higher-level API over the [Shader](https://api.playcanvas.com/engine/classes/Shader.md)
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.

## Methods

### createShader

```ts
static createShader(device: GraphicsDevice, options: object): Shader
```

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`](https://api.playcanvas.com/engine/classes/GraphicsDevice.md)): The graphics device.
- `options` (`object`): Object for passing optional arguments.
    - `options.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.
    - `options.fragmentChunk` (`string`, optional): The name of the fragment shader chunk to use.
    - `options.fragmentDefines` (`Map<string, string>`, optional): 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.
    - `options.fragmentGLSL` (`string`, optional): The fragment shader code in GLSL. Ignored if
      fragmentChunk is provided.
    - `options.fragmentIncludes` (`Map<string, string>`, optional): A map containing key-value pairs
      of include names and their content. These are used for resolving #include directives in the
      fragment shader source.
    - `options.fragmentOutputTypes` (`string | string[]`, optional): 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.
    - `options.fragmentWGSL` (`string`, optional): The fragment shader code in WGSL. Ignored if
      fragmentChunk is provided.
    - `options.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.
    - `options.useDualSourceBlending` (`boolean`, optional): Whether the fragment shader outputs a
      secondary color for dual-source blending. Defaults to false.
    - `options.useTransformFeedback` (`boolean`, optional): Whether to use transform feedback. Defaults
      to false. Only supported by WebGL.
    - `options.vertexChunk` (`string`, optional): The name of the vertex shader chunk to use.
    - `options.vertexDefines` (`Map<string, string>`, optional): 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.
    - `options.vertexGLSL` (`string`, optional): The vertex shader code in GLSL. Ignored if vertexChunk
      is provided.
    - `options.vertexIncludes` (`Map<string, string>`, optional): A map containing key-value pairs of
      include names and their content. These are used for resolving #include directives in the
      vertex shader source.
    - `options.vertexWGSL` (`string`, optional): The vertex shader code in WGSL. Ignored if vertexChunk
      is provided.

**Returns** [`Shader`](https://api.playcanvas.com/engine/classes/Shader.md): The newly created shader.
