webgl_pmrem_test.html 5.2 KB

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