webgpu_volume_lighting.html 9.4 KB

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