webgpu_water.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - 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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - water<br />
  13. <a href="https://skfb.ly/6WOOR" target="_blank" rel="noopener">The Night Pool</a> by
  14. <a href="https://sketchfab.com/syntheticplants" target="_blank" rel="noopener">syntheticplants</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">CC BY 4.0</a><br />
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/webgpu": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.tsl.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { pass, mrt, output, emissive, color, screenUV } from 'three/tsl';
  29. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  33. import { WaterMesh } from 'three/addons/objects/Water2Mesh.js';
  34. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  35. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  36. let scene, camera, renderer, water, postProcessing, controls;
  37. const params = {
  38. color: '#99e0ff',
  39. scale: 2,
  40. flowX: 1,
  41. flowY: 1
  42. };
  43. init();
  44. async function init() {
  45. scene = new THREE.Scene();
  46. const loader = new UltraHDRLoader();
  47. loader.setDataType( THREE.HalfFloatType );
  48. loader.load( `textures/equirectangular/moonless_golf_2k.hdr.jpg`, function ( texture ) {
  49. texture.mapping = THREE.EquirectangularReflectionMapping;
  50. texture.needsUpdate = true;
  51. scene.background = texture;
  52. scene.environment = texture;
  53. } );
  54. // camera
  55. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  56. camera.position.set( - 20, 6, - 30 );
  57. // asset loading
  58. const dracoLoader = new DRACOLoader();
  59. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  60. const gltfLoader = new GLTFLoader();
  61. gltfLoader.setDRACOLoader( dracoLoader );
  62. const textureLoader = new THREE.TextureLoader();
  63. const [ gltf, normalMap0, normalMap1 ] = await Promise.all( [
  64. gltfLoader.loadAsync( 'models/gltf/pool.glb' ),
  65. textureLoader.loadAsync( 'textures/water/Water_1_M_Normal.jpg' ),
  66. textureLoader.loadAsync( 'textures/water/Water_2_M_Normal.jpg' )
  67. ] );
  68. gltf.scene.position.z = 2;
  69. gltf.scene.scale.setScalar( 0.1 );
  70. scene.add( gltf.scene );
  71. // water
  72. normalMap0.wrapS = normalMap0.wrapT = THREE.RepeatWrapping;
  73. normalMap1.wrapS = normalMap1.wrapT = THREE.RepeatWrapping;
  74. const waterGeometry = new THREE.PlaneGeometry( 30, 40 );
  75. water = new WaterMesh( waterGeometry, {
  76. color: params.color,
  77. scale: params.scale,
  78. flowDirection: new THREE.Vector2( params.flowX, params.flowY ),
  79. normalMap0: normalMap0,
  80. normalMap1: normalMap1
  81. } );
  82. water.position.set( 0, 0.2, - 2 );
  83. water.rotation.x = Math.PI * - 0.5;
  84. water.renderOrder = Infinity;
  85. scene.add( water );
  86. // floor
  87. const floorGeometry = new THREE.PlaneGeometry( 1, 1 );
  88. floorGeometry.rotateX( - Math.PI * 0.5 );
  89. const floorMaterial = new THREE.MeshStandardMaterial( {
  90. color: 0x444444,
  91. roughness: 1,
  92. metalness: 0,
  93. side: THREE.DoubleSide
  94. } );
  95. {
  96. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  97. floor.position.set( 20, 0, 0 );
  98. floor.scale.set( 15, 1, 80 );
  99. scene.add( floor );
  100. }
  101. {
  102. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  103. floor.position.set( - 20, 0, 0 );
  104. floor.scale.set( 15, 1, 80 );
  105. scene.add( floor );
  106. }
  107. {
  108. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  109. floor.position.set( 0, 0, 30 );
  110. floor.scale.set( 30, 1, 20 );
  111. scene.add( floor );
  112. }
  113. {
  114. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  115. floor.position.set( 0, 0, - 30 );
  116. floor.scale.set( 30, 1, 20 );
  117. scene.add( floor );
  118. }
  119. // renderer
  120. renderer = new THREE.WebGPURenderer( { antialias: true } );
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. renderer.setPixelRatio( window.devicePixelRatio );
  123. renderer.setAnimationLoop( animate );
  124. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  125. renderer.toneMappingExposure = 0.5;
  126. document.body.appendChild( renderer.domElement );
  127. // postprocessing
  128. postProcessing = new THREE.PostProcessing( renderer );
  129. const scenePass = pass( scene, camera );
  130. scenePass.setMRT( mrt( {
  131. output,
  132. emissive
  133. } ) );
  134. const outputPass = scenePass.getTextureNode();
  135. const emissivePass = scenePass.getTextureNode( 'emissive' );
  136. const bloomPass = bloom( emissivePass, 2 );
  137. postProcessing.outputNode = outputPass.add( bloomPass );
  138. // gui
  139. const gui = new GUI();
  140. const waterNode = water.material.colorNode;
  141. gui.addColor( params, 'color' ).onChange( function ( value ) {
  142. waterNode.color.value.set( value );
  143. } );
  144. gui.add( params, 'scale', 1, 10 ).onChange( function ( value ) {
  145. waterNode.scale.value = value;
  146. } );
  147. gui.add( params, 'flowX', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  148. waterNode.flowDirection.value.x = value;
  149. waterNode.flowDirection.value.normalize();
  150. } );
  151. gui.add( params, 'flowY', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  152. waterNode.flowDirection.value.y = value;
  153. waterNode.flowDirection.value.normalize();
  154. } );
  155. gui.open();
  156. //
  157. controls = new OrbitControls( camera, renderer.domElement );
  158. controls.enableDamping = true;
  159. controls.target.set( 0, 0, -5 );
  160. controls.update();
  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. controls.update();
  171. postProcessing.render();
  172. }
  173. </script>
  174. </body>
  175. </html>
粤ICP备19079148号