webgl_materials_envmaps_hdr_nodes.html 8.2 KB

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