webgpu_volume_lighting_rectarea.html 9.5 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. <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 rect area
  12. <br>Improve the quality/performance adjusting the parameters in the Controls
  13. </div>
  14. <video id="video" loop muted crossOrigin="anonymous" playsinline style="display:none">
  15. <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
  16. <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  17. </video>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.webgpu.js",
  22. "three/webgpu": "../build/three.webgpu.js",
  23. "three/tsl": "../build/three.tsl.js",
  24. "three/addons/": "./jsm/"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three';
  30. import { vec3, Fn, time, texture3D, screenUV, uniform, screenCoordinate, pass } from 'three/tsl';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  33. import { RectAreaLightTexturesLib } from 'three/addons/lights/RectAreaLightTexturesLib.js';
  34. import { bayer16 } from 'three/addons/tsl/math/Bayer.js';
  35. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  38. let renderer, scene, camera;
  39. let volumetricMesh, meshKnot;
  40. let rectLight1, rectLight2, rectLight3;
  41. let clock;
  42. let postProcessing;
  43. let stats;
  44. init();
  45. function createTexture3D() {
  46. let i = 0;
  47. const size = 128;
  48. const data = new Uint8Array( size * size * size );
  49. const scale = 10;
  50. const perlin = new ImprovedNoise();
  51. const repeatFactor = 5.0;
  52. for ( let z = 0; z < size; z ++ ) {
  53. for ( let y = 0; y < size; y ++ ) {
  54. for ( let x = 0; x < size; x ++ ) {
  55. const nx = ( x / size ) * repeatFactor;
  56. const ny = ( y / size ) * repeatFactor;
  57. const nz = ( z / size ) * repeatFactor;
  58. const noiseValue = perlin.noise( nx * scale, ny * scale, nz * scale );
  59. data[ i ] = ( 128 + 128 * noiseValue );
  60. i ++;
  61. }
  62. }
  63. }
  64. const texture = new THREE.Data3DTexture( data, size, size, size );
  65. texture.format = THREE.RedFormat;
  66. texture.minFilter = THREE.LinearFilter;
  67. texture.magFilter = THREE.LinearFilter;
  68. texture.wrapS = THREE.RepeatWrapping;
  69. texture.wrapT = THREE.RepeatWrapping;
  70. texture.unpackAlignment = 1;
  71. texture.needsUpdate = true;
  72. return texture;
  73. }
  74. function init() {
  75. THREE.RectAreaLightNode.setLTC( RectAreaLightTexturesLib.init() );
  76. const LAYER_VOLUMETRIC_LIGHTING = 10;
  77. stats = new Stats();
  78. document.body.appendChild( stats.dom );
  79. clock = new THREE.Clock();
  80. renderer = new THREE.WebGPURenderer();
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.setAnimationLoop( animate );
  84. renderer.toneMapping = THREE.NeutralToneMapping;
  85. renderer.toneMappingExposure = 2;
  86. renderer.shadowMap.enabled = true;
  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, 5, 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, 5, 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, 5, 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: 0xbcbcbc, roughness: 0.1, metalness: 0 } );
  143. const mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
  144. scene.add( mshStdFloor );
  145. const geoKnot = new THREE.TorusKnotGeometry( 1.5, 0.5, 200, 16 );
  146. const matKnot = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0, metalness: 0 } );
  147. meshKnot = new THREE.Mesh( geoKnot, matKnot );
  148. meshKnot.position.set( 0, 5, 0 );
  149. scene.add( meshKnot );
  150. const controls = new OrbitControls( camera, renderer.domElement );
  151. controls.minDistance = 5;
  152. controls.maxDistance = 200;
  153. controls.target.copy( meshKnot.position );
  154. controls.update();
  155. // Post-Processing
  156. postProcessing = new THREE.PostProcessing( renderer );
  157. // Layers
  158. const volumetricLightingIntensity = uniform( 1 );
  159. const volumetricLayer = new THREE.Layers();
  160. volumetricLayer.disableAll();
  161. volumetricLayer.enable( LAYER_VOLUMETRIC_LIGHTING );
  162. // Scene Pass
  163. const scenePass = pass( scene, camera );
  164. const sceneLinearDepth = scenePass.getTextureNode( 'depth' );
  165. // Material - Apply occlusion depth of volumetric lighting based on the scene depth
  166. volumetricMaterial.depthNode = sceneLinearDepth.sample( screenUV );
  167. // Volumetric Lighting Pass
  168. const volumetricPass = pass( scene, camera, { depthBuffer: false } );
  169. volumetricPass.setLayers( volumetricLayer );
  170. volumetricPass.setResolution( .25 );
  171. // Compose and Denoise
  172. const denoiseStrength = uniform( .6 );
  173. const blurredVolumetricPass = gaussianBlur( volumetricPass, denoiseStrength );
  174. const scenePassColor = scenePass.add( blurredVolumetricPass.mul( volumetricLightingIntensity ) );
  175. postProcessing.outputNode = scenePassColor;
  176. // GUI
  177. const params = {
  178. quality: 1,
  179. resolution: volumetricPass.getResolution(),
  180. denoise: true
  181. };
  182. const gui = new GUI();
  183. const rayMarching = gui.addFolder( 'Ray Marching' ).close();
  184. rayMarching.add( params, 'resolution', .1, .5 ).onChange( ( resolution ) => {
  185. volumetricPass.setResolution( resolution );
  186. } );
  187. rayMarching.add( volumetricMaterial, 'steps', 2, 12 ).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. postProcessing.outputNode = scenePassColor;
  193. postProcessing.needsUpdate = true;
  194. } );
  195. const lighting = gui.addFolder( 'Lighting / Scene' ).close();
  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. const delta = clock.getDelta();
  207. stats.update();
  208. rectLight1.rotation.y += - delta;
  209. rectLight2.rotation.y += delta * .5;
  210. rectLight3.rotation.y += delta;
  211. postProcessing.render();
  212. }
  213. </script>
  214. </body>
  215. </html>
粤ICP备19079148号