GPUComputationRenderer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import {
  2. Camera,
  3. ClampToEdgeWrapping,
  4. DataTexture,
  5. FloatType,
  6. Mesh,
  7. NearestFilter,
  8. PlaneGeometry,
  9. RGBAFormat,
  10. Scene,
  11. ShaderMaterial,
  12. WebGLRenderTarget
  13. } from '../../../build/three.module.js';
  14. /**
  15. * GPUComputationRenderer, based on SimulationRenderer by zz85
  16. *
  17. * The GPUComputationRenderer uses the concept of variables. These variables are RGBA float textures that hold 4 floats
  18. * for each compute element (texel)
  19. *
  20. * Each variable has a fragment shader that defines the computation made to obtain the variable in question.
  21. * You can use as many variables you need, and make dependencies so you can use textures of other variables in the shader
  22. * (the sampler uniforms are added automatically) Most of the variables will need themselves as dependency.
  23. *
  24. * The renderer has actually two render targets per variable, to make ping-pong. Textures from the current frame are used
  25. * as inputs to render the textures of the next frame.
  26. *
  27. * The render targets of the variables can be used as input textures for your visualization shaders.
  28. *
  29. * Variable names should be valid identifiers and should not collide with THREE GLSL used identifiers.
  30. * a common approach could be to use 'texture' prefixing the variable name; i.e texturePosition, textureVelocity...
  31. *
  32. * The size of the computation (sizeX * sizeY) is defined as 'resolution' automatically in the shader. For example:
  33. * #DEFINE resolution vec2( 1024.0, 1024.0 )
  34. *
  35. * -------------
  36. *
  37. * Basic use:
  38. *
  39. * // Initialization...
  40. *
  41. * // Create computation renderer
  42. * const gpuCompute = new GPUComputationRenderer( 1024, 1024, renderer );
  43. *
  44. * // Create initial state float textures
  45. * const pos0 = gpuCompute.createTexture();
  46. * const vel0 = gpuCompute.createTexture();
  47. * // and fill in here the texture data...
  48. *
  49. * // Add texture variables
  50. * const velVar = gpuCompute.addVariable( "textureVelocity", fragmentShaderVel, pos0 );
  51. * const posVar = gpuCompute.addVariable( "texturePosition", fragmentShaderPos, vel0 );
  52. *
  53. * // Add variable dependencies
  54. * gpuCompute.setVariableDependencies( velVar, [ velVar, posVar ] );
  55. * gpuCompute.setVariableDependencies( posVar, [ velVar, posVar ] );
  56. *
  57. * // Add custom uniforms
  58. * velVar.material.uniforms.time = { value: 0.0 };
  59. *
  60. * // Check for completeness
  61. * const error = gpuCompute.init();
  62. * if ( error !== null ) {
  63. * console.error( error );
  64. * }
  65. *
  66. *
  67. * // In each frame...
  68. *
  69. * // Compute!
  70. * gpuCompute.compute();
  71. *
  72. * // Update texture uniforms in your visualization materials with the gpu renderer output
  73. * myMaterial.uniforms.myTexture.value = gpuCompute.getCurrentRenderTarget( posVar ).texture;
  74. *
  75. * // Do your rendering
  76. * renderer.render( myScene, myCamera );
  77. *
  78. * -------------
  79. *
  80. * Also, you can use utility functions to create ShaderMaterial and perform computations (rendering between textures)
  81. * Note that the shaders can have multiple input textures.
  82. *
  83. * const myFilter1 = gpuCompute.createShaderMaterial( myFilterFragmentShader1, { theTexture: { value: null } } );
  84. * const myFilter2 = gpuCompute.createShaderMaterial( myFilterFragmentShader2, { theTexture: { value: null } } );
  85. *
  86. * const inputTexture = gpuCompute.createTexture();
  87. *
  88. * // Fill in here inputTexture...
  89. *
  90. * myFilter1.uniforms.theTexture.value = inputTexture;
  91. *
  92. * const myRenderTarget = gpuCompute.createRenderTarget();
  93. * myFilter2.uniforms.theTexture.value = myRenderTarget.texture;
  94. *
  95. * const outputRenderTarget = gpuCompute.createRenderTarget();
  96. *
  97. * // Now use the output texture where you want:
  98. * myMaterial.uniforms.map.value = outputRenderTarget.texture;
  99. *
  100. * // And compute each frame, before rendering to screen:
  101. * gpuCompute.doRenderTarget( myFilter1, myRenderTarget );
  102. * gpuCompute.doRenderTarget( myFilter2, outputRenderTarget );
  103. *
  104. *
  105. *
  106. * @param {int} sizeX Computation problem size is always 2d: sizeX * sizeY elements.
  107. * @param {int} sizeY Computation problem size is always 2d: sizeX * sizeY elements.
  108. * @param {WebGLRenderer} renderer The renderer
  109. */
  110. class GPUComputationRenderer {
  111. constructor( sizeX, sizeY, renderer ) {
  112. this.variables = [];
  113. this.currentTextureIndex = 0;
  114. let dataType = FloatType;
  115. const scene = new Scene();
  116. const camera = new Camera();
  117. camera.position.z = 1;
  118. const passThruUniforms = {
  119. passThruTexture: { value: null }
  120. };
  121. const passThruShader = createShaderMaterial( getPassThroughFragmentShader(), passThruUniforms );
  122. const mesh = new Mesh( new PlaneGeometry( 2, 2 ), passThruShader );
  123. scene.add( mesh );
  124. this.setDataType = function ( type ) {
  125. dataType = type;
  126. return this;
  127. };
  128. this.addVariable = function ( variableName, computeFragmentShader, initialValueTexture ) {
  129. const material = this.createShaderMaterial( computeFragmentShader );
  130. const variable = {
  131. name: variableName,
  132. initialValueTexture: initialValueTexture,
  133. material: material,
  134. dependencies: null,
  135. renderTargets: [],
  136. wrapS: null,
  137. wrapT: null,
  138. minFilter: NearestFilter,
  139. magFilter: NearestFilter
  140. };
  141. this.variables.push( variable );
  142. return variable;
  143. };
  144. this.setVariableDependencies = function ( variable, dependencies ) {
  145. variable.dependencies = dependencies;
  146. };
  147. this.init = function () {
  148. if ( renderer.capabilities.isWebGL2 === false && renderer.extensions.has( 'OES_texture_float' ) === false ) {
  149. return 'No OES_texture_float support for float textures.';
  150. }
  151. if ( renderer.capabilities.maxVertexTextures === 0 ) {
  152. return 'No support for vertex shader textures.';
  153. }
  154. for ( let i = 0; i < this.variables.length; i ++ ) {
  155. const variable = this.variables[ i ];
  156. // Creates rendertargets and initialize them with input texture
  157. variable.renderTargets[ 0 ] = this.createRenderTarget( sizeX, sizeY, variable.wrapS, variable.wrapT, variable.minFilter, variable.magFilter );
  158. variable.renderTargets[ 1 ] = this.createRenderTarget( sizeX, sizeY, variable.wrapS, variable.wrapT, variable.minFilter, variable.magFilter );
  159. this.renderTexture( variable.initialValueTexture, variable.renderTargets[ 0 ] );
  160. this.renderTexture( variable.initialValueTexture, variable.renderTargets[ 1 ] );
  161. // Adds dependencies uniforms to the ShaderMaterial
  162. const material = variable.material;
  163. const uniforms = material.uniforms;
  164. if ( variable.dependencies !== null ) {
  165. for ( let d = 0; d < variable.dependencies.length; d ++ ) {
  166. const depVar = variable.dependencies[ d ];
  167. if ( depVar.name !== variable.name ) {
  168. // Checks if variable exists
  169. let found = false;
  170. for ( let j = 0; j < this.variables.length; j ++ ) {
  171. if ( depVar.name === this.variables[ j ].name ) {
  172. found = true;
  173. break;
  174. }
  175. }
  176. if ( ! found ) {
  177. return 'Variable dependency not found. Variable=' + variable.name + ', dependency=' + depVar.name;
  178. }
  179. }
  180. uniforms[ depVar.name ] = { value: null };
  181. material.fragmentShader = '\nuniform sampler2D ' + depVar.name + ';\n' + material.fragmentShader;
  182. }
  183. }
  184. }
  185. this.currentTextureIndex = 0;
  186. return null;
  187. };
  188. this.compute = function () {
  189. const currentTextureIndex = this.currentTextureIndex;
  190. const nextTextureIndex = this.currentTextureIndex === 0 ? 1 : 0;
  191. for ( let i = 0, il = this.variables.length; i < il; i ++ ) {
  192. const variable = this.variables[ i ];
  193. // Sets texture dependencies uniforms
  194. if ( variable.dependencies !== null ) {
  195. const uniforms = variable.material.uniforms;
  196. for ( let d = 0, dl = variable.dependencies.length; d < dl; d ++ ) {
  197. const depVar = variable.dependencies[ d ];
  198. uniforms[ depVar.name ].value = depVar.renderTargets[ currentTextureIndex ].texture;
  199. }
  200. }
  201. // Performs the computation for this variable
  202. this.doRenderTarget( variable.material, variable.renderTargets[ nextTextureIndex ] );
  203. }
  204. this.currentTextureIndex = nextTextureIndex;
  205. };
  206. this.getCurrentRenderTarget = function ( variable ) {
  207. return variable.renderTargets[ this.currentTextureIndex ];
  208. };
  209. this.getAlternateRenderTarget = function ( variable ) {
  210. return variable.renderTargets[ this.currentTextureIndex === 0 ? 1 : 0 ];
  211. };
  212. function addResolutionDefine( materialShader ) {
  213. materialShader.defines.resolution = 'vec2( ' + sizeX.toFixed( 1 ) + ', ' + sizeY.toFixed( 1 ) + ' )';
  214. }
  215. this.addResolutionDefine = addResolutionDefine;
  216. // The following functions can be used to compute things manually
  217. function createShaderMaterial( computeFragmentShader, uniforms ) {
  218. uniforms = uniforms || {};
  219. const material = new ShaderMaterial( {
  220. uniforms: uniforms,
  221. vertexShader: getPassThroughVertexShader(),
  222. fragmentShader: computeFragmentShader
  223. } );
  224. addResolutionDefine( material );
  225. return material;
  226. }
  227. this.createShaderMaterial = createShaderMaterial;
  228. this.createRenderTarget = function ( sizeXTexture, sizeYTexture, wrapS, wrapT, minFilter, magFilter ) {
  229. sizeXTexture = sizeXTexture || sizeX;
  230. sizeYTexture = sizeYTexture || sizeY;
  231. wrapS = wrapS || ClampToEdgeWrapping;
  232. wrapT = wrapT || ClampToEdgeWrapping;
  233. minFilter = minFilter || NearestFilter;
  234. magFilter = magFilter || NearestFilter;
  235. const renderTarget = new WebGLRenderTarget( sizeXTexture, sizeYTexture, {
  236. wrapS: wrapS,
  237. wrapT: wrapT,
  238. minFilter: minFilter,
  239. magFilter: magFilter,
  240. format: RGBAFormat,
  241. type: dataType,
  242. depthBuffer: false
  243. } );
  244. return renderTarget;
  245. };
  246. this.createTexture = function () {
  247. const data = new Float32Array( sizeX * sizeY * 4 );
  248. return new DataTexture( data, sizeX, sizeY, RGBAFormat, FloatType );
  249. };
  250. this.renderTexture = function ( input, output ) {
  251. // Takes a texture, and render out in rendertarget
  252. // input = Texture
  253. // output = RenderTarget
  254. passThruUniforms.passThruTexture.value = input;
  255. this.doRenderTarget( passThruShader, output );
  256. passThruUniforms.passThruTexture.value = null;
  257. };
  258. this.doRenderTarget = function ( material, output ) {
  259. const currentRenderTarget = renderer.getRenderTarget();
  260. mesh.material = material;
  261. renderer.setRenderTarget( output );
  262. renderer.render( scene, camera );
  263. mesh.material = passThruShader;
  264. renderer.setRenderTarget( currentRenderTarget );
  265. };
  266. // Shaders
  267. function getPassThroughVertexShader() {
  268. return 'void main() {\n' +
  269. '\n' +
  270. ' gl_Position = vec4( position, 1.0 );\n' +
  271. '\n' +
  272. '}\n';
  273. }
  274. function getPassThroughFragmentShader() {
  275. return 'uniform sampler2D passThruTexture;\n' +
  276. '\n' +
  277. 'void main() {\n' +
  278. '\n' +
  279. ' vec2 uv = gl_FragCoord.xy / resolution.xy;\n' +
  280. '\n' +
  281. ' gl_FragColor = texture2D( passThruTexture, uv );\n' +
  282. '\n' +
  283. '}\n';
  284. }
  285. }
  286. }
  287. export { GPUComputationRenderer };