-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobj_mesh.ts
161 lines (133 loc) · 5.05 KB
/
obj_mesh.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { vec3, vec2 } from "gl-matrix";
import { Triangle } from "./triangle";
import { Texture2D } from "./texture";
export class ObjMesh {
obj_no: number;
v: vec3[]
vt: vec2[]
vn: vec3[]
opacity: number = 1.0
refractive_index: number = 1.0
specularity: number = 1.0;
fileContents: string;
triangles: Triangle[]
color: vec3
tex: Texture2D;
constructor(fileContents: string, obj_no: number) {
this.fileContents = fileContents;
this.v = [];
this.vt = [];
this.vn = [];
this.obj_no = obj_no;
this.triangles = [];
}
async initialize(color: vec3, opacity: number, refractive_index: number, specularity: number) {
this.color = color;
this.opacity = opacity;
this.refractive_index = refractive_index;
this.specularity = specularity;
if(this.fileContents.endsWith('.obj')){
await this.readText(this.fileContents);
}
else {
this.readFile(this.fileContents);
}
}
async readText(url: string) {
const response: Response = await fetch(url);
const blob: Blob = await response.blob();
const file_contents = (await blob.text());
this.readFile(file_contents);
}
async readFile(file_contents: string) {
var result: number[] = [];
const lines = file_contents.split("\n");
lines.forEach(
(line) => {
//console.log(line);
if (line[0] == "v" && line[1] == " ") {
this.read_vertex_data(line);
}
else if (line[0] == "v" && line[1] == "t") {
this.read_texcoord_data(line);
}
else if (line[0] == "v" && line[1] == "n") {
this.read_normal_data(line);
}
else if (line[0] == "f") {
this.read_face_data(line, result);
}
}
)
}
read_vertex_data(line: string) {
const components = line.split(" ");
// ["v", "x", "y", "z"]
const new_vertex: vec3 = [
Number(components[1]).valueOf(),
Number(components[2]).valueOf(),
Number(components[3]).valueOf()
];
this.v.push(new_vertex);
}
read_texcoord_data(line: string) {
const components = line.split(" ");
// ["vt", "u", "v"]
const new_texcoord: vec2 = [
Number(components[1]).valueOf(),
Number(components[2]).valueOf()
];
this.vt.push(new_texcoord);
}
read_normal_data(line: string) {
const components = line.split(" ");
// ["vn", "nx", "ny", "nz"]
const new_normal: vec3 = [
Number(components[1]).valueOf(),
Number(components[2]).valueOf(),
Number(components[3]).valueOf()
];
this.vn.push(new_normal);
}
read_face_data(line: string, result: number[]) {
line = line.replace("\n", "");
const vertex_descriptions = line.split(" ");
// ["f", "v1", "v2", ...]
/*
triangle fan setup, eg.
v1 v2 v3 v4 => (v1, v2, v3), (v1, v3, v4)
no. of triangles = no. of vertices - 2
*/
const triangle_count = vertex_descriptions.length - 3; // accounting also for "f"
for (var i = 0; i < triangle_count; i++) {
var tri: Triangle = new Triangle(this.opacity, this.refractive_index,this.specularity);
tri.corners.push(this.read_corner_vertex(vertex_descriptions[1], result));
tri.uv.push(this.read_corner_tex_coord(vertex_descriptions[1], result));
tri.normal.push(this.read_corner_normal(vertex_descriptions[1], result));
tri.corners.push(this.read_corner_vertex(vertex_descriptions[2 + i], result));
tri.uv.push(this.read_corner_tex_coord(vertex_descriptions[2 + i], result));
tri.normal.push(this.read_corner_normal(vertex_descriptions[2 + i], result));
tri.corners.push(this.read_corner_vertex(vertex_descriptions[3 + i], result));
tri.uv.push(this.read_corner_tex_coord(vertex_descriptions[3 + i], result));
tri.normal.push(this.read_corner_normal(vertex_descriptions[3 + i], result));
tri.color = this.color;
tri.make_centroid();
this.triangles.push(tri);
}
}
read_corner_vertex(vertex_description: string, result: number[]): vec3 {
const v_vt_vn = vertex_description.split("/");
const v = this.v[Number(v_vt_vn[0]).valueOf() - 1];
return v;
}
read_corner_tex_coord(vertex_description: string, result: number[]): vec2 {
const v_vt_vn = vertex_description.split("/");
const vt = this.vt[Number(v_vt_vn[1]).valueOf() - 1];
return vt;
}
read_corner_normal(vertex_description: string, result: number[]): vec3 {
const v_vt_vn = vertex_description.split("/");
const vn = this.vn[Number(v_vt_vn[2]).valueOf() - 1];
return vn;
}
}