webgpu_backdrop_water.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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, depth, vec2, pass, depthTexture, normalWorld, triplanarTexture, texture, objectPosition, viewportTopLeft, 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. const depthWater = depthTexture( viewportDepthTexture() ).sub( depth );
  114. const depthEffect = depthWater.remapClamp( - .002, .04 );
  115. const refractionUV = viewportTopLeft.add( vec2( 0, waterIntensity.mul( .1 ) ) );
  116. const depthTestForRefraction = depthTexture( viewportDepthTexture( refractionUV ) ).sub( depth );
  117. const depthRefraction = depthTestForRefraction.remapClamp( 0, .1 );
  118. const finalUV = depthTestForRefraction.lessThan( 0 ).cond( viewportTopLeft, refractionUV );
  119. const viewportTexture = viewportSharedTexture( finalUV );
  120. const waterMaterial = new MeshBasicNodeMaterial();
  121. waterMaterial.colorNode = waterColor;
  122. waterMaterial.backdropNode = depthEffect.mix( viewportSharedTexture(), viewportTexture.mul( depthRefraction.mix( 1, waterColor ) ) );
  123. waterMaterial.backdropAlphaNode = depthRefraction.oneMinus();
  124. waterMaterial.transparent = true;
  125. const water = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), waterMaterial );
  126. water.position.set( 0, 0, 0 );
  127. scene.add( water );
  128. // floor
  129. floor = new THREE.Mesh( new THREE.CylinderGeometry( 1.1, 1.1, 10 ), new MeshStandardNodeMaterial( { colorNode: iceColorNode } ) );
  130. floor.position.set( 0, - 5, 0 );
  131. scene.add( floor );
  132. // caustics
  133. const waterPosY = positionWorld.y.sub( water.position.y );
  134. let transition = waterPosY.add( .1 ).saturate().oneMinus();
  135. transition = waterPosY.lessThan( 0 ).cond( transition, normalWorld.y.mix( transition, 0 ) ).toVar();
  136. const colorNode = transition.mix( material.colorNode, material.colorNode.add( waterLayer0 ) );
  137. //material.colorNode = colorNode;
  138. floor.material.colorNode = colorNode;
  139. // renderer
  140. renderer = new WebGPURenderer( /*{ antialias: true }*/ );
  141. renderer.setPixelRatio( window.devicePixelRatio );
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. renderer.setAnimationLoop( animate );
  144. document.body.appendChild( renderer.domElement );
  145. stats = new Stats();
  146. document.body.appendChild( stats.dom );
  147. controls = new OrbitControls( camera, renderer.domElement );
  148. controls.minDistance = 1;
  149. controls.maxDistance = 10;
  150. controls.maxPolarAngle = Math.PI * 0.9;
  151. controls.autoRotate = true;
  152. controls.autoRotateSpeed = 1;
  153. controls.target.set( 0, .2, 0 );
  154. controls.update();
  155. // gui
  156. const gui = new GUI();
  157. floorPosition = new THREE.Vector3( 0, .2, 0 );
  158. gui.add( floorPosition, 'y', - 1, 1, .001 ).name( 'position' );
  159. // post processing
  160. const scenePass = pass( scene, camera );
  161. const scenePassColor = scenePass.getTextureNode();
  162. const scenePassDepth = scenePass.getDepthNode().remapClamp( .3, .5 );
  163. const waterMask = objectPosition( camera ).y.greaterThan( 0 );
  164. const scenePassColorBlurred = scenePassColor.gaussianBlur();
  165. scenePassColorBlurred.directionNode = waterMask.cond( scenePassDepth, scenePass.getDepthNode().mul( 5 ) );
  166. const vignet = viewportTopLeft.distance( .5 ).mul( 1.35 ).clamp().oneMinus();
  167. postProcessing = new PostProcessing( renderer );
  168. postProcessing.outputNode = waterMask.cond( scenePassColorBlurred, scenePassColorBlurred.mul( color( 0x74ccf4 ) ).mul( vignet ) );
  169. //
  170. window.addEventListener( 'resize', onWindowResize );
  171. }
  172. function onWindowResize() {
  173. camera.aspect = window.innerWidth / window.innerHeight;
  174. camera.updateProjectionMatrix();
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. }
  177. function animate() {
  178. stats.update();
  179. controls.update();
  180. const delta = clock.getDelta();
  181. floor.position.y = floorPosition.y - 5;
  182. if ( model ) {
  183. mixer.update( delta );
  184. model.position.y = floorPosition.y;
  185. }
  186. for ( const object of objects.children ) {
  187. object.position.y = Math.sin( clock.elapsedTime + object.id ) * .3;
  188. object.rotation.y += delta * .3;
  189. }
  190. postProcessing.render();
  191. }
  192. </script>
  193. </body>
  194. </html>