-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj-generator.js
326 lines (248 loc) · 9.47 KB
/
obj-generator.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
'use strict';
import colors from 'colors';
import vector from './vector.js';
export {
generate,
}
function generate(reflections, wall, eye, wall_face_divisions) {
const program = Array.from(createObjFile(reflections, wall, eye, wall_face_divisions)).join('\n');
return program;
}
function * createObjFile(reflections, wall, eye, wall_face_divisions) {
console.log('mirrors', reflections.length);
var vertex = {current: 1};
//#yield 'mtllib material.mtl';
yield 's off';
yield 'o mirror'
var textures = getWallTextureMap(wall_face_divisions)
yield * convertTexturesToObj(textures);
yield * convertMirrorsToObj(reflections, vertex);
yield * convertWallToObj(wall, vertex, textures.wall_texture_ids);
yield * convertEyeToObj(eye, vertex);
yield * convertReflectionsToObj(reflections, vertex);
yield * convertReflectionEllipsesToObj(reflections, vertex);
yield * convertMirrorNormalsToObj(reflections, vertex);
console.log(colors.green(`created mesh (.obj) file`));
}
function * convertTexturesToObj(textures) {
for (var uv of textures.uv) {
yield `vt ${uv.u.toFixed(5)} ${uv.v.toFixed(5)}`;
}
}
function getWallTextureMap(wall_face_divisions) {
var output = {};
output.uv = [
{id: 1, u: 0, v: 1},
{id: 2, u: 1, v: 1},
{id: 3, u: 1, v: 0},
{id: 4, u: 0, v: 0},
];
var texture_id = output.uv[output.uv.length-1].id + 1;
var grids = wall_face_divisions;
var wall_texture_ids = Array.from({length: grids + 1}).fill(0).map( _ => Array.from({length: grids + 1}).fill(0));
for (var i = 0; i < wall_texture_ids.length; i++) {
for (var j = 0; j < wall_texture_ids[i].length; j++) {
var u = i / grids;
var v = j / grids;
var id = texture_id++;
output.uv.push({id, u, v});
wall_texture_ids[i][j] = id;
}
}
output.wall_texture_ids = wall_texture_ids;
return output;
}
function * convertMirrorsToObj(reflections, vertex) {
//#yield `usemtl mirror_face`;
const mirrorFaces = reflections.map(r => r.mirror).map( mirror => getMirrorPolygons(mirror, vertex));
// create vertices for the mirror
for (const face of mirrorFaces) {
yield * createMirrorVertices(face);
}
yield `g mirror_glass`;
for (const face of mirrorFaces) {
yield * createMirrorGlassFaces(face);
}
yield `g mirror_reflector`;
for (const face of mirrorFaces) {
yield * createMirrorReflectorFace(face);
}
}
function getMirrorPolygons(mirror, vertex) {
const globalUp = vector().globalUp;
const globalRight = vector().globalRight;
const globalDown = vector().globalDown;
const right = globalUp.cross(mirror.normal).normalized().scale(mirror.width/2);
const down = right.cross(mirror.normal).normalized().scale(mirror.height/2);
const edges = 10;
const bottom_vertices = enumerate(0, edges - 1)
.map(i => Math.PI * 2 / edges * i - Math.PI / 2)
.map(a => ({x: Math.cos(a) * 1, y: Math.sin(a) * 1}))
.map(local_pos => mirror.pos.add(right.scale(local_pos.x)).add(down.scale(local_pos.y)))
.map(pos => ({id: vertex.current++, pos}))
const top_vertices = enumerate(0, edges - 1)
.map(i => Math.PI * 2 / edges * i - Math.PI / 2)
.map(a => ({x: Math.cos(a) * 1, y: Math.sin(a) * 1}))
.map(local_pos => mirror.pos.add(right.scale(local_pos.x)).add(down.scale(local_pos.y)))
// THE NORMALS ARE INVERTED SOMEHOW. THIS SHOULD NOT BE NEGATIVE TO GET TO THE TOP
.map(pos => pos.add(mirror.normal.scale(mirror.thickness))) // TODO: THIS SHOULD BE PARAMETERIZED FROM SETTINGS.
.map(pos => ({id: vertex.current++, pos}));
return {
id: mirror.id,
top_vertices,
bottom_vertices,
};
}
function * createMirrorVertices(face) {
for (let vertex of face.bottom_vertices) {
yield vertice(vertex.pos);
}
for (let vertex of face.top_vertices) {
yield vertice(vertex.pos);
}
}
function * createMirrorGlassFaces(mirror) {
const top_verts = mirror.top_vertices.reverse().map((vert, i) => `${vert.id}/${i+1}`).join(' ');
yield `f ${top_verts}`;
const bottom_verts = mirror.bottom_vertices.map((vert, i) => `${vert.id}/${i+1}`).join(' ');
yield `f ${bottom_verts}`;
// sides
const l = mirror.top_vertices.length;
const tv = mirror.top_vertices.reverse();
const bv = mirror.bottom_vertices;
const sides = enumerate(0, l-1).map(i => {
return `f ${tv[(i)%l].id}/1 ${tv[(i+1)%l].id}/2 ${bv[(i+1)%l].id}/3 ${bv[i%l].id}/4`
}).join('\n');
yield sides;
}
function * createMirrorReflectorFace(mirror) {
const bottom_verts = mirror.bottom_vertices.reverse().map((vert, i) => `${vert.id}/${i+1}`).join(' ');
yield `f ${bottom_verts}`;
}
function vertice(vector) {
return `v ${vector.x.toFixed(5)} ${vector.y.toFixed(5)} ${vector.z.toFixed(5)}`;
}
function * convertMirrorNormalsToObj(reflections, vertex) {
yield `g debug_mirror_normals`;
const mirrors = reflections.map(r => r.mirror);
for (let mirror of mirrors) {
yield * convertMirrorNormalToObj(mirror, vertex);
}
}
function * convertMirrorNormalToObj(mirror, vertex) {
const normal = mirror.normal.scale(0.01);
yield vertice(mirror.pos);
yield vertice(mirror.pos.add(normal));
yield `l ${vertex.current++} ${vertex.current++}`;
const globalUp = vector().globalUp;
const globalRight = vector().globalRight;
const right = globalUp.cross(mirror.normal).normalized().scale(0.01);
const up = globalRight.cross(mirror.normal).normalized().scale(0.01).negated();
const p1 = mirror.pos;
const p2 = mirror.pos.add(right.scale(0.3));
const p3 = mirror.pos.add(up.scale(0.5));
yield vertice(p1);
yield vertice(p2);
yield vertice(p3);
let v1 = vertex.current++;
let v2 = vertex.current++;
let v3 = vertex.current++;
yield `l ${v1} ${v2}`;
yield `l ${v1} ${v3}`;
}
function * convertWallToObj(wall, vertex, wall_texture_ids) {
const grids = wall_texture_ids.length - 1;
var vertices = Array.from({length: grids + 1}).fill(0).map( _ => Array.from({length: grids + 1}).fill(0));
yield `g wall`;
for (var i = 0; i < grids + 1; i++) {
for (var j = 0; j < grids + 1; j++) {
var x = i / grids - 0.5;
var y = j / grids - 0.5;
const vert = vertice(wall.worldPosAtTextureCoord(x, y)); // lower left
yield vert;
var vertex_id = vertex.current++;
vertices[i][j] = vertex_id;
}
}
for (var i = 0; i < grids; i++) {
for (var j = 0; j < grids; j++) {
var vertice_lower_left = vertices[i][j+1];
var vertice_lower_right = vertices[i+1][j+1];
var vertice_upper_right = vertices[i+1][j];
var vertice_upper_left = vertices[i][j];
var uv_lower_left = wall_texture_ids[i][j+1];
var uv_lower_right = wall_texture_ids[i+1][j+1];
var uv_upper_right = wall_texture_ids[i+1][j];
var uv_upper_left = wall_texture_ids[i][j];
yield `f ${vertice_lower_left}/${uv_lower_left} ${vertice_lower_right}/${uv_lower_right} ${vertice_upper_right}/${uv_upper_right} ${vertice_upper_left}/${uv_upper_left}`;
}
}
}
function * convertEyeToObj(eye, vertex) {
const steps = 50;
yield `g debug_eye`;
//#yield `usemtl blue_line`;
for (let i = 0; i < steps; i++) {
const angle = Math.PI * 2 / steps * i;
//const x = eye.pos.x + Math.cos(angle) * eye.size;
//const y = eye.pos.y + Math.sin(angle) * eye.size;
let pos = eye.pos.add(vector(Math.cos(angle), Math.sin(angle),0).scale(eye.size))
yield vertice(pos);
//yield `v ${x} ${y} ${eye.pos.z}`;
}
const segments = create(0, steps, ()=>{ return `${vertex.current++}`}).join(' ');
yield `l ${segments}`;
}
function * convertReflectionsToObj(reflections, vertex) {
yield `g debug_mirrorreflections`;
//#yield `usemtl gray_line`;
for (const reflection of reflections) {
yield * convertMirrorReflectionToObj(reflection, vertex);
}
yield `g debug_eyereflections`;
for (const reflection of reflections) {
yield * convertEyeReflectionToObj(reflection, vertex);
}
}
function * convertReflectionEllipsesToObj(reflections, vertex) {
yield `g debug_mirrorreflectionellipses`;
for (const reflection of reflections) {
yield * convertEyeReflectionEllipseToObj(reflection, vertex);
}
}
function * convertMirrorReflectionToObj(reflection, vertex) {
//yield `v ${reflection.mirror.pos.x} ${reflection.mirror.pos.y} ${reflection.mirror.pos.z}`;
yield vertice(reflection.mirror.pos);
//yield `v ${reflection.target.x} ${reflection.target.y} ${reflection.target.z}`;
yield vertice(reflection.target);
yield `l ${vertex.current++} ${vertex.current++}`;
}
function * convertEyeReflectionToObj(reflection, vertex) {
//yield `v ${reflection.eye.pos.x} ${reflection.eye.pos.y} ${reflection.eye.pos.z}`;
yield vertice(reflection.eye.pos);
//yield `v ${reflection.mirror.pos.x} ${reflection.mirror.pos.y} ${reflection.mirror.pos.z}`;
yield vertice(reflection.mirror.pos);
yield `l ${vertex.current++} ${vertex.current++}`;
}
function * convertEyeReflectionEllipseToObj(reflection, vertex) {
if (!reflection.ellipse_points) {
throw new Error('The ellipse points are missing so they need to be implemented. Probably for the disc version');
}
for (var point of reflection.ellipse_points) {
yield vertice(point);
}
let verts = reflection.ellipse_points.map( _ => `${vertex.current++}`).join(' ');
yield `l ${verts}`;
}
function enumerate(from, to) {
return Array(Math.abs(Math.max(to) - Math.min(from)) + 1)
.fill()
.map( (_, i , a) => from + (to - from) * (i / (a.length - 1)))
}
function create(from, to, callback) {
let result = [];
for (var i = from; i < to; i++) {
result.push(callback(i));
}
return result;
}