Repository navigation
Expand file tree
/
Copy pathobjLoader.js
More file actions
122 lines (115 loc) · 3.53 KB
/
Copy pathobjLoader.js
File metadata and controls
122 lines (115 loc) · 3.53 KB
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
async function loadObject(url, loadMaterial = true) {
const objText = await (await fetch(url)).text();
let materialFile = null;
for (const line of objText.split("\n")) {
const trimmed = line.trim();
if (trimmed.startsWith("mtllib ")) {
materialFile = trimmed.substring(7).trim();
break;
}
}
let material = null;
if (loadMaterial && materialFile) {
const basePath =
url.substring(0, url.lastIndexOf("/") + 1);
material = await loadMtl(
basePath + materialFile
);
}
const mesh = parseObject(objText);
return {
...mesh,
material
}
}
async function loadMtl(url) {
const res = await fetch(url);
if (!res.ok) {
console.error("mtl failed", url);
return { mapKd: null };
}
const text = await res.text();
let mapKd = null;
for (const line of text.split("\n")) {
const parts = line.trim().split(/\s+/);
if (parts[0] === "map_Kd") {
mapKd = parts.slice(1).join(" ");
}
}
return { mapKd };
}
function parseObject(source) {
const positions = [];
const texcoords = [];
const normals = [];
const vertices = [];
const textures = [];
const outNormals = [];
const indices = [];
const vertexMap = new Map();
const lines = source.split('\n');
for (let line of lines) {
line = line.trim();
if (!line || line.startsWith('#')) {
continue;
}
const parts = line.split(/\s+/);
switch (parts[0]) {
case "v":
positions.push([
parseFloat(parts[1]),
parseFloat(parts[2]),
parseFloat(parts[3])
]);
break;
case "vt":
texcoords.push([
parseFloat(parts[1]),
parseFloat(parts[2])
]);
break;
case "vn":
normals.push([
parseFloat(parts[1]),
parseFloat(parts[2]),
parseFloat(parts[3])
]);
break;
case "f":
for (let i = 1; i <= 3; i++) {
const key = parts[i];
if (!vertexMap.has(key)) {
const [v, vt, vn] = key
.split('/')
.map(x => parseInt(x) - 1);
const pos = positions[v];
vertices.push( pos[0], pos[1], pos[2]);
if (vt >= 0) {
const uv = texcoords[vt];
textures.push( uv[0], uv[1]);
} else {
textures.push(0, 0);
}
if (vn >= 0) {
const n = normals[vn];
outNormals.push( n[0], n[1], n[2]);
} else {
outNormals.push(0, 0, 0);
}
vertexMap.set( key, vertexMap.size);
}
indices.push( vertexMap.get(key));
}
break;
}
}
console.log("positions", positions.length);
console.log("texcoords", texcoords.length);
console.log("normals", normals.length);
return {
vertices,
textures,
normals: outNormals,
indices
};
}