webgl_materials_envmaps_exr.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { EXRLoader } from './jsm/loaders/EXRLoader.js';
  18. var params = {
  19. envMap: 'EXR',
  20. roughness: 0.0,
  21. metalness: 0.0,
  22. exposure: 1.0,
  23. debug: false,
  24. };
  25. var container, stats;
  26. var camera, scene, renderer, controls;
  27. var torusMesh, planeMesh;
  28. var pngCubeRenderTarget, exrCubeRenderTarget;
  29. var pngBackground, exrBackground;
  30. init();
  31. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  36. camera.position.set( 0, 0, 120 );
  37. scene = new THREE.Scene();
  38. renderer = new THREE.WebGLRenderer();
  39. renderer.toneMapping = THREE.LinearToneMapping;
  40. //
  41. var geometry = new THREE.TorusKnotBufferGeometry( 18, 8, 150, 20 );
  42. var material = new THREE.MeshStandardMaterial( {
  43. metalness: params.roughness,
  44. roughness: params.metalness,
  45. envMapIntensity: 1.0
  46. } );
  47. torusMesh = new THREE.Mesh( geometry, material );
  48. scene.add( torusMesh );
  49. var geometry = new THREE.PlaneBufferGeometry( 200, 200 );
  50. var material = new THREE.MeshBasicMaterial();
  51. planeMesh = new THREE.Mesh( geometry, material );
  52. planeMesh.position.y = - 50;
  53. planeMesh.rotation.x = - Math.PI * 0.5;
  54. scene.add( planeMesh );
  55. THREE.DefaultLoadingManager.onLoad = function ( ) {
  56. pmremGenerator.dispose();
  57. };
  58. new EXRLoader()
  59. .setDataType( THREE.FloatType )
  60. .load( 'textures/piz_compressed.exr', function ( texture ) {
  61. exrCubeRenderTarget = pmremGenerator.fromEquirectangular( texture );
  62. exrBackground = exrCubeRenderTarget.texture;
  63. texture.dispose();
  64. } );
  65. new THREE.TextureLoader().load( 'textures/equirectangular.png', function ( texture ) {
  66. texture.encoding = THREE.sRGBEncoding;
  67. pngCubeRenderTarget = pmremGenerator.fromEquirectangular( texture );
  68. pngBackground = pngCubeRenderTarget.texture;
  69. texture.dispose();
  70. } );
  71. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  72. pmremGenerator.compileEquirectangularShader();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. container.appendChild( renderer.domElement );
  76. renderer.outputEncoding = THREE.sRGBEncoding;
  77. stats = new Stats();
  78. container.appendChild( stats.dom );
  79. controls = new OrbitControls( camera, renderer.domElement );
  80. controls.minDistance = 50;
  81. controls.maxDistance = 300;
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. var gui = new GUI();
  84. gui.add( params, 'envMap', [ 'EXR', 'PNG' ] );
  85. gui.add( params, 'roughness', 0, 1, 0.01 );
  86. gui.add( params, 'metalness', 0, 1, 0.01 );
  87. gui.add( params, 'exposure', 0, 2, 0.01 );
  88. gui.add( params, 'debug', false );
  89. gui.open();
  90. }
  91. function onWindowResize() {
  92. var width = window.innerWidth;
  93. var height = window.innerHeight;
  94. camera.aspect = width / height;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( width, height );
  97. }
  98. function animate() {
  99. requestAnimationFrame( animate );
  100. stats.begin();
  101. render();
  102. stats.end();
  103. }
  104. function render() {
  105. torusMesh.material.roughness = params.roughness;
  106. torusMesh.material.metalness = params.metalness;
  107. var newEnvMap = torusMesh.material.envMap;
  108. var background = scene.background;
  109. switch ( params.envMap ) {
  110. case 'EXR':
  111. newEnvMap = exrCubeRenderTarget ? exrCubeRenderTarget.texture : null;
  112. background = exrBackground;
  113. break;
  114. case 'PNG':
  115. newEnvMap = pngCubeRenderTarget ? pngCubeRenderTarget.texture : null;
  116. background = pngBackground;
  117. break;
  118. }
  119. if ( newEnvMap !== torusMesh.material.envMap ) {
  120. torusMesh.material.envMap = newEnvMap;
  121. torusMesh.material.needsUpdate = true;
  122. planeMesh.material.map = newEnvMap;
  123. planeMesh.material.needsUpdate = true;
  124. }
  125. torusMesh.rotation.y += 0.005;
  126. planeMesh.visible = params.debug;
  127. scene.background = background;
  128. renderer.toneMappingExposure = params.exposure;
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>