Skip to content
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

Add texture plugin to support ktx loader #1630

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 7 additions & 7 deletions packages/core/src/mesh/BlendShapeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class BlendShapeManager {
/** @internal */
_bufferBindingOffset: number = -1;
/** @internal */
_vertexElementOffset: number = 0;
_vertexElementOffset: number;

private _useBlendNormal: boolean = false;
private _useBlendTangent: boolean = false;
Expand Down Expand Up @@ -203,15 +203,15 @@ export class BlendShapeManager {
if (this._bufferBindingOffset !== -1) {
return;
}
const { _internalVertexBufferIndex, _vertexBufferBindings } = this._modelMesh;
let i = 0;
const n = Math.max(_vertexBufferBindings.length, _internalVertexBufferIndex + 1);
for (; i < n; i++) {
if (!_vertexBufferBindings[i] && i !== _internalVertexBufferIndex) {

const internalVertexBufferIndex = this._modelMesh._internalVertexBufferIndex;
const vertexBufferBindings = this._modelMesh._vertexBufferBindings;
for (let i = 0, n = vertexBufferBindings.length; i < n; i++) {
if (!vertexBufferBindings[i] && i !== internalVertexBufferIndex) {
break;
}
}
this._bufferBindingOffset = i;
this._bufferBindingOffset = internalVertexBufferIndex + 1;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/mesh/BufferMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ export class BufferMesh extends Mesh {

/**
* Set vertex buffer binding.
* @param vertexBufferBinding - Vertex buffer binding
* @param vertexBufferBindings - Vertex buffer binding
* @param index - Vertex buffer index, the default value is 0
*/
setVertexBufferBinding(vertexBufferBinding: VertexBufferBinding, index?: number): void;
setVertexBufferBinding(vertexBufferBindings: VertexBufferBinding, index?: number): void;

/**
* Set vertex buffer binding.
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/mesh/ModelMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,14 @@ export class ModelMesh extends Mesh {
this._advancedElementUpdateFlag = VertexElementFlags.None;
this._vertexCountDirty = true;
this._blendShapeManager._bufferBindingOffset = -1;
this._blendShapeManager._vertexElementOffset = count;
}

/**
* Set vertex buffer binding.
* @param vertexBufferBinding - Vertex buffer binding
* @param vertexBufferBindings - Vertex buffer binding
* @param index - Vertex buffer index, the default value is 0
*/
setVertexBufferBinding(vertexBufferBinding: VertexBufferBinding, index?: number): void;
setVertexBufferBinding(vertexBufferBindings: VertexBufferBinding, index?: number): void;

/**
* Set vertex buffer binding.
Expand Down
94 changes: 58 additions & 36 deletions packages/loader/src/gltf/parser/GLTFTextureParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { AssetPromise, AssetType, Texture2D, TextureFilterMode, TextureWrapMode, Utils } from "@galacean/engine-core";
import {
AssetPromise,
AssetType,
Texture,
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 @@ -18,47 +26,61 @@ export class GLTFTextureParser extends GLTFParser {

if (glTF.textures) {
const texturesPromiseInfo = context.texturesPromiseInfo;

AssetPromise.all(
glTF.textures.map(({ sampler, source = 0, name: textureName }, index) => {
glTF.textures.map((textureInfo, index) => {
const { sampler, source = 0, name: textureName, extensions } = textureInfo;
const { uri, bufferView: bufferViewIndex, mimeType, name: imageName } = glTF.images[source];
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(".");
const ext = uri.substring(index + 1);
const type = ext.startsWith("ktx") ? AssetType.KTX : AssetType.Texture2D;
return engine.resourceManager
.load<Texture2D>({
url: Utils.resolveAbsoluteUrl(url, uri),
type: type
})
.then((texture) => {
if (!texture.name) {
texture.name = textureName || imageName || `texture_${index}`;
}

let texture = <Texture | Promise<Texture>>(
GLTFParser.executeExtensionsCreateAndParse(extensions, context, textureInfo)
);

if (!texture) {
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(".");
const ext = uri.substring(index + 1);
const type = ext.startsWith("ktx") ? AssetType.KTX : AssetType.Texture2D;
texture = engine.resourceManager
.load<Texture2D>({
url: Utils.resolveAbsoluteUrl(url, uri),
type: type
})
.then((texture) => {
if (!texture.name) {
texture.name = textureName || imageName || `texture_${index}`;
}
if (sampler !== undefined) {
this._parseSampler(texture, glTF.samplers[sampler]);
}
return texture;
});
} else {
const bufferView = glTF.bufferViews[bufferViewIndex];
const buffer = buffers[bufferView.buffer];
const imageBuffer = new Uint8Array(buffer, bufferView.byteOffset, bufferView.byteLength);

texture = GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then((image) => {
const texture = new Texture2D(engine, image.width, image.height);
texture.setImageSource(image);
texture.generateMipmaps();
texture.name = textureName || imageName || `texture_${index}`;
if (sampler !== undefined) {
this._parseSampler(texture, glTF.samplers[sampler]);
}
const bufferTextureRestoreInfo = new BufferTextureRestoreInfo(texture, bufferView, mimeType);
context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);

return texture;
});
} else {
const bufferView = glTF.bufferViews[bufferViewIndex];
const buffer = buffers[bufferView.buffer];
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);
texture.setImageSource(image);
texture.generateMipmaps();
texture.name = textureName || imageName || `texture_${index}`;
if (sampler !== undefined) {
this._parseSampler(texture, glTF.samplers[sampler]);
}
const bufferTextureRestoreInfo = new BufferTextureRestoreInfo(texture, bufferView, mimeType);
context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);

return texture;
});
}
}

return Promise.resolve(texture).then((texture) => {
GLTFParser.executeExtensionsAdditiveAndParse(extensions, context, texture, textureInfo);
return texture;
});
})
)
.then((textures: Texture2D[]) => {
Expand Down
25 changes: 0 additions & 25 deletions tests/src/core/ModelMesh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,29 +516,4 @@ describe("ModelMesh Test", async function () {
expect(normals1).deep.eq(rightNormals);
expect(uvs1).deep.eq(rightUVs);
});

it("Test blend shape buffer offset", () => {
const mesh = new ModelMesh(engine);
const deltaPositions = [new Vector3(0.0, 0.0, 0.0)];
const blendShape = new BlendShape("BlendShapeTest");
blendShape.addFrame(1.0, deltaPositions);
mesh.addBlendShape(blendShape);
const vertexElements = [
new VertexElement(VertexAttribute.Position, 0, VertexElementFormat.Vector3, 0),
new VertexElement(VertexAttribute.UV, 0, VertexElementFormat.Vector3, 1)
];
// 顶点
const pos = new Float32Array(3);
// UV
const uv = new Float32Array(2);
(pos[0] = -1), (pos[1] = 1), (pos[2] = 1);
(uv[0] = 0), (uv[1] = 0);
const posBuffer = new Buffer(engine, BufferBindFlag.VertexBuffer, pos, BufferUsage.Static, true);
const uvBuffer = new Buffer(engine, BufferBindFlag.VertexBuffer, uv, BufferUsage.Static, true);
mesh.setVertexElements(vertexElements);
mesh.setVertexBufferBinding(posBuffer, 12, 0);
mesh.setVertexBufferBinding(uvBuffer, 8, 1);
mesh.uploadData(false);
expect(mesh.vertexBufferBindings.length).eq(3);
});
});