webgpu_backdrop_water.html 7.2 KB

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