webgl_lights_physical.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - 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. <!-- Import maps polyfill -->
  17. <!-- Remove this when import maps will be widely supported -->
  18. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. let camera, scene, renderer, bulbLight, bulbMat, hemiLight, stats;
  33. let ballMat, cubeMat, floorMat;
  34. let previousShadowMap = false;
  35. // ref for lumens: http://www.power-sure.com/lumens.htm
  36. const bulbLuminousPowers = {
  37. '110000 lm (1000W)': 110000,
  38. '3500 lm (300W)': 3500,
  39. '1700 lm (100W)': 1700,
  40. '800 lm (60W)': 800,
  41. '400 lm (40W)': 400,
  42. '180 lm (25W)': 180,
  43. '20 lm (4W)': 20,
  44. 'Off': 0
  45. };
  46. // ref for solar irradiances: https://en.wikipedia.org/wiki/Lux
  47. const hemiLuminousIrradiances = {
  48. '0.0001 lx (Moonless Night)': 0.0001,
  49. '0.002 lx (Night Airglow)': 0.002,
  50. '0.5 lx (Full Moon)': 0.5,
  51. '3.4 lx (City Twilight)': 3.4,
  52. '50 lx (Living Room)': 50,
  53. '100 lx (Very Overcast)': 100,
  54. '350 lx (Office Room)': 350,
  55. '400 lx (Sunrise/Sunset)': 400,
  56. '1000 lx (Overcast)': 1000,
  57. '18000 lx (Daylight)': 18000,
  58. '50000 lx (Direct Sun)': 50000
  59. };
  60. const params = {
  61. shadows: true,
  62. exposure: 0.68,
  63. bulbPower: Object.keys( bulbLuminousPowers )[ 4 ],
  64. hemiIrradiance: Object.keys( hemiLuminousIrradiances )[ 0 ]
  65. };
  66. init();
  67. animate();
  68. function init() {
  69. const container = document.getElementById( 'container' );
  70. stats = new Stats();
  71. container.appendChild( stats.dom );
  72. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  73. camera.position.x = - 4;
  74. camera.position.z = 4;
  75. camera.position.y = 2;
  76. scene = new THREE.Scene();
  77. const bulbGeometry = new THREE.SphereGeometry( 0.02, 16, 8 );
  78. bulbLight = new THREE.PointLight( 0xffee88, 1, 100, 2 );
  79. bulbMat = new THREE.MeshStandardMaterial( {
  80. emissive: 0xffffee,
  81. emissiveIntensity: 1,
  82. color: 0x000000
  83. } );
  84. bulbLight.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
  85. bulbLight.position.set( 0, 2, 0 );
  86. bulbLight.castShadow = true;
  87. scene.add( bulbLight );
  88. hemiLight = new THREE.HemisphereLight( 0xddeeff, 0x0f0e0d, 0.02 );
  89. scene.add( hemiLight );
  90. floorMat = new THREE.MeshStandardMaterial( {
  91. roughness: 0.8,
  92. color: 0xffffff,
  93. metalness: 0.2,
  94. bumpScale: 0.0005
  95. } );
  96. const textureLoader = new THREE.TextureLoader();
  97. textureLoader.load( 'textures/hardwood2_diffuse.jpg', function ( map ) {
  98. map.wrapS = THREE.RepeatWrapping;
  99. map.wrapT = THREE.RepeatWrapping;
  100. map.anisotropy = 4;
  101. map.repeat.set( 10, 24 );
  102. map.colorSpace = THREE.SRGBColorSpace;
  103. floorMat.map = map;
  104. floorMat.needsUpdate = true;
  105. } );
  106. textureLoader.load( 'textures/hardwood2_bump.jpg', function ( map ) {
  107. map.wrapS = THREE.RepeatWrapping;
  108. map.wrapT = THREE.RepeatWrapping;
  109. map.anisotropy = 4;
  110. map.repeat.set( 10, 24 );
  111. floorMat.bumpMap = map;
  112. floorMat.needsUpdate = true;
  113. } );
  114. textureLoader.load( 'textures/hardwood2_roughness.jpg', function ( map ) {
  115. map.wrapS = THREE.RepeatWrapping;
  116. map.wrapT = THREE.RepeatWrapping;
  117. map.anisotropy = 4;
  118. map.repeat.set( 10, 24 );
  119. floorMat.roughnessMap = map;
  120. floorMat.needsUpdate = true;
  121. } );
  122. cubeMat = new THREE.MeshStandardMaterial( {
  123. roughness: 0.7,
  124. color: 0xffffff,
  125. bumpScale: 0.002,
  126. metalness: 0.2
  127. } );
  128. textureLoader.load( 'textures/brick_diffuse.jpg', function ( map ) {
  129. map.wrapS = THREE.RepeatWrapping;
  130. map.wrapT = THREE.RepeatWrapping;
  131. map.anisotropy = 4;
  132. map.repeat.set( 1, 1 );
  133. map.colorSpace = THREE.SRGBColorSpace;
  134. cubeMat.map = map;
  135. cubeMat.needsUpdate = true;
  136. } );
  137. textureLoader.load( 'textures/brick_bump.jpg', function ( map ) {
  138. map.wrapS = THREE.RepeatWrapping;
  139. map.wrapT = THREE.RepeatWrapping;
  140. map.anisotropy = 4;
  141. map.repeat.set( 1, 1 );
  142. cubeMat.bumpMap = map;
  143. cubeMat.needsUpdate = true;
  144. } );
  145. ballMat = new THREE.MeshStandardMaterial( {
  146. color: 0xffffff,
  147. roughness: 0.5,
  148. metalness: 1.0
  149. } );
  150. textureLoader.load( 'textures/planets/earth_atmos_2048.jpg', function ( map ) {
  151. map.anisotropy = 4;
  152. map.colorSpace = THREE.SRGBColorSpace;
  153. ballMat.map = map;
  154. ballMat.needsUpdate = true;
  155. } );
  156. textureLoader.load( 'textures/planets/earth_specular_2048.jpg', function ( map ) {
  157. map.anisotropy = 4;
  158. map.colorSpace = THREE.SRGBColorSpace;
  159. ballMat.metalnessMap = map;
  160. ballMat.needsUpdate = true;
  161. } );
  162. const floorGeometry = new THREE.PlaneGeometry( 20, 20 );
  163. const floorMesh = new THREE.Mesh( floorGeometry, floorMat );
  164. floorMesh.receiveShadow = true;
  165. floorMesh.rotation.x = - Math.PI / 2.0;
  166. scene.add( floorMesh );
  167. const ballGeometry = new THREE.SphereGeometry( 0.25, 32, 32 );
  168. const ballMesh = new THREE.Mesh( ballGeometry, ballMat );
  169. ballMesh.position.set( 1, 0.25, 1 );
  170. ballMesh.rotation.y = Math.PI;
  171. ballMesh.castShadow = true;
  172. scene.add( ballMesh );
  173. const boxGeometry = new THREE.BoxGeometry( 0.5, 0.5, 0.5 );
  174. const boxMesh = new THREE.Mesh( boxGeometry, cubeMat );
  175. boxMesh.position.set( - 0.5, 0.25, - 1 );
  176. boxMesh.castShadow = true;
  177. scene.add( boxMesh );
  178. const boxMesh2 = new THREE.Mesh( boxGeometry, cubeMat );
  179. boxMesh2.position.set( 0, 0.25, - 5 );
  180. boxMesh2.castShadow = true;
  181. scene.add( boxMesh2 );
  182. const boxMesh3 = new THREE.Mesh( boxGeometry, cubeMat );
  183. boxMesh3.position.set( 7, 0.25, 0 );
  184. boxMesh3.castShadow = true;
  185. scene.add( boxMesh3 );
  186. renderer = new THREE.WebGLRenderer();
  187. renderer.useLegacyLights = false;
  188. renderer.shadowMap.enabled = true;
  189. renderer.toneMapping = THREE.ReinhardToneMapping;
  190. renderer.setPixelRatio( window.devicePixelRatio );
  191. renderer.setSize( window.innerWidth, window.innerHeight );
  192. container.appendChild( renderer.domElement );
  193. const controls = new OrbitControls( camera, renderer.domElement );
  194. controls.minDistance = 1;
  195. controls.maxDistance = 20;
  196. window.addEventListener( 'resize', onWindowResize );
  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. requestAnimationFrame( animate );
  212. render();
  213. }
  214. function render() {
  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. stats.update();
  231. }
  232. </script>
  233. </body>
  234. </html>