Skip to content

Commit

Permalink
Fix glTF texture mipmap bug (#1637)
Browse files Browse the repository at this point in the history
* fix:  glTF texture mipmap bug
---------

Co-authored-by: GuoLei1990 <[email protected]>
  • Loading branch information
zhuxudong and GuoLei1990 authored Jul 7, 2023
1 parent ed875dc commit 97ab014
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/loader/src/Texture2DLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Texture2DLoader extends Loader<Texture2D> {
};
this.request<HTMLImageElement>(url, requestConfig)
.then((image) => {
const params = item.params;
const params = item.params as Texture2DParams;
const texture = new Texture2D(
resourceManager.engine,
image.width,
Expand Down
64 changes: 49 additions & 15 deletions packages/loader/src/gltf/parser/GLTFTextureParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AssetPromise, AssetType, Texture2D, TextureFilterMode, TextureWrapMode, Utils } from "@galacean/engine-core";
import { GLTFParserContext } from ".";
import { BufferTextureRestoreInfo } from "../../GLTFContentRestorer";
import { TextureWrapMode as GLTFTextureWrapMode, ISampler, TextureMagFilter, TextureMinFilter } from "../GLTFSchema";
import { GLTFUtils } from "../GLTFUtils";
import { ISampler, TextureMagFilter, TextureMinFilter, TextureWrapMode as GLTFTextureWrapMode } from "../GLTFSchema";
import { GLTFParser } from "./GLTFParser";
import { GLTFParserContext } from ".";

export class GLTFTextureParser extends GLTFParser {
private static _wrapMap = {
Expand All @@ -21,6 +21,7 @@ export class GLTFTextureParser extends GLTFParser {
AssetPromise.all(
glTF.textures.map(({ sampler, source = 0, name: textureName }, index) => {
const { uri, bufferView: bufferViewIndex, mimeType, name: imageName } = glTF.images[source];
const samplerInfo = sampler !== undefined && this._getSamplerInfo(glTF.samplers[sampler]);
if (uri) {
// TODO: support ktx extension https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_texture_basisu/README.md
const index = uri.lastIndexOf(".");
Expand All @@ -29,14 +30,17 @@ export class GLTFTextureParser extends GLTFParser {
return engine.resourceManager
.load<Texture2D>({
url: Utils.resolveAbsoluteUrl(url, uri),
type: type
type: type,
params: {
mipmap: samplerInfo?.mipmap
}
})
.then((texture) => {
if (!texture.name) {
texture.name = textureName || imageName || `texture_${index}`;
}
if (sampler !== undefined) {
this._parseSampler(texture, glTF.samplers[sampler]);
this._parseSampler(texture, samplerInfo);
}
return texture;
});
Expand All @@ -46,12 +50,12 @@ export class GLTFTextureParser extends GLTFParser {
const imageBuffer = new Uint8Array(buffer, bufferView.byteOffset, bufferView.byteLength);

return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then((image) => {
const texture = new Texture2D(engine, image.width, image.height);
const texture = new Texture2D(engine, image.width, image.height, undefined, samplerInfo?.mipmap);
texture.setImageSource(image);
texture.generateMipmaps();
texture.name = textureName || imageName || `texture_${index}`;
if (sampler !== undefined) {
this._parseSampler(texture, glTF.samplers[sampler]);
this._parseSampler(texture, samplerInfo);
}
const bufferTextureRestoreInfo = new BufferTextureRestoreInfo(texture, bufferView, mimeType);
context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);
Expand All @@ -70,25 +74,55 @@ export class GLTFTextureParser extends GLTFParser {
}
}

private _parseSampler(texture: Texture2D, sampler: ISampler): void {
const { magFilter, minFilter, wrapS, wrapT } = sampler;
private _getSamplerInfo(sampler: ISampler): ISamplerInfo {
const { minFilter, magFilter, wrapS, wrapT } = sampler;
const info = <ISamplerInfo>{};

if (minFilter || magFilter) {
info.mipmap = minFilter >= TextureMinFilter.NEAREST_MIPMAP_NEAREST;

if (magFilter || minFilter) {
if (magFilter === TextureMagFilter.NEAREST) {
texture.filterMode = TextureFilterMode.Point;
} else if (minFilter <= TextureMinFilter.LINEAR_MIPMAP_NEAREST) {
texture.filterMode = TextureFilterMode.Bilinear;
info.filterMode = TextureFilterMode.Point;
} else {
texture.filterMode = TextureFilterMode.Trilinear;
if (minFilter <= TextureMinFilter.LINEAR_MIPMAP_NEAREST) {
info.filterMode = TextureFilterMode.Bilinear;
} else {
info.filterMode = TextureFilterMode.Trilinear;
}
}
}

if (wrapS) {
texture.wrapModeU = GLTFTextureParser._wrapMap[wrapS];
info.wrapModeU = GLTFTextureParser._wrapMap[wrapS];
}

if (wrapT) {
texture.wrapModeV = GLTFTextureParser._wrapMap[wrapT];
info.wrapModeV = GLTFTextureParser._wrapMap[wrapT];
}

return info;
}

private _parseSampler(texture: Texture2D, samplerInfo: ISamplerInfo): void {
const { filterMode, wrapModeU, wrapModeV } = samplerInfo;

if (filterMode) {
texture.filterMode = filterMode;
}

if (wrapModeU) {
texture.wrapModeU = wrapModeU;
}

if (wrapModeV) {
texture.wrapModeV = wrapModeV;
}
}
}

interface ISamplerInfo {
filterMode?: TextureFilterMode;
wrapModeU?: TextureWrapMode;
wrapModeV?: TextureWrapMode;
mipmap?: boolean;
}

0 comments on commit 97ab014

Please sign in to comment.