Skip to main content

Model

Model is the main engine-level rendering class in luma.gl. It assembles shaders, manages geometry and bindings, reuses cached pipelines, and issues draw calls through a RenderPass.

Usage

import {CubeGeometry, DynamicTexture, Model} from '@luma.gl/engine';

const dynamicTexture = new DynamicTexture(device, {data: loadImageBitmap(url)});

const model = new Model(device, {
vs: GLSL_VERTEX_SHADER,
fs: GLSL_FRAGMENT_SHADER,
geometry: new CubeGeometry(),
bindings: {
uSampler: dynamicTexture
}
});

const renderPass = device.beginRenderPass({framebuffer});
model.draw(renderPass);
renderPass.end();

Types

ModelProps

PropertyTypeDescription
source?stringUnified WGSL source that contains both stages.
vs?string | nullGLSL vertex shader source.
fs?string | nullGLSL fragment shader source.
modules?ShaderModule[]Shader modules to assemble into the shader source.
defines?Record<string, boolean>Shader module defines.
shaderInputs?ShaderInputsPre-created shader input manager.
bindings?Record<string, Binding | DynamicTexture>Textures, samplers, uniform buffers, and dynamic textures.
parameters?RenderPipelineParametersPipeline parameters baked into the model's pipeline.
geometry?Geometry | GPUGeometry | nullGeometry source for attributes and indices.
isInstanced?booleanOptional override for instancing.
instanceCount?numberNumber of instances to draw.
vertexCount?numberNumber of vertices to draw.
indexBuffer?Buffer | nullOptional index buffer.
attributes?Record<string, Buffer>Buffer-valued attributes.
constantAttributes?Record<string, TypedArray>Constant attributes, primarily for WebGL.
disableWarnings?booleanSuppress warnings for unused attributes and bindings.
varyings?string[]WebGL transform-feedback varyings.
transformFeedback?TransformFeedbackOptional transform feedback object.
debugShaders?'never' | 'errors' | 'warnings' | 'always'Debug shader output policy.
pipelineFactory?PipelineFactoryFactory used to create cached pipelines.
shaderFactory?ShaderFactoryFactory used to create cached shaders.
shaderAssembler?ShaderAssemblerShader assembler override.

ModelProps also includes the standard RenderPipelineProps, except that bindings, vs, and fs are specialized for engine usage.

Properties

id, device

Application-provided identifier and owning device.

source, vs, fs

The assembled WGSL source or the GLSL stage sources used to create the current pipeline.

pipelineFactory, shaderFactory

Factories used to reuse cached pipelines and shaders.

parameters, topology, bufferLayout

Current pipeline parameters and geometry layout.

isInstanced, instanceCount, vertexCount

Draw-count state for the model.

indexBuffer, bufferAttributes, constantAttributes

Attribute and index data currently bound to the model.

bindings

Current binding map, including DynamicTexture instances that have not yet resolved to concrete textures.

vertexArray

Underlying vertex array object used to track attribute bindings.

transformFeedback

Optional WebGL transform-feedback object.

pipeline

Current render pipeline.

shaderInputs

Active ShaderInputs manager.

userData

Application-owned metadata attached to the model.

Methods

constructor(device: Device, props: ModelProps)

Creates a render model for one device.

destroy(): void

Releases cached pipeline and shader references and destroys the internal uniform store.

needsRedraw(): false | string

Returns the current redraw reason and clears the internal redraw flag.

setNeedsRedraw(reason: string): void

Marks the model as needing redraw.

predraw(): void

Updates shader inputs and rebuilds the pipeline if necessary.

draw(renderPass: RenderPass): boolean

Draws once into the supplied render pass. Returns false when required resources, such as unresolved DynamicTexture bindings, are not ready yet.

setGeometry(geometry: Geometry | GPUGeometry | null): void

Replaces the geometry source.

setTopology(topology: PrimitiveTopology): void

Updates the primitive topology.

setBufferLayout(bufferLayout: BufferLayout[]): void

Replaces the buffer layout and marks the pipeline dirty.

setParameters(parameters: RenderPipelineParameters): void

Updates pipeline parameters and marks the pipeline dirty when needed.

setInstanceCount(instanceCount: number): void

Updates the instance count.

setVertexCount(vertexCount: number): void

Updates the vertex count.

setShaderInputs(shaderInputs: ShaderInputs): void

Replaces the current ShaderInputs instance.

updateShaderInputs(): void

Flushes current ShaderInputs values into the model's internal uniform store and bindings.

setBindings(bindings: Record<string, Binding | DynamicTexture>): void

Sets textures, samplers, uniform buffers, and dynamic textures.

setTransformFeedback(transformFeedback: TransformFeedback | null): void

Attaches or removes a transform-feedback object.

setIndexBuffer(indexBuffer: Buffer | null): void

Replaces the index buffer.

setAttributes(buffers: Record<string, Buffer>, options?): void

Sets buffer-valued attributes.

setConstantAttributes(attributes: Record<string, TypedArray>, options?): void

Sets constant-valued attributes.

Remarks