This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
vertex.ts
79 lines (73 loc) · 2.44 KB
/
vertex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Color4, Vector2, Vector3, Vector4 } from '@microsoft/mixed-reality-extension-common';
import {
ColorAttribute,
NormalAttribute,
PositionAttribute,
TangentAttribute,
TexCoordAttribute,
VertexAttribute
} from './vertexattributes';
type Attribute2 = Vector2 | [number, number];
type Attribute3 = Vector3 | [number, number, number];
type Attribute4 = Vector4 | [number, number, number, number];
type AttributeColor4 = Color4 | [number, number, number, number];
export interface VertexLike {
position?: Attribute3;
normal?: Attribute3;
tangent?: Attribute4;
texCoord0?: Attribute2;
texCoord1?: Attribute2;
color0?: AttributeColor4;
}
export class Vertex implements VertexLike {
public position: Vector3;
public normal: Vector3;
public tangent: Vector4;
public texCoord0: Vector2;
public texCoord1: Vector2;
public color0: Color4;
constructor(init: VertexLike = {}) {
if (init.position) {
this.position = init.position instanceof Vector3 ?
init.position :
new Vector3(init.position[0], init.position[1], init.position[2]);
}
if (init.normal) {
this.normal = init.normal instanceof Vector3 ?
init.normal :
new Vector3(init.normal[0], init.normal[1], init.normal[2]);
}
if (init.tangent) {
this.tangent = init.tangent instanceof Vector4 ?
init.tangent :
new Vector4(init.tangent[0], init.tangent[1], init.tangent[2], init.tangent[3]);
}
if (init.texCoord0) {
this.texCoord0 = init.texCoord0 instanceof Vector2 ?
init.texCoord0 :
new Vector2(init.texCoord0[0], init.texCoord0[1]);
}
if (init.texCoord1) {
this.texCoord1 = init.texCoord1 instanceof Vector2 ?
init.texCoord1 :
new Vector2(init.texCoord1[0], init.texCoord1[1]);
}
if (init.color0) {
this.color0 = init.color0 instanceof Color4 ?
init.color0 :
new Color4(init.color0[0], init.color0[1], init.color0[2], init.color0[3]);
}
}
public static positionAttribute: VertexAttribute = new PositionAttribute();
public static normalAttribute: VertexAttribute = new NormalAttribute();
public static tangentAttribute: VertexAttribute = new TangentAttribute();
public static texCoordAttribute: VertexAttribute[] = [
new TexCoordAttribute(0),
new TexCoordAttribute(1)
];
public static colorAttribute: VertexAttribute = new ColorAttribute(0);
}