Skip to content

Commit

Permalink
refactor: VertexAttributeEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Shimada committed Jan 25, 2022
1 parent 82c3904 commit 33603f1
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 90 deletions.
9 changes: 3 additions & 6 deletions src/foundation/definitions/VertexAttribute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ test('Vertex Attributes with Positions', async () => {
expect(position3).toStrictEqual(['POSITION.X', 'POSITION.Y', 'POSITION.Z']);

const position4 = VertexAttribute.Position.XYZW;
expect(position4).toStrictEqual([
'POSITION.X',
'POSITION.Y',
'POSITION.Z',
'POSITION.W',
]);
expect(position4).toStrictEqual(
'POSITION.X,POSITION.Y,POSITION.Z,POSITION.W'
);
});

test('Custom Vertex Layout contains Positions, Normals and Texcoords', async () => {
Expand Down
77 changes: 28 additions & 49 deletions src/foundation/definitions/VertexAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ export interface VertexAttributeEnum extends EnumIO {
Y: VertexAttributeComponent;
Z: VertexAttributeComponent;
W: VertexAttributeComponent;
XY: Array2<VertexAttributeComponent>;
XYZ: Array3<VertexAttributeComponent>;
XYZW: Array4<VertexAttributeComponent>;
XYjoined: VertexAttributeSemanticsJoinedString;
XYZjoined: VertexAttributeSemanticsJoinedString;
XYZWjoined: VertexAttributeSemanticsJoinedString;
XY: VertexAttributeSemanticsJoinedString;
XYZ: VertexAttributeSemanticsJoinedString;
XYZW: VertexAttributeSemanticsJoinedString;
}

type VertexAttributeDescriptor = {
Expand Down Expand Up @@ -106,37 +103,19 @@ export class VertexAttributeClass
get W(): VertexAttributeComponent {
return `${this.attributeTypeName}.W`;
}
get XY(): Array2<VertexAttributeComponent> {
return [`${this.attributeTypeName}.X`, `${this.attributeTypeName}.Y`];
}
get XYZ(): Array3<VertexAttributeComponent> {
return [
`${this.attributeTypeName}.X`,
`${this.attributeTypeName}.Y`,
`${this.attributeTypeName}.Z`,
];
}
get XYZW(): Array4<VertexAttributeComponent> {
return [
`${this.attributeTypeName}.X`,
`${this.attributeTypeName}.Y`,
`${this.attributeTypeName}.Z`,
`${this.attributeTypeName}.W`,
];
}
get XYjoined(): VertexAttributeSemanticsJoinedString {
get XY(): VertexAttributeSemanticsJoinedString {
return `${this.attributeTypeName}.X,${this.attributeTypeName}.Y`;
}
get XYZjoined(): VertexAttributeSemanticsJoinedString {
get XYZ(): VertexAttributeSemanticsJoinedString {
return `${this.attributeTypeName}.X,${this.attributeTypeName}.Y,${this.attributeTypeName}.Z`;
}
get XYZWjoined(): VertexAttributeSemanticsJoinedString {
get XYZW(): VertexAttributeSemanticsJoinedString {
return `${this.attributeTypeName}.X,${this.attributeTypeName}.Y,${this.attributeTypeName}.Z,${this.attributeTypeName}.W`;
}

getVertexAttributeComponentsAsGltf(): Array1to4<VertexAttributeComponent> {
getVertexAttributeComponentsAsGltf(): VertexAttributeSemanticsJoinedString {
if (this.__gltfComponentN === 1) {
return [this.X];
return this.X;
} else if (this.__gltfComponentN === 2) {
return this.XY;
} else if (this.__gltfComponentN === 3) {
Expand Down Expand Up @@ -297,27 +276,27 @@ function toVertexAttributeSemanticJoinedStringAsGltfStyle(
): VertexAttributeSemanticsJoinedString {
switch (attribute) {
case Position:
return attribute.XYZjoined;
return attribute.XYZ;
case Color0:
return attribute.XYZjoined;
return attribute.XYZ;
case Normal:
return attribute.XYZjoined;
return attribute.XYZ;
case Tangent:
return attribute.XYZjoined;
return attribute.XYZ;
case Texcoord0:
return attribute.XYjoined;
return attribute.XY;
case Texcoord1:
return attribute.XYjoined;
return attribute.XY;
case Joints0:
return attribute.XYZWjoined;
return attribute.XYZW;
case Weights0:
return attribute.XYZWjoined;
return attribute.XYZW;
case Instance:
return attribute.X;
case FaceNormal:
return attribute.XYZWjoined;
return attribute.XYZW;
case BaryCentricCoord:
return attribute.XYZWjoined;
return attribute.XYZW;
default:
throw new Error('Invalied glTF VertexAttributeEnum');
}
Expand All @@ -327,27 +306,27 @@ function toAttributeSlotFromJoinedString(
str: VertexAttributeSemanticsJoinedString
): Index {
switch (str) {
case Position.XYZjoined:
case Position.XYZ:
return Position.getAttributeSlot();
case Color0.XYZjoined:
case Color0.XYZ:
return Color0.getAttributeSlot();
case Normal.XYZjoined:
case Normal.XYZ:
return Normal.getAttributeSlot();
case Tangent.XYZjoined:
case Tangent.XYZ:
return Tangent.getAttributeSlot();
case Texcoord0.XYjoined:
case Texcoord0.XY:
return Texcoord0.getAttributeSlot();
case Texcoord1.XYjoined:
case Texcoord1.XY:
return Texcoord1.getAttributeSlot();
case Joints0.XYZWjoined:
case Joints0.XYZW:
return Joints0.getAttributeSlot();
case Weights0.XYZWjoined:
case Weights0.XYZW:
return Weights0.getAttributeSlot();
case Instance.X:
return Instance.getAttributeSlot();
case FaceNormal.XYZjoined:
case FaceNormal.XYZ:
return FaceNormal.getAttributeSlot();
case BaryCentricCoord.XYZjoined:
case BaryCentricCoord.XYZ:
return BaryCentricCoord.getAttributeSlot();
default:
throw new Error('Invalied glTF VertexAttributeEnum');
Expand Down
8 changes: 4 additions & 4 deletions src/foundation/geometry/Cube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ export class Cube extends Primitive {
CompositionType.Vec2,
];
const attributeSemantics = [
VertexAttribute.Position.XYZjoined,
VertexAttribute.Normal.XYZjoined,
VertexAttribute.Texcoord0.XYjoined,
VertexAttribute.Position.XYZ,
VertexAttribute.Normal.XYZ,
VertexAttribute.Texcoord0.XY,
];
const attributes = [
new Float32Array(positions),
Expand All @@ -193,7 +193,7 @@ export class Cube extends Primitive {
];
if (Is.exist(desc.color)) {
attributeCompositionTypes.push(CompositionType.Vec3);
attributeSemantics.push(VertexAttribute.Color0.XYZjoined);
attributeSemantics.push(VertexAttribute.Color0.XYZ);
attributes.push(new Float32Array(colors));
}
const primitiveMode = PrimitiveMode.Triangles;
Expand Down
22 changes: 11 additions & 11 deletions src/foundation/geometry/Mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,20 +549,20 @@ export default class Mesh {
}
for (const primitive of this.__primitives) {
const tangentIdx = primitive.attributeSemantics.indexOf(
VertexAttribute.Tangent.XYZjoined
VertexAttribute.Tangent.XYZ
);
if (tangentIdx !== -1 && this.tangentCalculationMode === 2) {
continue;
}
const texcoordIdx = primitive.attributeSemantics.indexOf(
VertexAttribute.Texcoord0.XYjoined
VertexAttribute.Texcoord0.XY
);
const normalIdx = primitive.attributeSemantics.indexOf(
VertexAttribute.Normal.XYZjoined
VertexAttribute.Normal.XYZ
);
if (texcoordIdx !== -1 && normalIdx !== -1) {
const positionIdx = primitive.attributeSemantics.indexOf(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
);

const positionAccessor = primitive.attributeAccessors[positionIdx];
Expand Down Expand Up @@ -617,7 +617,7 @@ export default class Mesh {
}
primitive.setVertexAttribute(
tangentAccessor,
VertexAttribute.Tangent.XYZjoined
VertexAttribute.Tangent.XYZ
);
}
}
Expand Down Expand Up @@ -757,7 +757,7 @@ export default class Mesh {

for (const primitive of this.__primitives) {
const BaryCentricCoordId = primitive.attributeSemantics.indexOf(
VertexAttribute.BaryCentricCoord.XYZjoined
VertexAttribute.BaryCentricCoord.XYZ
);
if (BaryCentricCoordId !== -1) {
return;
Expand All @@ -767,7 +767,7 @@ export default class Mesh {
BufferUse.CPUGeneric
);
const positionIdx = primitive.attributeSemantics.indexOf(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
);
const positionAccessor = primitive.attributeAccessors[positionIdx];
const vertexNum = positionAccessor.elementCount;
Expand Down Expand Up @@ -797,7 +797,7 @@ export default class Mesh {
}
primitive.setVertexAttribute(
baryCentricCoordAccessor,
VertexAttribute.BaryCentricCoord.XYZjoined
VertexAttribute.BaryCentricCoord.XYZ
);
}
}
Expand All @@ -812,7 +812,7 @@ export default class Mesh {

for (const primitive of this.__primitives) {
const normalIdx = primitive.attributeSemantics.indexOf(
VertexAttribute.Normal.XYZjoined
VertexAttribute.Normal.XYZ
);
if (normalIdx !== -1) {
return;
Expand All @@ -821,7 +821,7 @@ export default class Mesh {
this.__hasFaceNormal = true;

const positionIdx = primitive.attributeSemantics.indexOf(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
);
const positionAccessor = primitive.attributeAccessors[positionIdx];
const indicesAccessor = primitive.indicesAccessor;
Expand Down Expand Up @@ -865,7 +865,7 @@ export default class Mesh {
}
primitive.setVertexAttribute(
normalAccessor,
VertexAttribute.Normal.XYZjoined
VertexAttribute.Normal.XYZ
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/foundation/geometry/Plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export class Plane extends Primitive {
CompositionType.Vec2,
];
const attributeSemantics = [
VertexAttribute.Position.XYZjoined,
VertexAttribute.Normal.XYZjoined,
VertexAttribute.Texcoord0.XYjoined,
VertexAttribute.Position.XYZ,
VertexAttribute.Normal.XYZ,
VertexAttribute.Texcoord0.XY,
];
const primitiveMode = PrimitiveMode.TriangleStrip;
const attributes = [
Expand Down
12 changes: 6 additions & 6 deletions src/foundation/geometry/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ export class Primitive extends RnObject {

get isPositionAccessorUpdated(): boolean {
const positionAccessor = this.__attributes.get(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
);
return positionAccessor?.isMinMaxDirty || false;
}

get AABB() {
if (this.__aabb.isVanilla() || this.isPositionAccessorUpdated) {
const positionAccessor = this.__attributes.get(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
)!;

if (positionAccessor.isMinMaxDirty) {
Expand Down Expand Up @@ -550,7 +550,7 @@ export class Primitive extends RnObject {

if (hasFaceNormal) {
const normalAccessor = this.__attributes.get(
VertexAttribute.Normal.XYZjoined
VertexAttribute.Normal.XYZ
);
if (normalAccessor) {
const normal = normalAccessor.getVec3(i, {});
Expand Down Expand Up @@ -588,7 +588,7 @@ export class Primitive extends RnObject {
const fDat = 1.0 - u - v;

const positionAccessor = this.__attributes.get(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
)!;
const pos0Vec3 = positionAccessor.getVec3(pos0IndexBase, {});
const pos1Vec3 = positionAccessor.getVec3(pos1IndexBase, {});
Expand All @@ -611,7 +611,7 @@ export class Primitive extends RnObject {
}

const positionAccessor = this.__attributes.get(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
)!;

let incrementNum = 3; // gl.TRIANGLES
Expand Down Expand Up @@ -667,7 +667,7 @@ export class Primitive extends RnObject {
pos2IndexBase: Index
) {
const positionAccessor = this.__attributes.get(
VertexAttribute.Position.XYZjoined
VertexAttribute.Position.XYZ
)!;
const pos0Vec3 = positionAccessor.getVec3(pos0IndexBase, {});
const pos1Vec3 = positionAccessor.getVec3(pos1IndexBase, {});
Expand Down
6 changes: 3 additions & 3 deletions src/foundation/geometry/Sphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export class Sphere extends Primitive {
CompositionType.Vec2,
];
const attributeSemantics = [
VertexAttribute.Position.XYZjoined,
VertexAttribute.Normal.XYZjoined,
VertexAttribute.Texcoord0.XYjoined,
VertexAttribute.Position.XYZ,
VertexAttribute.Normal.XYZ,
VertexAttribute.Texcoord0.XY,
];
const primitiveMode = PrimitiveMode.Triangles;
const attributes = [
Expand Down
2 changes: 1 addition & 1 deletion src/foundation/gizmos/AABBGizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export default class AABBGizmo extends Gizmo {

const primitive = Primitive.createPrimitive({
indices: indices,
attributeSemantics: [VertexAttribute.Position.XYZjoined],
attributeSemantics: [VertexAttribute.Position.XYZ],
attributes: [positions],
primitiveMode: PrimitiveMode.Lines,
});
Expand Down
4 changes: 2 additions & 2 deletions src/foundation/gizmos/LocatorGizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ export default class LocatorGizmo extends Gizmo {

const primitive = Primitive.createPrimitive({
attributeSemantics: [
VertexAttribute.Position.XYZjoined,
VertexAttribute.Color0.XYZjoined,
VertexAttribute.Position.XYZ,
VertexAttribute.Color0.XYZ,
],
attributes: [positions, color],
primitiveMode: PrimitiveMode.Lines,
Expand Down
8 changes: 4 additions & 4 deletions src/foundation/importer/DrcPointCloudImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ export default class DrcPointCloudImporter {
}

attributeCompositionTypes.push(CompositionType.Vec3);
attributeSemantics.push(VertexAttribute.Position.XYZjoined);
attributeSemantics.push(VertexAttribute.Position.XYZ);
attributes.push(positions);

draco.destroy(posAttributeData);
Expand Down Expand Up @@ -1249,7 +1249,7 @@ export default class DrcPointCloudImporter {
}

attributeCompositionTypes.push(CompositionType.Vec3);
attributeSemantics.push(VertexAttribute.Color0.XYZjoined);
attributeSemantics.push(VertexAttribute.Color0.XYZ);
attributes.push(colors);

draco.destroy(colAttributeData);
Expand Down Expand Up @@ -1287,7 +1287,7 @@ export default class DrcPointCloudImporter {
normals[i] = norAttributeData.GetValue(i); // XYZ XYZ
}
attributeCompositionTypes.push(CompositionType.Vec3);
attributeSemantics.push(VertexAttribute.Normal.XYZjoined);
attributeSemantics.push(VertexAttribute.Normal.XYZ);
attributes.push(normals);

draco.destroy(norAttributeData);
Expand Down Expand Up @@ -1329,7 +1329,7 @@ export default class DrcPointCloudImporter {
texCoords[i] = texCoordAttributeData.GetValue(i); // XYZ XYZ
}
attributeCompositionTypes.push(CompositionType.Vec2);
attributeSemantics.push(VertexAttribute.Texcoord0.XYjoined);
attributeSemantics.push(VertexAttribute.Texcoord0.XY);
attributes.push(texCoords);

draco.destroy(texCoordAttributeData);
Expand Down
Loading

0 comments on commit 33603f1

Please sign in to comment.