webgpu_backdrop_water.html 8.9 KB

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