webgl_materials_envmaps_hdr.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgl - materials - hdr environment mapping</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">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - High dynamic range (RGBE) Image-based Lighting (IBL)<br />using run-time generated pre-filtered roughness mipmaps (PMREM)<br/>
  13. Created by Prashant Sharma and <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { GUI } from './jsm/libs/dat.gui.module.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
  21. var params = {
  22. envMap: 'HDR',
  23. roughness: 0.0,
  24. metalness: 0.0,
  25. exposure: 1.0,
  26. debug: false
  27. };
  28. var container, stats;
  29. var camera, scene, renderer, controls;
  30. var torusMesh, planeMesh;
  31. var generatedCubeRenderTarget, ldrCubeRenderTarget, hdrCubeRenderTarget, rgbmCubeRenderTarget;
  32. var ldrCubeMap, hdrCubeMap, rgbmCubeMap;
  33. init();
  34. animate();
  35. function getEnvScene() {
  36. var envScene = new THREE.Scene();
  37. var geometry = new THREE.BoxBufferGeometry();
  38. geometry.deleteAttribute( 'uv' );
  39. var roomMaterial = new THREE.MeshStandardMaterial( { metalness: 0, side: THREE.BackSide } );
  40. var room = new THREE.Mesh( geometry, roomMaterial );
  41. room.scale.setScalar( 10 );
  42. envScene.add( room );
  43. var mainLight = new THREE.PointLight( 0xffffff, 50, 0, 2 );
  44. envScene.add( mainLight );
  45. var lightMaterial = new THREE.MeshLambertMaterial( { color: 0x000000, emissive: 0xffffff, emissiveIntensity: 10 } );
  46. var light1 = new THREE.Mesh( geometry, lightMaterial );
  47. light1.position.set( - 5, 2, 0 );
  48. light1.scale.set( 0.1, 1, 1 );
  49. envScene.add( light1 );
  50. var light2 = new THREE.Mesh( geometry, lightMaterial );
  51. light2.position.set( 0, 5, 0 );
  52. light2.scale.set( 1, 0.1, 1 );
  53. envScene.add( light2 );
  54. var light2 = new THREE.Mesh( geometry, lightMaterial );
  55. light2.position.set( 2, 1, 5 );
  56. light2.scale.set( 1.5, 2, 0.1 );
  57. envScene.add( light2 );
  58. return envScene;
  59. }
  60. function init() {
  61. container = document.createElement( 'div' );
  62. document.body.appendChild( container );
  63. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  64. camera.position.set( 0, 0, 120 );
  65. scene = new THREE.Scene();
  66. scene.background = new THREE.Color( 0x000000 );
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.physicallyCorrectLights = true;
  69. renderer.toneMapping = THREE.LinearToneMapping;
  70. //
  71. var geometry = new THREE.TorusKnotBufferGeometry( 18, 8, 150, 20 );
  72. var material = new THREE.MeshStandardMaterial( {
  73. color: 0xffffff,
  74. metalness: params.metalness,
  75. roughness: params.roughness
  76. } );
  77. torusMesh = new THREE.Mesh( geometry, material );
  78. scene.add( torusMesh );
  79. var geometry = new THREE.PlaneBufferGeometry( 200, 200 );
  80. var material = new THREE.MeshBasicMaterial();
  81. planeMesh = new THREE.Mesh( geometry, material );
  82. planeMesh.position.y = - 50;
  83. planeMesh.rotation.x = - Math.PI * 0.5;
  84. scene.add( planeMesh );
  85. THREE.DefaultLoadingManager.onLoad = function ( ) {
  86. pmremGenerator.dispose();
  87. };
  88. var hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
  89. hdrCubeMap = new HDRCubeTextureLoader()
  90. .setPath( './textures/cube/pisaHDR/' )
  91. .setDataType( THREE.UnsignedByteType )
  92. .load( hdrUrls, function () {
  93. hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
  94. hdrCubeMap.magFilter = THREE.LinearFilter;
  95. hdrCubeMap.needsUpdate = true;
  96. } );
  97. var ldrUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
  98. ldrCubeMap = new THREE.CubeTextureLoader()
  99. .setPath( './textures/cube/pisa/' )
  100. .load( ldrUrls, function () {
  101. ldrCubeMap.encoding = THREE.sRGBEncoding;
  102. ldrCubeRenderTarget = pmremGenerator.fromCubemap( ldrCubeMap );
  103. } );
  104. var rgbmUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
  105. rgbmCubeMap = new THREE.CubeTextureLoader()
  106. .setPath( './textures/cube/pisaRGBM16/' )
  107. .load( rgbmUrls, function () {
  108. rgbmCubeMap.encoding = THREE.RGBM16Encoding;
  109. rgbmCubeMap.format = THREE.RGBAFormat;
  110. rgbmCubeRenderTarget = pmremGenerator.fromCubemap( rgbmCubeMap );
  111. rgbmCubeMap.magFilter = THREE.LinearFilter;
  112. rgbmCubeMap.needsUpdate = true;
  113. } );
  114. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  115. pmremGenerator.compileCubemapShader();
  116. var envScene = getEnvScene();
  117. generatedCubeRenderTarget = pmremGenerator.fromScene( envScene, 0.04 );
  118. renderer.setPixelRatio( window.devicePixelRatio );
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. container.appendChild( renderer.domElement );
  121. //renderer.toneMapping = ReinhardToneMapping;
  122. renderer.outputEncoding = THREE.sRGBEncoding;
  123. stats = new Stats();
  124. container.appendChild( stats.dom );
  125. controls = new OrbitControls( camera, renderer.domElement );
  126. controls.minDistance = 50;
  127. controls.maxDistance = 300;
  128. window.addEventListener( 'resize', onWindowResize, false );
  129. var gui = new GUI();
  130. gui.add( params, 'envMap', [ 'Generated', 'LDR', 'HDR', 'RGBM16' ] );
  131. gui.add( params, 'roughness', 0, 1, 0.01 );
  132. gui.add( params, 'metalness', 0, 1, 0.01 );
  133. gui.add( params, 'exposure', 0, 2, 0.01 );
  134. gui.add( params, 'debug', false );
  135. gui.open();
  136. }
  137. function onWindowResize() {
  138. var width = window.innerWidth;
  139. var height = window.innerHeight;
  140. camera.aspect = width / height;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( width, height );
  143. }
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. stats.begin();
  147. render();
  148. stats.end();
  149. }
  150. function render() {
  151. torusMesh.material.roughness = params.roughness;
  152. torusMesh.material.metalness = params.metalness;
  153. var renderTarget, cubeMap;
  154. switch ( params.envMap ) {
  155. case 'Generated':
  156. renderTarget = generatedCubeRenderTarget;
  157. cubeMap = generatedCubeRenderTarget.texture;
  158. break;
  159. case 'LDR':
  160. renderTarget = ldrCubeRenderTarget;
  161. cubeMap = ldrCubeMap;
  162. break;
  163. case 'HDR':
  164. renderTarget = hdrCubeRenderTarget;
  165. cubeMap = hdrCubeMap;
  166. break;
  167. case 'RGBM16':
  168. renderTarget = rgbmCubeRenderTarget;
  169. cubeMap = rgbmCubeMap;
  170. break;
  171. }
  172. var newEnvMap = renderTarget ? renderTarget.texture : null;
  173. if ( newEnvMap && newEnvMap !== torusMesh.material.envMap ) {
  174. torusMesh.material.envMap = newEnvMap;
  175. torusMesh.material.needsUpdate = true;
  176. planeMesh.material.map = newEnvMap;
  177. planeMesh.material.needsUpdate = true;
  178. }
  179. torusMesh.rotation.y += 0.005;
  180. planeMesh.visible = params.debug;
  181. scene.background = cubeMap;
  182. renderer.toneMappingExposure = params.exposure;
  183. renderer.render( scene, camera );
  184. }
  185. </script>
  186. </body>
  187. </html>