webgl_materials_envmaps_exr.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. //
  55. let geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
  56. let material = new THREE.MeshStandardMaterial( {
  57. metalness: params.metalness,
  58. roughness: params.roughness,
  59. envMapIntensity: 1.0
  60. } );
  61. torusMesh = new THREE.Mesh( geometry, material );
  62. scene.add( torusMesh );
  63. geometry = new THREE.PlaneGeometry( 200, 200 );
  64. material = new THREE.MeshBasicMaterial();
  65. planeMesh = new THREE.Mesh( geometry, material );
  66. planeMesh.position.y = - 50;
  67. planeMesh.rotation.x = - Math.PI * 0.5;
  68. scene.add( planeMesh );
  69. THREE.DefaultLoadingManager.onLoad = function ( ) {
  70. pmremGenerator.dispose();
  71. };
  72. new EXRLoader().load( 'textures/piz_compressed.exr', function ( texture ) {
  73. texture.mapping = THREE.EquirectangularReflectionMapping;
  74. exrCubeRenderTarget = pmremGenerator.fromEquirectangular( texture );
  75. exrBackground = texture;
  76. } );
  77. new THREE.TextureLoader().load( 'textures/equirectangular.png', function ( texture ) {
  78. texture.mapping = THREE.EquirectangularReflectionMapping;
  79. texture.colorSpace = THREE.SRGBColorSpace;
  80. pngCubeRenderTarget = pmremGenerator.fromEquirectangular( texture );
  81. pngBackground = texture;
  82. } );
  83. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  84. pmremGenerator.compileEquirectangularShader();
  85. stats = new Stats();
  86. container.appendChild( stats.dom );
  87. controls = new OrbitControls( camera, renderer.domElement );
  88. controls.minDistance = 50;
  89. controls.maxDistance = 300;
  90. window.addEventListener( 'resize', onWindowResize );
  91. const gui = new GUI();
  92. gui.add( params, 'envMap', [ 'EXR', 'PNG' ] );
  93. gui.add( params, 'roughness', 0, 1, 0.01 );
  94. gui.add( params, 'metalness', 0, 1, 0.01 );
  95. gui.add( params, 'exposure', 0, 2, 0.01 );
  96. gui.add( params, 'debug', false );
  97. gui.open();
  98. }
  99. function onWindowResize() {
  100. const width = window.innerWidth;
  101. const height = window.innerHeight;
  102. camera.aspect = width / height;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( width, height );
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. stats.begin();
  109. render();
  110. stats.end();
  111. }
  112. function render() {
  113. torusMesh.material.roughness = params.roughness;
  114. torusMesh.material.metalness = params.metalness;
  115. let newEnvMap = torusMesh.material.envMap;
  116. let background = scene.background;
  117. switch ( params.envMap ) {
  118. case 'EXR':
  119. newEnvMap = exrCubeRenderTarget ? exrCubeRenderTarget.texture : null;
  120. background = exrBackground;
  121. break;
  122. case 'PNG':
  123. newEnvMap = pngCubeRenderTarget ? pngCubeRenderTarget.texture : null;
  124. background = pngBackground;
  125. break;
  126. }
  127. if ( newEnvMap !== torusMesh.material.envMap ) {
  128. torusMesh.material.envMap = newEnvMap;
  129. torusMesh.material.needsUpdate = true;
  130. planeMesh.material.map = newEnvMap;
  131. planeMesh.material.needsUpdate = true;
  132. }
  133. torusMesh.rotation.y += 0.005;
  134. planeMesh.visible = params.debug;
  135. scene.background = background;
  136. renderer.toneMappingExposure = params.exposure;
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>