-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPopObj.js
More file actions
261 lines (219 loc) · 6.36 KB
/
PopObj.js
File metadata and controls
261 lines (219 loc) · 6.36 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
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
// new PopEngine style geometry Object{} output
// .Positions = {.Data=float32array .Size=int_length_of_each_element}
// .Normals
// .Uv0
// .Triangles = int array of indexes
export default function ParseObjGeometry(Contents,OnGeometry,OnDebug)
{
OnDebug = OnDebug || function(){};
//Pop.Debug("Contents.length",Contents.length);
const Lines = Contents.split('\n');
let Scale = 1.0;
const ParsePositionFloat = function(FloatStr)
{
let f = parseFloat( FloatStr );
f *= Scale;
return f;
}
// obj lists vertex attributes as it goes, interleaved with objects
// but attribs aren't neccessarily 1:1:1
// we don't know when a list is finished, so flush a vertex when a face references it
const Positions = []; // array of [x,y,z...]
const Normals = []; // array of [x,y,z...]
const TexCoords = []; // array of [u,v,w...]
let Triangles = []; // array of 3x [v,v,v] Vertexes indexes
let VertexPositions = []; // unrolled positions per vertex
let VertexNormals = []; // unrolled positions per vertex
let VertexTexCoords = []; // unrolled positions per vertex
let CurrentGeo = null;
function FlushCurrentGeo()
{
if ( !CurrentGeo )
return;
const Geometry = Object.assign( {}, CurrentGeo );
Geometry.Positions = VertexPositions.length ? VertexPositions : undefined;
Geometry.Normals = VertexNormals.length ? VertexNormals : undefined;
Geometry.TexCoords = VertexTexCoords.length ? VertexTexCoords : undefined;
Geometry.TriangleIndexes = Triangles;
OnGeometry( Geometry );
// setup for new geo
Triangles = []; // array of 3x [v,v,v] Vertexes indexes
VertexPositions = []; // unrolled positions per vertex
VertexNormals = []; // unrolled positions per vertex
VertexTexCoords = []; // unrolled positions per vertex
CurrentGeo = null;
}
function OnComment()
{
}
// returns new vertex index
function OnVertex(PositionIndex,TexCoordIndex,NormalIndex)
{
// indexes start from 1
if ( PositionIndex ) PositionIndex--;
if ( NormalIndex ) NormalIndex--;
if ( TexCoordIndex ) TexCoordIndex--;
// gr: spreading here is gonna cause problems later when we have something with non-3 components
// should unroll later
if ( PositionIndex !== undefined )
VertexPositions.push( ...Positions[PositionIndex] );
if ( NormalIndex !== undefined )
VertexNormals.push( ...Normals[NormalIndex] );
if ( TexCoordIndex !== undefined )
{
// gr: this seems to only occur for texcoords, (find out why!)
// but I have models where we have a valid TexCoordIndex, but not enough texcoords
if ( TexCoordIndex < TexCoords.length )
{
VertexTexCoords.push( ...TexCoords[TexCoordIndex] );
}
else
{
OnDebug(`Warning OBJ Texcoord Index (${TexCoordIndex}) out of bounds(${TexCoords.length})`);
VertexTexCoords.push( undefined,undefined,undefined );
}
}
// check all the arrays align
const Lengths =
[
VertexPositions.length,
VertexNormals.length,
VertexTexCoords.length,
].filter( l => l>0 );
// check all same
if ( !Lengths.every( l => l==Lengths[0] ) )
throw "Vertex attribute arrays misaligned";
// return new index
return Lengths[0]-1;
}
function OnFace(Face)
{
// Face is [ p/n/t, p/n/t, p/n/t ... ]
// split the face into triangles
if ( Face.length < 3 )
throw "OBJ face has less than 3 points";
// quad
if ( Face.length == 4 )
{
// we don't have to recurse, but for now...
// https://stackoverflow.com/a/23724231/355753
// order for quad restarts at 0 so it's always triangle fan?
const t0 = [0,1,2];
const t1 = [0,2,3];
OnFace( t0.map( i => Face[i] ) );
OnFace( t1.map( i => Face[i] ) );
return;
}
// tri strip?
if ( Face.length != 3 )
{
OnDebug(`Skipping OBJ face with ${Face.length} points`);
//throw `OBJ face with ${Face.length} != 3 points`;
return;
}
// flush each vertex
const FaceVertexIndexes = [];
function ParseFaceIndexes(FaceIndexes,FaceIndexIndex)
{
const Indexes = FaceIndexes.split('/');
const VertexIndex = OnVertex( ...Indexes );
FaceVertexIndexes.push( VertexIndex );
}
Face.forEach(ParseFaceIndexes);
Triangles.push( ...FaceVertexIndexes );
}
function OnPosition(Values)
{
const xyz = Values.map( ParsePositionFloat );
xyz[2] = -xyz[2];
Positions.push( xyz );
}
function OnNormal(Values)
{
Values = Values.map(parseFloat);
// we invert z position, so we need to do the same on normal...
// gr: is this correct? simply a localspace inversion?
Values[2] = -Values[2];
Normals.push( Values );
}
function OnTexCoord(Values)
{
// pad to 3 values (for now) for the OnVertex alignment check
Values = Values.map(parseFloat);
Values.push(0,0,0);
Values = Values.slice(0,3);
TexCoords.push( Values );
}
function OnScale()
{
Scale = parseFloat( Line );
//Pop.Debug("Found scale in obj: ",Scale);
}
function OnMaterialAsset()
{
}
function OnMaterial()
{
}
function OnObject(Names)
{
const Name = Names.join(' ');
// order CAN go
// verts
// geo
// faces
// so we flush if there's already one
FlushCurrentGeo();
// todo: flush current face list
CurrentGeo = {};
CurrentGeo.Name = Name;
}
function OnGroup(Names)
{
OnObject( Names );
}
function OnSmoothShading()
{
}
const ParseFuncTable = {};
ParseFuncTable['# Scale '] = OnScale;
ParseFuncTable['#'] = OnComment;
ParseFuncTable['v '] = OnPosition;
ParseFuncTable['vn '] = OnNormal;
ParseFuncTable['vt '] = OnTexCoord;
ParseFuncTable['mtllib '] = OnMaterialAsset;
ParseFuncTable['usemtl '] = OnMaterial;
ParseFuncTable['g '] = OnGroup;
ParseFuncTable['o '] = OnObject;
ParseFuncTable['f '] = OnFace;
ParseFuncTable['s '] = OnSmoothShading;
const ParseLine = function(Line)
{
Line = Line.trim();
if ( !Line.length )
return;
// gr: this depends on the table's keys being on populated-order as
// we want scale to come before comments
let ParseFunc = null;
for ( let Command in ParseFuncTable )
{
if ( !Line.startsWith(Command) )
continue;
// get func and remove prefix
ParseFunc = ParseFuncTable[Command];
Line = Line.replace( Command,'');
break;
}
if ( !ParseFunc )
{
OnDebug("OBJ skipping unknown prefixed line", Line );
return;
}
// split spaces (&trim)
const LineParts = Line.split(' ').filter( x => x.length != 0 );
ParseFunc( LineParts );
}
Lines.forEach( ParseLine );
// finish off any geo
FlushCurrentGeo();
}