webgpu_lights_physical.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - physical lights</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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  13. <div class="title-wrapper">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Physical Lighting Model</span>
  15. </div>
  16. <small>
  17. Physically accurate incandescent bulb by <a href="http://clara.io" target="_blank" rel="noopener">Ben Houston</a><br />
  18. Real world scale: Brick cube is 50 cm in size. Globe is 50 cm in diameter.<br/>
  19. Reinhard tonemapping with real-world light falloff (decay = 2).
  20. </small>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. let camera, scene, renderer, bulbLight, bulbMat, hemiLight;
  37. let ballMat, cubeMat, floorMat;
  38. let previousShadowMap = false;
  39. // ref for lumens: http://www.power-sure.com/lumens.htm
  40. const bulbLuminousPowers = {
  41. '110000 lm (1000W)': 110000,
  42. '3500 lm (300W)': 3500,
  43. '1700 lm (100W)': 1700,
  44. '800 lm (60W)': 800,
  45. '400 lm (40W)': 400,
  46. '180 lm (25W)': 180,
  47. '20 lm (4W)': 20,
  48. 'Off': 0
  49. };
  50. // ref for solar irradiances: https://en.wikipedia.org/wiki/Lux
  51. const hemiLuminousIrradiances = {
  52. '0.0001 lx (Moonless Night)': 0.0001,
  53. '0.002 lx (Night Airglow)': 0.002,
  54. '0.5 lx (Full Moon)': 0.5,
  55. '3.4 lx (City Twilight)': 3.4,
  56. '50 lx (Living Room)': 50,
  57. '100 lx (Very Overcast)': 100,
  58. '350 lx (Office Room)': 350,
  59. '400 lx (Sunrise/Sunset)': 400,
  60. '1000 lx (Overcast)': 1000,
  61. '18000 lx (Daylight)': 18000,
  62. '50000 lx (Direct Sun)': 50000
  63. };
  64. const params = {
  65. shadows: true,
  66. exposure: 0.68,
  67. bulbPower: Object.keys( bulbLuminousPowers )[ 4 ],
  68. hemiIrradiance: Object.keys( hemiLuminousIrradiances )[ 0 ]
  69. };
  70. init();
  71. function init() {
  72. const container = document.getElementById( 'container' );
  73. //
  74. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  75. camera.position.x = - 4;
  76. camera.position.z = 4;
  77. camera.position.y = 2;
  78. scene = new THREE.Scene();
  79. const bulbGeometry = new THREE.SphereGeometry( 0.02, 16, 8 );
  80. bulbLight = new THREE.PointLight( 0xffee88, 1, 100, 2 );
  81. bulbMat = new THREE.MeshStandardMaterial( {
  82. emissive: 0xffffee,
  83. emissiveIntensity: 1,
  84. color: 0x000000
  85. } );
  86. bulbLight.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
  87. bulbLight.position.set( 0, 2, 0 );
  88. bulbLight.castShadow = true;
  89. scene.add( bulbLight );
  90. hemiLight = new THREE.HemisphereLight( 0xddeeff, 0x0f0e0d, 0.02 );
  91. scene.add( hemiLight );
  92. floorMat = new THREE.MeshStandardMaterial( {
  93. roughness: 0.8,
  94. color: 0xffffff,
  95. metalness: 0.2,
  96. bumpScale: 1
  97. } );
  98. const textureLoader = new THREE.TextureLoader();
  99. textureLoader.load( 'textures/hardwood2_diffuse.jpg', function ( map ) {
  100. map.wrapS = THREE.RepeatWrapping;
  101. map.wrapT = THREE.RepeatWrapping;
  102. map.anisotropy = 4;
  103. map.repeat.set( 10, 24 );
  104. map.colorSpace = THREE.SRGBColorSpace;
  105. floorMat.map = map;
  106. floorMat.needsUpdate = true;
  107. } );
  108. textureLoader.load( 'textures/hardwood2_bump.jpg', function ( map ) {
  109. map.wrapS = THREE.RepeatWrapping;
  110. map.wrapT = THREE.RepeatWrapping;
  111. map.anisotropy = 4;
  112. map.repeat.set( 10, 24 );
  113. floorMat.bumpMap = map;
  114. floorMat.needsUpdate = true;
  115. } );
  116. textureLoader.load( 'textures/hardwood2_roughness.jpg', function ( map ) {
  117. map.wrapS = THREE.RepeatWrapping;
  118. map.wrapT = THREE.RepeatWrapping;
  119. map.anisotropy = 4;
  120. map.repeat.set( 10, 24 );
  121. floorMat.roughnessMap = map;
  122. floorMat.needsUpdate = true;
  123. } );
  124. cubeMat = new THREE.MeshStandardMaterial( {
  125. roughness: 0.7,
  126. color: 0xffffff,
  127. bumpScale: 1,
  128. metalness: 0.2
  129. } );
  130. textureLoader.load( 'textures/brick_diffuse.jpg', function ( map ) {
  131. map.wrapS = THREE.RepeatWrapping;
  132. map.wrapT = THREE.RepeatWrapping;
  133. map.anisotropy = 4;
  134. map.repeat.set( 1, 1 );
  135. map.colorSpace = THREE.SRGBColorSpace;
  136. cubeMat.map = map;
  137. cubeMat.needsUpdate = true;
  138. } );
  139. textureLoader.load( 'textures/brick_bump.jpg', function ( map ) {
  140. map.wrapS = THREE.RepeatWrapping;
  141. map.wrapT = THREE.RepeatWrapping;
  142. map.anisotropy = 4;
  143. map.repeat.set( 1, 1 );
  144. cubeMat.bumpMap = map;
  145. cubeMat.needsUpdate = true;
  146. } );
  147. ballMat = new THREE.MeshStandardMaterial( {
  148. color: 0xffffff,
  149. roughness: 0.5,
  150. metalness: 1.0
  151. } );
  152. textureLoader.load( 'textures/planets/earth_atmos_2048.jpg', function ( map ) {
  153. map.anisotropy = 4;
  154. map.colorSpace = THREE.SRGBColorSpace;
  155. ballMat.map = map;
  156. ballMat.needsUpdate = true;
  157. } );
  158. textureLoader.load( 'textures/planets/earth_specular_2048.jpg', function ( map ) {
  159. map.anisotropy = 4;
  160. map.colorSpace = THREE.SRGBColorSpace;
  161. ballMat.metalnessMap = map;
  162. ballMat.needsUpdate = true;
  163. } );
  164. const floorGeometry = new THREE.PlaneGeometry( 20, 20 );
  165. const floorMesh = new THREE.Mesh( floorGeometry, floorMat );
  166. floorMesh.receiveShadow = true;
  167. floorMesh.rotation.x = - Math.PI / 2.0;
  168. scene.add( floorMesh );
  169. const ballGeometry = new THREE.SphereGeometry( 0.25, 32, 32 );
  170. const ballMesh = new THREE.Mesh( ballGeometry, ballMat );
  171. ballMesh.position.set( 1, 0.25, 1 );
  172. ballMesh.rotation.y = Math.PI;
  173. ballMesh.castShadow = true;
  174. scene.add( ballMesh );
  175. const boxGeometry = new THREE.BoxGeometry( 0.5, 0.5, 0.5 );
  176. const boxMesh = new THREE.Mesh( boxGeometry, cubeMat );
  177. boxMesh.position.set( - 0.5, 0.25, - 1 );
  178. boxMesh.castShadow = true;
  179. scene.add( boxMesh );
  180. const boxMesh2 = new THREE.Mesh( boxGeometry, cubeMat );
  181. boxMesh2.position.set( 0, 0.25, - 5 );
  182. boxMesh2.castShadow = true;
  183. scene.add( boxMesh2 );
  184. const boxMesh3 = new THREE.Mesh( boxGeometry, cubeMat );
  185. boxMesh3.position.set( 7, 0.25, 0 );
  186. boxMesh3.castShadow = true;
  187. scene.add( boxMesh3 );
  188. //
  189. renderer = new THREE.WebGPURenderer();
  190. renderer.setPixelRatio( window.devicePixelRatio );
  191. renderer.setSize( window.innerWidth, window.innerHeight );
  192. renderer.setAnimationLoop( animate );
  193. renderer.shadowMap.enabled = true;
  194. renderer.toneMapping = THREE.ReinhardToneMapping;
  195. renderer.inspector = new Inspector();
  196. container.appendChild( renderer.domElement );
  197. const controls = new OrbitControls( camera, renderer.domElement );
  198. controls.minDistance = 1;
  199. controls.maxDistance = 20;
  200. window.addEventListener( 'resize', onWindowResize );
  201. //
  202. const gui = renderer.inspector.createParameters( 'Settings' );
  203. gui.add( params, 'hemiIrradiance', Object.keys( hemiLuminousIrradiances ) );
  204. gui.add( params, 'bulbPower', Object.keys( bulbLuminousPowers ) );
  205. gui.add( params, 'exposure', 0, 1 );
  206. gui.add( params, 'shadows' );
  207. }
  208. function onWindowResize() {
  209. camera.aspect = window.innerWidth / window.innerHeight;
  210. camera.updateProjectionMatrix();
  211. renderer.setSize( window.innerWidth, window.innerHeight );
  212. }
  213. //
  214. function animate() {
  215. renderer.toneMappingExposure = Math.pow( params.exposure, 5.0 ); // to allow for very bright scenes.
  216. renderer.shadowMap.enabled = params.shadows;
  217. bulbLight.castShadow = params.shadows;
  218. if ( params.shadows !== previousShadowMap ) {
  219. ballMat.needsUpdate = true;
  220. cubeMat.needsUpdate = true;
  221. floorMat.needsUpdate = true;
  222. previousShadowMap = params.shadows;
  223. }
  224. bulbLight.power = bulbLuminousPowers[ params.bulbPower ];
  225. bulbMat.emissiveIntensity = bulbLight.intensity / Math.pow( 0.02, 2.0 ); // convert from intensity to irradiance at bulb surface
  226. hemiLight.intensity = hemiLuminousIrradiances[ params.hemiIrradiance ];
  227. const time = Date.now() * 0.0005;
  228. bulbLight.position.y = Math.cos( time ) * 0.75 + 1.25;
  229. renderer.render( scene, camera );
  230. }
  231. </script>
  232. </body>
  233. </html>
粤ICP备19079148号