-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathloadObj.js
More file actions
267 lines (232 loc) · 9.74 KB
/
Copy pathloadObj.js
File metadata and controls
267 lines (232 loc) · 9.74 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
262
263
264
265
266
267
//var objloader = function(){
var loadObj = function( filename){
var vertexGroup = [];
var normalGroup = [];
var texcoordGroup = [];
var indexGroup = [];
var isReady = false;
var callbackFunArray = [];
var loader = new THREE.OBJLoader();
var eventlistener = function(object){
content = object;
console.log("load file: "+filename);
console.log("children count: " + object.children.length );
//Start parse vertices, normals, indices, and texcoords
content.traverse( function(child){
if( child instanceof THREE.Mesh ){
var numVertices = child.geometry.vertices.length;
var numFaces = child.geometry.faces.length;
var numTexcoords = child.geometry.faceVertexUvs[0].length;
var vertices = [];
var normals = [];
var indices = [];
var texcoords = [];
console.log("start traverse OBJ");
if( numFaces != 0 ){
console.log("extracting faces");
//Array of texture coordinates of 1st layer texture
var UVs = child.geometry.faceVertexUvs[0];
//Extract faces info (UVs, normals, indices)
for( var i = 0; i < numFaces;i++ ){
var offset = child.geometry.faces[i].a;
vertices.push( child.geometry.vertices[offset].x);
vertices.push( child.geometry.vertices[offset].y);
vertices.push( child.geometry.vertices[offset].z);
offset = child.geometry.faces[i].b;
vertices.push( child.geometry.vertices[offset].x);
vertices.push( child.geometry.vertices[offset].y);
vertices.push( child.geometry.vertices[offset].z);
offset = child.geometry.faces[i].c;
vertices.push( child.geometry.vertices[offset].x);
vertices.push( child.geometry.vertices[offset].y);
vertices.push( child.geometry.vertices[offset].z);
indices.push( i*3 );
indices.push( i*3+1 );
indices.push( i*3+2 );
offset = 3*child.geometry.faces[i].a;
normals[ 9*i ] = child.geometry.faces[i].normal.x;
normals[ 9*i+1 ] = child.geometry.faces[i].normal.y;
normals[ 9*i+2 ] = child.geometry.faces[i].normal.z;
offset = 3*child.geometry.faces[i].b;
normals[ 9*i+3 ] = child.geometry.faces[i].normal.x;
normals[ 9*i+4 ] = child.geometry.faces[i].normal.y;
normals[ 9*i+5 ] = child.geometry.faces[i].normal.z;
offset = 3*child.geometry.faces[i].c;
normals[ 9*i+6 ] = child.geometry.faces[i].normal.x;
normals[ 9*i+7 ] = child.geometry.faces[i].normal.y;
normals[ 9*i+8 ] = child.geometry.faces[i].normal.z;
var uv = UVs[i];
offset = 2*child.geometry.faces[i].a;
texcoords[ 6*i ] = uv[0].x;
texcoords[ 6*i+1 ] = 1.0 - uv[0].y;
offset = 2*child.geometry.faces[i].b;
texcoords[ 6*i+2 ] = uv[1].x;
texcoords[ 6*i+3 ] = 1.0 - uv[1].y;
offset = 2*child.geometry.faces[i].c;
texcoords[ 6*i+4 ] = uv[2].x;
texcoords[ 6*i+5 ] = 1.0 - uv[2].y;
//console.log( 'vertices: '+vertices);
}
vertexGroup.push( vertices);
normalGroup.push( normals );
texcoordGroup.push( texcoords );
indexGroup.push( indices );
}
}
}); // end of traverse
isReady = true;
// console.log( "num of groups: "+vertexGroup.length);
// console.log("obj normal: " + normalGroup);
// console.log("obj vertex: " + vertexGroup);
// console.log("obj texcoord: " +texcoordGroup);
// console.log("obj indice: " + indexGroup);
}; //end of event listener
loader.load( filename, eventlistener );
return {
numGroups: function(){
return vertexGroup.length;
},
vertices: function(i){
return vertexGroup[i];
},
normals: function(i){
return normalGroup[i];
},
indices: function(i){
return indexGroup[i];
},
texcoords: function(i){
return texcoordGroup[i];
},
// loadFile: function(file){
// loadObj(file);
// }
isReady: function(){
return isReady;
},
addCallback: function( functor ){
callbackFunArray[callbackFunArray.length] = functor;
},
executeCallBackFunc: function(){
var i;
for( i = 0; i < callbackFunArray.length; ++i ){
callbackFunArray[i]();
//console.log(callbackFunArray[i]);
}
}
};
}; //end of loadObj
//};
// Model object that creates VBO, IBO and NBO upon creation
var createModel = function(gl,objRaw) {
var numGroups = objRaw.numGroups();
var VBO = [];
var IBO = [];
var NBO = [];
var TBO = [];
var numIndices = [];
var TextureVBO = [];
var TextureNBO = [];
var TextureIBO = [];
var TextureSizeVBO = [];
var TextureSizeNBO = [];
var TextureSizeIBO = [];
for (var i = 0; i < numGroups; i++) {
// Initialize buffer objects
VBO[i] = gl.createBuffer();
IBO[i] = gl.createBuffer();
NBO[i] = gl.createBuffer();
TBO[i] = gl.createBuffer();
TextureVBO[i] = gl.createTexture();
TextureNBO[i] = gl.createTexture();
TextureIBO[i] = gl.createTexture();
var texSize = Math.ceil(Math.sqrt( objRaw.vertices(i).length/3));
TextureSizeVBO[i] = texSize;
TextureSizeNBO[i] = texSize;
var vertices = [];
var normals = [];
for(var j=0; j<texSize*texSize*3; j++){ //fill in data
if(j<objRaw.vertices(i).length){
vertices.push(objRaw.vertices(i)[j]);
normals.push(objRaw.normals(i)[j]);
}
else{
vertices.push(1.0e6);
normals.push(1.0e6);
}
}
console.log("VBO texSize: " + texSize);
// console.log("now vertices length: " + vertices.length);
// console.log("original vertices length: " + objRaw.vertices(i).length);
var filter = OES_texture_float_linear? gl.LINEAR : gl.NEAREST;
// Add VBO, IBO, NBO and TBO
gl.bindBuffer(gl.ARRAY_BUFFER, VBO[i]);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(objRaw.vertices(i)), gl.STATIC_DRAW);
initCustomeTexture(TextureVBO[i], gl.RGB, filter, gl.FLOAT, texSize, texSize, new Float32Array(vertices));
gl.bindBuffer(gl.ARRAY_BUFFER, NBO[i]);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(objRaw.normals(i)), gl.STATIC_DRAW);
initCustomeTexture(TextureNBO[i], gl.RGB, filter, gl.FLOAT, texSize, texSize, new Float32Array(normals));
texSize = Math.ceil(Math.sqrt( objRaw.indices(i).length/3));
TextureSizeIBO[i] = texSize;
var indices = [];
for(var j=0; j<texSize*texSize*3; j++){ //fill in data
if(j<objRaw.vertices(i).length){
indices.push(objRaw.vertices(i)[j]);
}
else{
indices.push(1.0e6);
}
}
console.log("IBO texSize: " + texSize);
// console.log("now indices length: " + indices.length);
// console.log("original indices length: " + objRaw.indices(i).length);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, IBO[i]);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(objRaw.indices(i)), gl.STATIC_DRAW);
initCustomeTexture(TextureIBO[i], gl.RGB, filter, gl.FLOAT, texSize, texSize, new Float32Array(indices));
gl.bindBuffer(gl.ARRAY_BUFFER, TBO[i]);
// console.log("texture coord number: " + objRaw.texcoords(i).length);
gl.bufferData(gl.ARRAY_BUFFER, new Uint32Array(objRaw.texcoords(i)), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
numIndices.push(objRaw.indices(i).length);
//console.log("creating Model with indices: "+ objRaw.indices(i).length);
}
return {
VBO: function(i) {
return VBO[i];
},
IBO: function(i) {
return IBO[i];
},
NBO: function(i) {
return NBO[i];
},
TBO: function(i){
return TBO[i];
},
numGroups: function() {
return numGroups;
},
numIndices: function(i) {
return numIndices[i];
},
TextureIBO: function(i){
return TextureIBO[i];
},
TextureVBO: function(i){
return TextureVBO[i];
},
TextureNBO: function(i){
return TextureNBO[i];
},
TextureSizeIBO: function(i){
return TextureSizeIBO[i];
},
TextureSizeVBO: function(i){
return TextureSizeVBO[i];
},
TextureSizeNBO: function(i){
return TextureSizeNBO[i];
},
};
};