webgpu_volume_lighting.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - volumetric lighting</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</a> webgpu - volumetric lighting
  12. <br>Improve the quality/performance adjusting the parameters in the Controls
  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/webgpu';
  26. import { vec3, Fn, time, texture3D, screenUV, uniform, screenCoordinate, pass } from 'three/tsl';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  29. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  30. import { bayer16 } from 'three/addons/tsl/math/Bayer.js';
  31. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  32. import Stats from 'three/addons/libs/stats.module.js';
  33. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  34. let renderer, scene, camera;
  35. let volumetricMesh, teapot, pointLight, spotLight;
  36. let postProcessing;
  37. let stats;
  38. init();
  39. function createTexture3D() {
  40. let i = 0;
  41. const size = 128;
  42. const data = new Uint8Array( size * size * size );
  43. const scale = 10;
  44. const perlin = new ImprovedNoise();
  45. const repeatFactor = 5.0;
  46. for ( let z = 0; z < size; z ++ ) {
  47. for ( let y = 0; y < size; y ++ ) {
  48. for ( let x = 0; x < size; x ++ ) {
  49. const nx = ( x / size ) * repeatFactor;
  50. const ny = ( y / size ) * repeatFactor;
  51. const nz = ( z / size ) * repeatFactor;
  52. const noiseValue = perlin.noise( nx * scale, ny * scale, nz * scale );
  53. data[ i ] = ( 128 + 128 * noiseValue );
  54. i ++;
  55. }
  56. }
  57. }
  58. const texture = new THREE.Data3DTexture( data, size, size, size );
  59. texture.format = THREE.RedFormat;
  60. texture.minFilter = THREE.LinearFilter;
  61. texture.magFilter = THREE.LinearFilter;
  62. texture.wrapS = THREE.RepeatWrapping;
  63. texture.wrapT = THREE.RepeatWrapping;
  64. texture.unpackAlignment = 1;
  65. texture.needsUpdate = true;
  66. return texture;
  67. }
  68. function init() {
  69. const LAYER_VOLUMETRIC_LIGHTING = 10;
  70. stats = new Stats();
  71. document.body.appendChild( stats.dom );
  72. renderer = new THREE.WebGPURenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.setAnimationLoop( animate );
  76. renderer.toneMapping = THREE.NeutralToneMapping;
  77. renderer.toneMappingExposure = 2;
  78. renderer.shadowMap.enabled = true;
  79. document.body.appendChild( renderer.domElement );
  80. scene = new THREE.Scene();
  81. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  82. camera.position.set( - 8, 1, - 6 );
  83. const controls = new OrbitControls( camera, renderer.domElement );
  84. controls.maxDistance = 40;
  85. controls.minDistance = 2;
  86. // Volumetric Fog Area
  87. const noiseTexture3D = createTexture3D();
  88. const smokeAmount = uniform( 2 );
  89. const volumetricMaterial = new THREE.VolumeNodeMaterial();
  90. volumetricMaterial.steps = 12;
  91. volumetricMaterial.offsetNode = bayer16( screenCoordinate ); // Add dithering to reduce banding
  92. volumetricMaterial.scatteringNode = Fn( ( { positionRay } ) => {
  93. // Return the amount of fog based on the noise texture
  94. const timeScaled = vec3( time, 0, time.mul( .3 ) );
  95. const sampleGrain = ( scale, timeScale = 1 ) => texture3D( noiseTexture3D, positionRay.add( timeScaled.mul( timeScale ) ).mul( scale ).mod( 1 ), 0 ).r.add( .5 );
  96. let density = sampleGrain( .1 );
  97. density = density.mul( sampleGrain( .05, 1 ) );
  98. density = density.mul( sampleGrain( .02, 2 ) );
  99. return smokeAmount.mix( 1, density );
  100. } );
  101. volumetricMesh = new THREE.Mesh( new THREE.BoxGeometry( 20, 10, 20 ), volumetricMaterial );
  102. volumetricMesh.receiveShadow = true;
  103. volumetricMesh.position.y = 2;
  104. volumetricMesh.layers.disableAll();
  105. volumetricMesh.layers.enable( LAYER_VOLUMETRIC_LIGHTING );
  106. scene.add( volumetricMesh );
  107. // Objects
  108. teapot = new THREE.Mesh( new TeapotGeometry( .8, 18 ), new THREE.MeshStandardMaterial( { color: 0xffffff, side: THREE.DoubleSide } ) );
  109. teapot.castShadow = true;
  110. scene.add( teapot );
  111. const floor = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshStandardMaterial( { color: 0xffffff } ) );
  112. floor.rotation.x = - Math.PI / 2;
  113. floor.position.y = - 3;
  114. floor.receiveShadow = true;
  115. scene.add( floor );
  116. // Lights
  117. pointLight = new THREE.PointLight( 0xf9bb50, 3, 100 );
  118. pointLight.castShadow = true;
  119. pointLight.position.set( 0, 1.4, 0 );
  120. pointLight.layers.enable( LAYER_VOLUMETRIC_LIGHTING );
  121. //lightBase.add( new THREE.Mesh( new THREE.SphereGeometry( 0.1, 16, 16 ), new THREE.MeshBasicMaterial( { color: 0xf9bb50 } ) ) );
  122. scene.add( pointLight );
  123. spotLight = new THREE.SpotLight( 0xffffff, 100 );
  124. spotLight.position.set( 2.5, 5, 2.5 );
  125. spotLight.angle = Math.PI / 6;
  126. spotLight.penumbra = 1;
  127. spotLight.decay = 2;
  128. spotLight.distance = 0;
  129. spotLight.map = new THREE.TextureLoader().setPath( 'textures/' ).load( 'colors.png' );
  130. spotLight.castShadow = true;
  131. spotLight.shadow.intensity = .98;
  132. spotLight.shadow.mapSize.width = 1024;
  133. spotLight.shadow.mapSize.height = 1024;
  134. spotLight.shadow.camera.near = 1;
  135. spotLight.shadow.camera.far = 15;
  136. spotLight.shadow.focus = 1;
  137. spotLight.shadow.bias = - .003;
  138. spotLight.layers.enable( LAYER_VOLUMETRIC_LIGHTING );
  139. //sunLight.add( new THREE.Mesh( new THREE.SphereGeometry( 0.1, 16, 16 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  140. scene.add( spotLight );
  141. // Post-Processing
  142. postProcessing = new THREE.PostProcessing( renderer );
  143. // Layers
  144. const volumetricLightingIntensity = uniform( 1 );
  145. const volumetricLayer = new THREE.Layers();
  146. volumetricLayer.disableAll();
  147. volumetricLayer.enable( LAYER_VOLUMETRIC_LIGHTING );
  148. // Scene Pass
  149. const scenePass = pass( scene, camera );
  150. const sceneDepth = scenePass.getTextureNode( 'depth' );
  151. // Material - Apply occlusion depth of volumetric lighting based on the scene depth
  152. volumetricMaterial.depthNode = sceneDepth.sample( screenUV );
  153. // Volumetric Lighting Pass
  154. const volumetricPass = pass( scene, camera, { depthBuffer: false } );
  155. volumetricPass.setLayers( volumetricLayer );
  156. volumetricPass.setResolution( .25 );
  157. // Compose and Denoise
  158. const denoiseStrength = uniform( .6 );
  159. const blurredVolumetricPass = gaussianBlur( volumetricPass, denoiseStrength );
  160. const scenePassColor = scenePass.add( blurredVolumetricPass.mul( volumetricLightingIntensity ) );
  161. postProcessing.outputNode = scenePassColor;
  162. // GUI
  163. const params = {
  164. resolution: volumetricPass.getResolution(),
  165. denoise: true
  166. };
  167. const gui = new GUI();
  168. const rayMarching = gui.addFolder( 'Ray Marching' ).close();
  169. rayMarching.add( params, 'resolution', .1, .5 ).onChange( ( resolution ) => {
  170. volumetricPass.setResolution( resolution );
  171. } );
  172. rayMarching.add( volumetricMaterial, 'steps', 2, 12 ).name( 'step count' );
  173. rayMarching.add( denoiseStrength, 'value', 0, 1 ).name( 'denoise strength' );
  174. rayMarching.add( params, 'denoise' ).onChange( ( denoise ) => {
  175. const volumetric = denoise ? blurredVolumetricPass : volumetricPass;
  176. const scenePassColor = scenePass.add( volumetric.mul( volumetricLightingIntensity ) );
  177. postProcessing.outputNode = scenePassColor;
  178. postProcessing.needsUpdate = true;
  179. } );
  180. const lighting = gui.addFolder( 'Lighting / Scene' ).close();
  181. lighting.add( pointLight, 'intensity', 0, 6 ).name( 'light intensity' );
  182. lighting.add( spotLight, 'intensity', 0, 200 ).name( 'spot intensity' );
  183. lighting.add( volumetricLightingIntensity, 'value', 0, 2 ).name( 'fog intensity' );
  184. lighting.add( smokeAmount, 'value', 0, 3 ).name( 'smoke amount' );
  185. window.addEventListener( 'resize', onWindowResize );
  186. }
  187. function onWindowResize() {
  188. camera.aspect = window.innerWidth / window.innerHeight;
  189. camera.updateProjectionMatrix();
  190. renderer.setSize( window.innerWidth, window.innerHeight );
  191. }
  192. function animate() {
  193. stats.update();
  194. const time = performance.now() * 0.001;
  195. const scale = 2.4;
  196. pointLight.position.x = Math.sin( time * 0.7 ) * scale;
  197. pointLight.position.y = Math.cos( time * 0.5 ) * scale;
  198. pointLight.position.z = Math.cos( time * 0.3 ) * scale;
  199. spotLight.position.x = Math.cos( time * 0.3 ) * scale;
  200. spotLight.lookAt( 0, 0, 0 );
  201. teapot.rotation.y = time * 0.2;
  202. postProcessing.render();
  203. }
  204. </script>
  205. </body>
  206. </html>
粤ICP备19079148号