webgl_lights_physical.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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="http://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. floorMat.map = map;
  92. floorMat.needsUpdate = true;
  93. } );
  94. textureLoader.load( "textures/hardwood2_bump.jpg", function ( map ) {
  95. map.wrapS = THREE.RepeatWrapping;
  96. map.wrapT = THREE.RepeatWrapping;
  97. map.anisotropy = 4;
  98. map.repeat.set( 10, 24 );
  99. floorMat.bumpMap = map;
  100. floorMat.needsUpdate = true;
  101. } );
  102. textureLoader.load( "textures/hardwood2_roughness.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.roughnessMap = map;
  108. floorMat.needsUpdate = true;
  109. } );
  110. cubeMat = new THREE.MeshStandardMaterial( {
  111. roughness: 0.7,
  112. color: 0xffffff,
  113. bumpScale: 0.002,
  114. metalness: 0.2
  115. } );
  116. textureLoader.load( "textures/brick_diffuse.jpg", function ( map ) {
  117. map.wrapS = THREE.RepeatWrapping;
  118. map.wrapT = THREE.RepeatWrapping;
  119. map.anisotropy = 4;
  120. map.repeat.set( 1, 1 );
  121. cubeMat.map = map;
  122. cubeMat.needsUpdate = true;
  123. } );
  124. textureLoader.load( "textures/brick_bump.jpg", function ( map ) {
  125. map.wrapS = THREE.RepeatWrapping;
  126. map.wrapT = THREE.RepeatWrapping;
  127. map.anisotropy = 4;
  128. map.repeat.set( 1, 1 );
  129. cubeMat.bumpMap = map;
  130. cubeMat.needsUpdate = true;
  131. } );
  132. ballMat = new THREE.MeshStandardMaterial( {
  133. color: 0xffffff,
  134. roughness: 0.5,
  135. metalness: 1.0
  136. } );
  137. textureLoader.load( "textures/planets/earth_atmos_2048.jpg", function ( map ) {
  138. map.anisotropy = 4;
  139. ballMat.map = map;
  140. ballMat.needsUpdate = true;
  141. } );
  142. textureLoader.load( "textures/planets/earth_specular_2048.jpg", function ( map ) {
  143. map.anisotropy = 4;
  144. ballMat.metalnessMap = map;
  145. ballMat.needsUpdate = true;
  146. } );
  147. var floorGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
  148. var floorMesh = new THREE.Mesh( floorGeometry, floorMat );
  149. floorMesh.receiveShadow = true;
  150. floorMesh.rotation.x = - Math.PI / 2.0;
  151. scene.add( floorMesh );
  152. var ballGeometry = new THREE.SphereBufferGeometry( 0.25, 32, 32 );
  153. var ballMesh = new THREE.Mesh( ballGeometry, ballMat );
  154. ballMesh.position.set( 1, 0.25, 1 );
  155. ballMesh.rotation.y = Math.PI;
  156. ballMesh.castShadow = true;
  157. scene.add( ballMesh );
  158. var boxGeometry = new THREE.BoxBufferGeometry( 0.5, 0.5, 0.5 );
  159. var boxMesh = new THREE.Mesh( boxGeometry, cubeMat );
  160. boxMesh.position.set( - 0.5, 0.25, - 1 );
  161. boxMesh.castShadow = true;
  162. scene.add( boxMesh );
  163. var boxMesh2 = new THREE.Mesh( boxGeometry, cubeMat );
  164. boxMesh2.position.set( 0, 0.25, - 5 );
  165. boxMesh2.castShadow = true;
  166. scene.add( boxMesh2 );
  167. var boxMesh3 = new THREE.Mesh( boxGeometry, cubeMat );
  168. boxMesh3.position.set( 7, 0.25, 0 );
  169. boxMesh3.castShadow = true;
  170. scene.add( boxMesh3 );
  171. renderer = new THREE.WebGLRenderer();
  172. renderer.physicallyCorrectLights = true;
  173. renderer.gammaInput = true;
  174. renderer.gammaOutput = true;
  175. renderer.shadowMap.enabled = true;
  176. renderer.toneMapping = THREE.ReinhardToneMapping;
  177. renderer.setPixelRatio( window.devicePixelRatio );
  178. renderer.setSize( window.innerWidth, window.innerHeight );
  179. container.appendChild( renderer.domElement );
  180. var controls = new OrbitControls( camera, renderer.domElement );
  181. window.addEventListener( 'resize', onWindowResize, false );
  182. var gui = new GUI();
  183. gui.add( params, 'hemiIrradiance', Object.keys( hemiLuminousIrradiances ) );
  184. gui.add( params, 'bulbPower', Object.keys( bulbLuminousPowers ) );
  185. gui.add( params, 'exposure', 0, 1 );
  186. gui.add( params, 'shadows' );
  187. gui.open();
  188. }
  189. function onWindowResize() {
  190. camera.aspect = window.innerWidth / window.innerHeight;
  191. camera.updateProjectionMatrix();
  192. renderer.setSize( window.innerWidth, window.innerHeight );
  193. }
  194. //
  195. function animate() {
  196. requestAnimationFrame( animate );
  197. render();
  198. }
  199. var previousShadowMap = false;
  200. function render() {
  201. renderer.toneMappingExposure = Math.pow( params.exposure, 5.0 ); // to allow for very bright scenes.
  202. renderer.shadowMap.enabled = params.shadows;
  203. bulbLight.castShadow = params.shadows;
  204. if ( params.shadows !== previousShadowMap ) {
  205. ballMat.needsUpdate = true;
  206. cubeMat.needsUpdate = true;
  207. floorMat.needsUpdate = true;
  208. previousShadowMap = params.shadows;
  209. }
  210. bulbLight.power = bulbLuminousPowers[ params.bulbPower ];
  211. bulbMat.emissiveIntensity = bulbLight.intensity / Math.pow( 0.02, 2.0 ); // convert from intensity to irradiance at bulb surface
  212. hemiLight.intensity = hemiLuminousIrradiances[ params.hemiIrradiance ];
  213. var time = Date.now() * 0.0005;
  214. bulbLight.position.y = Math.cos( time ) * 0.75 + 1.25;
  215. renderer.render( scene, camera );
  216. stats.update();
  217. }
  218. </script>
  219. </body>
  220. </html>