GPUComputationRenderer class

GPUComputationRenderer, based on SimulationRenderer by zz85

The GPUComputationRenderer uses the concept of variables. These variables are RGBA float textures that hold 4 floats for each compute element (texel)

Each variable has a fragment shader that defines the computation made to obtain the variable in question. You can use as many variables you need, and make dependencies so you can use textures of other variables in the shader (the sampler uniforms are added automatically) Most of the variables will need themselves as dependency.

The renderer has actually two render targets per variable, to make ping-pong. Textures from the current frame are used as inputs to render the textures of the next frame.

The render targets of the variables can be used as input textures for your visualization shaders.

Variable names should be valid identifiers and should not collide with THREE GLSL used identifiers. a common approach could be to use 'texture' prefixing the variable name; i.e texturePosition, textureVelocity...

The size of the computation (sizeX * sizeY) is defined as 'resolution' automatically in the shader. For example: #DEFINE resolution vec2( 1024.0, 1024.0 )


Basic use:

// Initialization...

// Create computation renderer final gpuCompute = GPUComputationRenderer( 1024, 1024, renderer );

// Create initial state float textures final pos0 = gpuCompute.createTexture(); final vel0 = gpuCompute.createTexture(); // and fill in here the texture data...

// Add texture variables final velVar = gpuCompute.addVariable( "textureVelocity", fragmentShaderVel, pos0 ); final posVar = gpuCompute.addVariable( "texturePosition", fragmentShaderPos, vel0 );

// Add variable dependencies gpuCompute.setVariableDependencies( velVar, velVar, posVar ); gpuCompute.setVariableDependencies( posVar, velVar, posVar );

// Add custom uniforms velVar.material.uniforms.time = { value: 0.0 };

// Check for completeness final error = gpuCompute.init(); if ( error !== null ) { console.error( error );

  • }

// In each frame...

// Compute! gpuCompute.compute();

// Update texture uniforms in your visualization materials with the gpu renderer output myMaterial.uniforms.myTexture.value = gpuCompute.getCurrentRenderTarget( posVar ).texture;

// Do your rendering renderer.render( myScene, myCamera );


Also, you can use utility functions to create ShaderMaterial and perform computations (rendering between textures) Note that the shaders can have multiple input textures.

final myFilter1 = gpuCompute.createShaderMaterial( myFilterFragmentShader1, { theTexture: { value: null } } ); final myFilter2 = gpuCompute.createShaderMaterial( myFilterFragmentShader2, { theTexture: { value: null } } );

final inputTexture = gpuCompute.createTexture();

// Fill in here inputTexture...

myFilter1.uniforms.theTexture.value = inputTexture;

final myRenderTarget = gpuCompute.createRenderTarget(); myFilter2.uniforms.theTexture.value = myRenderTarget.texture;

final outputRenderTarget = gpuCompute.createRenderTarget();

// Now use the output texture where you want: myMaterial.uniforms.map.value = outputRenderTarget.texture;

// And compute each frame, before rendering to screen: gpuCompute.doRenderTarget( myFilter1, myRenderTarget ); gpuCompute.doRenderTarget( myFilter2, outputRenderTarget );

@param {int} sizeX Computation problem size is always 2d: sizeX * sizeY elements. @param {int} sizeY Computation problem size is always 2d: sizeX * sizeY elements. @param {WebGLRenderer} renderer The renderer

Constructors

GPUComputationRenderer(int sizeX, int sizeY, WebGLRenderer renderer)

Properties

addResolutionDefine ↔ void Function(dynamic)
getter/setter pair
addVariable Map<String, dynamic> Function(String, String, DataTexture)
getter/setter pair
camera → Camera
final
compute ↔ void Function()
getter/setter pair
createRenderTarget ↔ WebGLRenderTarget Function(dynamic, dynamic, dynamic, dynamic, dynamic, dynamic)
getter/setter pair
createShaderMaterial ↔ ShaderMaterial Function(String, [Map<String, dynamic>?])
getter/setter pair
createTexture ↔ DataTexture Function()
getter/setter pair
currentTextureIndex int
getter/setter pair
dispose ↔ void Function()
getter/setter pair
doRenderTarget ↔ void Function(Material, RenderTarget)
getter/setter pair
getAlternateRenderTarget ↔ WebGLRenderTarget Function(Map<String, dynamic>)
getter/setter pair
getCurrentRenderTarget ↔ WebGLRenderTarget Function(Map<String, dynamic>)
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
init String? Function()
getter/setter pair
renderTexture ↔ void Function(Texture, RenderTarget)
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scene → Scene
final
setDataType GPUComputationRenderer Function(int)
getter/setter pair
setVariableDependencies ↔ void Function(Map<String, dynamic>, dynamic)
getter/setter pair
variables List<Map<String, dynamic>>
getter/setter pair

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited