webgpu_lights_physical.html 8.4 KB

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