Skip to content

通过渲染环境决定加载什么格式的压缩纹理 #1792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: Master3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/layaAir/laya/RenderEngine/RenderEnum/TextureFormat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { RenderCapable } from './RenderCapable';

/**
* 纹理格式
*/
Expand Down Expand Up @@ -72,4 +74,41 @@ export enum TextureFormat {
KTXTEXTURE = -1,
/**pvr图片 */
PVRTEXTURE = -2
}
}

/** 通过纹理格式获取压缩纹理类型 */
export function getCompressTextureRenderCapable(format: TextureFormat): RenderCapable | null {
switch (format) {
case TextureFormat.ASTC4x4:
case TextureFormat.ASTC4x4SRGB:
case TextureFormat.ASTC6x6:
case TextureFormat.ASTC6x6SRGB:
case TextureFormat.ASTC8x8:
case TextureFormat.ASTC8x8SRGB:
case TextureFormat.ASTC10x10:
case TextureFormat.ASTC10x10SRGB:
case TextureFormat.ASTC12x12:
case TextureFormat.ASTC12x12SRGB:
return RenderCapable.COMPRESS_TEXTURE_ASTC;
case TextureFormat.DXT1:
case TextureFormat.DXT3:
case TextureFormat.DXT5:
return RenderCapable.COMPRESS_TEXTURE_S3TC;
case TextureFormat.PVRTCRGB_2BPPV:
case TextureFormat.PVRTCRGBA_2BPPV:
case TextureFormat.PVRTCRGB_4BPPV:
case TextureFormat.PVRTCRGBA_4BPPV:
return RenderCapable.COMPRESS_TEXTURE_PVRTC;
case TextureFormat.ETC2RGB:
case TextureFormat.ETC2RGBA:
case TextureFormat.ETC2SRGB_Alpha8:
case TextureFormat.ETC2SRGB:
case TextureFormat.ETC2RGB_Alpha1:
case TextureFormat.ETC2SRGB_Alpha1:
return RenderCapable.COMPRESS_TEXTURE_ETC;
case TextureFormat.ETC1RGB:
return RenderCapable.COMPRESS_TEXTURE_ETC1;
default:
return null;
}
}
28 changes: 22 additions & 6 deletions src/layaAir/laya/loaders/TextureLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { KTXTextureInfo } from "../RenderEngine/KTXTextureInfo";
import { TextureDimension } from "../RenderEngine/RenderEnum/TextureDimension";
import { ClassUtils } from "../utils/ClassUtils";
import { BaseTexture } from "../resource/BaseTexture";
import { TextureFormat } from "../RenderEngine/RenderEnum/TextureFormat";
import { getCompressTextureRenderCapable, TextureFormat } from "../RenderEngine/RenderEnum/TextureFormat";
import { Browser } from "../utils/Browser";
import { AssetDb } from "../resource/AssetDb";
import { Resource } from "../resource/Resource";
Expand Down Expand Up @@ -57,15 +57,31 @@ export class Texture2DLoader implements IResourceLoader {
let ext = task.ext;
let url = task.url;
if (meta) {
let platform = Browser.platform;
let fileIndex = meta.platforms?.[platform] || 0;
let fileInfo = meta.files?.[fileIndex] || {};
const RGBA = { format: TextureFormat.R8G8B8A8, file: null as string, ext: null as string };
let fileInfo = RGBA;

if (meta.platforms && meta.files) {
if (Browser.platform in meta.platforms) {
const fileIndex = meta.platforms[Browser.platform];
fileInfo = meta.files[fileIndex];
}
let capable = getCompressTextureRenderCapable(fileInfo.format);
if (capable && !LayaGL.renderEngine.getCapable(capable)) { // 当前环境是不支持 meta 中设置的压缩纹理格式
const fallback = (meta.files as (typeof fileInfo)[]).find(f => {
// 找到第一个支持的压缩纹理格式
const c = getCompressTextureRenderCapable(f.format);
return LayaGL.renderEngine.getCapable(c);
});
fileInfo = fallback || RGBA;
}
}

if (fileInfo.file) {
url = AssetDb.inst.getSubAssetURL(url, task.uuid, fileInfo.file, fileInfo.ext);
ext = fileInfo.ext;
}

constructParams = [0, 0, fileInfo.format ?? 1, meta.mipmap, meta.readWrite, meta.sRGB];
constructParams = [0, 0, fileInfo.format, meta.mipmap, meta.readWrite, meta.sRGB];
propertyParams = {
wrapModeU: meta.wrapMode,
wrapModeV: meta.wrapMode,
Expand Down Expand Up @@ -293,4 +309,4 @@ const videoFormats = ["mp4", "webm"];
Loader.registerLoader(["tga", "tif", "tiff", "png", "jpg", "jpeg", "webp", "rendertexture", ...videoFormats, ...compressedFormats], TextureLoader, Loader.IMAGE, true);
Loader.registerLoader([], Texture2DLoader, Loader.TEXTURE2D, true);
Loader.registerLoader(["rendertexture"], RenderTextureLoader, Loader.TEXTURE2D, true);
Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D);
Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D);