webgl_gpgpu_water.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - gpgpu - water</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. text-align:center;
  16. }
  17. a {
  18. color:#0078ff;
  19. }
  20. #info {
  21. color: #ffffff;
  22. position: absolute;
  23. top: 10px;
  24. width: 100%;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">
  30. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - <span id="waterSize"></span> webgl gpgpu water<br/>
  31. Select <span id="options"></span> water size<br/>
  32. Move mouse to disturb water.<br>
  33. Press mouse button to orbit around. 'W' key toggles wireframe.
  34. </div>
  35. <script src="../build/three.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/libs/stats.min.js"></script>
  38. <script src="js/libs/dat.gui.min.js"></script>
  39. <script src="js/controls/OrbitControls.js"></script>
  40. <script src="js/SimplexNoise.js"></script>
  41. <script src="js/GPUComputationRenderer.js"></script>
  42. <!-- This is the 'compute shader' for the water heightmap: -->
  43. <script id="heightmapFragmentShader" type="x-shader/x-fragment">
  44. #include <common>
  45. uniform vec2 mousePos;
  46. uniform float mouseSize;
  47. uniform float viscosityConstant;
  48. uniform float heightCompensation;
  49. #define deltaTime ( 1.0 / 60.0 )
  50. #define GRAVITY_CONSTANT ( resolution.x * deltaTime * 3.0 )
  51. void main() {
  52. vec2 cellSize = 1.0 / resolution.xy;
  53. vec2 uv = gl_FragCoord.xy * cellSize;
  54. // heightmapValue.x == height
  55. // heightmapValue.y == velocity
  56. // heightmapValue.z, heightmapValue.w not used
  57. vec4 heightmapValue = texture2D( heightmap, uv );
  58. // Get neighbours
  59. vec4 north = texture2D( heightmap, uv + vec2( 0.0, cellSize.y ) );
  60. vec4 south = texture2D( heightmap, uv + vec2( 0.0, - cellSize.y ) );
  61. vec4 east = texture2D( heightmap, uv + vec2( cellSize.x, 0.0 ) );
  62. vec4 west = texture2D( heightmap, uv + vec2( - cellSize.x, 0.0 ) );
  63. float sump = north.x + south.x + east.x + west.x - 4.0 * heightmapValue.x;
  64. float accel = sump * GRAVITY_CONSTANT;
  65. // Dynamics
  66. heightmapValue.y += accel;
  67. heightmapValue.x += heightmapValue.y * deltaTime;
  68. // Viscosity
  69. heightmapValue.x += sump * viscosityConstant;
  70. // Mouse influence
  71. float mousePhase = clamp( length( ( uv - vec2( 0.5 ) ) * BOUNDS - vec2( mousePos.x, - mousePos.y ) ) * PI / mouseSize, 0.0, PI );
  72. heightmapValue.x += cos( mousePhase ) + 1.0;
  73. // Water height reset compensation
  74. heightmapValue.x += heightCompensation;
  75. gl_FragColor = heightmapValue;
  76. }
  77. </script>
  78. <!-- This is just a smoothing 'compute shader' for using manually: -->
  79. <script id="smoothFragmentShader" type="x-shader/x-fragment">
  80. uniform sampler2D texture;
  81. void main() {
  82. vec2 cellSize = 1.0 / resolution.xy;
  83. vec2 uv = gl_FragCoord.xy * cellSize;
  84. // Computes the mean of texel and 4 neighbours
  85. vec4 textureValue = texture2D( texture, uv );
  86. textureValue += texture2D( texture, uv + vec2( 0.0, cellSize.y ) );
  87. textureValue += texture2D( texture, uv + vec2( 0.0, - cellSize.y ) );
  88. textureValue += texture2D( texture, uv + vec2( cellSize.x, 0.0 ) );
  89. textureValue += texture2D( texture, uv + vec2( - cellSize.x, 0.0 ) );
  90. textureValue /= 5.0;
  91. gl_FragColor = textureValue;
  92. }
  93. </script>
  94. <!-- This is a 'compute shader' to calculate the current volume of water -->
  95. <!-- It is used with a variable of size 1x1 -->
  96. <script id="sumFragmentShader" type="x-shader/x-fragment">
  97. uniform sampler2D texture;
  98. // Integer to float conversion from https://stackoverflow.com/questions/17981163/webgl-read-pixels-from-floating-point-render-target
  99. float shift_right( float v, float amt ) {
  100. v = floor( v ) + 0.5;
  101. return floor( v / exp2( amt ) );
  102. }
  103. float shift_left( float v, float amt ) {
  104. return floor( v * exp2( amt ) + 0.5 );
  105. }
  106. float mask_last( float v, float bits ) {
  107. return mod( v, shift_left( 1.0, bits ) );
  108. }
  109. float extract_bits( float num, float from, float to ) {
  110. from = floor( from + 0.5 ); to = floor( to + 0.5 );
  111. return mask_last( shift_right( num, from ), to - from );
  112. }
  113. vec4 encode_float( float val ) {
  114. if ( val == 0.0 ) return vec4( 0, 0, 0, 0 );
  115. float sign = val > 0.0 ? 0.0 : 1.0;
  116. val = abs( val );
  117. float exponent = floor( log2( val ) );
  118. float biased_exponent = exponent + 127.0;
  119. float fraction = ( ( val / exp2( exponent ) ) - 1.0 ) * 8388608.0;
  120. float t = biased_exponent / 2.0;
  121. float last_bit_of_biased_exponent = fract( t ) * 2.0;
  122. float remaining_bits_of_biased_exponent = floor( t );
  123. float byte4 = extract_bits( fraction, 0.0, 8.0 ) / 255.0;
  124. float byte3 = extract_bits( fraction, 8.0, 16.0 ) / 255.0;
  125. float byte2 = ( last_bit_of_biased_exponent * 128.0 + extract_bits( fraction, 16.0, 23.0 ) ) / 255.0;
  126. float byte1 = ( sign * 128.0 + remaining_bits_of_biased_exponent ) / 255.0;
  127. return vec4( byte4, byte3, byte2, byte1 );
  128. }
  129. void main() {
  130. vec2 cellSize = 1.0 / resolution.xy;
  131. float volume = 0.0;
  132. const int rx = int( resolution.x );
  133. const int ry = int( resolution.y );
  134. // Sum of water height over all water cells
  135. for ( int j = 0; j < ry; j++ ) {
  136. for ( int i = 0; i < rx; i++ ) {
  137. vec2 uv = vec2( i, j ) * cellSize;
  138. vec4 textureValue = texture2D( texture, uv );
  139. volume += textureValue.x;
  140. }
  141. }
  142. gl_FragColor = encode_float( volume );
  143. }
  144. </script>
  145. <!-- This is the water visualization shader, copied from the MeshPhongMaterial and modified: -->
  146. <script id="waterVertexShader" type="x-shader/x-vertex">
  147. uniform sampler2D heightmap;
  148. #define PHONG
  149. varying vec3 vViewPosition;
  150. #ifndef FLAT_SHADED
  151. varying vec3 vNormal;
  152. #endif
  153. #include <common>
  154. #include <uv_pars_vertex>
  155. #include <uv2_pars_vertex>
  156. #include <displacementmap_pars_vertex>
  157. #include <envmap_pars_vertex>
  158. #include <color_pars_vertex>
  159. #include <morphtarget_pars_vertex>
  160. #include <skinning_pars_vertex>
  161. #include <shadowmap_pars_vertex>
  162. #include <logdepthbuf_pars_vertex>
  163. #include <clipping_planes_pars_vertex>
  164. void main() {
  165. vec2 cellSize = vec2( 1.0 / WIDTH, 1.0 / WIDTH );
  166. #include <uv_vertex>
  167. #include <uv2_vertex>
  168. #include <color_vertex>
  169. // # include <beginnormal_vertex>
  170. // Compute normal from heightmap
  171. vec3 objectNormal = vec3(
  172. ( texture2D( heightmap, uv + vec2( - cellSize.x, 0 ) ).x - texture2D( heightmap, uv + vec2( cellSize.x, 0 ) ).x ) * WIDTH / BOUNDS,
  173. ( texture2D( heightmap, uv + vec2( 0, - cellSize.y ) ).x - texture2D( heightmap, uv + vec2( 0, cellSize.y ) ).x ) * WIDTH / BOUNDS,
  174. 1.0 );
  175. //<beginnormal_vertex>
  176. #include <morphnormal_vertex>
  177. #include <skinbase_vertex>
  178. #include <skinnormal_vertex>
  179. #include <defaultnormal_vertex>
  180. #ifndef FLAT_SHADED // Normal computed with derivatives when FLAT_SHADED
  181. vNormal = normalize( transformedNormal );
  182. #endif
  183. //# include <begin_vertex>
  184. float heightValue = texture2D( heightmap, uv ).x;
  185. vec3 transformed = vec3( position.x, position.y, heightValue );
  186. //<begin_vertex>
  187. #include <morphtarget_vertex>
  188. #include <skinning_vertex>
  189. #include <displacementmap_vertex>
  190. #include <project_vertex>
  191. #include <logdepthbuf_vertex>
  192. #include <clipping_planes_vertex>
  193. vViewPosition = - mvPosition.xyz;
  194. #include <worldpos_vertex>
  195. #include <envmap_vertex>
  196. #include <shadowmap_vertex>
  197. }
  198. </script>
  199. <script>
  200. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  201. var hash = document.location.hash.substr( 1 );
  202. if ( hash ) hash = parseInt( hash, 0 );
  203. // Texture width for simulation
  204. var WIDTH = hash || 128;
  205. var NUM_TEXELS = WIDTH * WIDTH;
  206. // Water size in system units
  207. var BOUNDS = 512;
  208. var BOUNDS_HALF = BOUNDS * 0.5;
  209. var container, stats;
  210. var camera, scene, renderer, controls;
  211. var mouseMoved = false;
  212. var mouseCoords = new THREE.Vector2();
  213. var raycaster = new THREE.Raycaster();
  214. var waterMesh;
  215. var meshRay;
  216. var gpuCompute;
  217. var heightmapVariable;
  218. var waterUniforms;
  219. var smoothShader;
  220. var readVolumeShader;
  221. var readVolumeRenderTarget;
  222. var readVolumeImage;
  223. var heightCompensation = 0;
  224. var numFrames = 0;
  225. var simplex = new SimplexNoise();
  226. var windowHalfX = window.innerWidth / 2;
  227. var windowHalfY = window.innerHeight / 2;
  228. document.getElementById( 'waterSize' ).innerText = WIDTH + ' x ' + WIDTH;
  229. function change(n) {
  230. location.hash = n;
  231. location.reload();
  232. return false;
  233. }
  234. var options = '';
  235. for ( var i = 4; i < 10; i++ ) {
  236. var j = Math.pow( 2, i );
  237. options += '<a href="#" onclick="return change(' + j + ')">' + j + 'x' + j + '</a> ';
  238. }
  239. document.getElementById('options').innerHTML = options;
  240. init();
  241. animate();
  242. function init() {
  243. container = document.createElement( 'div' );
  244. document.body.appendChild( container );
  245. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  246. camera.position.set( 0, 200, 350 );
  247. scene = new THREE.Scene();
  248. var sun = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
  249. sun.position.set( 300, 400, 175 );
  250. scene.add( sun );
  251. var sun2 = new THREE.DirectionalLight( 0x40A040, 0.6 );
  252. sun2.position.set( -100, 350, -200 );
  253. scene.add( sun2 );
  254. renderer = new THREE.WebGLRenderer( { premultipliedAlpha : false } );
  255. renderer.setPixelRatio( window.devicePixelRatio );
  256. renderer.setSize( window.innerWidth, window.innerHeight );
  257. container.appendChild( renderer.domElement );
  258. controls = new THREE.OrbitControls( camera, renderer.domElement );
  259. stats = new Stats();
  260. container.appendChild( stats.dom );
  261. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  262. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  263. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  264. document.addEventListener( 'keydown', function( event ) {
  265. // W Pressed: Toggle wireframe
  266. if ( event.keyCode === 87 ) {
  267. waterMesh.material.wireframe = ! waterMesh.material.wireframe;
  268. waterMesh.material.needsUpdate = true;
  269. }
  270. } , false );
  271. window.addEventListener( 'resize', onWindowResize, false );
  272. var gui = new dat.GUI();
  273. var effectController = {
  274. mouseSize: 20.0,
  275. viscosity: 0.03
  276. };
  277. var valuesChanger = function() {
  278. heightmapVariable.material.uniforms.mouseSize.value = effectController.mouseSize;
  279. heightmapVariable.material.uniforms.viscosityConstant.value = effectController.viscosity;
  280. };
  281. gui.add( effectController, "mouseSize", 1.0, 100.0, 1.0 ).onChange( valuesChanger );
  282. gui.add( effectController, "viscosity", 0.0, 0.1, 0.001 ).onChange( valuesChanger );
  283. var buttonSmooth = {
  284. smoothWater: function() {
  285. smoothWater();
  286. }
  287. };
  288. gui.add( buttonSmooth, 'smoothWater' );
  289. initWater();
  290. valuesChanger();
  291. }
  292. function initWater() {
  293. var materialColor = 0x0040C0;
  294. var geometry = new THREE.PlaneBufferGeometry( BOUNDS, BOUNDS, WIDTH - 1, WIDTH -1 );
  295. // material: make a ShaderMaterial clone of MeshPhongMaterial, with customized vertex shader
  296. var material = new THREE.ShaderMaterial( {
  297. uniforms: THREE.UniformsUtils.merge( [
  298. THREE.ShaderLib[ 'phong' ].uniforms,
  299. {
  300. heightmap: { value: null }
  301. }
  302. ] ),
  303. vertexShader: document.getElementById( 'waterVertexShader' ).textContent,
  304. fragmentShader: THREE.ShaderChunk[ 'meshphong_frag' ]
  305. } );
  306. material.lights = true;
  307. // Material attributes from MeshPhongMaterial
  308. material.color = new THREE.Color( materialColor );
  309. material.specular = new THREE.Color( 0x111111 );
  310. material.shininess = 50;
  311. // Sets the uniforms with the material values
  312. material.uniforms.diffuse.value = material.color;
  313. material.uniforms.specular.value = material.specular;
  314. material.uniforms.shininess.value = Math.max( material.shininess, 1e-4 );
  315. material.uniforms.opacity.value = material.opacity;
  316. // Defines
  317. material.defines.WIDTH = WIDTH.toFixed( 1 );
  318. material.defines.BOUNDS = BOUNDS.toFixed( 1 );
  319. waterUniforms = material.uniforms;
  320. waterMesh = new THREE.Mesh( geometry, material );
  321. waterMesh.rotation.x = - Math.PI / 2;
  322. waterMesh.matrixAutoUpdate = false;
  323. waterMesh.updateMatrix();
  324. scene.add( waterMesh );
  325. // Mesh just for mouse raycasting
  326. var geometryRay = new THREE.PlaneBufferGeometry( BOUNDS, BOUNDS, 1, 1 );
  327. meshRay = new THREE.Mesh( geometryRay, new THREE.MeshBasicMaterial( { color: 0xFFFFFF, visible: false } ) );
  328. meshRay.rotation.x = - Math.PI / 2;
  329. meshRay.matrixAutoUpdate = false;
  330. meshRay.updateMatrix();
  331. scene.add( meshRay );
  332. // Creates the gpu computation class and sets it up
  333. gpuCompute = new GPUComputationRenderer( WIDTH, WIDTH, renderer );
  334. var heightmap0 = gpuCompute.createTexture();
  335. fillTexture( heightmap0 );
  336. heightmapVariable = gpuCompute.addVariable( "heightmap", document.getElementById( 'heightmapFragmentShader' ).textContent, heightmap0 );
  337. gpuCompute.setVariableDependencies( heightmapVariable, [ heightmapVariable ] );
  338. heightmapVariable.material.uniforms.mousePos = { value: new THREE.Vector2( 10000, 10000 ) };
  339. heightmapVariable.material.uniforms.mouseSize = { value: 20.0 };
  340. heightmapVariable.material.uniforms.viscosityConstant = { value: 0.03 };
  341. heightmapVariable.material.uniforms.heightCompensation = { value: 0 };
  342. heightmapVariable.material.defines.BOUNDS = BOUNDS.toFixed( 1 );
  343. var error = gpuCompute.init();
  344. if ( error !== null ) {
  345. console.error( error );
  346. }
  347. // Create compute shader to smooth the water surface and velocity
  348. smoothShader = gpuCompute.createShaderMaterial( document.getElementById( 'smoothFragmentShader' ).textContent, { texture: { value: null } } );
  349. // Create compute shader to read water volume
  350. readVolumeShader = gpuCompute.createShaderMaterial( document.getElementById( 'sumFragmentShader' ).textContent, { texture: { value: null } } );
  351. readVolumeShader.blending = 0;
  352. // Create a 1x1 pixel image and a render target (Uint8, 4 channels, 1 byte per channel) to read water height
  353. readVolumeImage = new Uint8Array( 1 * 1 * 4 );
  354. readVolumeRenderTarget = new THREE.WebGLRenderTarget( 1, 1, {
  355. wrapS: THREE.ClampToEdgeWrapping,
  356. wrapT: THREE.ClampToEdgeWrapping,
  357. minFilter: THREE.NearestFilter,
  358. magFilter: THREE.NearestFilter,
  359. format: THREE.RGBAFormat,
  360. type: THREE.UnsignedByteType,
  361. stencilBuffer: false,
  362. depthBuffer: false
  363. } );
  364. }
  365. function fillTexture( texture ) {
  366. var waterMaxHeight = 10;
  367. function noise( x, y, z ) {
  368. var multR = waterMaxHeight;
  369. var mult = 0.025;
  370. var r = 0;
  371. for ( var i = 0; i < 15; i++ ) {
  372. r += multR * simplex.noise( x * mult, y * mult );
  373. multR *= 0.53 + 0.025 * i;
  374. mult *= 1.25;
  375. }
  376. return r;
  377. }
  378. var pixels = texture.image.data;
  379. var p = 0;
  380. for ( var j = 0; j < WIDTH; j++ ) {
  381. for ( var i = 0; i < WIDTH; i++ ) {
  382. var x = i * 128 / WIDTH;
  383. var y = j * 128 / WIDTH;
  384. pixels[ p + 0 ] = noise( x, y, 123.4 );
  385. pixels[ p + 1 ] = 0;
  386. pixels[ p + 2 ] = 0;
  387. pixels[ p + 3 ] = 1;
  388. p += 4;
  389. }
  390. }
  391. }
  392. function smoothWater() {
  393. var currentRenderTarget = gpuCompute.getCurrentRenderTarget( heightmapVariable );
  394. var alternateRenderTarget = gpuCompute.getAlternateRenderTarget( heightmapVariable );
  395. for ( var i = 0; i < 10; i++ ) {
  396. smoothShader.uniforms.texture.value = currentRenderTarget.texture;
  397. gpuCompute.doRenderTarget( smoothShader, alternateRenderTarget );
  398. smoothShader.uniforms.texture.value = alternateRenderTarget.texture;
  399. gpuCompute.doRenderTarget( smoothShader, currentRenderTarget );
  400. }
  401. }
  402. function readWaterLevel() {
  403. // Returns current average water level
  404. var currentRenderTarget = gpuCompute.getCurrentRenderTarget( heightmapVariable );
  405. readVolumeShader.uniforms.texture.value = currentRenderTarget.texture;
  406. gpuCompute.doRenderTarget( readVolumeShader, readVolumeRenderTarget );
  407. var gl = renderer.context;
  408. gl.readPixels( 0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, readVolumeImage );
  409. var pixels = new Float32Array( readVolumeImage.buffer );
  410. return pixels[ 0 ] / NUM_TEXELS;
  411. }
  412. function onWindowResize() {
  413. windowHalfX = window.innerWidth / 2;
  414. windowHalfY = window.innerHeight / 2;
  415. camera.aspect = window.innerWidth / window.innerHeight;
  416. camera.updateProjectionMatrix();
  417. renderer.setSize( window.innerWidth, window.innerHeight );
  418. }
  419. function setMouseCoords( x, y ) {
  420. mouseCoords.set( ( x / renderer.domElement.clientWidth ) * 2 - 1, - ( y / renderer.domElement.clientHeight ) * 2 + 1 );
  421. mouseMoved = true;
  422. }
  423. function onDocumentMouseMove( event ) {
  424. setMouseCoords( event.clientX, event.clientY );
  425. }
  426. function onDocumentTouchStart( event ) {
  427. if ( event.touches.length === 1 ) {
  428. event.preventDefault();
  429. setMouseCoords( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  430. }
  431. }
  432. function onDocumentTouchMove( event ) {
  433. if ( event.touches.length === 1 ) {
  434. event.preventDefault();
  435. setMouseCoords( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  436. }
  437. }
  438. function animate() {
  439. requestAnimationFrame( animate );
  440. render();
  441. stats.update();
  442. }
  443. function render() {
  444. // Set uniforms: mouse interaction
  445. var uniforms = heightmapVariable.material.uniforms;
  446. if ( mouseMoved ) {
  447. raycaster.setFromCamera( mouseCoords, camera );
  448. var intersects = raycaster.intersectObject( meshRay );
  449. if ( intersects.length > 0 ) {
  450. var point = intersects[ 0 ].point;
  451. uniforms.mousePos.value.set( point.x, point.z );
  452. }
  453. else {
  454. uniforms.mousePos.value.set( 10000, 10000 );
  455. }
  456. mouseMoved = false;
  457. }
  458. else {
  459. uniforms.mousePos.value.set( 10000, 10000 );
  460. }
  461. // Read height value once in a time, since it has some cost
  462. if ( ++numFrames > 120 ) {
  463. numFrames = 0;
  464. heightCompensation = readWaterLevel();
  465. }
  466. // Apply gradually height compensation to reset water level
  467. if ( heightCompensation !== 0 ) {
  468. var decremHeight = Math.min( 1, Math.abs( heightCompensation ) ) * Math.sign( heightCompensation );
  469. heightCompensation -= decremHeight;
  470. heightmapVariable.material.uniforms.heightCompensation.value = - decremHeight * Math.sign( heightCompensation );
  471. }
  472. // Do the gpu computation
  473. gpuCompute.compute();
  474. // Get compute output in custom uniform
  475. waterUniforms.heightmap.value = gpuCompute.getCurrentRenderTarget( heightmapVariable ).texture;
  476. // Render
  477. renderer.render( scene, camera );
  478. }
  479. </script>
  480. </body>
  481. </html>