webgpu_tsl_raging_sea.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - raging sea</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js webgpu</a> - raging sea
  12. <br>
  13. Based on <a href="https://threejs-journey.com/lessons/raging-sea" target="_blank" rel="noopener">Three.js Journey</a> lesson
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { float, mx_noise_float, Loop, color, positionLocal, sin, vec2, vec3, mul, timerLocal, uniform, Fn, transformNormalToView } from 'three/tsl';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. let camera, scene, renderer, controls;
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  33. camera.position.set( 1.25, 1.25, 1.25 );
  34. scene = new THREE.Scene();
  35. // lights
  36. const directionalLight = new THREE.DirectionalLight( '#ffffff', 3 );
  37. directionalLight.position.set( - 4, 2, 0 );
  38. scene.add( directionalLight );
  39. // material
  40. const material = new THREE.MeshStandardNodeMaterial( { color: '#271442', roughness: 0.15 } );
  41. const emissiveColor = uniform( color( '#ff0a81' ) );
  42. const emissiveLow = uniform( - 0.25 );
  43. const emissiveHigh = uniform( 0.2 );
  44. const emissivePower = uniform( 7 );
  45. const largeWavesFrequency = uniform( vec2( 3, 1 ) );
  46. const largeWavesSpeed = uniform( 1.25 );
  47. const largeWavesMultiplier = uniform( 0.15 );
  48. const smallWavesIterations = uniform( 3 );
  49. const smallWavesFrequency = uniform( 2 );
  50. const smallWavesSpeed = uniform( 0.3 );
  51. const smallWavesMultiplier = uniform( 0.18 );
  52. const normalComputeShift = uniform( 0.01 );
  53. // TSL functions
  54. const wavesElevation = Fn( ( [ position ] ) => {
  55. const time = timerLocal();
  56. // large waves
  57. const elevation = mul(
  58. sin( position.x.mul( largeWavesFrequency.x ).add( time.mul( largeWavesSpeed ) ) ),
  59. sin( position.z.mul( largeWavesFrequency.y ).add( time.mul( largeWavesSpeed ) ) ),
  60. largeWavesMultiplier
  61. ).toVar();
  62. Loop( { start: float( 1 ), end: smallWavesIterations.add( 1 ) }, ( { i } ) => {
  63. const noiseInput = vec3(
  64. position.xz
  65. .add( 2 ) // avoids a-hole pattern
  66. .mul( smallWavesFrequency )
  67. .mul( i ),
  68. time.mul( smallWavesSpeed )
  69. );
  70. const wave = mx_noise_float( noiseInput, 1, 0 )
  71. .mul( smallWavesMultiplier )
  72. .div( i )
  73. .abs();
  74. elevation.subAssign( wave );
  75. } );
  76. return elevation;
  77. } );
  78. // position
  79. const elevation = wavesElevation( positionLocal );
  80. const position = positionLocal.add( vec3( 0, elevation, 0 ) );
  81. material.positionNode = position;
  82. // normals
  83. let positionA = positionLocal.add( vec3( normalComputeShift, 0, 0 ) );
  84. let positionB = positionLocal.add( vec3( 0, 0, normalComputeShift.negate() ) );
  85. positionA = positionA.add( vec3( 0, wavesElevation( positionA ), 0 ) );
  86. positionB = positionB.add( vec3( 0, wavesElevation( positionB ), 0 ) );
  87. const toA = positionA.sub( position ).normalize();
  88. const toB = positionB.sub( position ).normalize();
  89. const normal = toA.cross( toB );
  90. material.normalNode = transformNormalToView( normal );
  91. // emissive
  92. const emissive = elevation.remap( emissiveHigh, emissiveLow ).pow( emissivePower );
  93. material.emissiveNode = emissiveColor.mul( emissive );
  94. // mesh
  95. const geometry = new THREE.PlaneGeometry( 2, 2, 256, 256 );
  96. geometry.rotateX( - Math.PI * 0.5 );
  97. const mesh = new THREE.Mesh( geometry, material );
  98. scene.add( mesh );
  99. // debug
  100. const gui = new GUI();
  101. gui.addColor( { color: material.color.getHex( THREE.SRGBColorSpace ) }, 'color' ).name( 'color' ).onChange( value => material.color.set( value ) );
  102. gui.add( material, 'roughness', 0, 1, 0.001 );
  103. const colorGui = gui.addFolder( 'emissive' );
  104. colorGui.addColor( { color: emissiveColor.value.getHex( THREE.SRGBColorSpace ) }, 'color' ).name( 'color' ).onChange( value => emissiveColor.value.set( value ) );
  105. colorGui.add( emissiveLow, 'value', - 1, 0, 0.001 ).name( 'low' );
  106. colorGui.add( emissiveHigh, 'value', 0, 1, 0.001 ).name( 'high' );
  107. colorGui.add( emissivePower, 'value', 1, 10, 1 ).name( 'power' );
  108. const wavesGui = gui.addFolder( 'waves' );
  109. wavesGui.add( largeWavesSpeed, 'value', 0, 5 ).name( 'largeSpeed' );
  110. wavesGui.add( largeWavesMultiplier, 'value', 0, 1 ).name( 'largeMultiplier' );
  111. wavesGui.add( largeWavesFrequency.value, 'x', 0, 10 ).name( 'largeFrequencyX' );
  112. wavesGui.add( largeWavesFrequency.value, 'y', 0, 10 ).name( 'largeFrequencyY' );
  113. wavesGui.add( smallWavesIterations, 'value', 0, 5, 1 ).name( 'smallIterations' );
  114. wavesGui.add( smallWavesFrequency, 'value', 0, 10 ).name( 'smallFrequency' );
  115. wavesGui.add( smallWavesSpeed, 'value', 0, 1 ).name( 'smallSpeed' );
  116. wavesGui.add( smallWavesMultiplier, 'value', 0, 1 ).name( 'smallMultiplier' );
  117. wavesGui.add( normalComputeShift, 'value', 0, 0.1, 0.0001 ).name( 'normalComputeShift' );
  118. // renderer
  119. renderer = new THREE.WebGPURenderer( { antialias: true } );
  120. renderer.setPixelRatio( window.devicePixelRatio );
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. renderer.setAnimationLoop( animate );
  123. document.body.appendChild( renderer.domElement );
  124. // controls
  125. controls = new OrbitControls( camera, renderer.domElement );
  126. controls.target.y = - 0.25;
  127. controls.enableDamping = true;
  128. controls.minDistance = 0.1;
  129. controls.maxDistance = 50;
  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. async function animate() {
  138. controls.update();
  139. renderer.render( scene, camera );
  140. }
  141. </script>
  142. </body>
  143. </html>