-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadMesh.js
188 lines (159 loc) · 5.07 KB
/
loadMesh.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
/* globals loadMtl, self */
(function (global) {
global.loadMesh = loadMesh
function loadMesh (path, fileName) {
return fetch([path, fileName].join('/'))
.then(function (response) { return response.text() })
.then(parseObj).then(function (objData) {
return loadMaterials(path, objData)
}).then(convertToMesh)
}
function parseObj (data) {
var objData = {
vertices: [],
normals: [],
objects: [],
mtllibs: [],
materials: {}
}
var currentObject = null
function addObject (name, mtllib) {
console.info('New object %s', name)
var object = {
name: name,
material: undefined,
faces: []
}
objData.objects.push(object)
currentObject = object
}
if ('groupCollapsed' in console) console.groupCollapsed('Parsing obj')
data.split(/\r?\n/).forEach(handleLine)
if ('groupCollapsed' in console) console.groupEnd()
function handleLine (line) {
var words = line.split('#')[0].trim().split(/\s+/)
switch (words[0]) {
case '':
case undefined:
return
case 'v':
addVertex(words)
break
case 'vn':
addNormal(words)
break
case 'vt':
addTexCoord(words)
break
case 'f':
addFace(words)
break
case 'o':
addObject(line.replace(/^o\s+/, '')) // ugh, I don't know if we'll get spaces in here.
break
case 'usemtl':
setMaterial(words)
break
case 'g':
console.info('Found group %s', words.slice(1).join(' '))
break
case 'mtllib':
addMtllib(words)
break
default:
console.warn('Unrecognized obj directive %s', words[0])
}
}
// v x y z [w]
function addVertex (words) {
if (words.length > 4) console.warn('Discarding w coordinate.')
objData.vertices.push([Number(words[1]), Number(words[2]), Number(words[3])])
}
// vn x y z
function addNormal (words) {
objData.normals.push([Number(words[1]), Number(words[2]), Number(words[3])])
}
// vt u v [w]
function addTexCoord (words) {
console.warn('Texture coordinates not implemented')
}
// mtllib file_name
function addMtllib (words) {
if (words.length !== 2) console.error('Unrecognized mtllib directive.')
var name = words[1]
console.info('Found mtllib %s', name)
objData.mtllibs.push(name)
}
function setMaterial (words) {
if (!currentObject) {
console.warn('Setting material before object declared. Creating anonymous object.')
addObject(undefined)
}
console.info('Object %s uses material %s', currentObject.name, words[1])
currentObject.material = words[1]
}
function addFace (words) {
if (!currentObject) {
console.warn('Adding faces before object declared. Creating anonymous object.')
addObject(undefined)
}
words.shift()
var face = {v: [], t: [], n: []}
words.forEach(function (word) {
var parts = word.split('/')
face.v.push(parts[0] - 1)
face.t.push(parts[1] - 1)
face.n.push(parts[2] - 1)
})
currentObject.faces.push(face)
}
console.log('Obj loaded with %d vertices and %d objects', objData.vertices.length, objData.objects.length)
return objData
}
function loadMaterials (path, objData) {
return Promise.all(objData.mtllibs.map(function (mtllib) {
return loadMtl(path, mtllib)
})).then(function (libs) {
libs.forEach(function (lib) {
lib.forEach(function (material) {
if (material.name in objData) {
console.error('Duplicate material definition for %s', material.name)
}
objData.materials[material.name] = material
})
})
return objData
})
}
function convertToMesh (objData) {
function constructObject (object) {
var verts = []
var norms = []
object.faces.forEach(function (face) {
// convert a triangle fan into a list of triangles
for (var i = 2; i < face.v.length; i++) {
addVert(face.v[0])
addNorm(face.n[0])
addVert(face.v[i - 1])
addNorm(face.n[i - 1])
addVert(face.v[i])
addNorm(face.n[i])
}
})
function addVert (vertIndex) {
verts.push(objData.vertices[vertIndex][0], objData.vertices[vertIndex][1], objData.vertices[vertIndex][2])
}
function addNorm (normIndex) {
norms.push(objData.normals[normIndex][0], objData.normals[normIndex][1], objData.normals[normIndex][2])
}
console.log('Object %s created with %d faces and %d vertices', object.name, object.faces.length, verts.length)
return {
name: object.name,
vertices: new Float32Array(verts),
normals: new Float32Array(norms),
material: objData.materials[object.material]
}
}
return objData.objects.map(constructObject)
}
})(typeof window === 'undefined' ? self : window)