webgpu_backdrop_water.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Backdrop 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Backdrop Water
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { color, vec2, pass, linearDepth, normalWorld, triplanarTexture, texture, objectPosition, screenUV, viewportLinearDepth, viewportDepthTexture, viewportSharedTexture, mx_worley_noise_float, positionWorld, timerLocal } from 'three/tsl';
  25. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. let camera, scene, renderer;
  31. let mixer, objects, clock;
  32. let model, floor, floorPosition;
  33. let postProcessing;
  34. let controls;
  35. let stats;
  36. init();
  37. function init() {
  38. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
  39. camera.position.set( 3, 2, 4 );
  40. scene = new THREE.Scene();
  41. scene.fog = new THREE.Fog( 0x0487e2, 7, 25 );
  42. scene.backgroundNode = normalWorld.y.mix( color( 0x0487e2 ), color( 0x0066ff ) );
  43. camera.lookAt( 0, 1, 0 );
  44. const sunLight = new THREE.DirectionalLight( 0xFFE499, 5 );
  45. sunLight.castShadow = true;
  46. sunLight.shadow.camera.near = .1;
  47. sunLight.shadow.camera.far = 5;
  48. sunLight.shadow.camera.right = 2;
  49. sunLight.shadow.camera.left = - 2;
  50. sunLight.shadow.camera.top = 1;
  51. sunLight.shadow.camera.bottom = - 2;
  52. sunLight.shadow.mapSize.width = 2048;
  53. sunLight.shadow.mapSize.height = 2048;
  54. sunLight.shadow.bias = - 0.001;
  55. sunLight.position.set( .5, 3, .5 );
  56. const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
  57. const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
  58. scene.add( sunLight );
  59. scene.add( skyAmbientLight );
  60. scene.add( waterAmbientLight );
  61. clock = new THREE.Clock();
  62. // animated model
  63. const loader = new GLTFLoader();
  64. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  65. model = gltf.scene;
  66. model.children[ 0 ].children[ 0 ].castShadow = true;
  67. mixer = new THREE.AnimationMixer( model );
  68. const action = mixer.clipAction( gltf.animations[ 0 ] );
  69. action.play();
  70. scene.add( model );
  71. } );
  72. // objects
  73. const textureLoader = new THREE.TextureLoader();
  74. const iceDiffuse = textureLoader.load( './textures/water.jpg' );
  75. iceDiffuse.wrapS = THREE.RepeatWrapping;
  76. iceDiffuse.wrapT = THREE.RepeatWrapping;
  77. iceDiffuse.colorSpace = THREE.NoColorSpace;
  78. const iceColorNode = triplanarTexture( texture( iceDiffuse ) ).add( color( 0x0066ff ) ).mul( .8 );
  79. const geometry = new THREE.IcosahedronGeometry( 1, 3 );
  80. const material = new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } );
  81. const count = 100;
  82. const scale = 3.5;
  83. const column = 10;
  84. objects = new THREE.Group();
  85. for ( let i = 0; i < count; i ++ ) {
  86. const x = i % column;
  87. const y = i / column;
  88. const mesh = new THREE.Mesh( geometry, material );
  89. mesh.position.set( x * scale, 0, y * scale );
  90. mesh.rotation.set( Math.random(), Math.random(), Math.random() );
  91. objects.add( mesh );
  92. }
  93. objects.position.set(
  94. ( ( column - 1 ) * scale ) * - .5,
  95. - 1,
  96. ( ( count / column ) * scale ) * - .5
  97. );
  98. scene.add( objects );
  99. // water
  100. const timer = timerLocal( .8 );
  101. const floorUV = positionWorld.xzy;
  102. const waterLayer0 = mx_worley_noise_float( floorUV.mul( 4 ).add( timer ) );
  103. const waterLayer1 = mx_worley_noise_float( floorUV.mul( 2 ).add( timer ) );
  104. const waterIntensity = waterLayer0.mul( waterLayer1 );
  105. const waterColor = waterIntensity.mul( 1.4 ).mix( color( 0x0487e2 ), color( 0x74ccf4 ) );
  106. // linearDepth() returns the linear depth of the mesh
  107. const depth = linearDepth();
  108. const depthWater = viewportLinearDepth.sub( depth );
  109. const depthEffect = depthWater.remapClamp( - .002, .04 );
  110. const refractionUV = screenUV.add( vec2( 0, waterIntensity.mul( .1 ) ) );
  111. // linearDepth( viewportDepthTexture( uv ) ) return the linear depth of the scene
  112. const depthTestForRefraction = linearDepth( viewportDepthTexture( refractionUV ) ).sub( depth );
  113. const depthRefraction = depthTestForRefraction.remapClamp( 0, .1 );
  114. const finalUV = depthTestForRefraction.lessThan( 0 ).select( screenUV, refractionUV );
  115. const viewportTexture = viewportSharedTexture( finalUV );
  116. const waterMaterial = new THREE.MeshBasicNodeMaterial();
  117. waterMaterial.colorNode = waterColor;
  118. waterMaterial.backdropNode = depthEffect.mix( viewportSharedTexture(), viewportTexture.mul( depthRefraction.mix( 1, waterColor ) ) );
  119. waterMaterial.backdropAlphaNode = depthRefraction.oneMinus();
  120. waterMaterial.transparent = true;
  121. const water = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), waterMaterial );
  122. water.position.set( 0, 0, 0 );
  123. scene.add( water );
  124. // floor
  125. floor = new THREE.Mesh( new THREE.CylinderGeometry( 1.1, 1.1, 10 ), new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } ) );
  126. floor.position.set( 0, - 5, 0 );
  127. scene.add( floor );
  128. // caustics
  129. const waterPosY = positionWorld.y.sub( water.position.y );
  130. let transition = waterPosY.add( .1 ).saturate().oneMinus();
  131. transition = waterPosY.lessThan( 0 ).select( transition, normalWorld.y.mix( transition, 0 ) ).toVar();
  132. const colorNode = transition.mix( material.colorNode, material.colorNode.add( waterLayer0 ) );
  133. //material.colorNode = colorNode;
  134. floor.material.colorNode = colorNode;
  135. // renderer
  136. renderer = new THREE.WebGPURenderer( /*{ antialias: true }*/ );
  137. renderer.setPixelRatio( window.devicePixelRatio );
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. renderer.setAnimationLoop( animate );
  140. document.body.appendChild( renderer.domElement );
  141. stats = new Stats();
  142. document.body.appendChild( stats.dom );
  143. controls = new OrbitControls( camera, renderer.domElement );
  144. controls.minDistance = 1;
  145. controls.maxDistance = 10;
  146. controls.maxPolarAngle = Math.PI * 0.9;
  147. controls.autoRotate = true;
  148. controls.autoRotateSpeed = 1;
  149. controls.target.set( 0, .2, 0 );
  150. controls.update();
  151. // gui
  152. const gui = new GUI();
  153. floorPosition = new THREE.Vector3( 0, .2, 0 );
  154. gui.add( floorPosition, 'y', - 1, 1, .001 ).name( 'position' );
  155. // post processing
  156. const scenePass = pass( scene, camera );
  157. const scenePassColor = scenePass.getTextureNode();
  158. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .3, .5 );
  159. const waterMask = objectPosition( camera ).y.greaterThan( screenUV.y.sub( .5 ).mul( camera.near ) );
  160. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  161. scenePassColorBlurred.directionNode = waterMask.select( scenePassDepth, scenePass.getLinearDepthNode().mul( 5 ) );
  162. const vignet = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus();
  163. postProcessing = new THREE.PostProcessing( renderer );
  164. postProcessing.outputNode = waterMask.select( scenePassColorBlurred, scenePassColorBlurred.mul( color( 0x74ccf4 ) ).mul( vignet ) );
  165. //
  166. window.addEventListener( 'resize', onWindowResize );
  167. }
  168. function onWindowResize() {
  169. camera.aspect = window.innerWidth / window.innerHeight;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( window.innerWidth, window.innerHeight );
  172. }
  173. function animate() {
  174. stats.update();
  175. controls.update();
  176. const delta = clock.getDelta();
  177. floor.position.y = floorPosition.y - 5;
  178. if ( model ) {
  179. mixer.update( delta );
  180. model.position.y = floorPosition.y;
  181. }
  182. for ( const object of objects.children ) {
  183. object.position.y = Math.sin( clock.elapsedTime + object.id ) * .3;
  184. object.rotation.y += delta * .3;
  185. }
  186. postProcessing.render();
  187. }
  188. </script>
  189. </body>
  190. </html>