webgpu_materials_cubemap_mipmaps.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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="example.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  13. <div class="title-wrapper">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Custom CubeMap Mipmaps</span>
  15. </div>
  16. <small>
  17. Cubemap customized mipmaps demo.<br/>
  18. Left: WebGPU generated mipmaps<br/>
  19. Right: manual mipmaps<br/>
  20. </small>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/webgpu": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.tsl.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. let container;
  36. let camera, scene, renderer;
  37. init();
  38. // load customized cube texture
  39. async function loadCubeTextureWithMipmaps() {
  40. const path = 'textures/cube/angus/';
  41. const format = '.jpg';
  42. const mipmaps = [];
  43. const maxLevel = 8;
  44. async function loadCubeTexture( urls ) {
  45. return new Promise( function ( resolve ) {
  46. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  47. resolve( cubeTexture );
  48. } );
  49. } );
  50. }
  51. // load mipmaps
  52. const pendings = [];
  53. for ( let level = 0; level <= maxLevel; ++ level ) {
  54. const urls = [];
  55. for ( let face = 0; face < 6; ++ face ) {
  56. urls.push( path + 'cube_m0' + level + '_c0' + face + format );
  57. }
  58. const mipmapLevel = level;
  59. pendings.push( loadCubeTexture( urls ).then( function ( cubeTexture ) {
  60. mipmaps[ mipmapLevel ] = cubeTexture;
  61. } ) );
  62. }
  63. await Promise.all( pendings );
  64. const customizedCubeTexture = mipmaps.shift();
  65. customizedCubeTexture.mipmaps = mipmaps;
  66. customizedCubeTexture.colorSpace = THREE.SRGBColorSpace;
  67. customizedCubeTexture.minFilter = THREE.LinearMipMapLinearFilter;
  68. customizedCubeTexture.magFilter = THREE.LinearFilter;
  69. customizedCubeTexture.generateMipmaps = false;
  70. customizedCubeTexture.needsUpdate = true;
  71. return customizedCubeTexture;
  72. }
  73. function init() {
  74. container = document.createElement( 'div' );
  75. document.body.appendChild( container );
  76. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  77. camera.position.z = 500;
  78. scene = new THREE.Scene();
  79. loadCubeTextureWithMipmaps().then( function ( cubeTexture ) {
  80. // model
  81. const sphere = new THREE.SphereGeometry( 100, 128, 128 );
  82. // manual mipmaps
  83. let material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: cubeTexture } );
  84. material.name = 'manual mipmaps';
  85. let mesh = new THREE.Mesh( sphere, material );
  86. mesh.position.set( 100, 0, 0 );
  87. scene.add( mesh );
  88. // auto mipmaps
  89. material = material.clone();
  90. material.name = 'auto mipmaps';
  91. const autoCubeTexture = cubeTexture.clone();
  92. autoCubeTexture.mipmaps = [];
  93. autoCubeTexture.generateMipmaps = true;
  94. autoCubeTexture.needsUpdate = true;
  95. material.envMap = autoCubeTexture;
  96. mesh = new THREE.Mesh( sphere, material );
  97. mesh.position.set( - 100, 0, 0 );
  98. scene.add( mesh );
  99. } );
  100. //renderer
  101. renderer = new THREE.WebGPURenderer( { antialias: true } );
  102. renderer.setPixelRatio( window.devicePixelRatio );
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. renderer.setAnimationLoop( animate );
  105. container.appendChild( renderer.domElement );
  106. //controls
  107. const controls = new OrbitControls( camera, renderer.domElement );
  108. controls.minPolarAngle = Math.PI / 4;
  109. controls.maxPolarAngle = Math.PI / 1.5;
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function animate() {
  118. renderer.render( scene, camera );
  119. }
  120. </script>
  121. </body>
  122. </html>
粤ICP备19079148号