Used as a base to create a Material.
The purpose of Material is to create and update the bind groups and their bindings (GPU buffers, textures and samplers), create a PipelineEntry and use them to render.

Bind groups

A Material automatically creates a TextureBindGroup, but it is actually added to the active bind groups array only if necessary, which means if your shaders use a GPUSampler, a GPUTexture or a GPUExternalTexture.

Another BindGroup will be created if you pass any uniforms or storages parameters.

Finally, you can also pass already created BindGroup to a Material via the bindGroups parameter.


Note that this class is not intended to be used as is, but as a base for ComputeMaterial and RenderMaterial classes.

Hierarchy (view full)

Constructors

Properties

type: string

The type of the Material

uuid: string

The universal unique id of the Material

renderer: Renderer

The Renderer used

Options used to create this Material

pipelineEntry: AllowedPipelineEntries

Pipeline entry used by this Material

bindGroups: AllowedBindGroups[]

Array of bind groups used by this Material This array respects a specific order:

  1. The textures bind groups
  2. The bind group created using uniforms and storages parameters if any
  3. Additional bind groups parameters if any
texturesBindGroups: TextureBindGroup[]

Array of texture bind groups used by this Material

inputsBindGroups: BindGroup[]

Array of bind groups created using the uniforms and storages parameters when instancing this Material

clonedBindGroups: AllowedBindGroups[]

Array of cloned bind groups created by this Material

uniforms: Record<string, Record<string, BufferBindingInput>>

Object containing all uniforms inputs handled by this Material

storages: Record<string, Record<string, BufferBindingInput>>

Object containing all read only or read/write storages inputs handled by this Material

inputsBindings: Map<string, BindGroupBindingElement>

Map of bindings created using the uniforms and storages parameters when instancing this Material

domTextures: DOMTexture[]

Array of DOMTexture handled by this Material

textures: Texture[]

Array of Texture handled by this Material

samplers: Sampler[]

Array of Sampler handled by this Material

Accessors

  • get ready(): boolean
  • Get whether the renderer is ready, our pipeline entry and pipeline have been created and successfully compiled

    Returns boolean

Methods

  • Called when the device has been lost to prepare everything for restoration. Basically set all the GPUBuffer to null so they will be reset next time we try to render

    Returns void

  • Get the complete code of a given shader including all the WGSL fragment code snippets added by the pipeline

    Parameters

    Returns string

    • The corresponding shader code
  • Get the added code of a given shader, i.e. all the WGSL fragment code snippets added by the pipeline

    Parameters

    • Optional shaderType: FullShadersType = 'vertex'

      shader to get the code from

    Returns string

    • The corresponding shader code
  • Process all BindGroup struct and add them to the corresponding objects based on their binding types. Also store them in a inputsBindings array to facilitate further access to struct.

    Parameters

    Returns void

  • Force setting a given buffer binding shouldUpdate flag to true to update it at next render

    Parameters

    • Optional bufferBindingName: string

      the buffer binding name

    • Optional bindingName: string

      the binding name

    Returns void

  • Add a texture to our array, and add it to the textures bind group only if used in the shaders (avoid binding useless data)

    Parameters

    Returns void

  • Prepare our samplers array and always add a default sampler if not already passed as parameter

    Returns void

  • Add a sampler to our array, and add it to the textures bind group only if used in the shaders (avoid binding useless data)

    Parameters

    Returns void

  • Map the content of a specific buffer element belonging to a BufferBinding GPU buffer and put a copy of the data into a Float32Array

    Parameters

    • parameters: {
          bindingName: string;
          bufferElementName: string;
      }

      parameters used to get the result

    Returns Promise<Float32Array>

    • Float32Array holding GPUBuffer data
  • Set the current pipeline

    Parameters

    • pass: GPURenderPassEncoder | GPUComputePassEncoder

      current pass encoder

    Returns void

  • Render the material if it is ready: Set the current pipeline and set the bind groups

    Parameters

    • pass: GPURenderPassEncoder | GPUComputePassEncoder

      current pass encoder

    Returns void