webgpu_volume_lighting.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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="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</span>
  14. </div>
  15. <small>Compatible with native 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 } from 'three/tsl';
  30. import { Inspector } from 'three/addons/inspector/Inspector.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  33. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.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, teapot, pointLight, spotLight;
  38. let postProcessing;
  39. init();
  40. function createTexture3D() {
  41. let i = 0;
  42. const size = 128;
  43. const data = new Uint8Array( size * size * size );
  44. const scale = 10;
  45. const perlin = new ImprovedNoise();
  46. const repeatFactor = 5.0;
  47. for ( let z = 0; z < size; z ++ ) {
  48. for ( let y = 0; y < size; y ++ ) {
  49. for ( let x = 0; x < size; x ++ ) {
  50. const nx = ( x / size ) * repeatFactor;
  51. const ny = ( y / size ) * repeatFactor;
  52. const nz = ( z / size ) * repeatFactor;
  53. const noiseValue = perlin.noise( nx * scale, ny * scale, nz * scale );
  54. data[ i ] = ( 128 + 128 * noiseValue );
  55. i ++;
  56. }
  57. }
  58. }
  59. const texture = new THREE.Data3DTexture( data, size, size, size );
  60. texture.format = THREE.RedFormat;
  61. texture.minFilter = THREE.LinearFilter;
  62. texture.magFilter = THREE.LinearFilter;
  63. texture.wrapS = THREE.RepeatWrapping;
  64. texture.wrapT = THREE.RepeatWrapping;
  65. texture.unpackAlignment = 1;
  66. texture.needsUpdate = true;
  67. return texture;
  68. }
  69. function init() {
  70. const LAYER_VOLUMETRIC_LIGHTING = 10;
  71. renderer = new THREE.WebGPURenderer();
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.setAnimationLoop( animate );
  75. renderer.toneMapping = THREE.NeutralToneMapping;
  76. renderer.toneMappingExposure = 2;
  77. renderer.shadowMap.enabled = true;
  78. renderer.inspector = new Inspector();
  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.name = 'Volumetric Lighting';
  156. volumetricPass.setLayers( volumetricLayer );
  157. volumetricPass.setResolutionScale( .25 );
  158. // Compose and Denoise
  159. const denoiseStrength = uniform( .6 );
  160. const blurredVolumetricPass = gaussianBlur( volumetricPass, denoiseStrength );
  161. const scenePassColor = scenePass.add( blurredVolumetricPass.mul( volumetricLightingIntensity ) );
  162. postProcessing.outputNode = scenePassColor;
  163. // GUI
  164. const params = {
  165. resolution: volumetricPass.getResolutionScale(),
  166. denoise: true
  167. };
  168. const gui = renderer.inspector.createParameters( 'Volumetric Lighting' );
  169. const rayMarching = gui.addFolder( 'Ray Marching' );
  170. rayMarching.add( params, 'resolution', .1, .5 ).onChange( ( resolution ) => {
  171. volumetricPass.setResolutionScale( resolution );
  172. } );
  173. rayMarching.add( volumetricMaterial, 'steps', 2, 12 ).name( 'step count' );
  174. rayMarching.add( denoiseStrength, 'value', 0, 1 ).name( 'denoise strength' );
  175. rayMarching.add( params, 'denoise' ).onChange( ( denoise ) => {
  176. const volumetric = denoise ? blurredVolumetricPass : volumetricPass;
  177. const scenePassColor = scenePass.add( volumetric.mul( volumetricLightingIntensity ) );
  178. postProcessing.outputNode = scenePassColor;
  179. postProcessing.needsUpdate = true;
  180. } );
  181. const lighting = gui.addFolder( 'Lighting / Scene' );
  182. lighting.add( pointLight, 'intensity', 0, 6 ).name( 'light intensity' );
  183. lighting.add( spotLight, 'intensity', 0, 200 ).name( 'spot intensity' );
  184. lighting.add( volumetricLightingIntensity, 'value', 0, 2 ).name( 'fog intensity' );
  185. lighting.add( smokeAmount, 'value', 0, 3 ).name( 'smoke amount' );
  186. window.addEventListener( 'resize', onWindowResize );
  187. }
  188. function onWindowResize() {
  189. camera.aspect = window.innerWidth / window.innerHeight;
  190. camera.updateProjectionMatrix();
  191. renderer.setSize( window.innerWidth, window.innerHeight );
  192. }
  193. function animate() {
  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号