webgl_materials_cubemap_mipmaps.html 4.2 KB

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