webgpu_water.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.webgpu.js",
  18. "three/webgpu": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.tsl.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { WaterMesh } from 'three/addons/objects/Water2Mesh.js';
  29. let scene, camera, clock, renderer, water;
  30. let torusKnot;
  31. const params = {
  32. color: '#ffffff',
  33. scale: 4,
  34. flowX: 1,
  35. flowY: 1
  36. };
  37. init();
  38. function init() {
  39. // scene
  40. scene = new THREE.Scene();
  41. // camera
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  43. camera.position.set( - 15, 7, 15 );
  44. camera.lookAt( scene.position );
  45. // clock
  46. clock = new THREE.Clock();
  47. // mesh
  48. const torusKnotGeometry = new THREE.TorusKnotGeometry( 3, 1, 256, 32 );
  49. const torusKnotMaterial = new THREE.MeshNormalMaterial();
  50. torusKnot = new THREE.Mesh( torusKnotGeometry, torusKnotMaterial );
  51. torusKnot.position.y = 4;
  52. torusKnot.scale.set( 0.5, 0.5, 0.5 );
  53. scene.add( torusKnot );
  54. // ground
  55. const groundGeometry = new THREE.PlaneGeometry( 20, 20 );
  56. const groundMaterial = new THREE.MeshStandardMaterial( { roughness: 0.8, metalness: 0.4 } );
  57. const ground = new THREE.Mesh( groundGeometry, groundMaterial );
  58. ground.rotation.x = Math.PI * - 0.5;
  59. scene.add( ground );
  60. const textureLoader = new THREE.TextureLoader();
  61. textureLoader.load( 'textures/hardwood2_diffuse.jpg', function ( map ) {
  62. map.wrapS = THREE.RepeatWrapping;
  63. map.wrapT = THREE.RepeatWrapping;
  64. map.anisotropy = 16;
  65. map.repeat.set( 4, 4 );
  66. map.colorSpace = THREE.SRGBColorSpace;
  67. groundMaterial.map = map;
  68. groundMaterial.needsUpdate = true;
  69. } );
  70. //
  71. const normalMap0 = textureLoader.load( 'textures/water/Water_1_M_Normal.jpg' );
  72. const normalMap1 = textureLoader.load( 'textures/water/Water_2_M_Normal.jpg' );
  73. normalMap0.wrapS = normalMap0.wrapT = THREE.RepeatWrapping;
  74. normalMap1.wrapS = normalMap1.wrapT = THREE.RepeatWrapping;
  75. // water
  76. const waterGeometry = new THREE.PlaneGeometry( 20, 20 );
  77. water = new WaterMesh( waterGeometry, {
  78. color: params.color,
  79. scale: params.scale,
  80. flowDirection: new THREE.Vector2( params.flowX, params.flowY ),
  81. normalMap0: normalMap0,
  82. normalMap1: normalMap1
  83. } );
  84. water.position.y = 1;
  85. water.rotation.x = Math.PI * - 0.5;
  86. scene.add( water );
  87. // skybox
  88. const cubeTextureLoader = new THREE.CubeTextureLoader();
  89. cubeTextureLoader.setPath( 'textures/cube/Park2/' );
  90. const cubeTexture = cubeTextureLoader.load( [
  91. 'posx.jpg', 'negx.jpg',
  92. 'posy.jpg', 'negy.jpg',
  93. 'posz.jpg', 'negz.jpg'
  94. ] );
  95. scene.background = cubeTexture;
  96. // light
  97. const ambientLight = new THREE.AmbientLight( 0xe7e7e7, 1.2 );
  98. scene.add( ambientLight );
  99. const directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  100. directionalLight.position.set( - 1, 1, 1 );
  101. scene.add( directionalLight );
  102. // renderer
  103. renderer = new THREE.WebGPURenderer();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. renderer.setPixelRatio( window.devicePixelRatio );
  106. renderer.setAnimationLoop( animate );
  107. document.body.appendChild( renderer.domElement );
  108. // gui
  109. const gui = new GUI();
  110. const waterNode = water.material.fragmentNode;
  111. gui.addColor( params, 'color' ).onChange( function ( value ) {
  112. waterNode.color.value.set( value );
  113. } );
  114. gui.add( params, 'scale', 1, 10 ).onChange( function ( value ) {
  115. waterNode.scale.value = value;
  116. } );
  117. gui.add( params, 'flowX', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  118. waterNode.flowDirection.value.x = value;
  119. waterNode.flowDirection.value.normalize();
  120. } );
  121. gui.add( params, 'flowY', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  122. waterNode.flowDirection.value.y = value;
  123. waterNode.flowDirection.value.normalize();
  124. } );
  125. gui.open();
  126. //
  127. const controls = new OrbitControls( camera, renderer.domElement );
  128. controls.minDistance = 5;
  129. controls.maxDistance = 50;
  130. //
  131. window.addEventListener( 'resize', onWindowResize );
  132. }
  133. function onWindowResize() {
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. function animate() {
  139. const delta = clock.getDelta();
  140. torusKnot.rotation.x += delta;
  141. torusKnot.rotation.y += delta * 0.5;
  142. renderer.render( scene, camera );
  143. }
  144. </script>
  145. </body>
  146. </html>
粤ICP备19079148号