webgl_materials_cubemap_mipmaps.html 4.0 KB

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