Class: GPUComposer

January 16, 2023 · View on GitHub

gpu-io / GPUComposer

Class: GPUComposer

Table of contents

Properties

Methods

Constructors

Accessors

Properties

gl

Readonly gl: WebGLRenderingContext | WebGL2RenderingContext

The WebGL context associated with this GPUComposer.


glslVersion

Readonly glslVersion: GLSLVersion

The GLSL version being used by the GPUComposer.


isWebGL2

Readonly isWebGL2: boolean

Flag for WebGL version.


intPrecision

Readonly intPrecision: GLSLPrecision

The global integer precision to apply to shader programs.


floatPrecision

Readonly floatPrecision: GLSLPrecision

The global float precision to apply to shader programs.


verboseLogging

verboseLogging: boolean = false

Flag to set GPUComposer for verbose logging, defaults to false.

Methods

initWithThreeRenderer

Static initWithThreeRenderer(renderer, params?): GPUComposer

Create a GPUComposer from an existing THREE.WebGLRenderer that shares a single WebGL context.

Parameters

NameTypeDescription
rendererWebGLRenderer | WebGL1RendererThreejs WebGLRenderer.
params?ObjectGPUComposer parameters.
params.glslVersion?GLSLVersionSet the GLSL version to use, defaults to GLSL3 for WebGL2 contexts.
params.intPrecision?GLSLPrecisionSet the global integer precision in shader programs.
params.floatPrecision?GLSLPrecisionSet the global float precision in shader programs.
params.clearValue?number | number[]Value to write to canvas when GPUComposer.clear() is called.
params.verboseLogging?booleanSet the verbosity of GPUComposer logging (defaults to false).
params.errorCallback?ErrorCallbackCustom error handler, defaults to throwing an Error with message.

Returns

GPUComposer


resize

resize(dimensions): void

Notify the GPUComposer that the canvas should change size.

Parameters

NameTypeDescription
dimensions[number, number]The new [width, height] to resize to.

Returns

void


step

step(params): void

Step GPUProgram entire fullscreen quad.

Parameters

NameTypeDescription
paramsObjectStep parameters.
params.programGPUProgramGPUProgram to run.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers to GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


stepBoundary

stepBoundary(params): void

Step GPUProgram only for a 1px strip of pixels along the boundary.

Parameters

NameTypeDescription
paramsObjectStep parameters.
params.programGPUProgramGPUProgram to run.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers to GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.edges?BoundaryEdge | BoundaryEdge[]Specify which edges to step, defaults to stepping entire boundary.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


stepNonBoundary

stepNonBoundary(params): void

Step GPUProgram for all but a 1px strip of pixels along the boundary.

Parameters

NameTypeDescription
paramsObjectStep parameters.
params.programGPUProgramGPUProgram to run.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers to GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


stepCircle

stepCircle(params): void

Step GPUProgram inside a circular spot. This is useful for touch interactions.

Parameters

NameTypeDescription
paramsObjectStep parameters.
params.programGPUProgramGPUProgram to run.
params.positionnumber[]Position of center of circle.
params.diameternumberCircle diameter in pixels.
params.useOutputScale?booleanIf true position and diameter are scaled relative to the output dimensions, else they are scaled relative to the current canvas size, defaults to false.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers to GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.numSegments?numberNumber of segments in circle, defaults to 18.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


stepSegment

stepSegment(params): void

Step GPUProgram inside a line segment (rounded end caps available). This is useful for touch interactions during pointermove.

Parameters

NameTypeDescription
paramsObjectStep parameters.
params.programGPUProgramGPUProgram to run.
params.position1number[]Position of one end of segment.
params.position2number[]Position of the other end of segment.
params.thicknessnumberThickness in pixels.
params.useOutputScale?booleanIf true position and thickness are scaled relative to the output dimensions, else they are scaled relative to the current canvas size, defaults to false.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers to GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.endCaps?booleanFlag to draw with rounded end caps, defaults to false.
params.numCapSegments?numberNumber of segments in rounded end caps, defaults to 9, must be divisible by 3.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


stepRect

stepRect(params): void

Step GPUProgram inside a rectangle.

Parameters

NameTypeDescription
paramsObjectStep parameters.
params.programGPUProgramGPUProgram to run.
params.positionnumber[]Position of one top corner of rectangle.
params.sizenumber[]Width and height of rectangle.
params.useOutputScale?booleanIf true position and size are scaled relative to the output dimensions, else they are scaled relative to the current canvas size, defaults to false.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers to GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


drawLayerAsPoints

drawLayerAsPoints(params): void

Draw the contents of a GPULayer as points. This assumes the components of the GPULayer have the form [xPosition, yPosition] or [xPosition, yPosition, xOffset, yOffset].

Parameters

NameTypeDescription
paramsObjectDraw parameters.
params.layerGPULayerGPULayer containing position data.
params.program?GPUProgramGPUProgram to run, defaults to drawing points in red.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers for GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.pointSize?numberPixel size of points.
params.useOutputScale?booleanIf true position and pointSize are scaled relative to the output dimensions, else they are scaled relative to the current canvas size, defaults to false.
params.count?numberHow many points to draw, defaults to positions.length.
params.color?number[](If no program passed in) RGB color in range [0, 1] to draw points.
params.wrapX?booleanWrap points positions in X, defaults to false.
params.wrapY?booleanWrap points positions in Y, defaults to false.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


drawLayerAsVectorField

drawLayerAsVectorField(params): void

Draw the contents of a 2 component GPULayer as a vector field.

Parameters

NameTypeDescription
paramsObjectDraw parameters.
params.layerGPULayerGPULayer containing vector data.
params.program?GPUProgramGPUProgram to run, defaults to drawing vector lines in red.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers for GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.vectorSpacing?numberSpacing between vectors, defaults to drawing a vector every 10 pixels.
params.vectorScale?numberScale factor to apply to vector lengths.
params.color?number[](If no program passed in) RGB color in range [0, 1] to draw points.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


drawLayerAsMesh

drawLayerAsMesh(params): void

Draw 2D mesh to screen.

Parameters

NameTypeDescription
paramsObjectDraw parameters.
params.layerGPULayerGPULayer containing vector data.
params.indices?GPUIndexBufferGPUIndexBuffer containing mesh index data.
params.program?GPUProgramGPUProgram to run, defaults to drawing vector lines in red.
params.input?GPULayer | GPULayerState | (GPULayer | GPULayerState)[]Input GPULayers for GPUProgram.
params.output?GPULayer | GPULayer[]Output GPULayer, will draw to screen if undefined.
params.useOutputScale?booleanIf true positions are scaled relative to the output dimensions, else they are scaled relative to the current canvas size, defaults to false.
params.color?number[](If no program passed in) RGB color in range [0, 1] to draw points.
params.blendAlpha?booleanBlend mode for draw, defaults to false.

Returns

void


clear

clear(): void

Clear all data in canvas to GPUComposer.clearValue.

Returns

void


undoThreeState

undoThreeState(): void

If this GPUComposer has been inited via GPUComposer.initWithThreeRenderer(), call undoThreeState() in render loop before performing any gpu-io step or draw functions.

Returns

void


resetThreeState

resetThreeState(): void

If this GPUComposer has been inited via GPUComposer.initWithThreeRenderer(), call resetThreeState() in render loop after performing any gpu-io step or draw functions.

Returns

void


savePNG

savePNG(params?): void

Save the current state of the canvas to png.

Parameters

NameTypeDescription
paramsObjectPNG parameters.
params.filename?stringPNG filename (no extension).
params.dpi?numberPNG dpi (defaults to 72dpi).
params.callback?(blob: Blob, filename: string) => voidOptional callback when Blob is ready, default behavior saves the PNG using file-saver.

Returns

void


tick

tick(): Object

Call tick() from your render loop to measure the FPS of your application. Internally, this does some low pass filtering to give consistent results.

Returns

Object

An Object containing the current fps of your application and the number of times tick() has been called.

NameType
fpsnumber
numTicksnumber

dispose

dispose(): void

Deallocate GPUComposer instance and associated WebGL properties.

Returns

void

Constructors

constructor

new GPUComposer(params)

Create a GPUComposer.

Parameters

NameTypeDescription
paramsObjectGPUComposer parameters.
params.canvasHTMLCanvasElementHTMLCanvasElement associated with this GPUComposer (you must add to DOM yourself).
params.context?WebGLRenderingContext | WebGL2RenderingContextPass in a WebGL context for the GPUComposer to user.
params.contextID?stringSet the contextID to use when initing a new WebGL context.
params.contextAttributes?ObjectOptions to pass to WebGL context on initialization (see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext for more information).
params.glslVersion?GLSLVersionSet the GLSL version to use, defaults to GLSL3 for WebGL2 contexts.
params.intPrecision?GLSLPrecisionSet the global integer precision in shader programs.
params.floatPrecision?GLSLPrecisionSet the global float precision in shader programs.
params.clearValue?number | number[]Value to write to canvas when GPUComposer.clear() is called.
params.verboseLogging?booleanSet the verbosity of GPUComposer logging (defaults to false).
params.errorCallback?ErrorCallbackCustom error handler, defaults to throwing an Error with message.

Accessors

canvas

get canvas(): HTMLCanvasElement

Returns

HTMLCanvasElement


clearValue

get clearValue(): number | number[]

Get the clearValue of the GPUComposer.

Returns

number | number[]

set clearValue(clearValue): void

Set the clearValue of the GPUComposer, which is applied during GPUComposer.clear().

Parameters

NameType
clearValuenumber | number[]

Returns

void


numTicks

get numTicks(): number

Return the number of ticks of the simulation. Use GPUComposer.tick() to increment this value on each animation cycle.

Returns

number