webgl_lights_physical.html 8.2 KB

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