瀏覽代碼

KTX2Loader: Support THREE.DataTexture mipmaps field. (#25871)

Co-authored-by: Don McCurdy <[email protected]>
Co-authored-by: Levi Pesin <[email protected]>
Emmanuel Lee 2 年之前
父節點
當前提交
2ca91c7499
共有 1 個文件被更改,包括 31 次插入4 次删除
  1. 31 4
      examples/jsm/loaders/KTX2Loader.js

+ 31 - 4
examples/jsm/loaders/KTX2Loader.js

@@ -286,7 +286,31 @@ class KTX2Loader extends Loader {
 
 		if ( container.vkFormat !== VK_FORMAT_UNDEFINED ) {
 
-			return createDataTexture( container );
+			const mipmaps = [];
+			const pendings = [];
+
+			for ( let levelIndex = 0; levelIndex < container.levels.length; levelIndex ++ ) {
+
+				pendings.push( createDataTexture( container, levelIndex ).then( function ( dataTexture ) {
+
+					mipmaps[ levelIndex ] = dataTexture;
+
+				} ) );
+
+			}
+
+			await Promise.all( pendings );
+
+			const texture = mipmaps[ 0 ];
+			texture.mipmaps = mipmaps.map( dt => {
+				return {
+					data: dt.source.data,
+					width: dt.source.data.width,
+					height: dt.source.data.height,
+					depth: dt.source.data.depth
+				};
+			} );
+			return texture;
 
 		}
 
@@ -722,9 +746,12 @@ const COLOR_SPACE_MAP = {
 
 };
 
-async function createDataTexture( container ) {
+async function createDataTexture( container, levelIndex = 0 ) {
 
-	const { vkFormat, pixelWidth, pixelHeight, pixelDepth } = container;
+	const { vkFormat } = container;
+	const pixelWidth = Math.max( 1, container.pixelWidth >> levelIndex );
+	const pixelHeight = Math.max( 1, container.pixelHeight >> levelIndex );
+	const pixelDepth = Math.max( 1, container.pixelDepth >> levelIndex );
 
 	if ( FORMAT_MAP[ vkFormat ] === undefined ) {
 
@@ -732,7 +759,7 @@ async function createDataTexture( container ) {
 
 	}
 
-	const level = container.levels[ 0 ];
+	const level = container.levels[ levelIndex ];
 
 	let levelData;
 	let view;