webgl_materials_cubemap_mipmaps.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. //load customized cube texture
  31. async function loadCubeTextureWithMipmaps() {
  32. const path = 'textures/cube/angus/';
  33. const format = '.jpg';
  34. const mipmaps = [];
  35. const maxLevel = 8;
  36. async function loadCubeTexture( urls ) {
  37. return new Promise( function ( resolve ) {
  38. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  39. resolve( cubeTexture );
  40. } );
  41. } );
  42. }
  43. // load mipmaps
  44. const pendings = [];
  45. for ( let level = 0; level <= maxLevel; ++ level ) {
  46. const urls = [];
  47. for ( let face = 0; face < 6; ++ face ) {
  48. urls.push( path + 'cube_m0' + level + '_c0' + face + format );
  49. }
  50. const mipmapLevel = level;
  51. pendings.push( loadCubeTexture( urls ).then( function ( cubeTexture ) {
  52. mipmaps[ mipmapLevel ] = cubeTexture;
  53. } ) );
  54. }
  55. await Promise.all( pendings );
  56. const customizedCubeTexture = mipmaps.shift();
  57. customizedCubeTexture.mipmaps = mipmaps;
  58. customizedCubeTexture.colorSpace = THREE.SRGBColorSpace;
  59. customizedCubeTexture.minFilter = THREE.LinearMipMapLinearFilter;
  60. customizedCubeTexture.magFilter = THREE.LinearFilter;
  61. customizedCubeTexture.generateMipmaps = false;
  62. customizedCubeTexture.needsUpdate = true;
  63. return customizedCubeTexture;
  64. }
  65. function init() {
  66. container = document.createElement( 'div' );
  67. document.body.appendChild( container );
  68. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  69. camera.position.z = 500;
  70. scene = new THREE.Scene();
  71. loadCubeTextureWithMipmaps().then( function ( cubeTexture ) {
  72. //model
  73. const sphere = new THREE.SphereGeometry( 100, 128, 128 );
  74. //manual mipmaps
  75. let material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: cubeTexture } );
  76. material.name = 'manual mipmaps';
  77. let mesh = new THREE.Mesh( sphere, material );
  78. mesh.position.set( 100, 0, 0 );
  79. scene.add( mesh );
  80. //webgl mipmaps
  81. material = material.clone();
  82. material.name = 'auto mipmaps';
  83. const autoCubeTexture = cubeTexture.clone();
  84. autoCubeTexture.mipmaps = [];
  85. autoCubeTexture.generateMipmaps = true;
  86. autoCubeTexture.needsUpdate = true;
  87. material.envMap = autoCubeTexture;
  88. mesh = new THREE.Mesh( sphere, material );
  89. mesh.position.set( - 100, 0, 0 );
  90. scene.add( mesh );
  91. } );
  92. //renderer
  93. renderer = new THREE.WebGLRenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.setAnimationLoop( animate );
  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. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>