webgpu_lights_tiled.html 6.7 KB

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