webgl_lights_physical.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. <style>
  8. body {
  9. background-color: #000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 0px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family: Monospace;
  19. font-size: 13px;
  20. text-align: center;
  21. }
  22. a {
  23. color: #ff0080;
  24. text-decoration: none;
  25. }
  26. a:hover {
  27. color: #0080ff;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="container"></div>
  33. <div id="info">
  34. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Physically accurate lighting example using a incandescent bulb - by <a href="http://clara.io" target="_blank" rel="noopener">Ben Houston</a><br />
  35. Using real world scale: Brick cube is 1 meter in size. Light is 2 meters from floor. Globe is 25 cm in diameter.<br/>
  36. Using Reinhard inline tonemapping with real-world light falloff (decay = 2).
  37. </div>
  38. <script src="../build/three.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script src="js/libs/dat.gui.min.js"></script>
  41. <script src="js/controls/OrbitControls.js"></script>
  42. <script src="js/WebGL.js"></script>
  43. <script>
  44. if ( WEBGL.isWebGLAvailable() === false ) {
  45. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  46. }
  47. var camera, scene, renderer,
  48. bulbLight, bulbMat, hemiLight,
  49. object, loader, stats;
  50. var ballMat, cubeMat, floorMat;
  51. // ref for lumens: http://www.power-sure.com/lumens.htm
  52. var bulbLuminousPowers = {
  53. "110000 lm (1000W)": 110000,
  54. "3500 lm (300W)": 3500,
  55. "1700 lm (100W)": 1700,
  56. "800 lm (60W)": 800,
  57. "400 lm (40W)": 400,
  58. "180 lm (25W)": 180,
  59. "20 lm (4W)": 20,
  60. "Off": 0
  61. };
  62. // ref for solar irradiances: https://en.wikipedia.org/wiki/Lux
  63. var hemiLuminousIrradiances = {
  64. "0.0001 lx (Moonless Night)": 0.0001,
  65. "0.002 lx (Night Airglow)": 0.002,
  66. "0.5 lx (Full Moon)": 0.5,
  67. "3.4 lx (City Twilight)": 3.4,
  68. "50 lx (Living Room)": 50,
  69. "100 lx (Very Overcast)": 100,
  70. "350 lx (Office Room)": 350,
  71. "400 lx (Sunrise/Sunset)": 400,
  72. "1000 lx (Overcast)": 1000,
  73. "18000 lx (Daylight)": 18000,
  74. "50000 lx (Direct Sun)": 50000
  75. };
  76. var params = {
  77. shadows: true,
  78. exposure: 0.68,
  79. bulbPower: Object.keys( bulbLuminousPowers )[ 4 ],
  80. hemiIrradiance: Object.keys( hemiLuminousIrradiances )[0]
  81. };
  82. var clock = new THREE.Clock();
  83. init();
  84. animate();
  85. function init() {
  86. var container = document.getElementById( 'container' );
  87. stats = new Stats();
  88. container.appendChild( stats.dom );
  89. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  90. camera.position.x = -4;
  91. camera.position.z = 4;
  92. camera.position.y = 2;
  93. scene = new THREE.Scene();
  94. var bulbGeometry = new THREE.SphereBufferGeometry( 0.02, 16, 8 );
  95. bulbLight = new THREE.PointLight( 0xffee88, 1, 100, 2 );
  96. bulbMat = new THREE.MeshStandardMaterial( {
  97. emissive: 0xffffee,
  98. emissiveIntensity: 1,
  99. color: 0x000000
  100. });
  101. bulbLight.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
  102. bulbLight.position.set( 0, 2, 0 );
  103. bulbLight.castShadow = true;
  104. scene.add( bulbLight );
  105. hemiLight = new THREE.HemisphereLight( 0xddeeff, 0x0f0e0d, 0.02 );
  106. scene.add( hemiLight );
  107. floorMat = new THREE.MeshStandardMaterial( {
  108. roughness: 0.8,
  109. color: 0xffffff,
  110. metalness: 0.2,
  111. bumpScale: 0.0005
  112. });
  113. var textureLoader = new THREE.TextureLoader();
  114. textureLoader.load( "textures/hardwood2_diffuse.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.map = map;
  120. floorMat.needsUpdate = true;
  121. } );
  122. textureLoader.load( "textures/hardwood2_bump.jpg", function( map ) {
  123. map.wrapS = THREE.RepeatWrapping;
  124. map.wrapT = THREE.RepeatWrapping;
  125. map.anisotropy = 4;
  126. map.repeat.set( 10, 24 );
  127. floorMat.bumpMap = map;
  128. floorMat.needsUpdate = true;
  129. } );
  130. textureLoader.load( "textures/hardwood2_roughness.jpg", function( map ) {
  131. map.wrapS = THREE.RepeatWrapping;
  132. map.wrapT = THREE.RepeatWrapping;
  133. map.anisotropy = 4;
  134. map.repeat.set( 10, 24 );
  135. floorMat.roughnessMap = map;
  136. floorMat.needsUpdate = true;
  137. } );
  138. cubeMat = new THREE.MeshStandardMaterial( {
  139. roughness: 0.7,
  140. color: 0xffffff,
  141. bumpScale: 0.002,
  142. metalness: 0.2
  143. });
  144. textureLoader.load( "textures/brick_diffuse.jpg", function( map ) {
  145. map.wrapS = THREE.RepeatWrapping;
  146. map.wrapT = THREE.RepeatWrapping;
  147. map.anisotropy = 4;
  148. map.repeat.set( 1, 1 );
  149. cubeMat.map = map;
  150. cubeMat.needsUpdate = true;
  151. } );
  152. textureLoader.load( "textures/brick_bump.jpg", function( map ) {
  153. map.wrapS = THREE.RepeatWrapping;
  154. map.wrapT = THREE.RepeatWrapping;
  155. map.anisotropy = 4;
  156. map.repeat.set( 1, 1 );
  157. cubeMat.bumpMap = map;
  158. cubeMat.needsUpdate = true;
  159. } );
  160. ballMat = new THREE.MeshStandardMaterial( {
  161. color: 0xffffff,
  162. roughness: 0.5,
  163. metalness: 1.0
  164. });
  165. textureLoader.load( "textures/planets/earth_atmos_2048.jpg", function( map ) {
  166. map.anisotropy = 4;
  167. ballMat.map = map;
  168. ballMat.needsUpdate = true;
  169. } );
  170. textureLoader.load( "textures/planets/earth_specular_2048.jpg", function( map ) {
  171. map.anisotropy = 4;
  172. ballMat.metalnessMap = map;
  173. ballMat.needsUpdate = true;
  174. } );
  175. var floorGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
  176. var floorMesh = new THREE.Mesh( floorGeometry, floorMat );
  177. floorMesh.receiveShadow = true;
  178. floorMesh.rotation.x = -Math.PI / 2.0;
  179. scene.add( floorMesh );
  180. var ballGeometry = new THREE.SphereBufferGeometry( 0.5, 32, 32 );
  181. var ballMesh = new THREE.Mesh( ballGeometry, ballMat );
  182. ballMesh.position.set( 1, 0.5, 1 );
  183. ballMesh.rotation.y = Math.PI;
  184. ballMesh.castShadow = true;
  185. scene.add( ballMesh );
  186. var boxGeometry = new THREE.BoxBufferGeometry( 0.5, 0.5, 0.5 );
  187. var boxMesh = new THREE.Mesh( boxGeometry, cubeMat );
  188. boxMesh.position.set( -0.5, 0.25, -1 );
  189. boxMesh.castShadow = true;
  190. scene.add( boxMesh );
  191. var boxMesh2 = new THREE.Mesh( boxGeometry, cubeMat );
  192. boxMesh2.position.set( 0, 0.25, -5 );
  193. boxMesh2.castShadow = true;
  194. scene.add( boxMesh2 );
  195. var boxMesh3 = new THREE.Mesh( boxGeometry, cubeMat );
  196. boxMesh3.position.set( 7, 0.25, 0 );
  197. boxMesh3.castShadow = true;
  198. scene.add( boxMesh3 );
  199. renderer = new THREE.WebGLRenderer();
  200. renderer.physicallyCorrectLights = true;
  201. renderer.gammaInput = true;
  202. renderer.gammaOutput = true;
  203. renderer.shadowMap.enabled = true;
  204. renderer.toneMapping = THREE.ReinhardToneMapping;
  205. renderer.setPixelRatio( window.devicePixelRatio );
  206. renderer.setSize( window.innerWidth, window.innerHeight );
  207. container.appendChild( renderer.domElement );
  208. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  209. window.addEventListener( 'resize', onWindowResize, false );
  210. var gui = new dat.GUI();
  211. gui.add( params, 'hemiIrradiance', Object.keys( hemiLuminousIrradiances ) );
  212. gui.add( params, 'bulbPower', Object.keys( bulbLuminousPowers ) );
  213. gui.add( params, 'exposure', 0, 1 );
  214. gui.add( params, 'shadows' );
  215. gui.open();
  216. }
  217. function onWindowResize() {
  218. camera.aspect = window.innerWidth / window.innerHeight;
  219. camera.updateProjectionMatrix();
  220. renderer.setSize( window.innerWidth, window.innerHeight );
  221. }
  222. //
  223. function animate() {
  224. requestAnimationFrame( animate );
  225. render();
  226. }
  227. var previousShadowMap = false;
  228. function render() {
  229. renderer.toneMappingExposure = Math.pow( params.exposure, 5.0 ); // to allow for very bright scenes.
  230. renderer.shadowMap.enabled = params.shadows;
  231. bulbLight.castShadow = params.shadows;
  232. if( params.shadows !== previousShadowMap ) {
  233. ballMat.needsUpdate = true;
  234. cubeMat.needsUpdate = true;
  235. floorMat.needsUpdate = true;
  236. previousShadowMap = params.shadows;
  237. }
  238. bulbLight.power = bulbLuminousPowers[ params.bulbPower ];
  239. bulbMat.emissiveIntensity = bulbLight.intensity / Math.pow( 0.02, 2.0 ); // convert from intensity to irradiance at bulb surface
  240. hemiLight.intensity = hemiLuminousIrradiances[ params.hemiIrradiance ];
  241. var time = Date.now() * 0.0005;
  242. bulbLight.position.y = Math.cos( time ) * 0.75 + 1.25;
  243. renderer.render( scene, camera );
  244. stats.update();
  245. }
  246. </script>
  247. </body>
  248. </html>