webgpu_lights_tiled.html 6.1 KB

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