1
0

webgpu_backdrop_water.html 8.9 KB

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