webgl_materials_envmaps_exr.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgl - materials - equirectangular exr image based lighting</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"><a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - Example of an IBL (Image based lighting) pipeline based around<br> equirectangular EXR lightprobe data. Created by <a href="https://github.com/richardmonette" target="_blank" rel="noopener">Richard Monette</a></div>
  12. <!-- Import maps polyfill -->
  13. <!-- Remove this when import maps will be widely supported -->
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { EXRLoader } from 'three/addons/loaders/EXRLoader.js';
  29. const params = {
  30. envMap: 'EXR',
  31. roughness: 0.0,
  32. metalness: 0.0,
  33. exposure: 1.0,
  34. debug: false,
  35. };
  36. let container, stats;
  37. let camera, scene, renderer, controls;
  38. let torusMesh, planeMesh;
  39. let pngCubeRenderTarget, exrCubeRenderTarget;
  40. let pngBackground, exrBackground;
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.set( 0, 0, 120 );
  48. scene = new THREE.Scene();
  49. renderer = new THREE.WebGLRenderer();
  50. renderer.setPixelRatio( window.devicePixelRatio );
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  52. container.appendChild( renderer.domElement );
  53. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  54. renderer.outputEncoding = THREE.sRGBEncoding;
  55. //
  56. let geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
  57. let material = new THREE.MeshStandardMaterial( {
  58. metalness: params.metalness,
  59. roughness: params.roughness,
  60. envMapIntensity: 1.0
  61. } );
  62. torusMesh = new THREE.Mesh( geometry, material );
  63. scene.add( torusMesh );
  64. geometry = new THREE.PlaneGeometry( 200, 200 );
  65. material = new THREE.MeshBasicMaterial();
  66. planeMesh = new THREE.Mesh( geometry, material );
  67. planeMesh.position.y = - 50;
  68. planeMesh.rotation.x = - Math.PI * 0.5;
  69. scene.add( planeMesh );
  70. THREE.DefaultLoadingManager.onLoad = function ( ) {
  71. pmremGenerator.dispose();
  72. };
  73. new EXRLoader().load( 'textures/piz_compressed.exr', function ( texture ) {
  74. texture.mapping = THREE.EquirectangularReflectionMapping;
  75. exrCubeRenderTarget = pmremGenerator.fromEquirectangular( texture );
  76. exrBackground = texture;
  77. } );
  78. new THREE.TextureLoader().load( 'textures/equirectangular.png', function ( texture ) {
  79. texture.mapping = THREE.EquirectangularReflectionMapping;
  80. texture.encoding = THREE.sRGBEncoding;
  81. pngCubeRenderTarget = pmremGenerator.fromEquirectangular( texture );
  82. pngBackground = texture;
  83. } );
  84. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  85. pmremGenerator.compileEquirectangularShader();
  86. stats = new Stats();
  87. container.appendChild( stats.dom );
  88. controls = new OrbitControls( camera, renderer.domElement );
  89. controls.minDistance = 50;
  90. controls.maxDistance = 300;
  91. window.addEventListener( 'resize', onWindowResize );
  92. const gui = new GUI();
  93. gui.add( params, 'envMap', [ 'EXR', 'PNG' ] );
  94. gui.add( params, 'roughness', 0, 1, 0.01 );
  95. gui.add( params, 'metalness', 0, 1, 0.01 );
  96. gui.add( params, 'exposure', 0, 2, 0.01 );
  97. gui.add( params, 'debug', false );
  98. gui.open();
  99. }
  100. function onWindowResize() {
  101. const width = window.innerWidth;
  102. const height = window.innerHeight;
  103. camera.aspect = width / height;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( width, height );
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. stats.begin();
  110. render();
  111. stats.end();
  112. }
  113. function render() {
  114. torusMesh.material.roughness = params.roughness;
  115. torusMesh.material.metalness = params.metalness;
  116. let newEnvMap = torusMesh.material.envMap;
  117. let background = scene.background;
  118. switch ( params.envMap ) {
  119. case 'EXR':
  120. newEnvMap = exrCubeRenderTarget ? exrCubeRenderTarget.texture : null;
  121. background = exrBackground;
  122. break;
  123. case 'PNG':
  124. newEnvMap = pngCubeRenderTarget ? pngCubeRenderTarget.texture : null;
  125. background = pngBackground;
  126. break;
  127. }
  128. if ( newEnvMap !== torusMesh.material.envMap ) {
  129. torusMesh.material.envMap = newEnvMap;
  130. torusMesh.material.needsUpdate = true;
  131. planeMesh.material.map = newEnvMap;
  132. planeMesh.material.needsUpdate = true;
  133. }
  134. torusMesh.rotation.y += 0.005;
  135. planeMesh.visible = params.debug;
  136. scene.background = background;
  137. renderer.toneMappingExposure = params.exposure;
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>