webgpu_lights_tiled.html 6.3 KB

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