webgpu_tsl_raging_sea.html 6.6 KB

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