webgl_materials_cubemap_mipmaps.html 4.1 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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - cubemap customized mipmaps demo. Author <a href="https://github.com/AngusLang">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 Stats from './jsm/libs/stats.module.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. var container, stats;
  21. var camera, scene, renderer;
  22. init();
  23. animate();
  24. //load custmized cube texture
  25. async function loadCubeTextureWithMipmaps() {
  26. var path = 'textures/cube/angus/';
  27. var format = '.jpg';
  28. var mipmaps = [];
  29. async function loadCubeTexture( urls ) {
  30. return new Promise( function( resolve, reject) {
  31. new THREE.CubeTextureLoader().load( urls, function( cubeTexture ) {
  32. resolve( cubeTexture );
  33. } );
  34. } );
  35. }
  36. //load mipmaps
  37. var pendings = [];
  38. for ( var level = 0; level < 9; ++ level ) {
  39. var urls = [];
  40. for ( var face = 0; face < 6; ++ face ) {
  41. urls.push( path + 'cube_m0' + level + '_c0' + face + format );
  42. }
  43. pendings.push( loadCubeTexture( urls ).then( function( cubeTexture ) {
  44. mipmaps.push( cubeTexture );
  45. } ) );
  46. }
  47. await Promise.all( pendings );
  48. var customizedCubeTexture = mipmaps.shift();
  49. customizedCubeTexture.mipmaps = mipmaps;
  50. customizedCubeTexture.minFilter = THREE.LinearMipMapLinearFilter;
  51. customizedCubeTexture.magFilter = THREE.LinearFilter;
  52. customizedCubeTexture.generateMipmaps = false;
  53. customizedCubeTexture.needsUpdate = true;
  54. return customizedCubeTexture;
  55. }
  56. function init() {
  57. container = document.createElement( 'div' );
  58. document.body.appendChild( container );
  59. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  60. camera.position.z = 500;
  61. scene = new THREE.Scene();
  62. var pendings = [];
  63. loadCubeTextureWithMipmaps().then( function( cubeTexture ) {
  64. //model
  65. var sphere = new THREE.SphereBufferGeometry( 100, 128, 128 );
  66. //manual mipmaps
  67. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: cubeTexture } );
  68. material.name = 'manual mipmaps';
  69. var mesh = new THREE.Mesh( sphere, material );
  70. mesh.position.set( 150, 0, 0 );
  71. scene.add( mesh );
  72. //webgl mipmaps
  73. material = material.clone();
  74. material.name = 'auto mipmaps';
  75. var 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( -150, 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. var controls = new OrbitControls( camera, renderer.domElement );
  91. controls.minPolarAngle = Math.PI / 4;
  92. controls.maxPolarAngle = Math.PI / 1.5;
  93. //stats
  94. stats = new Stats();
  95. container.appendChild( stats.dom );
  96. window.addEventListener( 'resize', onWindowResize, false );
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. render();
  106. }
  107. function render() {
  108. renderer.render( scene, camera );
  109. stats.update();
  110. }
  111. </script>
  112. </body>
  113. </html>