webgpu_materials_cubemap_mipmaps.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGPU - 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: WebGPU generated mipmaps<br/>
  14. Right: manual mipmaps<br/>
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/webgpu": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.tsl.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three/webgpu';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. let container;
  30. let camera, scene, renderer;
  31. init();
  32. // load customized cube texture
  33. async function loadCubeTextureWithMipmaps() {
  34. const path = 'textures/cube/angus/';
  35. const format = '.jpg';
  36. const mipmaps = [];
  37. const maxLevel = 8;
  38. async function loadCubeTexture( urls ) {
  39. return new Promise( function ( resolve ) {
  40. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  41. resolve( cubeTexture );
  42. } );
  43. } );
  44. }
  45. // load mipmaps
  46. const pendings = [];
  47. for ( let level = 0; level <= maxLevel; ++ level ) {
  48. const urls = [];
  49. for ( let face = 0; face < 6; ++ face ) {
  50. urls.push( path + 'cube_m0' + level + '_c0' + face + format );
  51. }
  52. const mipmapLevel = level;
  53. pendings.push( loadCubeTexture( urls ).then( function ( cubeTexture ) {
  54. mipmaps[ mipmapLevel ] = cubeTexture;
  55. } ) );
  56. }
  57. await Promise.all( pendings );
  58. const customizedCubeTexture = mipmaps.shift();
  59. customizedCubeTexture.mipmaps = mipmaps;
  60. customizedCubeTexture.colorSpace = THREE.SRGBColorSpace;
  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. // auto 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.WebGPURenderer( { antialias: true } );
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.setAnimationLoop( animate );
  99. container.appendChild( renderer.domElement );
  100. //controls
  101. const controls = new OrbitControls( camera, renderer.domElement );
  102. controls.minPolarAngle = Math.PI / 4;
  103. controls.maxPolarAngle = Math.PI / 1.5;
  104. window.addEventListener( 'resize', onWindowResize );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. function animate() {
  112. renderer.render( scene, camera );
  113. }
  114. </script>
  115. </body>
  116. </html>
粤ICP备19079148号