webgpu_backdrop_water.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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, positionWorld, time } from 'three/tsl';
  34. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  35. import { voronoi2d, voronoi3d } from 'three/addons/tsl/math/voronoiNoise.js';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. let camera, scene, renderer;
  40. let mixer, objects, timer;
  41. let model, floor, floorPosition;
  42. let renderPipeline;
  43. let controls;
  44. init();
  45. function init() {
  46. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
  47. camera.position.set( 3, 2, 4 );
  48. scene = new THREE.Scene();
  49. scene.fog = new THREE.Fog( 0x0487e2, 7, 25 );
  50. scene.backgroundNode = normalWorld.y.mix( color( 0x0487e2 ), color( 0x0066ff ) );
  51. camera.lookAt( 0, 1, 0 );
  52. const sunLight = new THREE.DirectionalLight( 0xFFE499, 5 );
  53. sunLight.position.set( .5, 3, .5 );
  54. sunLight.castShadow = true;
  55. sunLight.shadow.mapSize.width = 512;
  56. sunLight.shadow.mapSize.height = 512;
  57. sunLight.shadow.camera.near = 0.5;
  58. sunLight.shadow.camera.far = 15;
  59. sunLight.shadow.camera.left = - 1.5;
  60. sunLight.shadow.camera.right = 1.5;
  61. sunLight.shadow.camera.top = 1.5;
  62. sunLight.shadow.camera.bottom = - 1.5;
  63. sunLight.shadow.bias = - 0.001;
  64. const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
  65. const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
  66. scene.add( sunLight );
  67. scene.add( skyAmbientLight );
  68. scene.add( waterAmbientLight );
  69. timer = new THREE.Timer();
  70. timer.connect( document );
  71. // animated model
  72. const loader = new GLTFLoader();
  73. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  74. model = gltf.scene;
  75. model.traverse( ( child ) => {
  76. if ( child.isMesh ) {
  77. child.castShadow = true;
  78. child.receiveShadow = true;
  79. }
  80. } );
  81. mixer = new THREE.AnimationMixer( model );
  82. const action = mixer.clipAction( gltf.animations[ 0 ] );
  83. action.play();
  84. scene.add( model );
  85. } );
  86. // objects
  87. const textureLoader = new THREE.TextureLoader();
  88. const iceDiffuse = textureLoader.load( './textures/water.jpg' );
  89. iceDiffuse.wrapS = THREE.RepeatWrapping;
  90. iceDiffuse.wrapT = THREE.RepeatWrapping;
  91. iceDiffuse.colorSpace = THREE.NoColorSpace;
  92. const iceColorNode = triplanarTexture( texture( iceDiffuse ) ).add( color( 0x0066ff ) ).mul( .8 );
  93. const geometry = new THREE.IcosahedronGeometry( 1, 3 );
  94. const material = new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } );
  95. const count = 100;
  96. const scale = 3.5;
  97. const column = 10;
  98. objects = new THREE.Group();
  99. for ( let i = 0; i < count; i ++ ) {
  100. const x = i % column;
  101. const y = i / column;
  102. const mesh = new THREE.Mesh( geometry, material );
  103. mesh.position.set( x * scale, 0, y * scale );
  104. mesh.rotation.set( Math.random(), Math.random(), Math.random() );
  105. objects.add( mesh );
  106. }
  107. objects.position.set(
  108. ( ( column - 1 ) * scale ) * - .5,
  109. - 1,
  110. ( ( count / column ) * scale ) * - .5
  111. );
  112. scene.add( objects );
  113. // water
  114. const t = time.mul( .8 );
  115. const floorUV = positionWorld.xz;
  116. const waterLayer0 = voronoi2d( floorUV.mul( 6 ), t );
  117. const waterLayer1 = voronoi2d( floorUV.mul( 3 ), t );
  118. const waterIntensity = waterLayer0.mul( waterLayer1 );
  119. const waterColor = waterIntensity.mul( 1.4 ).mix( color( 0x0487e2 ), color( 0x74ccf4 ) );
  120. // linearDepth() returns the linear depth of the mesh
  121. const depth = linearDepth();
  122. const depthWater = viewportLinearDepth.sub( depth ).toInspector( 'Water / Depth', ( node ) => node.oneMinus() );
  123. const depthEffect = depthWater.remapClamp( - .002, .04 );
  124. const refractionUV = screenUV.add( vec2( 0, waterIntensity.mul( .1 ) ) ).toInspector( 'Water / Refraction UV' );
  125. // linearDepth( viewportDepthTexture( uv ) ) return the linear depth of the scene
  126. const depthTestForRefraction = linearDepth( viewportDepthTexture( refractionUV ) ).sub( depth );
  127. const depthRefraction = depthTestForRefraction.remapClamp( 0, .1 );
  128. const finalUV = depthTestForRefraction.lessThan( 0 ).select( screenUV, refractionUV );
  129. const viewportTexture = viewportSharedTexture( finalUV ).toInspector( 'Water / Viewport Texture + Refraction UV' );
  130. const waterMaterial = new THREE.MeshBasicNodeMaterial();
  131. waterMaterial.colorNode = waterColor.toInspector( 'Water / Color' );
  132. waterMaterial.backdropNode = depthEffect.mix( viewportSharedTexture(), viewportTexture.mul( depthRefraction.mix( 1, waterColor ) ) ).mul( color( 0xd3ebf8 ) );
  133. waterMaterial.backdropAlphaNode = depthRefraction.oneMinus();
  134. waterMaterial.transparent = true;
  135. const water = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), waterMaterial );
  136. water.position.set( 0, 0, 0 );
  137. scene.add( water );
  138. // floor
  139. floor = new THREE.Mesh( new THREE.CylinderGeometry( 1.1, 1.1, 10 ), new THREE.MeshStandardNodeMaterial( { colorNode: iceColorNode } ) );
  140. floor.position.set( 0, - 5, 0 );
  141. floor.receiveShadow = true;
  142. scene.add( floor );
  143. // caustics
  144. const causticFade = normalWorld.y.mix( positionWorld.y.distance( 0 ).oneMinus().saturate(), 0 );
  145. const causticNoise = voronoi3d( positionWorld.mul( 6 ), t );
  146. //material.colorNode = colorNode;
  147. floor.material.colorNode = causticFade.mix( material.colorNode, material.colorNode.add( causticNoise ) );
  148. // renderer
  149. renderer = new THREE.WebGPURenderer();
  150. renderer.setPixelRatio( window.devicePixelRatio );
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. renderer.setAnimationLoop( animate );
  153. renderer.shadowMap.enabled = true;
  154. renderer.inspector = new Inspector();
  155. document.body.appendChild( renderer.domElement );
  156. controls = new OrbitControls( camera, renderer.domElement );
  157. controls.minDistance = 1;
  158. controls.maxDistance = 10;
  159. controls.maxPolarAngle = Math.PI * 0.9;
  160. controls.autoRotate = true;
  161. controls.autoRotateSpeed = 1;
  162. controls.target.set( 0, .2, 0 );
  163. controls.update();
  164. // gui
  165. const gui = renderer.inspector.createParameters( 'Settings' );
  166. floorPosition = new THREE.Vector3( 0, .2, 0 );
  167. gui.add( floorPosition, 'y', - 1, 1, .001 ).name( 'floor position' );
  168. // post processing
  169. const scenePass = pass( scene, camera );
  170. const scenePassColor = scenePass.getTextureNode();
  171. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .3, .5 );
  172. const waterMask = objectPosition( camera ).y.greaterThan( screenUV.y.sub( .5 ).mul( camera.near ) ).toInspector( 'Post-Processing / Water Mask' );
  173. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  174. scenePassColorBlurred.directionNode = waterMask.select( scenePassDepth, scenePass.getLinearDepthNode().mul( 5 ) ).toInspector( 'Post-Processing / Blur Strength [ Depth ]', ( node ) => node.toFloat() );
  175. const vignette = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus().toInspector( 'Post-Processing / Vignette' );
  176. renderPipeline = new THREE.RenderPipeline( renderer );
  177. renderPipeline.outputNode = waterMask.select( scenePassColorBlurred, scenePassColorBlurred.mul( color( 0x74ccf4 ) ).mul( vignette ) );
  178. //
  179. window.addEventListener( 'resize', onWindowResize );
  180. }
  181. function onWindowResize() {
  182. camera.aspect = window.innerWidth / window.innerHeight;
  183. camera.updateProjectionMatrix();
  184. renderer.setSize( window.innerWidth, window.innerHeight );
  185. }
  186. function animate() {
  187. timer.update();
  188. controls.update();
  189. const delta = timer.getDelta();
  190. floor.position.y = floorPosition.y - 5;
  191. if ( model ) {
  192. mixer.update( delta );
  193. model.position.y = floorPosition.y;
  194. }
  195. for ( const object of objects.children ) {
  196. object.position.y = Math.sin( timer.getElapsed() + object.id ) * .3;
  197. object.rotation.y += delta * .3;
  198. }
  199. renderPipeline.render();
  200. }
  201. </script>
  202. </body>
  203. </html>
粤ICP备19079148号