webgl_pmrem_test.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js PMREM directional light test</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">
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  13. PMREM directional light test <a href="https://github.com/elalish" target="_blank" rel="noopener">Emmett Lalish</a>
  14. <br>Top row is white metal
  15. <br>Middle row is white dielectric
  16. <br>Bottom row is black dielectric.
  17. <br>Mouse-out is a standard Directional Light.
  18. <br>Mouse-over is a PMREM of the skybox: a single bright pixel representing the same directional light source.
  19. <br>The difference between these renders indicates the error in the PMREM approximations.
  20. </div>
  21. </div>
  22. <!-- Import maps polyfill -->
  23. <!-- Remove this when import maps will be widely supported -->
  24. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three';
  34. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  35. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  36. let scene, camera, controls, renderer;
  37. function init() {
  38. const width = window.innerWidth;
  39. const height = window.innerHeight;
  40. const aspect = width / height;
  41. // renderer
  42. renderer = new THREE.WebGLRenderer( { antialias: true } );
  43. renderer.setPixelRatio( window.devicePixelRatio );
  44. renderer.setSize( width, height );
  45. renderer.outputEncoding = THREE.sRGBEncoding;
  46. renderer.physicallyCorrectLights = true;
  47. // tonemapping
  48. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  49. renderer.toneMappingExposure = 1;
  50. document.body.appendChild( renderer.domElement );
  51. window.addEventListener( 'resize', onWindowResize );
  52. // scene
  53. scene = new THREE.Scene();
  54. // camera
  55. camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
  56. updateCamera();
  57. camera.position.set( 0, 0, 16 );
  58. // controls
  59. controls = new OrbitControls( camera, renderer.domElement );
  60. controls.addEventListener( 'change', render ); // use if there is no animation loop
  61. controls.minDistance = 4;
  62. controls.maxDistance = 20;
  63. // light
  64. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0 ); // set intensity to 0 to start
  65. const x = 597;
  66. const y = 213;
  67. const theta = ( x + 0.5 ) * Math.PI / 512;
  68. const phi = ( y + 0.5 ) * Math.PI / 512;
  69. directionalLight.position.setFromSphericalCoords( 100, - phi, Math.PI / 2 - theta );
  70. scene.add( directionalLight );
  71. // scene.add( new THREE.DirectionalLightHelper( directionalLight ) );
  72. // The spot1Lux HDR environment map is expressed in nits (lux / sr). The directional light has units of lux,
  73. // so to match a 1 lux light, we set a single pixel with a value equal to 1 divided by the solid
  74. // angle of the pixel in steradians. This image is 1024 x 512,
  75. // so the value is 1 / ( sin( phi ) * ( pi / 512 ) ^ 2 ) = 27,490 nits.
  76. document.body.addEventListener( 'mouseover', function () {
  77. scene.traverse( function ( child ) {
  78. if ( child.isMesh ) {
  79. child.material.envMapIntensity = 1;
  80. directionalLight.intensity = 0;
  81. }
  82. } );
  83. render();
  84. } );
  85. document.body.addEventListener( 'mouseout', function () {
  86. scene.traverse( function ( child ) {
  87. if ( child.isMesh ) {
  88. child.material.envMapIntensity = 0;
  89. directionalLight.intensity = 1;
  90. }
  91. } );
  92. render();
  93. } );
  94. }
  95. function createObjects() {
  96. let radianceMap = null;
  97. new RGBELoader()
  98. // .setDataType( THREE.FloatType )
  99. .setPath( 'textures/equirectangular/' )
  100. .load( 'spot1Lux.hdr', function ( texture ) {
  101. radianceMap = pmremGenerator.fromEquirectangular( texture ).texture;
  102. pmremGenerator.dispose();
  103. scene.background = radianceMap;
  104. const geometry = new THREE.SphereGeometry( 0.4, 32, 32 );
  105. for ( let x = 0; x <= 10; x ++ ) {
  106. for ( let y = 0; y <= 2; y ++ ) {
  107. const material = new THREE.MeshPhysicalMaterial( {
  108. roughness: x / 10,
  109. metalness: y < 1 ? 1 : 0,
  110. color: y < 2 ? 0xffffff : 0x000000,
  111. envMap: radianceMap,
  112. envMapIntensity: 1
  113. } );
  114. const mesh = new THREE.Mesh( geometry, material );
  115. mesh.position.x = x - 5;
  116. mesh.position.y = 1 - y;
  117. scene.add( mesh );
  118. }
  119. }
  120. render();
  121. } );
  122. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  123. pmremGenerator.compileEquirectangularShader();
  124. }
  125. function onWindowResize() {
  126. const width = window.innerWidth;
  127. const height = window.innerHeight;
  128. camera.aspect = width / height;
  129. updateCamera();
  130. renderer.setSize( width, height );
  131. render();
  132. }
  133. function updateCamera() {
  134. const horizontalFoV = 40;
  135. const verticalFoV = 2 * Math.atan( Math.tan( horizontalFoV / 2 * Math.PI / 180 ) / camera.aspect ) * 180 / Math.PI;
  136. camera.fov = verticalFoV;
  137. camera.updateProjectionMatrix();
  138. }
  139. function render() {
  140. renderer.render( scene, camera );
  141. }
  142. Promise.resolve()
  143. .then( init )
  144. .then( createObjects )
  145. .then( render );
  146. </script>
  147. </body>
  148. </html>