webgl_loader_texture_hdrjpg.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - hdr jpg</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. <style>
  9. .lbl {
  10. color: #fff;
  11. font-size: 16px;
  12. font-weight: bold;
  13. position: absolute;
  14. bottom: 0px;
  15. z-index: 100;
  16. text-shadow: #000 1px 1px 1px;
  17. background-color: rgba(0,0,0,0.85);
  18. padding: 1em;
  19. }
  20. #lbl_left {
  21. text-align:left;
  22. left:0px;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - hdr jpg loader <br/>
  29. Converted from hdr with <a href="https://gainmap-creator.mono-grid.com/" target="_blank" rel="noopener">converter</a>. <br />
  30. See external <a href="https://github.com/MONOGRID/gainmap-js" target="_blank" rel="noopener">gainmap-js</a> for more information.
  31. </div>
  32. <div id="lbl_left" class="lbl"></div>
  33. <script type="importmap">
  34. {
  35. "imports": {
  36. "three": "../build/three.module.js",
  37. "three/addons/": "./jsm/",
  38. "@monogrid/gainmap-js": "https://cdn.jsdelivr.net/npm/@monogrid/[email protected]/dist/decode.js"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import Stats from 'three/addons/libs/stats.module.js';
  45. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  46. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  47. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  48. import { HDRJPGLoader } from '@monogrid/gainmap-js';
  49. const params = {
  50. envMap: 'HDR JPG',
  51. roughness: 0.0,
  52. metalness: 1.0,
  53. exposure: 1.0,
  54. debug: false
  55. };
  56. let container, stats;
  57. let camera, scene, renderer, controls;
  58. let torusMesh, planeMesh;
  59. let hdrJpg, hdrJpgPMREMRenderTarget, hdrJpgEquirectangularMap;
  60. let hdrPMREMRenderTarget, hdrEquirectangularMap;
  61. const fileSizes = {};
  62. const resolutions = {};
  63. init();
  64. function init() {
  65. const lbl = document.getElementById( 'lbl_left' );
  66. container = document.createElement( 'div' );
  67. document.body.appendChild( container );
  68. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 500 );
  69. camera.position.set( 0, 0, - 120 );
  70. scene = new THREE.Scene();
  71. renderer = new THREE.WebGLRenderer();
  72. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  73. //
  74. let geometry = new THREE.TorusKnotGeometry( 18, 8, 200, 40, 1, 3 );
  75. let material = new THREE.MeshStandardMaterial( {
  76. color: 0xffffff,
  77. metalness: params.metalness,
  78. roughness: params.roughness
  79. } );
  80. torusMesh = new THREE.Mesh( geometry, material );
  81. scene.add( torusMesh );
  82. geometry = new THREE.PlaneGeometry( 200, 200 );
  83. material = new THREE.MeshBasicMaterial();
  84. planeMesh = new THREE.Mesh( geometry, material );
  85. planeMesh.position.y = - 50;
  86. planeMesh.rotation.x = - Math.PI * 0.5;
  87. scene.add( planeMesh );
  88. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  89. pmremGenerator.compileEquirectangularShader();
  90. THREE.DefaultLoadingManager.onLoad = function ( ) {
  91. pmremGenerator.dispose();
  92. };
  93. hdrJpg = new HDRJPGLoader( renderer )
  94. .load( 'textures/equirectangular/spruit_sunrise_4k.hdr.jpg', function ( ) {
  95. resolutions[ 'HDR JPG' ] = hdrJpg.width + 'x' + hdrJpg.height;
  96. displayStats( 'HDR JPG' );
  97. hdrJpgEquirectangularMap = hdrJpg.renderTarget.texture;
  98. hdrJpgPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrJpgEquirectangularMap );
  99. hdrJpgEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
  100. hdrJpgEquirectangularMap.needsUpdate = true;
  101. hdrJpg.dispose();
  102. }, function ( progress ) {
  103. fileSizes[ 'HDR JPG' ] = humanFileSize( progress.total );
  104. } );
  105. hdrEquirectangularMap = new RGBELoader()
  106. .load( 'textures/equirectangular/spruit_sunrise_1k.hdr', function ( ) {
  107. resolutions[ 'HDR' ] = hdrEquirectangularMap.image.width + 'x' + hdrEquirectangularMap.image.height;
  108. hdrPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirectangularMap );
  109. hdrEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
  110. hdrEquirectangularMap.minFilter = THREE.LinearFilter;
  111. hdrEquirectangularMap.magFilter = THREE.LinearFilter;
  112. hdrEquirectangularMap.needsUpdate = true;
  113. }, function ( progress ) {
  114. fileSizes[ 'HDR' ] = humanFileSize( progress.total );
  115. } );
  116. renderer.setPixelRatio( window.devicePixelRatio );
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. renderer.setAnimationLoop( animate );
  119. container.appendChild( renderer.domElement );
  120. stats = new Stats();
  121. container.appendChild( stats.dom );
  122. controls = new OrbitControls( camera, renderer.domElement );
  123. controls.minDistance = 50;
  124. controls.maxDistance = 300;
  125. window.addEventListener( 'resize', onWindowResize );
  126. const gui = new GUI();
  127. gui.add( params, 'envMap', [ 'HDR JPG', 'HDR' ] ).onChange( displayStats );
  128. gui.add( params, 'roughness', 0, 1, 0.01 );
  129. gui.add( params, 'metalness', 0, 1, 0.01 );
  130. gui.add( params, 'exposure', 0, 2, 0.01 );
  131. gui.add( params, 'debug' );
  132. gui.open();
  133. function displayStats( value ) {
  134. lbl.innerHTML = value + ' size : ' + fileSizes[ value ] + ', Resolution: ' + resolutions[ value ];
  135. }
  136. }
  137. function humanFileSize( bytes, si = true, dp = 1 ) {
  138. const thresh = si ? 1000 : 1024;
  139. if ( Math.abs( bytes ) < thresh ) {
  140. return bytes + ' B';
  141. }
  142. const units = si
  143. ? [ 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ]
  144. : [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ];
  145. let u = - 1;
  146. const r = 10 ** dp;
  147. do {
  148. bytes /= thresh;
  149. ++ u;
  150. } while ( Math.round( Math.abs( bytes ) * r ) / r >= thresh && u < units.length - 1 );
  151. return bytes.toFixed( dp ) + ' ' + units[ u ];
  152. }
  153. function onWindowResize() {
  154. const width = window.innerWidth;
  155. const height = window.innerHeight;
  156. camera.aspect = width / height;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( width, height );
  159. }
  160. function animate() {
  161. stats.begin();
  162. render();
  163. stats.end();
  164. }
  165. function render() {
  166. torusMesh.material.roughness = params.roughness;
  167. torusMesh.material.metalness = params.metalness;
  168. let pmremRenderTarget, equirectangularMap;
  169. switch ( params.envMap ) {
  170. case 'HDR JPG':
  171. pmremRenderTarget = hdrJpgPMREMRenderTarget;
  172. equirectangularMap = hdrJpgEquirectangularMap;
  173. break;
  174. case 'HDR':
  175. pmremRenderTarget = hdrPMREMRenderTarget;
  176. equirectangularMap = hdrEquirectangularMap;
  177. break;
  178. }
  179. const newEnvMap = pmremRenderTarget ? pmremRenderTarget.texture : null;
  180. if ( newEnvMap && newEnvMap !== torusMesh.material.envMap ) {
  181. planeMesh.material.map = newEnvMap;
  182. planeMesh.material.needsUpdate = true;
  183. }
  184. torusMesh.rotation.y += 0.005;
  185. planeMesh.visible = params.debug;
  186. scene.environment = equirectangularMap;
  187. scene.background = equirectangularMap;
  188. renderer.toneMappingExposure = params.exposure;
  189. renderer.render( scene, camera );
  190. }
  191. </script>
  192. </body>
  193. </html>