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