webgl_loader_exr_htj2k.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - EXR with HTJ2K compression</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  12. EXR loader with HTJ2K compression support
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  25. import { EXRLoader } from 'three/addons/loaders/EXRLoader.js';
  26. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  27. let camera, scene, renderer;
  28. init();
  29. async function init() {
  30. const container = document.createElement( 'div' );
  31. document.body.appendChild( container );
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  33. camera.position.set( - 1.8, 0.6, 2.7 );
  34. scene = new THREE.Scene();
  35. // Add loading information
  36. const loadingDiv = document.createElement( 'div' );
  37. loadingDiv.style.position = 'absolute';
  38. loadingDiv.style.top = '50%';
  39. loadingDiv.style.left = '50%';
  40. loadingDiv.style.transform = 'translate(-50%, -50%)';
  41. loadingDiv.style.color = 'white';
  42. loadingDiv.style.fontSize = '20px';
  43. loadingDiv.innerHTML = 'Loading...';
  44. document.body.appendChild( loadingDiv );
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.setAnimationLoop( animate );
  49. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  50. renderer.toneMappingExposure = 1;
  51. container.appendChild( renderer.domElement );
  52. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  53. scene.environment = pmremGenerator.fromScene( new RoomEnvironment( renderer ), 0.04 ).texture;
  54. const controls = new OrbitControls( camera, renderer.domElement );
  55. controls.addEventListener( 'change', render );
  56. controls.minDistance = 2;
  57. controls.maxDistance = 10;
  58. controls.target.set( 0, 0, - 0.2 );
  59. controls.update();
  60. try {
  61. // Load HTJ2K-compressed EXR file
  62. // HTJ2K support is automatically initialized when needed
  63. loadingDiv.innerHTML = 'Loading HTJ2K-compressed EXR...';
  64. const loader = new EXRLoader();
  65. loader.load( 'textures/memorial_htj2k.exr', function ( texture ) {
  66. loadingDiv.remove();
  67. texture.mapping = THREE.EquirectangularReflectionMapping;
  68. const geometry = new THREE.SphereGeometry( 0.8, 64, 32 );
  69. const material = new THREE.MeshStandardMaterial( {
  70. metalness: 0,
  71. roughness: 0,
  72. envMap: texture,
  73. envMapIntensity: 1
  74. } );
  75. const mesh = new THREE.Mesh( geometry, material );
  76. scene.add( mesh );
  77. render();
  78. }, undefined, function ( error ) {
  79. loadingDiv.innerHTML = 'Error loading EXR: ' + error.message;
  80. console.error( error );
  81. } );
  82. } catch ( error ) {
  83. loadingDiv.innerHTML = 'Error: ' + error.message;
  84. console.error( error );
  85. // Fall back to loading a regular EXR without HTJ2K
  86. loadingDiv.innerHTML += '<br>Loading standard EXR instead...';
  87. const loader = new EXRLoader();
  88. loader.load( 'textures/memorial.exr', function ( texture ) {
  89. const infoDiv = document.createElement( 'div' );
  90. infoDiv.style.position = 'absolute';
  91. infoDiv.style.bottom = '20px';
  92. infoDiv.style.left = '20px';
  93. infoDiv.style.color = 'yellow';
  94. infoDiv.innerHTML = 'Using fallback: Standard PIZ-compressed EXR';
  95. document.body.appendChild( infoDiv );
  96. loadingDiv.remove();
  97. texture.mapping = THREE.EquirectangularReflectionMapping;
  98. const geometry = new THREE.SphereGeometry( 0.8, 64, 32 );
  99. const material = new THREE.MeshStandardMaterial( {
  100. metalness: 0,
  101. roughness: 0,
  102. envMap: texture,
  103. envMapIntensity: 1
  104. } );
  105. const mesh = new THREE.Mesh( geometry, material );
  106. scene.add( mesh );
  107. render();
  108. } );
  109. }
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. render();
  117. }
  118. function render() {
  119. renderer.render( scene, camera );
  120. }
  121. function animate() {
  122. render();
  123. }
  124. </script>
  125. </body>
  126. </html>
粤ICP备19079148号