-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_obj_model.js
124 lines (109 loc) · 3.42 KB
/
load_obj_model.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
import {StaticModel} from "./static_model.js";
import {getArea, getNormal} from "./kits.js";
import {LightModel} from "./light_model.js";
/**
*
* @param {string} code
* @return {{vArray: number[][], vtArray: number[][], vnArray: number[][], fArray: number[][][]}}
*/
function loadObj(code) {
let vArray = [];
let vtArray = [];
let vnArray = [];
let fArray = [];
let lines = code.split(new RegExp("\\r\\n|\\r|\\n"));
for (let line of lines) {
let tokens = line.split(" ").filter(token => token !== "");
switch (tokens[0]) {
case "v":
vArray.push([Number.parseFloat(tokens[1]), Number.parseFloat(tokens[2]), Number.parseFloat(tokens[3])]);
break;
case "vt":
vtArray.push([Number.parseFloat(tokens[1]), Number.parseFloat(tokens[2])]);
break;
case "vn":
vnArray.push([Number.parseFloat(tokens[1]), Number.parseFloat(tokens[2]), Number.parseFloat(tokens[3])]);
break;
case "f": {
let face = [];
for (let v = 0; v < 3; v++) {
let p = tokens[v + 1].split("/");
face[v] = p.map(i => Number.parseInt(i));
}
fArray.push(face);
break;
}
case "o":
case "s":
case "#":
case "mtllib":
case "usemtl":
case null:
break;
default:
console.warn("");
}
}
return {
vArray,
vtArray,
vnArray,
fArray,
}
}
/**
*
* @param {number[][]} vArray
* @param {number[][][]} fArray
*/
function getObjPositionBuffer(vArray, fArray) {
let buffer = new Float32Array(fArray.length * 9);
for (let f = 0; f < fArray.length; f++) {
for (let v = 0; v < 3; v++) {
let position = vArray[fArray[f][v][0] - 1];
for (let i = 0; i < 3; i++) {
buffer[f * 9 + v * 3 + i] = position[i];
}
}
}
return buffer;
}
/**
*
* @param {number[][]} vnArray
* @param {number[][][]} fArray
*/
function getObjNormalBuffer(vnArray, fArray) {
let buffer = new Float32Array(fArray.length * 9);
for (let f = 0; f < fArray.length; f++) {
for (let v = 0; v < 3; v++) {
let position = vnArray[fArray[f][3][1] - 1];
for (let i = 0; i < 3; i++) {
buffer[f * 9 + v * 3 + i] = position[i];
}
}
}
return buffer;
}
export function loadObjStaticModel(renderer, code) {
let obj = loadObj(code);
let positions = getObjPositionBuffer(obj.vArray, obj.fArray);
let normals = getObjNormalBuffer(obj.vnArray, obj.fArray);
let model = new StaticModel(renderer);
model.setData(positions, normals);
return model;
}
export function loadObjStaticModelWithoutNormal(renderer, code) {
let obj = loadObj(code);
let positions = getObjPositionBuffer(obj.vArray, obj.fArray);
let model = new StaticModel(renderer);
model.setData(positions, getNormal(positions));
return model;
}
export function loadObjLightModelWithoutNormal(renderer, code) {
let obj = loadObj(code);
let positions = getObjPositionBuffer(obj.vArray, obj.fArray);
let model = new LightModel(renderer);
model.setData(positions, getNormal(positions), getArea(positions));
return model;
}