webgpu_volume_lighting_rectarea.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - volumetric lighting rect area</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>Volumetric Lighting Rect Area</span>
  14. </div>
  15. <small>Compatible with rect area lights and shadows using post-processing pass.</small>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three/webgpu';
  29. import { vec3, Fn, time, texture3D, screenUV, uniform, screenCoordinate, pass, checker, uv } from 'three/tsl';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  32. import { RectAreaLightTexturesLib } from 'three/addons/lights/RectAreaLightTexturesLib.js';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import { bayer16 } from 'three/addons/tsl/math/Bayer.js';
  35. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  36. let renderer, scene, camera;
  37. let volumetricMesh;
  38. let rectLight1, rectLight2, rectLight3;
  39. let timer;
  40. let postProcessing;
  41. init();
  42. function createTexture3D() {
  43. let i = 0;
  44. const size = 128;
  45. const data = new Uint8Array( size * size * size );
  46. const scale = 10;
  47. const perlin = new ImprovedNoise();
  48. const repeatFactor = 5.0;
  49. for ( let z = 0; z < size; z ++ ) {
  50. for ( let y = 0; y < size; y ++ ) {
  51. for ( let x = 0; x < size; x ++ ) {
  52. const nx = ( x / size ) * repeatFactor;
  53. const ny = ( y / size ) * repeatFactor;
  54. const nz = ( z / size ) * repeatFactor;
  55. const noiseValue = perlin.noise( nx * scale, ny * scale, nz * scale );
  56. data[ i ] = ( 128 + 128 * noiseValue );
  57. i ++;
  58. }
  59. }
  60. }
  61. const texture = new THREE.Data3DTexture( data, size, size, size );
  62. texture.format = THREE.RedFormat;
  63. texture.minFilter = THREE.LinearFilter;
  64. texture.magFilter = THREE.LinearFilter;
  65. texture.wrapS = THREE.RepeatWrapping;
  66. texture.wrapT = THREE.RepeatWrapping;
  67. texture.unpackAlignment = 1;
  68. texture.needsUpdate = true;
  69. return texture;
  70. }
  71. function init() {
  72. THREE.RectAreaLightNode.setLTC( RectAreaLightTexturesLib.init() );
  73. const LAYER_VOLUMETRIC_LIGHTING = 10;
  74. timer = new THREE.Timer();
  75. renderer = new THREE.WebGPURenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.setAnimationLoop( animate );
  79. renderer.toneMapping = THREE.NeutralToneMapping;
  80. renderer.toneMappingExposure = 2;
  81. renderer.shadowMap.enabled = true;
  82. renderer.inspector = new Inspector();
  83. document.body.appendChild( renderer.domElement );
  84. scene = new THREE.Scene();
  85. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 250 );
  86. camera.position.set( 0, 5, - 15 );
  87. // Volumetric Fog Area
  88. const noiseTexture3D = createTexture3D();
  89. const smokeAmount = uniform( 2 );
  90. const volumetricMaterial = new THREE.VolumeNodeMaterial();
  91. volumetricMaterial.steps = 12;
  92. volumetricMaterial.offsetNode = bayer16( screenCoordinate ); // Add dithering to reduce banding
  93. volumetricMaterial.scatteringNode = Fn( ( { positionRay } ) => {
  94. // Return the amount of fog based on the noise texture
  95. const timeScaled = vec3( time, 0, time.mul( .3 ) );
  96. const sampleGrain = ( scale, timeScale = 1 ) => texture3D( noiseTexture3D, positionRay.add( timeScaled.mul( timeScale ) ).mul( scale ).mod( 1 ), 0 ).r.add( .5 );
  97. let density = sampleGrain( .1 );
  98. density = density.mul( sampleGrain( .05, 1 ) );
  99. density = density.mul( sampleGrain( .02, 2 ) );
  100. return smokeAmount.mix( 1, density );
  101. } );
  102. volumetricMesh = new THREE.Mesh( new THREE.BoxGeometry( 50, 40, 50 ), volumetricMaterial );
  103. volumetricMesh.receiveShadow = true;
  104. volumetricMesh.position.y = 20;
  105. volumetricMesh.layers.disableAll();
  106. volumetricMesh.layers.enable( LAYER_VOLUMETRIC_LIGHTING );
  107. scene.add( volumetricMesh );
  108. // Objects
  109. rectLight1 = new THREE.RectAreaLight( 0xff0000, 5, 4, 10 );
  110. rectLight1.layers.enable( LAYER_VOLUMETRIC_LIGHTING );
  111. rectLight1.position.set( - 5, 6, 5 );
  112. scene.add( rectLight1 );
  113. rectLight2 = new THREE.RectAreaLight( 0x00ff00, 5, 4, 10 );
  114. rectLight2.layers.enable( LAYER_VOLUMETRIC_LIGHTING );
  115. rectLight2.position.set( 0, 6, 5 );
  116. scene.add( rectLight2 );
  117. rectLight3 = new THREE.RectAreaLight( 0x0000ff, 5, 4, 10 );
  118. rectLight3.layers.enable( LAYER_VOLUMETRIC_LIGHTING );
  119. rectLight3.position.set( 5, 6, 5 );
  120. scene.add( rectLight3 );
  121. //
  122. const createRectLightMesh = ( rectLight ) => {
  123. const geometry = new THREE.PlaneGeometry( 4, 10 );
  124. const frontMaterial = new THREE.MeshBasicMaterial( { color: rectLight.color, side: THREE.BackSide } );
  125. const backMaterial = new THREE.MeshStandardMaterial( { color: 0x111111 } );
  126. const backSide = new THREE.Mesh( geometry, backMaterial );
  127. backSide.position.set( 0, 0, .08 );
  128. const frontSide = new THREE.Mesh( geometry, frontMaterial );
  129. frontSide.position.set( 0, 0, .01 );
  130. rectLight.add( backSide );
  131. rectLight.add( frontSide );
  132. };
  133. createRectLightMesh( rectLight1 );
  134. createRectLightMesh( rectLight2 );
  135. createRectLightMesh( rectLight3 );
  136. //
  137. const geoFloor = new THREE.BoxGeometry( 2000, 0.1, 2000 );
  138. const matStdFloor = new THREE.MeshStandardMaterial( { color: 0x444444 } );
  139. matStdFloor.roughnessNode = checker( uv().mul( 400 ) );
  140. const mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
  141. scene.add( mshStdFloor );
  142. const geoKnot = new THREE.TorusKnotGeometry( 1.5, 0.5, 200, 16 );
  143. const matKnot = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0, metalness: 0 } );
  144. const meshKnot = new THREE.Mesh( geoKnot, matKnot );
  145. meshKnot.position.set( 0, 5.5, 0 );
  146. scene.add( meshKnot );
  147. const controls = new OrbitControls( camera, renderer.domElement );
  148. controls.minDistance = 5;
  149. controls.maxDistance = 200;
  150. controls.target.copy( meshKnot.position );
  151. controls.update();
  152. // Post-Processing
  153. postProcessing = new THREE.PostProcessing( renderer );
  154. // Layers
  155. const volumetricLightingIntensity = uniform( 1 );
  156. const volumetricLayer = new THREE.Layers();
  157. volumetricLayer.disableAll();
  158. volumetricLayer.enable( LAYER_VOLUMETRIC_LIGHTING );
  159. // Scene Pass
  160. const scenePass = pass( scene, camera );
  161. const sceneDepth = scenePass.getTextureNode( 'depth' );
  162. // Material - Apply occlusion depth of volumetric lighting based on the scene depth
  163. volumetricMaterial.depthNode = sceneDepth.sample( screenUV );
  164. // Volumetric Lighting Pass
  165. const volumetricPass = pass( scene, camera, { depthBuffer: false } );
  166. volumetricPass.setLayers( volumetricLayer );
  167. volumetricPass.setResolutionScale( .25 );
  168. // Compose and Denoise
  169. const denoiseStrength = uniform( .6 );
  170. const blurredVolumetricPass = gaussianBlur( volumetricPass, denoiseStrength );
  171. const scenePassColor = scenePass.add( blurredVolumetricPass.mul( volumetricLightingIntensity ) );
  172. postProcessing.outputNode = scenePassColor;
  173. // GUI
  174. const params = {
  175. resolution: volumetricPass.getResolutionScale(),
  176. denoise: true
  177. };
  178. const gui = renderer.inspector.createParameters( 'Volumetric Lighting' );
  179. const rayMarching = gui.addFolder( 'Ray Marching' );
  180. rayMarching.add( params, 'resolution', .1, .5 ).onChange( ( resolution ) => {
  181. volumetricPass.setResolutionScale( resolution );
  182. } );
  183. rayMarching.add( volumetricMaterial, 'steps', 2, 12 ).name( 'step count' );
  184. rayMarching.add( denoiseStrength, 'value', 0, 1 ).name( 'denoise strength' );
  185. rayMarching.add( params, 'denoise' ).onChange( ( denoise ) => {
  186. const volumetric = denoise ? blurredVolumetricPass : volumetricPass;
  187. const scenePassColor = scenePass.add( volumetric.mul( volumetricLightingIntensity ) );
  188. postProcessing.outputNode = scenePassColor;
  189. postProcessing.needsUpdate = true;
  190. } );
  191. const lighting = gui.addFolder( 'Lighting / Scene' );
  192. lighting.add( volumetricLightingIntensity, 'value', 0, 2 ).name( 'fog intensity' );
  193. lighting.add( smokeAmount, 'value', 0, 3 ).name( 'smoke amount' );
  194. window.addEventListener( 'resize', onWindowResize );
  195. }
  196. function onWindowResize() {
  197. camera.aspect = window.innerWidth / window.innerHeight;
  198. camera.updateProjectionMatrix();
  199. renderer.setSize( window.innerWidth, window.innerHeight );
  200. }
  201. function animate() {
  202. timer.update();
  203. const delta = timer.getDelta();
  204. rectLight1.rotation.y += - delta;
  205. rectLight2.rotation.y += delta * .5;
  206. rectLight3.rotation.y += delta;
  207. postProcessing.render();
  208. }
  209. </script>
  210. </body>
  211. </html>
粤ICP备19079148号