-
Notifications
You must be signed in to change notification settings - Fork 1
/
obj-parser.js
177 lines (148 loc) · 5.6 KB
/
obj-parser.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var ObjParser = {
triangulate: function(face) {
let faces = [];
for (var prev = 1, i = 2; i < face.length; prev = i++) {
faces.push([face[0], face[prev], face[i]]);
}
return faces;
},
parse: function(fileCode) {
let objFile = fileCode.split("\n");
let newObj = {
metadata: [],
vertices: [],
faces: []
};
let vals;
for (var i = 0; i < objFile.length; i++) {
var cmd = objFile[i];
var type = cmd.split(" ")[0];
var valStart = 1;
while (cmd.charAt(valStart) !== " " && valStart < cmd.length) {
valStart++;
}
while (cmd.charAt(valStart) === " " && valStart < cmd.length) {
valStart++;
}
let params, param;
switch (type) {
case "#":
var comment = cmd.slice(valStart, cmd.length);
if (comment.length > 0) {
newObj.metadata.push(comment);
}
break;
case "v":
params = cmd.slice(valStart, cmd.length).split(" ");
for (var p = 0; p < params.length; p++) {
param = params[p];
if (param.length > 0) {
newObj.vertices.push(Number(param));
}
}
break;
case "f":
params = cmd.slice(valStart, cmd.length).split(" ");
vals = [];
for (var p = 0; p < params.length; p++) {
param = params[p];
if (param.length > 0) {
if (param.includes("/")) {
param = param.split("/")[0];
}
vals.push(Number(param) - 1);
}
}
newObj.faces.push(vals);
break;
}
}
return newObj;
},
toBabylonMesh: function(objFile, sz=[1, 1, 1], trans=[0, 0, 0], rot=[0, 0, 0]) {
let BABYLON = window.BABYLON;
let scene = window.scene;
let myMesh = new BABYLON.Mesh("custom", scene);
let vertexData = new BABYLON.VertexData();
if (typeof sz === "number") {
sz = [sz, sz, sz];
}
if (typeof objFile === "string") {
objFile = ObjParser.parse(objFile);
}
let vertices = objFile.vertices;
let cos = Math.cos;
let sin = Math.sin;
let trigCache = [
cos(rot[0]), sin(rot[0]),
cos(rot[1]), sin(rot[1]),
cos(rot[2]), sin(rot[2]),
]
let vx, vy;
for (var i = 0; i < vertices.length; i += 3) {
// rotate x
if (rot[0] !== 0) {
vy = vertices[i+1];
vertices[i+1] = vy * trigCache[0] - vertices[i+2] * trigCache[1];
vertices[i+2] = vy* trigCache[1] + vertices[i+2] * trigCache[0]
}
// rotate y
if (rot[1] !== 0) {
vx = vertices[i];
vertices[i] = vx * trigCache[2] + vertices[i+2] * trigCache[3];
vertices[i+2] = vertices[i+2] * trigCache[2] - vx * trigCache[3];
}
// rotate z
if (rot[2] !== 0) {
vx = vertices[i];
vertices[i] = vx * trigCache[4] - vertices[i+1] * trigCache[5];
vertices[i+1] = vx * trigCache[5] + vertices[i+1] * trigCache[4];
}
// translate
vertices[i] += trans[0];
vertices[i+1] += trans[1];
vertices[i+2] += trans[2];
}
let newFaces = [], f;
for (var i = 0; i < objFile.faces.length; i++) {
f = objFile.faces[i];
switch (f.length) {
case 2:
newFaces.push(f[0], f[1], f[0]);
break;
case 2:
newFaces.push(f[0], f[1], f[2]);
break;
default: {
let triangulatedFaces = ObjParser.triangulate(f), face;
for (var j = 0; j < triangulatedFaces.length; j++) {
face = triangulatedFaces[j];
newFaces.push(face[0], face[1], face[2]);
}
break;
}
}
}
let faces = newFaces,
normals = [],
uvs = [];
// take uv value relative to bottom left corner of roof (-4, -4) noting length and width of roof is 8; base uv value on the x, z coordinates only
for (var p = 0, len = vertices.length / 3; p < len; p++) {
// *0.125 rather than /8 for micro-optimization
uvs.push((vertices[3 * p] + 4) * 0.125, (vertices[3 * p + 2] + 4) * 0.125);
}
// Calculations of normals added
BABYLON.VertexData.ComputeNormals(vertices, faces, normals);
vertexData.positions = vertices;
vertexData.indices = faces;
vertexData.normals = normals; //Assignment of normal to vertexData added
vertexData.uvs = uvs;
vertexData.applyToMesh(myMesh);
myMesh.convertToFlatShadedMesh();
// use scaling
myMesh.scaling.x = sz[0];
myMesh.scaling.y = sz[1];
myMesh.scaling.z = sz[2];
return myMesh;
}
};