webgpu_backdrop_water.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Backdrop Water</span>
  14. </div>
  15. <small>Water refraction with depth effect.</small>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three/webgpu';
  29. import { color, vec2, pass, linearDepth, normalWorld, triplanarTexture, texture, objectPosition, screenUV, viewportLinearDepth, viewportDepthTexture, viewportSharedTexture, mx_worley_noise_float, positionWorld, time } from 'three/tsl';
  30. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  31. import { Inspector } from 'three/addons/inspector/Inspector.js';
  32. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. let camera, scene, renderer;
  35. let mixer, objects, clock;
  36. let model, floor, floorPosition;
  37. let postProcessing;
  38. let controls;
  39. init();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
  42. camera.position.set( 3, 2, 4 );
  43. scene = new THREE.Scene();
  44. scene.fog = new THREE.Fog( 0x0487e2, 7, 25 );
  45. scene.backgroundNode = normalWorld.y.mix( color( 0x0487e2 ), color( 0x0066ff ) );
  46. camera.lookAt( 0, 1, 0 );
  47. const sunLight = new THREE.DirectionalLight( 0xFFE499, 5 );
  48. sunLight.castShadow = true;
  49. sunLight.shadow.camera.near = .1;
  50. sunLight.shadow.camera.far = 5;
  51. sunLight.shadow.camera.right = 2;
  52. sunLight.shadow.camera.left = - 2;
  53. sunLight.shadow.camera.top = 1;
  54. sunLight.shadow.camera.bottom = - 2;
  55. sunLight.shadow.mapSize.width = 2048;
  56. sunLight.shadow.mapSize.height = 2048;
  57. sunLight.shadow.bias = - 0.001;
  58. sunLight.position.set( .5, 3, .5 );
  59. const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
  60. const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
  61. scene.add( sunLight );
  62. scene.add( skyAmbientLight );
  63. scene.add( waterAmbientLight );
  64. clock = new THREE.Clock();
  65. // animated model
  66. const loader = new GLTFLoader();
  67. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  68. model = gltf.scene;
  69. model.children[ 0 ].children[ 0 ].castShadow = true;
  70. mixer = new THREE.AnimationMixer( model );
  71. const action = mixer.clipAction( gltf.animations[ 0 ] );
  72. action.play();
  73. scene.add( model );
  74. } );
  75. // objects
  76. const textureLoader = new THREE.TextureLoader();
  77. const iceDiffuse = textureLoader.load( './textures/water.jpg' );
  78. iceDiffuse.wrapS = THREE.RepeatWrapping;
  79. iceDiffuse.wrapT = THREE.RepeatWrapping;
  80. iceDiffuse.colorSpace = THREE.NoColorSpace;
  81. const iceColorNode = triplanarTexture( texture( iceDiffuse ) ).add( color( 0x0066ff ) ).mul( .8 );
  82. const geometry = new THREE.IcosahedronGeometry( 1, 3 );
  83. const material = new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } );
  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. - 1,
  99. ( ( count / column ) * scale ) * - .5
  100. );
  101. scene.add( objects );
  102. // water
  103. const timer = time.mul( .8 );
  104. const floorUV = positionWorld.xzy;
  105. const waterLayer0 = mx_worley_noise_float( floorUV.mul( 4 ).add( timer ) );
  106. const waterLayer1 = mx_worley_noise_float( floorUV.mul( 2 ).add( timer ) );
  107. const waterIntensity = waterLayer0.mul( waterLayer1 );
  108. const waterColor = waterIntensity.mul( 1.4 ).mix( color( 0x0487e2 ), color( 0x74ccf4 ) );
  109. // linearDepth() returns the linear depth of the mesh
  110. const depth = linearDepth();
  111. const depthWater = viewportLinearDepth.sub( depth ).toInspector( 'Water / Depth', ( node ) => node.oneMinus() );
  112. const depthEffect = depthWater.remapClamp( - .002, .04 );
  113. const refractionUV = screenUV.add( vec2( 0, waterIntensity.mul( .1 ) ) ).toInspector( 'Water / Refraction UV' );
  114. // linearDepth( viewportDepthTexture( uv ) ) return the linear depth of the scene
  115. const depthTestForRefraction = linearDepth( viewportDepthTexture( refractionUV ) ).sub( depth );
  116. const depthRefraction = depthTestForRefraction.remapClamp( 0, .1 );
  117. const finalUV = depthTestForRefraction.lessThan( 0 ).select( screenUV, refractionUV );
  118. const viewportTexture = viewportSharedTexture( finalUV ).toInspector( 'Water / Viewport Texture + Refraction UV' );
  119. const waterMaterial = new THREE.MeshBasicNodeMaterial();
  120. waterMaterial.colorNode = waterColor.toInspector( 'Water / Color' );
  121. waterMaterial.backdropNode = depthEffect.mix( viewportSharedTexture(), viewportTexture.mul( depthRefraction.mix( 1, waterColor ) ) );
  122. waterMaterial.backdropAlphaNode = depthRefraction.oneMinus();
  123. waterMaterial.transparent = true;
  124. const water = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), waterMaterial );
  125. water.position.set( 0, 0, 0 );
  126. scene.add( water );
  127. // floor
  128. floor = new THREE.Mesh( new THREE.CylinderGeometry( 1.1, 1.1, 10 ), new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } ) );
  129. floor.position.set( 0, - 5, 0 );
  130. scene.add( floor );
  131. // caustics
  132. const waterPosY = positionWorld.y.sub( water.position.y );
  133. let transition = waterPosY.add( .1 ).saturate().oneMinus();
  134. transition = waterPosY.lessThan( 0 ).select( transition, normalWorld.y.mix( transition, 0 ) ).toVar();
  135. const colorNode = transition.mix( material.colorNode, material.colorNode.add( waterLayer0 ) );
  136. //material.colorNode = colorNode;
  137. floor.material.colorNode = colorNode;
  138. // renderer
  139. renderer = new THREE.WebGPURenderer();
  140. renderer.setPixelRatio( window.devicePixelRatio );
  141. renderer.setSize( window.innerWidth, window.innerHeight );
  142. renderer.setAnimationLoop( animate );
  143. renderer.inspector = new Inspector();
  144. document.body.appendChild( renderer.domElement );
  145. controls = new OrbitControls( camera, renderer.domElement );
  146. controls.minDistance = 1;
  147. controls.maxDistance = 10;
  148. controls.maxPolarAngle = Math.PI * 0.9;
  149. controls.autoRotate = true;
  150. controls.autoRotateSpeed = 1;
  151. controls.target.set( 0, .2, 0 );
  152. controls.update();
  153. // gui
  154. const gui = renderer.inspector.createParameters( 'Settings' );
  155. floorPosition = new THREE.Vector3( 0, .2, 0 );
  156. gui.add( floorPosition, 'y', - 1, 1, .001 ).name( 'floor position' );
  157. // post processing
  158. const scenePass = pass( scene, camera );
  159. const scenePassColor = scenePass.getTextureNode();
  160. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .3, .5 );
  161. const waterMask = objectPosition( camera ).y.greaterThan( screenUV.y.sub( .5 ).mul( camera.near ) ).toInspector( 'Post-Processing / Water Mask' );
  162. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  163. scenePassColorBlurred.directionNode = waterMask.select( scenePassDepth, scenePass.getLinearDepthNode().mul( 5 ) ).toInspector( 'Post-Processing / Blur Strength [ Depth ]', ( node ) => node.toFloat() );
  164. const vignette = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus().toInspector( 'Post-Processing / Vignette' );
  165. postProcessing = new THREE.PostProcessing( renderer );
  166. postProcessing.outputNode = waterMask.select( scenePassColorBlurred, scenePassColorBlurred.mul( color( 0x74ccf4 ) ).mul( vignette ) );
  167. //
  168. window.addEventListener( 'resize', onWindowResize );
  169. }
  170. function onWindowResize() {
  171. camera.aspect = window.innerWidth / window.innerHeight;
  172. camera.updateProjectionMatrix();
  173. renderer.setSize( window.innerWidth, window.innerHeight );
  174. }
  175. function animate() {
  176. controls.update();
  177. const delta = clock.getDelta();
  178. floor.position.y = floorPosition.y - 5;
  179. if ( model ) {
  180. mixer.update( delta );
  181. model.position.y = floorPosition.y;
  182. }
  183. for ( const object of objects.children ) {
  184. object.position.y = Math.sin( clock.elapsedTime + object.id ) * .3;
  185. object.rotation.y += delta * .3;
  186. }
  187. postProcessing.render();
  188. }
  189. </script>
  190. </body>
  191. </html>
粤ICP备19079148号