webgl_materials_envmaps_fasthdr.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgl - materials - equirectangular exr image based lighting</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"><a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - Example of <a href="https://cloud.needle.tools/hdris" target="_blank">FastHDR</a>, loading 10x faster and using 95% less GPU memory than EXR.<br/>Photography by <a href="https://cloud.needle.tools/hdris?img=Ballroom" target="_blank" rel="noopener">Sergej Majboroda</a></div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import Stats from 'three/addons/libs/stats.module.js';
  23. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  24. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  25. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  26. const params = {
  27. 'image': 'ballroom',
  28. 'fov': 40,
  29. 'exposure': 1.0,
  30. 'backgroundBlurriness': 0.0
  31. };
  32. let container, stats;
  33. let camera, scene, renderer, controls;
  34. init();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. camera = new THREE.PerspectiveCamera( params.fov, window.innerWidth / window.innerHeight, .1, 50 );
  39. camera.position.set( 7, 0, 0 );
  40. scene = new THREE.Scene();
  41. renderer = new THREE.WebGLRenderer();
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. container.appendChild( renderer.domElement );
  46. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  47. //
  48. const sphereGeometry = new THREE.SphereGeometry( 0.45, 64, 32 );
  49. const sphere01 = new THREE.Mesh( sphereGeometry, new THREE.MeshPhysicalMaterial( {
  50. transmission: 1.0,
  51. thickness: 2.0,
  52. metalness: 0.0,
  53. roughness: 0.0,
  54. } ) );
  55. sphere01.position.z += 2;
  56. scene.add( sphere01 );
  57. const sphere02 = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( {
  58. metalness: 0.0,
  59. roughness: 1.0,
  60. } ) );
  61. sphere02.position.z += 1;
  62. scene.add( sphere02 );
  63. const sphere03 = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( {
  64. metalness: 1.0,
  65. roughness: 0.0,
  66. } ) );
  67. sphere03.position.z += 0;
  68. scene.add( sphere03 );
  69. const sphere04 = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( {
  70. metalness: 1.0,
  71. roughness: 0.5,
  72. color: 0x888888
  73. } ) );
  74. sphere04.position.z -= 1;
  75. scene.add( sphere04 );
  76. const sphere05 = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( {
  77. metalness: 0.0,
  78. roughness: 0.0,
  79. color: 0x6ab440
  80. } ) );
  81. sphere05.position.z -= 2;
  82. scene.add( sphere05 );
  83. const loader = new KTX2Loader()
  84. .setTranscoderPath( 'jsm/libs/basis/' )
  85. .detectSupport( renderer );
  86. function loadTexture( url ) {
  87. loader.load( url, ( texture ) => {
  88. texture.mapping = THREE.CubeUVReflectionMapping;
  89. scene.environment = texture;
  90. scene.background = texture;
  91. } );
  92. }
  93. loadTexture( 'https://cdn.needle.tools/static/hdris/ballroom_2k.pmrem.ktx2' );
  94. stats = new Stats();
  95. container.appendChild( stats.dom );
  96. controls = new OrbitControls( camera, renderer.domElement );
  97. controls.minDistance = .1;
  98. controls.maxDistance = 20;
  99. controls.enableDamping = true;
  100. window.addEventListener( 'resize', onWindowResize );
  101. const gui = new GUI();
  102. gui.add( params, 'image', {
  103. 'ballroom': 'https://cdn.needle.tools/static/hdris/ballroom_2k.pmrem.ktx2',
  104. 'brown photostudio': 'https://cdn.needle.tools/static/hdris/brown_photostudio_02_2k.pmrem.ktx2',
  105. 'cape hill': 'https://cdn.needle.tools/static/hdris/cape_hill_2k.pmrem.ktx2',
  106. 'cannon': 'https://cdn.needle.tools/static/hdris/cannon_2k.pmrem.ktx2',
  107. 'metro noord': 'https://cdn.needle.tools/static/hdris/metro_noord_2k.pmrem.ktx2',
  108. 'the sky is on fire': 'https://cdn.needle.tools/static/hdris/the_sky_is_on_fire_2k.pmrem.ktx2',
  109. 'studio small 09': 'https://cdn.needle.tools/static/hdris/studio_small_09_2k.pmrem.ktx2',
  110. 'wide street 01': 'https://cdn.needle.tools/static/hdris/wide_street_01_2k.pmrem.ktx2'
  111. } ).onChange( () => {
  112. loadTexture( params.image );
  113. } );
  114. gui.add( params, 'exposure', 0, 2, 0.01 );
  115. gui.add( params, 'fov', 10, 100 ).onChange( () => {
  116. camera.fov = params.fov;
  117. camera.updateProjectionMatrix();
  118. } );
  119. gui.add( params, 'backgroundBlurriness', 0, 1, 0.01 ).name( 'background blurriness' );
  120. gui.open();
  121. }
  122. function onWindowResize() {
  123. const width = window.innerWidth;
  124. const height = window.innerHeight;
  125. camera.aspect = width / height;
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( width, height );
  128. }
  129. function animate() {
  130. stats.begin();
  131. render();
  132. stats.end();
  133. }
  134. function render() {
  135. renderer.toneMappingExposure = params.exposure;
  136. scene.backgroundBlurriness = params.backgroundBlurriness;
  137. controls.update();
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>
粤ICP备19079148号