@@ -11,10 +11,10 @@ import {
11
11
import { FullScreenQuad } from '../postprocessing/Pass.js' ;
12
12
13
13
/**
14
- * GPUComputationRenderer, based on SimulationRenderer by zz85
14
+ * GPUComputationRenderer, based on SimulationRenderer by @ zz85.
15
15
*
16
16
* The GPUComputationRenderer uses the concept of variables. These variables are RGBA float textures that hold 4 floats
17
- * for each compute element (texel)
17
+ * for each compute element (texel).
18
18
*
19
19
* Each variable has a fragment shader that defines the computation made to obtain the variable in question.
20
20
* You can use as many variables you need, and make dependencies so you can use textures of other variables in the shader
@@ -29,12 +29,11 @@ import { FullScreenQuad } from '../postprocessing/Pass.js';
29
29
* a common approach could be to use 'texture' prefixing the variable name; i.e texturePosition, textureVelocity...
30
30
*
31
31
* The size of the computation (sizeX * sizeY) is defined as 'resolution' automatically in the shader. For example:
32
+ * ```
32
33
* #DEFINE resolution vec2( 1024.0, 1024.0 )
33
- *
34
- * -------------
35
- *
34
+ * ```
36
35
* Basic use:
37
- *
36
+ * ```js
38
37
* // Initialization...
39
38
*
40
39
* // Create computation renderer
@@ -62,7 +61,6 @@ import { FullScreenQuad } from '../postprocessing/Pass.js';
62
61
* console.error( error );
63
62
* }
64
63
*
65
- *
66
64
* // In each frame...
67
65
*
68
66
* // Compute!
@@ -73,12 +71,12 @@ import { FullScreenQuad } from '../postprocessing/Pass.js';
73
71
*
74
72
* // Do your rendering
75
73
* renderer.render( myScene, myCamera );
76
- *
77
- * -------------
74
+ * ```
78
75
*
79
76
* Also, you can use utility functions to create ShaderMaterial and perform computations (rendering between textures)
80
77
* Note that the shaders can have multiple input textures.
81
78
*
79
+ * ```js
82
80
* const myFilter1 = gpuCompute.createShaderMaterial( myFilterFragmentShader1, { theTexture: { value: null } } );
83
81
* const myFilter2 = gpuCompute.createShaderMaterial( myFilterFragmentShader2, { theTexture: { value: null } } );
84
82
*
@@ -99,14 +97,16 @@ import { FullScreenQuad } from '../postprocessing/Pass.js';
99
97
* // And compute each frame, before rendering to screen:
100
98
* gpuCompute.doRenderTarget( myFilter1, myRenderTarget );
101
99
* gpuCompute.doRenderTarget( myFilter2, outputRenderTarget );
100
+ * ```
102
101
*/
103
-
104
102
class GPUComputationRenderer {
105
103
106
104
/**
107
- * @param {number } sizeX Computation problem size is always 2d: sizeX * sizeY elements.
108
- * @param {number } sizeY Computation problem size is always 2d: sizeX * sizeY elements.
109
- * @param {WebGLRenderer } renderer The renderer
105
+ * Constructs a new GPU computation renderer.
106
+ *
107
+ * @param {number } sizeX - Computation problem size is always 2d: sizeX * sizeY elements.
108
+ * @param {number } sizeY - Computation problem size is always 2d: sizeX * sizeY elements.
109
+ * @param {WebGLRenderer } renderer - The renderer.
110
110
*/
111
111
constructor ( sizeX , sizeY , renderer ) {
112
112
@@ -124,13 +124,27 @@ class GPUComputationRenderer {
124
124
125
125
const quad = new FullScreenQuad ( passThruShader ) ;
126
126
127
+ /**
128
+ * Sets the data type of the internal textures.
129
+ *
130
+ * @param {(FloatType|HalfFloatType) } type - The type to set.
131
+ * @return {GPUComputationRenderer } A reference to this renderer.
132
+ */
127
133
this . setDataType = function ( type ) {
128
134
129
135
dataType = type ;
130
136
return this ;
131
137
132
138
} ;
133
139
140
+ /**
141
+ * Adds a compute variable to the renderer.
142
+ *
143
+ * @param {string } variableName - The variable name.
144
+ * @param {string } computeFragmentShader - The compute (fragment) shader source.
145
+ * @param {Texture } initialValueTexture - The initial value texture.
146
+ * @return {Object } The compute variable.
147
+ */
134
148
this . addVariable = function ( variableName , computeFragmentShader , initialValueTexture ) {
135
149
136
150
const material = this . createShaderMaterial ( computeFragmentShader ) ;
@@ -153,12 +167,23 @@ class GPUComputationRenderer {
153
167
154
168
} ;
155
169
170
+ /**
171
+ * Sets variable dependencies.
172
+ *
173
+ * @param {Object } variable - The compute variable.
174
+ * @param {Array<Object> } dependencies - Ohter compute variables that represents the dependencies.
175
+ */
156
176
this . setVariableDependencies = function ( variable , dependencies ) {
157
177
158
178
variable . dependencies = dependencies ;
159
179
160
180
} ;
161
181
182
+ /**
183
+ * Initializes the renderer.
184
+ *
185
+ * @return {?string } Returns `null` if no errors are detected. Otherwise returns the error message.
186
+ */
162
187
this . init = function ( ) {
163
188
164
189
if ( renderer . capabilities . maxVertexTextures === 0 ) {
@@ -227,6 +252,9 @@ class GPUComputationRenderer {
227
252
228
253
} ;
229
254
255
+ /**
256
+ * Executes the compute. This method is usually called in the animation loop.
257
+ */
230
258
this . compute = function ( ) {
231
259
232
260
const currentTextureIndex = this . currentTextureIndex ;
@@ -260,18 +288,34 @@ class GPUComputationRenderer {
260
288
261
289
} ;
262
290
291
+ /**
292
+ * Returns the current render target for the given compute variable.
293
+ *
294
+ * @param {Object } variable - The compute variable.
295
+ * @return {WebGLRenderTarget } The current render target.
296
+ */
263
297
this . getCurrentRenderTarget = function ( variable ) {
264
298
265
299
return variable . renderTargets [ this . currentTextureIndex ] ;
266
300
267
301
} ;
268
302
303
+ /**
304
+ * Returns the alternate render target for the given compute variable.
305
+ *
306
+ * @param {Object } variable - The compute variable.
307
+ * @return {WebGLRenderTarget } The alternate render target.
308
+ */
269
309
this . getAlternateRenderTarget = function ( variable ) {
270
310
271
311
return variable . renderTargets [ this . currentTextureIndex === 0 ? 1 : 0 ] ;
272
312
273
313
} ;
274
314
315
+ /**
316
+ * Frees all internal resources. Call this method if you don't need the
317
+ * renderer anymore.
318
+ */
275
319
this . dispose = function ( ) {
276
320
277
321
quad . dispose ( ) ;
@@ -303,6 +347,11 @@ class GPUComputationRenderer {
303
347
304
348
}
305
349
350
+ /**
351
+ * Adds a resolution defined for the given material shader.
352
+ *
353
+ * @param {Object } materialShader - The material shader.
354
+ */
306
355
this . addResolutionDefine = addResolutionDefine ;
307
356
308
357
@@ -327,6 +376,17 @@ class GPUComputationRenderer {
327
376
328
377
this . createShaderMaterial = createShaderMaterial ;
329
378
379
+ /**
380
+ * Creates a new render target from the given paramters.
381
+ *
382
+ * @param {number } sizeXTexture - The width of the render target.
383
+ * @param {number } sizeYTexture - The height of the render target.
384
+ * @param {number } wrapS - The wrapS value.
385
+ * @param {number } wrapT - The wrapS value.
386
+ * @param {number } minFilter - The minFilter value.
387
+ * @param {number } magFilter - The magFilter value.
388
+ * @return {WebGLRenderTarget } The new render target.
389
+ */
330
390
this . createRenderTarget = function ( sizeXTexture , sizeYTexture , wrapS , wrapT , minFilter , magFilter ) {
331
391
332
392
sizeXTexture = sizeXTexture || sizeX ;
@@ -352,6 +412,11 @@ class GPUComputationRenderer {
352
412
353
413
} ;
354
414
415
+ /**
416
+ * Creates a new data texture.
417
+ *
418
+ * @return {DataTexture } The new data texture.
419
+ */
355
420
this . createTexture = function ( ) {
356
421
357
422
const data = new Float32Array ( sizeX * sizeY * 4 ) ;
@@ -361,12 +426,14 @@ class GPUComputationRenderer {
361
426
362
427
} ;
363
428
429
+ /**
430
+ * Renders the given texture into the given render target.
431
+ *
432
+ * @param {Texture } input - The input.
433
+ * @param {WebGLRenderTarget } output - The output.
434
+ */
364
435
this . renderTexture = function ( input , output ) {
365
436
366
- // Takes a texture, and render out in rendertarget
367
- // input = Texture
368
- // output = RenderTarget
369
-
370
437
passThruUniforms . passThruTexture . value = input ;
371
438
372
439
this . doRenderTarget ( passThruShader , output ) ;
@@ -375,6 +442,14 @@ class GPUComputationRenderer {
375
442
376
443
} ;
377
444
445
+
446
+ /**
447
+ * Renders the given material into the given render target
448
+ * with a full-screen pass.
449
+ *
450
+ * @param {Material } material - The material.
451
+ * @param {WebGLRenderTarget } output - The output.
452
+ */
378
453
this . doRenderTarget = function ( material , output ) {
379
454
380
455
const currentRenderTarget = renderer . getRenderTarget ( ) ;
0 commit comments