webgpu_backdrop_water.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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, timer;
  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.position.set( .5, 3, .5 );
  49. const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
  50. const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
  51. scene.add( sunLight );
  52. scene.add( skyAmbientLight );
  53. scene.add( waterAmbientLight );
  54. timer = new THREE.Timer();
  55. timer.connect( document );
  56. // animated model
  57. const loader = new GLTFLoader();
  58. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  59. model = gltf.scene;
  60. mixer = new THREE.AnimationMixer( model );
  61. const action = mixer.clipAction( gltf.animations[ 0 ] );
  62. action.play();
  63. scene.add( model );
  64. } );
  65. // objects
  66. const textureLoader = new THREE.TextureLoader();
  67. const iceDiffuse = textureLoader.load( './textures/water.jpg' );
  68. iceDiffuse.wrapS = THREE.RepeatWrapping;
  69. iceDiffuse.wrapT = THREE.RepeatWrapping;
  70. iceDiffuse.colorSpace = THREE.NoColorSpace;
  71. const iceColorNode = triplanarTexture( texture( iceDiffuse ) ).add( color( 0x0066ff ) ).mul( .8 );
  72. const geometry = new THREE.IcosahedronGeometry( 1, 3 );
  73. const material = new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } );
  74. const count = 100;
  75. const scale = 3.5;
  76. const column = 10;
  77. objects = new THREE.Group();
  78. for ( let i = 0; i < count; i ++ ) {
  79. const x = i % column;
  80. const y = i / column;
  81. const mesh = new THREE.Mesh( geometry, material );
  82. mesh.position.set( x * scale, 0, y * scale );
  83. mesh.rotation.set( Math.random(), Math.random(), Math.random() );
  84. objects.add( mesh );
  85. }
  86. objects.position.set(
  87. ( ( column - 1 ) * scale ) * - .5,
  88. - 1,
  89. ( ( count / column ) * scale ) * - .5
  90. );
  91. scene.add( objects );
  92. // water
  93. const t = time.mul( .8 );
  94. const floorUV = positionWorld.xzy;
  95. const waterLayer0 = mx_worley_noise_float( floorUV.mul( 4 ).add( t ) );
  96. const waterLayer1 = mx_worley_noise_float( floorUV.mul( 2 ).add( t ) );
  97. const waterIntensity = waterLayer0.mul( waterLayer1 );
  98. const waterColor = waterIntensity.mul( 1.4 ).mix( color( 0x0487e2 ), color( 0x74ccf4 ) );
  99. // linearDepth() returns the linear depth of the mesh
  100. const depth = linearDepth();
  101. const depthWater = viewportLinearDepth.sub( depth ).toInspector( 'Water / Depth', ( node ) => node.oneMinus() );
  102. const depthEffect = depthWater.remapClamp( - .002, .04 );
  103. const refractionUV = screenUV.add( vec2( 0, waterIntensity.mul( .1 ) ) ).toInspector( 'Water / Refraction UV' );
  104. // linearDepth( viewportDepthTexture( uv ) ) return the linear depth of the scene
  105. const depthTestForRefraction = linearDepth( viewportDepthTexture( refractionUV ) ).sub( depth );
  106. const depthRefraction = depthTestForRefraction.remapClamp( 0, .1 );
  107. const finalUV = depthTestForRefraction.lessThan( 0 ).select( screenUV, refractionUV );
  108. const viewportTexture = viewportSharedTexture( finalUV ).toInspector( 'Water / Viewport Texture + Refraction UV' );
  109. const waterMaterial = new THREE.MeshBasicNodeMaterial();
  110. waterMaterial.colorNode = waterColor.toInspector( 'Water / Color' );
  111. waterMaterial.backdropNode = depthEffect.mix( viewportSharedTexture(), viewportTexture.mul( depthRefraction.mix( 1, waterColor ) ) );
  112. waterMaterial.backdropAlphaNode = depthRefraction.oneMinus();
  113. waterMaterial.transparent = true;
  114. const water = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), waterMaterial );
  115. water.position.set( 0, 0, 0 );
  116. scene.add( water );
  117. // floor
  118. floor = new THREE.Mesh( new THREE.CylinderGeometry( 1.1, 1.1, 10 ), new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } ) );
  119. floor.position.set( 0, - 5, 0 );
  120. scene.add( floor );
  121. // caustics
  122. const waterPosY = positionWorld.y.sub( water.position.y );
  123. let transition = waterPosY.add( .1 ).saturate().oneMinus();
  124. transition = waterPosY.lessThan( 0 ).select( transition, normalWorld.y.mix( transition, 0 ) ).toVar();
  125. const colorNode = transition.mix( material.colorNode, material.colorNode.add( waterLayer0 ) );
  126. //material.colorNode = colorNode;
  127. floor.material.colorNode = colorNode;
  128. // renderer
  129. renderer = new THREE.WebGPURenderer();
  130. renderer.setPixelRatio( window.devicePixelRatio );
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. renderer.setAnimationLoop( animate );
  133. renderer.inspector = new Inspector();
  134. document.body.appendChild( renderer.domElement );
  135. controls = new OrbitControls( camera, renderer.domElement );
  136. controls.minDistance = 1;
  137. controls.maxDistance = 10;
  138. controls.maxPolarAngle = Math.PI * 0.9;
  139. controls.autoRotate = true;
  140. controls.autoRotateSpeed = 1;
  141. controls.target.set( 0, .2, 0 );
  142. controls.update();
  143. // gui
  144. const gui = renderer.inspector.createParameters( 'Settings' );
  145. floorPosition = new THREE.Vector3( 0, .2, 0 );
  146. gui.add( floorPosition, 'y', - 1, 1, .001 ).name( 'floor position' );
  147. // post processing
  148. const scenePass = pass( scene, camera );
  149. const scenePassColor = scenePass.getTextureNode();
  150. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .3, .5 );
  151. const waterMask = objectPosition( camera ).y.greaterThan( screenUV.y.sub( .5 ).mul( camera.near ) ).toInspector( 'Post-Processing / Water Mask' );
  152. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  153. scenePassColorBlurred.directionNode = waterMask.select( scenePassDepth, scenePass.getLinearDepthNode().mul( 5 ) ).toInspector( 'Post-Processing / Blur Strength [ Depth ]', ( node ) => node.toFloat() );
  154. const vignette = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus().toInspector( 'Post-Processing / Vignette' );
  155. postProcessing = new THREE.PostProcessing( renderer );
  156. postProcessing.outputNode = waterMask.select( scenePassColorBlurred, scenePassColorBlurred.mul( color( 0x74ccf4 ) ).mul( vignette ) );
  157. //
  158. window.addEventListener( 'resize', onWindowResize );
  159. }
  160. function onWindowResize() {
  161. camera.aspect = window.innerWidth / window.innerHeight;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( window.innerWidth, window.innerHeight );
  164. }
  165. function animate() {
  166. timer.update();
  167. controls.update();
  168. const delta = timer.getDelta();
  169. floor.position.y = floorPosition.y - 5;
  170. if ( model ) {
  171. mixer.update( delta );
  172. model.position.y = floorPosition.y;
  173. }
  174. for ( const object of objects.children ) {
  175. object.position.y = Math.sin( timer.getElapsed() + object.id ) * .3;
  176. object.rotation.y += delta * .3;
  177. }
  178. postProcessing.render();
  179. }
  180. </script>
  181. </body>
  182. </html>
粤ICP备19079148号