webgpu_water.html 5.2 KB

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