webgpu_volume_lighting_rectarea.html 9.7 KB

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