webgpu_backdrop_water.html 7.3 KB

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