webgl_lights_physical.html 8.3 KB

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