webgpu_water.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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">Creative Commons Attribution</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 { WaterMesh } from 'three/addons/objects/Water2Mesh.js';
  33. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. let scene, camera, renderer, water, postProcessing, controls;
  36. const params = {
  37. color: '#ffffff',
  38. scale: 2,
  39. flowX: 0.25,
  40. flowY: 0.25
  41. };
  42. init();
  43. async function init() {
  44. // scene
  45. scene = new THREE.Scene();
  46. scene.backgroundNode = screenUV.distance( .5 ).remap( 0, 0.5 ).mix( color( 0x666666 ), color( 0x111111 ) );
  47. // camera
  48. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  49. camera.position.set( - 25, 10, - 25 );
  50. camera.lookAt( scene.position );
  51. // asset loading
  52. const dracoLoader = new DRACOLoader();
  53. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  54. const gltfLoader = new GLTFLoader();
  55. gltfLoader.setDRACOLoader( dracoLoader );
  56. const textureLoader = new THREE.TextureLoader();
  57. const [ gltf, normalMap0, normalMap1 ] = await Promise.all( [
  58. gltfLoader.loadAsync( 'models/gltf/pool.glb' ),
  59. textureLoader.loadAsync( 'textures/water/Water_1_M_Normal.jpg' ),
  60. textureLoader.loadAsync( 'textures/water/Water_2_M_Normal.jpg' )
  61. ] );
  62. gltf.scene.scale.setScalar( 0.1 );
  63. scene.add( gltf.scene );
  64. // water
  65. normalMap0.wrapS = normalMap0.wrapT = THREE.RepeatWrapping;
  66. normalMap1.wrapS = normalMap1.wrapT = THREE.RepeatWrapping;
  67. const waterGeometry = new THREE.PlaneGeometry( 30, 40 );
  68. water = new WaterMesh( waterGeometry, {
  69. color: params.color,
  70. scale: params.scale,
  71. flowDirection: new THREE.Vector2( params.flowX, params.flowY ),
  72. normalMap0: normalMap0,
  73. normalMap1: normalMap1
  74. } );
  75. water.position.set( 0, 0.2, - 2 );
  76. water.rotation.x = Math.PI * - 0.5;
  77. water.renderOrder = Infinity;
  78. scene.add( water );
  79. // light
  80. const ambientLight = new THREE.AmbientLight( 0xccccccc, 0.4 );
  81. scene.add( ambientLight );
  82. const directionalLight = new THREE.DirectionalLight( 0xf435ab, 3 );
  83. directionalLight.position.set( - 1, 1, 1 );
  84. scene.add( directionalLight );
  85. // renderer
  86. renderer = new THREE.WebGPURenderer();
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. renderer.setPixelRatio( window.devicePixelRatio );
  89. renderer.setAnimationLoop( animate );
  90. renderer.toneMapping = THREE.NeutralToneMapping;
  91. document.body.appendChild( renderer.domElement );
  92. postProcessing = new THREE.PostProcessing( renderer );
  93. const scenePass = pass( scene, camera );
  94. scenePass.setMRT( mrt( {
  95. output,
  96. emissive
  97. } ) );
  98. const outputPass = scenePass.getTextureNode();
  99. const emissivePass = scenePass.getTextureNode( 'emissive' );
  100. const bloomPass = bloom( emissivePass );
  101. postProcessing.outputNode = outputPass.add( bloomPass );
  102. // gui
  103. const gui = new GUI();
  104. const waterNode = water.material.colorNode;
  105. gui.addColor( params, 'color' ).onChange( function ( value ) {
  106. waterNode.color.value.set( value );
  107. } );
  108. gui.add( params, 'scale', 1, 10 ).onChange( function ( value ) {
  109. waterNode.scale.value = value;
  110. } );
  111. gui.add( params, 'flowX', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  112. waterNode.flowDirection.value.x = value;
  113. waterNode.flowDirection.value.normalize();
  114. } );
  115. gui.add( params, 'flowY', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  116. waterNode.flowDirection.value.y = value;
  117. waterNode.flowDirection.value.normalize();
  118. } );
  119. gui.open();
  120. //
  121. controls = new OrbitControls( camera, renderer.domElement );
  122. controls.minDistance = 5;
  123. controls.maxDistance = 50;
  124. controls.enableDamping = true;
  125. //
  126. window.addEventListener( 'resize', onWindowResize );
  127. }
  128. function onWindowResize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. function animate() {
  134. controls.update();
  135. postProcessing.render();
  136. }
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号