webgl_materials_cubemap_mipmaps.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - cubemap mipmaps</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">three.js</a> - cubemap customized mipmaps demo.<br/>
  13. Left: webgl generated mipmaps<br/>
  14. Right: manual mipmaps<br/>
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. let container;
  28. let camera, scene, renderer;
  29. init();
  30. animate();
  31. //load customized cube texture
  32. async function loadCubeTextureWithMipmaps() {
  33. const path = 'textures/cube/angus/';
  34. const format = '.jpg';
  35. const mipmaps = [];
  36. const maxLevel = 8;
  37. async function loadCubeTexture( urls ) {
  38. return new Promise( function ( resolve ) {
  39. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  40. resolve( cubeTexture );
  41. } );
  42. } );
  43. }
  44. // load mipmaps
  45. const pendings = [];
  46. for ( let level = 0; level <= maxLevel; ++ level ) {
  47. const urls = [];
  48. for ( let face = 0; face < 6; ++ face ) {
  49. urls.push( path + 'cube_m0' + level + '_c0' + face + format );
  50. }
  51. const mipmapLevel = level;
  52. pendings.push( loadCubeTexture( urls ).then( function ( cubeTexture ) {
  53. mipmaps[ mipmapLevel ] = cubeTexture;
  54. } ) );
  55. }
  56. await Promise.all( pendings );
  57. const customizedCubeTexture = mipmaps.shift();
  58. customizedCubeTexture.mipmaps = mipmaps;
  59. customizedCubeTexture.colorSpace = THREE.SRGBColorSpace;
  60. customizedCubeTexture.minFilter = THREE.LinearMipMapLinearFilter;
  61. customizedCubeTexture.magFilter = THREE.LinearFilter;
  62. customizedCubeTexture.generateMipmaps = false;
  63. customizedCubeTexture.needsUpdate = true;
  64. return customizedCubeTexture;
  65. }
  66. function init() {
  67. container = document.createElement( 'div' );
  68. document.body.appendChild( container );
  69. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  70. camera.position.z = 500;
  71. scene = new THREE.Scene();
  72. loadCubeTextureWithMipmaps().then( function ( cubeTexture ) {
  73. //model
  74. const sphere = new THREE.SphereGeometry( 100, 128, 128 );
  75. //manual mipmaps
  76. let material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: cubeTexture } );
  77. material.name = 'manual mipmaps';
  78. let mesh = new THREE.Mesh( sphere, material );
  79. mesh.position.set( 100, 0, 0 );
  80. scene.add( mesh );
  81. //webgl mipmaps
  82. material = material.clone();
  83. material.name = 'auto mipmaps';
  84. const autoCubeTexture = cubeTexture.clone();
  85. autoCubeTexture.mipmaps = [];
  86. autoCubeTexture.generateMipmaps = true;
  87. autoCubeTexture.needsUpdate = true;
  88. material.envMap = autoCubeTexture;
  89. mesh = new THREE.Mesh( sphere, material );
  90. mesh.position.set( - 100, 0, 0 );
  91. scene.add( mesh );
  92. } );
  93. //renderer
  94. renderer = new THREE.WebGLRenderer( { antialias: true } );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. container.appendChild( renderer.domElement );
  98. //controls
  99. const controls = new OrbitControls( camera, renderer.domElement );
  100. controls.minPolarAngle = Math.PI / 4;
  101. controls.maxPolarAngle = Math.PI / 1.5;
  102. window.addEventListener( 'resize', onWindowResize );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>