webgpu_materials_cubemap_mipmaps.html 4.7 KB

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