webgl_loader_texture_hdrjpg.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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://unpkg.com/@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 { GainMapLoader, 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 gainMap, gainMapPMREMRenderTarget, gainMapBackground;
  61. let hdrPMREMRenderTarget, hdrEquirectangularMap;
  62. const fileSizes = {};
  63. const resolutions = {};
  64. init();
  65. animate();
  66. function init() {
  67. const lbl = document.getElementById( 'lbl_left' );
  68. container = document.createElement( 'div' );
  69. document.body.appendChild( container );
  70. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 500 );
  71. camera.position.set( 0, 0, - 120 );
  72. scene = new THREE.Scene();
  73. renderer = new THREE.WebGLRenderer();
  74. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  75. //
  76. let geometry = new THREE.TorusKnotGeometry( 18, 8, 200, 40, 1, 3 );
  77. let material = new THREE.MeshStandardMaterial( {
  78. color: 0xffffff,
  79. metalness: params.metalness,
  80. roughness: params.roughness
  81. } );
  82. torusMesh = new THREE.Mesh( geometry, material );
  83. scene.add( torusMesh );
  84. geometry = new THREE.PlaneGeometry( 200, 200 );
  85. material = new THREE.MeshBasicMaterial();
  86. planeMesh = new THREE.Mesh( geometry, material );
  87. planeMesh.position.y = - 50;
  88. planeMesh.rotation.x = - Math.PI * 0.5;
  89. scene.add( planeMesh );
  90. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  91. pmremGenerator.compileEquirectangularShader();
  92. THREE.DefaultLoadingManager.onLoad = function ( ) {
  93. pmremGenerator.dispose();
  94. };
  95. hdrJpg = new HDRJPGLoader( renderer )
  96. .load( 'textures/gainmap/spruit_sunrise_4k.jpg', function ( ) {
  97. resolutions[ 'HDR JPG' ] = hdrJpg.width + 'x' + hdrJpg.height;
  98. displayStats( 'HDR JPG' );
  99. hdrJpgEquirectangularMap = hdrJpg.renderTarget.texture;
  100. hdrJpgPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrJpgEquirectangularMap );
  101. hdrJpgEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
  102. hdrJpgEquirectangularMap.needsUpdate = true;
  103. hdrJpg.dispose();
  104. }, function ( progress ) {
  105. fileSizes[ 'HDR JPG' ] = humanFileSize( progress.total );
  106. } );
  107. gainMap = new GainMapLoader( renderer )
  108. .load( [
  109. 'textures/gainmap/spruit_sunrise_4k.webp',
  110. 'textures/gainmap/spruit_sunrise_4k-gainmap.webp',
  111. 'textures/gainmap/spruit_sunrise_4k.json'
  112. ], function ( ) {
  113. resolutions[ 'Webp Gain map (separate)' ] = gainMap.width + 'x' + gainMap.height;
  114. gainMapBackground = hdrJpg.renderTarget.texture;
  115. gainMapPMREMRenderTarget = pmremGenerator.fromEquirectangular( gainMapBackground );
  116. gainMapBackground.mapping = THREE.EquirectangularReflectionMapping;
  117. gainMapBackground.needsUpdate = true;
  118. gainMap.dispose();
  119. }, function ( progress ) {
  120. fileSizes[ 'Webp Gain map (separate)' ] = humanFileSize( progress.total );
  121. } );
  122. hdrEquirectangularMap = new RGBELoader()
  123. .load( 'textures/gainmap/spruit_sunrise_1k.hdr', function ( ) {
  124. resolutions[ 'HDR' ] = hdrEquirectangularMap.image.width + 'x' + hdrEquirectangularMap.image.height;
  125. hdrPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirectangularMap );
  126. hdrEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
  127. hdrEquirectangularMap.minFilter = THREE.LinearFilter;
  128. hdrEquirectangularMap.magFilter = THREE.LinearFilter;
  129. hdrEquirectangularMap.needsUpdate = true;
  130. }, function ( progress ) {
  131. fileSizes[ 'HDR' ] = humanFileSize( progress.total );
  132. } );
  133. renderer.setPixelRatio( window.devicePixelRatio );
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. container.appendChild( renderer.domElement );
  136. stats = new Stats();
  137. container.appendChild( stats.dom );
  138. controls = new OrbitControls( camera, renderer.domElement );
  139. controls.minDistance = 50;
  140. controls.maxDistance = 300;
  141. window.addEventListener( 'resize', onWindowResize );
  142. const gui = new GUI();
  143. gui.add( params, 'envMap', [ 'HDR JPG', 'Webp Gain map (separate)', 'HDR' ] ).onChange( displayStats );
  144. gui.add( params, 'roughness', 0, 1, 0.01 );
  145. gui.add( params, 'metalness', 0, 1, 0.01 );
  146. gui.add( params, 'exposure', 0, 2, 0.01 );
  147. gui.add( params, 'debug' );
  148. gui.open();
  149. function displayStats( value ) {
  150. lbl.innerHTML = value + ' size : ' + fileSizes[ value ] + ', Resolution: ' + resolutions[ value ];
  151. }
  152. }
  153. function humanFileSize( bytes, si = true, dp = 1 ) {
  154. const thresh = si ? 1000 : 1024;
  155. if ( Math.abs( bytes ) < thresh ) {
  156. return bytes + ' B';
  157. }
  158. const units = si
  159. ? [ 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ]
  160. : [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ];
  161. let u = - 1;
  162. const r = 10 ** dp;
  163. do {
  164. bytes /= thresh;
  165. ++ u;
  166. } while ( Math.round( Math.abs( bytes ) * r ) / r >= thresh && u < units.length - 1 );
  167. return bytes.toFixed( dp ) + ' ' + units[ u ];
  168. }
  169. function onWindowResize() {
  170. const width = window.innerWidth;
  171. const height = window.innerHeight;
  172. camera.aspect = width / height;
  173. camera.updateProjectionMatrix();
  174. renderer.setSize( width, height );
  175. }
  176. function animate() {
  177. requestAnimationFrame( animate );
  178. stats.begin();
  179. render();
  180. stats.end();
  181. }
  182. function render() {
  183. torusMesh.material.roughness = params.roughness;
  184. torusMesh.material.metalness = params.metalness;
  185. let pmremRenderTarget, equirectangularMap;
  186. switch ( params.envMap ) {
  187. case 'HDR JPG':
  188. pmremRenderTarget = hdrJpgPMREMRenderTarget;
  189. equirectangularMap = hdrJpgEquirectangularMap;
  190. break;
  191. case 'Webp Gain map (separate)':
  192. pmremRenderTarget = gainMapPMREMRenderTarget;
  193. equirectangularMap = gainMapBackground;
  194. break;
  195. case 'HDR':
  196. pmremRenderTarget = hdrPMREMRenderTarget;
  197. equirectangularMap = hdrEquirectangularMap;
  198. break;
  199. }
  200. const newEnvMap = pmremRenderTarget ? pmremRenderTarget.texture : null;
  201. if ( newEnvMap && newEnvMap !== torusMesh.material.envMap ) {
  202. planeMesh.material.map = newEnvMap;
  203. planeMesh.material.needsUpdate = true;
  204. }
  205. torusMesh.rotation.y += 0.005;
  206. planeMesh.visible = params.debug;
  207. scene.environment = equirectangularMap;
  208. scene.background = equirectangularMap;
  209. renderer.toneMappingExposure = params.exposure;
  210. renderer.render( scene, camera );
  211. }
  212. </script>
  213. </body>
  214. </html>