forked from sketchpunk/FunWithWebGL2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfungi.primatives.js
147 lines (127 loc) · 4.07 KB
/
fungi.primatives.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
Fungi.Primatives = {
Quad2U:function(){
if(Fungi.Res.Vao["FungiQuad2U"]) return Fungi.Res.Vao["FungiQuad2U"];
var aVert = [-1,1,0, -1,-1,0, 1,-1,0, 1,-1,0, 1,1,0, -1,1,0],
aUV = [ 0,0, 0,1, 1,1, 1,1, 1,0, 0,0];
return Fungi.Shaders.VAO.standardMesh("FungiQuad2U",3,aVert,null,aUV,null,false);
},
Quad1U:function(){
if(Fungi.Res.Vao["FungiQuad1U"]) return Fungi.Res.Vao["FungiQuad1U"];
var aVert = [ -0.5,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0, 0.5,0.5,0 ],
aUV = [ 0,0, 0,1, 1,1, 1,0 ],
aIndex = [ 0,1,2, 2,3,0 ];
return Fungi.Shaders.VAO.standardMesh("FungiQuad1U",3,aVert,null,aUV,aIndex,false);
},
FacedCube:function(keepRawData){
var width = 1, height = 1, depth = 1, x = 0, y = 0, z = 0;
var w = width*0.5, h = height*0.5, d = depth*0.5;
var x0 = x-w, x1 = x+w, y0 = y-h, y1 = y+h, z0 = z-d, z1 = z+d;
//Starting bottom left corner, then working counter clockwise to create the front face.
//Backface is the first face but in reverse (3,2,1,0)
//keep each quad face built the same way to make index and uv easier to assign
var aVert = [
x0, y1, z1, 0, //0 Front
x0, y0, z1, 0, //1
x1, y0, z1, 0, //2
x1, y1, z1, 0, //3
x1, y1, z0, 1, //4 Back
x1, y0, z0, 1, //5
x0, y0, z0, 1, //6
x0, y1, z0, 1, //7
x0, y1, z0, 2, //7 Left
x0, y0, z0, 2, //6
x0, y0, z1, 2, //1
x0, y1, z1, 2, //0
x0, y0, z1, 3, //1 Bottom
x0, y0, z0, 3, //6
x1, y0, z0, 3, //5
x1, y0, z1, 3, //2
x1, y1, z1, 4, //3 Right
x1, y0, z1, 4, //2
x1, y0, z0, 4, //5
x1, y1, z0, 4, //4
x0, y1, z0, 5, //7 Top
x0, y1, z1, 5, //0
x1, y1, z1, 5, //3
x1, y1, z0, 5 //4
];
//Build the index of each quad [0,1,2, 2,3,0]
var aIndex = [];
for(var i=0; i < aVert.length / 4; i+=2) aIndex.push(i, i+1, (Math.floor(i/4)*4)+((i+2)%4));
//Build UV data for each vertex
var aUV = [];
for(var i=0; i < 6; i++) aUV.push(0,0, 0,1, 1,1, 1,0);
//Build Normal data for each vertex
var aNorm = [
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, //Front
0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, //Back
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, //Left
0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, //Bottom
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, //Right
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 //Top
];
return Fungi.Shaders.VAO.standardMesh("FungiFCube",4,aVert,aNorm,aUV,aIndex,false);
},
AppendCube:function(ary,ind,width,height,depth,x,y,z,id){
var w = width*0.5, h = height*0.5, d = depth*0.5;
var x0 = x-w, x1 = x+w, y0 = y-h, y1 = y+h, z0 = z-d, z1 = z+d;
var ii = ary.length/4 ;
//Front
ary.push( x0, y1, z1, id,
x0, y0, z1, id,
x1, y0, z1, id,
x1, y1, z1, id);
//Back
ary.push( x1, y1, z0, id,
x1, y0, z0, id,
x0, y0, z0, id,
x0, y1, z0, id);
//Left
ary.push( x0, y1, z0, id,
x0, y0, z0, id,
x0, y0, z1, id,
x0, y1, z1, id);
//Bottom
ary.push( x0, y0, z1, id,
x0, y0, z0, id,
x1, y0, z0, id,
x1, y0, z1, id);
//Right
ary.push( x1, y1, z1, id,
x1, y0, z1, id,
x1, y0, z0, id,
x1, y1, z0, id);
//Top
ary.push( x0, y1, z0, id,
x0, y1, z1, id,
x1, y1, z1, id,
x1, y1, z0, id);
for(var i=0; i < 6*4; i+=2) ind.push(ii+i, ii+i+1, ii+(Math.floor(i/4)*4)+((i+2)%4) );
},
PolarSphere:function(verSteps,horSteps,radius,aryVerts,aryIndex){
var isGeoOnly = (aryVerts !== undefined && aryIndex !== undefined);
//Build a Half Circle
var origin = Math.PI * 0.5, //90Degrees
inc = Math.PI / (verSteps-1),
rad = 0,
x = 0,
y = 0,
pathAry = [];
for(var i=0; i < verSteps; i++){
rad = origin - (inc*i);
x = radius * Math.cos(rad);
y = radius * Math.sin(rad);
pathAry.push(x,y,0);
}
//Rotate the half circle in increments to form a sphere.
aryVerts = aryVerts || [];
aryIndex = aryIndex || [];
//var aryVerts = [], aryIndex = [];
FungiExt.Mesh.lathe(pathAry,horSteps,"y",aryVerts);
FungiExt.Mesh.triangleStrip(horSteps,pathAry.length/3,aryIndex,true);
if(!isGeoOnly){
var vao = Fungi.Shaders.VAO.standardMesh("FungiPolarSphere",3,aryVerts,null,null,aryIndex,false);
return vao;
}
}
};