webgl_materials_cubemap_mipmaps.html 4.3 KB

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