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

feat: glTF parser add more uv channel and color #548

Merged
merged 1 commit into from
Oct 22, 2021
Merged
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
19 changes: 18 additions & 1 deletion packages/loader/src/gltf/GLTFUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vector3, Vector4, Vector2 } from "@oasis-engine/math";
import { IndexFormat, TypedArray, VertexElement, VertexElementFormat } from "@oasis-engine/core";
import { Color, Vector2, Vector3, Vector4 } from "@oasis-engine/math";
import { AccessorComponentType, AccessorType, IAccessor, IBufferView, IGLTF } from "./Schema";

/**
Expand Down Expand Up @@ -33,6 +33,23 @@ export class GLTFUtil {
return array;
}

public static floatBufferToColorArray(buffer: Float32Array, isColor3: boolean): Color[] {
const bufferLen = buffer.length;
const colors = new Array<Color>(bufferLen / (isColor3 ? 3 : 4));

if (isColor3) {
for (let i = 0; i < bufferLen; i += 3) {
colors[i / 3] = new Color(buffer[i], buffer[i + 1], buffer[i + 2], 1.0);
}
} else {
for (let i = 0; i < bufferLen; i += 4) {
colors[i / 4] = new Color(buffer[i], buffer[i + 1], buffer[i + 2], buffer[i + 3]);
}
}

return colors;
}

/**
* Parse binary text for glb loader.
*/
Expand Down
45 changes: 39 additions & 6 deletions packages/loader/src/gltf/parser/MeshParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BlendShape, Engine, EngineObject, ModelMesh, TypedArray } from "@oasis-
import { Vector3 } from "@oasis-engine/math";
import { GLTFResource } from "../GLTFResource";
import { GLTFUtil } from "../GLTFUtil";
import { IGLTF, IMesh, IMeshPrimitive } from "../Schema";
import { AccessorType, IGLTF, IMesh, IMeshPrimitive } from "../Schema";
import { Parser } from "./Parser";

export class MeshParser extends Parser {
Expand Down Expand Up @@ -112,7 +112,8 @@ export class MeshParser extends Parser {
const { attributes, targets, indices, mode } = gltfPrimitive;
let vertexCount: number;

const accessor = gltf.accessors[attributes["POSITION"]];
const { accessors } = gltf;
const accessor = accessors[attributes["POSITION"]];
const positionBuffer = <Float32Array>getVertexBufferData("POSITION");
const positions = GLTFUtil.floatBufferToVector3Array(positionBuffer);
mesh.setPositions(positions);
Expand Down Expand Up @@ -148,14 +149,46 @@ export class MeshParser extends Parser {
const normals = GLTFUtil.floatBufferToVector3Array(<Float32Array>bufferData);
mesh.setNormals(normals);
break;
case "TANGENT":
const tangents = GLTFUtil.floatBufferToVector4Array(<Float32Array>bufferData);
mesh.setTangents(tangents);
break;
case "TEXCOORD_0":
const texturecoords = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords, 0);
break;
case "TEXCOORD_1":
const texturecoords1 = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords1, 1);
break;
case "TEXCOORD_2":
const texturecoords2 = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords2, 2);
break;
case "TEXCOORD_3":
const texturecoords3 = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords3, 3);
case "TEXCOORD_4":
const texturecoords4 = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords4, 4);
case "TEXCOORD_5":
const texturecoords5 = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords5, 5);
case "TEXCOORD_6":
const texturecoords6 = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords6, 6);
case "TEXCOORD_7":
const texturecoords7 = GLTFUtil.floatBufferToVector2Array(<Float32Array>bufferData);
mesh.setUVs(texturecoords7, 7);
break;
case "COLOR_0":
const colors = GLTFUtil.floatBufferToColorArray(
<Float32Array>bufferData,
accessors[attributes["COLOR_0"]].type === AccessorType.VEC3
);
mesh.setColors(colors);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

floatBufferToColorArray support vec3, but vertexElements not

break;
case "TANGENT":
const tangents = GLTFUtil.floatBufferToVector4Array(<Float32Array>bufferData);
mesh.setTangents(tangents);
break;

case "JOINTS_0":
const joints = GLTFUtil.floatBufferToVector4Array(<Float32Array>bufferData);
mesh.setBoneIndices(joints);
Expand Down