webgl_pmrem_test.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 test by <a href="https://github.com/elalish" target="_blank" rel="noopener">Emmett Lalish</a>
  14. <br>
  15. <br>1: white metal. 2: white dielectric. 3: black dielectric.
  16. <br>PMREM on: HDR with a single bright pixel. PMREM off: DirectionalLight.
  17. <br>The difference between these renders indicates the error in the PMREM approximations.
  18. </div>
  19. </div>
  20. <!-- Import maps polyfill -->
  21. <!-- Remove this when import maps will be widely supported -->
  22. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.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. const gui = new GUI();
  77. gui.add( { enabled: true }, 'enabled' )
  78. .name( 'PMREM' )
  79. .onChange( value => {
  80. directionalLight.intensity = value ? 0 : 1;
  81. scene.traverse( function ( child ) {
  82. if ( child.isMesh ) {
  83. child.material.envMapIntensity = 1 - directionalLight.intensity;
  84. }
  85. } );
  86. render();
  87. } );
  88. }
  89. function createObjects() {
  90. let radianceMap = null;
  91. new RGBELoader()
  92. // .setDataType( THREE.FloatType )
  93. .setPath( 'textures/equirectangular/' )
  94. .load( 'spot1Lux.hdr', function ( texture ) {
  95. radianceMap = pmremGenerator.fromEquirectangular( texture ).texture;
  96. pmremGenerator.dispose();
  97. scene.background = radianceMap;
  98. const geometry = new THREE.SphereGeometry( 0.4, 32, 32 );
  99. for ( let x = 0; x <= 10; x ++ ) {
  100. for ( let y = 0; y <= 2; y ++ ) {
  101. const material = new THREE.MeshPhysicalMaterial( {
  102. roughness: x / 10,
  103. metalness: y < 1 ? 1 : 0,
  104. color: y < 2 ? 0xffffff : 0x000000,
  105. envMap: radianceMap,
  106. envMapIntensity: 1
  107. } );
  108. const mesh = new THREE.Mesh( geometry, material );
  109. mesh.position.x = x - 5;
  110. mesh.position.y = 1 - y;
  111. scene.add( mesh );
  112. }
  113. }
  114. render();
  115. } );
  116. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  117. pmremGenerator.compileEquirectangularShader();
  118. }
  119. function onWindowResize() {
  120. const width = window.innerWidth;
  121. const height = window.innerHeight;
  122. camera.aspect = width / height;
  123. updateCamera();
  124. renderer.setSize( width, height );
  125. render();
  126. }
  127. function updateCamera() {
  128. const horizontalFoV = 40;
  129. const verticalFoV = 2 * Math.atan( Math.tan( horizontalFoV / 2 * Math.PI / 180 ) / camera.aspect ) * 180 / Math.PI;
  130. camera.fov = verticalFoV;
  131. camera.updateProjectionMatrix();
  132. }
  133. function render() {
  134. renderer.render( scene, camera );
  135. }
  136. Promise.resolve()
  137. .then( init )
  138. .then( createObjects )
  139. .then( render );
  140. </script>
  141. </body>
  142. </html>