webgpu_lights_tiled.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - tiled 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 - Compute-based Tiled Lighting<br />
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { texture, uv, pass, uniform } from 'three/tsl';
  26. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  27. import { TiledLighting } from 'three/addons/lighting/TiledLighting.js';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  32. let camera, scene, renderer,
  33. lights, lightDummy,
  34. stats, controls,
  35. compose, tileInfluence,
  36. lighting,
  37. count,
  38. postProcessing;
  39. init();
  40. function init() {
  41. if ( WebGPU.isAvailable() === false ) {
  42. document.body.appendChild( WebGPU.getErrorMessage() );
  43. throw new Error( 'No WebGPU support' );
  44. }
  45. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 600 );
  46. camera.position.z = 200;
  47. camera.position.y = 30;
  48. scene = new THREE.Scene();
  49. scene.fog = new THREE.Fog( 0x111111, 300, 500 );
  50. scene.background = new THREE.Color( 0x111111 );
  51. count = 1000;
  52. const material = new THREE.MeshBasicMaterial();
  53. lightDummy = new THREE.InstancedMesh( new THREE.SphereGeometry( 0.1, 16, 8 ), material, count );
  54. lightDummy.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  55. scene.add( lightDummy );
  56. // lights
  57. lights = new THREE.Group();
  58. scene.add( lights );
  59. const addLight = ( hexColor, power = 10, distance = 3 ) => {
  60. const light = new THREE.PointLight( hexColor, 1, distance );
  61. light.position.set( Math.random() * 300 - 150, 1, Math.random() * 300 - 150 );
  62. light.power = power;
  63. light.userData.fixedPosition = light.position.clone();
  64. lights.add( light );
  65. return light;
  66. };
  67. const color = new THREE.Color();
  68. for ( let i = 0; i < count; i ++ ) {
  69. const hex = ( Math.random() * 0xffffff ) + 0x666666;
  70. lightDummy.setColorAt( i, color.setHex( hex ) );
  71. addLight( hex );
  72. }
  73. //
  74. const lightAmbient = new THREE.AmbientLight( 0xffffff, .1 );
  75. scene.add( lightAmbient );
  76. // textures
  77. const textureLoader = new THREE.TextureLoader();
  78. const floorColor = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
  79. floorColor.wrapS = THREE.RepeatWrapping;
  80. floorColor.wrapT = THREE.RepeatWrapping;
  81. floorColor.colorSpace = THREE.SRGBColorSpace;
  82. const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  83. floorNormal.wrapS = THREE.RepeatWrapping;
  84. floorNormal.wrapT = THREE.RepeatWrapping;
  85. const planeGeometry = new THREE.PlaneGeometry( 1000, 1000 );
  86. const planeMaterial = new THREE.MeshPhongNodeMaterial( {
  87. colorNode: texture( floorColor, uv().mul( 50 ) ),
  88. normalMap: floorNormal
  89. } );
  90. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  91. ground.rotation.x = - Math.PI / 2;
  92. ground.position.y = 0;
  93. ground.castShadow = true;
  94. ground.receiveShadow = true;
  95. scene.add( ground );
  96. // renderer
  97. lighting = new TiledLighting(); // ( maxLights = 1024, tileSize = 32 )
  98. renderer = new THREE.WebGPURenderer( { antialias: true } );
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. renderer.setAnimationLoop( animate );
  102. renderer.lighting = lighting; // set lighting system
  103. renderer.toneMapping = THREE.NeutralToneMapping;
  104. renderer.toneMappingExposure = 5;
  105. document.body.appendChild( renderer.domElement );
  106. // controls
  107. controls = new OrbitControls( camera, renderer.domElement );
  108. controls.maxDistance = 400;
  109. // stats
  110. stats = new Stats();
  111. document.body.appendChild( stats.dom );
  112. window.addEventListener( 'resize', onWindowResize );
  113. // post processing
  114. const scenePass = pass( scene, camera );
  115. const bloomPass = bloom( scenePass, 3, .9, .2 );
  116. // compose
  117. compose = scenePass.add( bloomPass );
  118. tileInfluence = uniform( 0 );
  119. postProcessing = new THREE.PostProcessing( renderer );
  120. updatePostProcessing();
  121. // gui
  122. const gui = new GUI();
  123. gui.add( tileInfluence, 'value', 0, 1 ).name( 'tile indexes debug' );
  124. }
  125. function updatePostProcessing() {
  126. // tile indexes debug, needs to be updated every time the renderer size changes
  127. const debugBlockIndexes = lighting.getNode( scene, camera ).setSize( window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio ).getBlock().toColor().div( count * 2 );
  128. postProcessing.outputNode = compose.add( debugBlockIndexes.mul( tileInfluence ) );
  129. postProcessing.needsUpdate = true;
  130. }
  131. function onWindowResize() {
  132. camera.aspect = window.innerWidth / window.innerHeight;
  133. camera.updateProjectionMatrix();
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. updatePostProcessing();
  136. }
  137. function animate() {
  138. const time = performance.now() / 1000;
  139. for ( let i = 0; i < lights.children.length; i ++ ) {
  140. const light = lights.children[ i ];
  141. const lightTime = ( time * 0.5 ) + light.id;
  142. light.position.copy( light.userData.fixedPosition );
  143. light.position.x += Math.sin( lightTime * 0.7 ) * 3;
  144. light.position.y += Math.cos( lightTime * 0.5 ) * .5;
  145. light.position.z += Math.cos( lightTime * 0.3 ) * 3;
  146. lightDummy.setMatrixAt( i, light.matrixWorld );
  147. }
  148. postProcessing.render();
  149. stats.update();
  150. }
  151. </script>
  152. </body>
  153. </html>
粤ICP备19079148号