webgl_pmrem_test.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.module.js",
  24. "three/addons/": "./jsm/"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  32. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  33. let scene, camera, controls, renderer;
  34. function init() {
  35. const width = window.innerWidth;
  36. const height = window.innerHeight;
  37. const aspect = width / height;
  38. // renderer
  39. renderer = new THREE.WebGLRenderer( { antialias: true } );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( width, height );
  42. // tonemapping
  43. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  44. renderer.toneMappingExposure = 1;
  45. document.body.appendChild( renderer.domElement );
  46. window.addEventListener( 'resize', onWindowResize );
  47. // scene
  48. scene = new THREE.Scene();
  49. // camera
  50. camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
  51. updateCamera();
  52. camera.position.set( 0, 0, 16 );
  53. // controls
  54. controls = new OrbitControls( camera, renderer.domElement );
  55. controls.addEventListener( 'change', render ); // use if there is no animation loop
  56. controls.minDistance = 4;
  57. controls.maxDistance = 20;
  58. // light
  59. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0 ); // set intensity to 0 to start
  60. const x = 597;
  61. const y = 213;
  62. const theta = ( x + 0.5 ) * Math.PI / 512;
  63. const phi = ( y + 0.5 ) * Math.PI / 512;
  64. directionalLight.position.setFromSphericalCoords( 100, - phi, Math.PI / 2 - theta );
  65. scene.add( directionalLight );
  66. // scene.add( new THREE.DirectionalLightHelper( directionalLight ) );
  67. // The spot1Lux HDR environment map is expressed in nits (lux / sr). The directional light has units of lux,
  68. // so to match a 1 lux light, we set a single pixel with a value equal to 1 divided by the solid
  69. // angle of the pixel in steradians. This image is 1024 x 512,
  70. // so the value is 1 / ( sin( phi ) * ( pi / 512 ) ^ 2 ) = 27,490 nits.
  71. const gui = new GUI();
  72. gui.add( { enabled: true }, 'enabled' )
  73. .name( 'PMREM' )
  74. .onChange( value => {
  75. directionalLight.intensity = value ? 0 : 1;
  76. scene.traverse( function ( child ) {
  77. if ( child.isMesh ) {
  78. child.material.envMapIntensity = 1 - directionalLight.intensity;
  79. }
  80. } );
  81. render();
  82. } );
  83. }
  84. function createObjects() {
  85. let radianceMap = null;
  86. new RGBELoader()
  87. // .setDataType( THREE.FloatType )
  88. .setPath( 'textures/equirectangular/' )
  89. .load( 'spot1Lux.hdr', function ( texture ) {
  90. radianceMap = pmremGenerator.fromEquirectangular( texture ).texture;
  91. pmremGenerator.dispose();
  92. scene.background = radianceMap;
  93. const geometry = new THREE.SphereGeometry( 0.4, 32, 32 );
  94. for ( let x = 0; x <= 10; x ++ ) {
  95. for ( let y = 0; y <= 2; y ++ ) {
  96. const material = new THREE.MeshPhysicalMaterial( {
  97. roughness: x / 10,
  98. metalness: y < 1 ? 1 : 0,
  99. color: y < 2 ? 0xffffff : 0x000000,
  100. envMap: radianceMap,
  101. envMapIntensity: 1
  102. } );
  103. const mesh = new THREE.Mesh( geometry, material );
  104. mesh.position.x = x - 5;
  105. mesh.position.y = 1 - y;
  106. scene.add( mesh );
  107. }
  108. }
  109. render();
  110. } );
  111. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  112. pmremGenerator.compileEquirectangularShader();
  113. }
  114. function onWindowResize() {
  115. const width = window.innerWidth;
  116. const height = window.innerHeight;
  117. camera.aspect = width / height;
  118. updateCamera();
  119. renderer.setSize( width, height );
  120. render();
  121. }
  122. function updateCamera() {
  123. const horizontalFoV = 40;
  124. const verticalFoV = 2 * Math.atan( Math.tan( horizontalFoV / 2 * Math.PI / 180 ) / camera.aspect ) * 180 / Math.PI;
  125. camera.fov = verticalFoV;
  126. camera.updateProjectionMatrix();
  127. }
  128. function render() {
  129. renderer.render( scene, camera );
  130. }
  131. Promise.resolve()
  132. .then( init )
  133. .then( createObjects )
  134. .then( render );
  135. </script>
  136. </body>
  137. </html>