webgl_lights_physical.html 8.2 KB

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