webgpu_backdrop_water.html 8.4 KB

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