-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobj_loader.js
131 lines (93 loc) · 3.53 KB
/
obj_loader.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
var resourceContent;
//pathObj = path del file obj
//colorTriple = eventuale colore definito staticamente
//booleanText = true/false se oggetto con texture o meno
var objLoader = function (pathObj, booleanText) {
this.vertices = [];
this.normals = [];
this.vertices_array = [];
this.normals_array = [];
$.ajax({
url: pathObj,
async: false, // asynchronous request? (synchronous requests are discouraged...)
cache: false, // with this, you can force the browser to not make cache of the retrieved data
dataType: "text", // jQuery will infer this, but you can set explicitly
success: function( data, textStatus, jqXHR ) {
resourceContent = data; // can be a global variable too...
// process the content...
}
});
mesh = ReadOBJ(resourceContent,mesh);
this.nvert=mesh.nvert;
this.nedge=mesh.nedge;
this.nface=mesh.nface;
this.nnormal=mesh.nnormals;
this.ntexcoord=mesh.ntexcoord;
for(var i=0; i< this.nvert; i++) {
this.vertices[i] = new Array(3);
this.vertices[i][0]=mesh.vert[i+1].x;
this.vertices[i][1]=mesh.vert[i+1].y;
this.vertices[i][2]=mesh.vert[i+1].z;
}
for(var i=0; i<this.nnormal; i++) {
this.normals[i] = new Array(3);
this.normals[i][0]=mesh.normal[i].x;
this.normals[i][1]=mesh.normal[i].y;
this.normals[i][2]=mesh.normal[i].z;
}
for (var i=1; i<=this.nface; i++) this.vertices_array.push(fill_vertices(mesh.face[i].vert[0]-1,mesh.face[i].vert[1]-1,mesh.face[i].vert[2]-1, this.vertices).flat());
for (var i=1; i<=this.nface; i++) this.normals_array.push(fill_normals(mesh.face[i].normal[0]-1,mesh.face[i].normal[1]-1,mesh.face[i].normal[2]-1, this.normals).flat());
this.vertices_array=m4.flatten(this.vertices_array);
this.normals_array=m4.flatten(this.normals_array);
/* if(colorTriple!=[]){
this.colors = [];
this.colors_array = [];
for(var i=0; i<this.nvert; i++) {
this.colors[i] = new Array(3);
this.colors[i][0]=colorTriple[0];
this.colors[i][1]=colorTriple[1];
this.colors[i][2]=colorTriple[2];
}
for (var i=1; i<=this.nface; i++) this.colors_array.push(fill_colors(mesh.face[i].vert[0]-1,mesh.face[i].vert[1]-1,mesh.face[i].vert[2]-1, this.colors).flat());
this.colors_array=m4.flatten(this.colors_array);
} */
if(booleanText==true){
this.texcoord2D = [];
this.texcoord2D_array = [];
for(var i=0; i<this.ntexcoord; i++) {
this.texcoord2D[i] = new Array(2);
this.texcoord2D[i][0]=mesh.texcoord[i].x;
this.texcoord2D[i][1]=mesh.texcoord[i].y;
}
for (var i=1; i<=this.nface; i++) this.texcoord2D_array.push(fill_texture(mesh.face[i].texcoord[0]-1,mesh.face[i].texcoord[1]-1, mesh.face[i].texcoord[2]-1, this.texcoord2D).flat());
this.texcoord2D_array = m4.flatten(this.texcoord2D_array);
}
};
function fill_vertices(a, b, c, vertices) {
vert=[];
vert.push(vertices[a]);
vert.push(vertices[b]);
vert.push(vertices[c]);
return vert;
}
/* function fill_colors(a, b, c, color) {
col=[];
col.push(color[a]);
col.push(color[b]);
col.push(color[c]);
return col;
} */
function fill_normals(a, b, c, normals) {
norm=[];
norm.push(normalize(normals[a]));
norm.push(normalize(normals[b]));
norm.push(normalize(normals[c]));
return norm;
}
function fill_texture(a, b, c, texture) {
text=[];
text.push(texture[a]);
text.push(texture[b]);
text.push(texture[c]);
return text;
}