aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhwc4872012-04-04 05:52:54 -0700
committerhwc4872012-04-04 05:52:54 -0700
commit0f31002ca696c1ef303d2926a504afd27305e94f (patch)
tree8f3684ff426fd94db338b802ecf1eed643efde9d
parent4b199cf04af83f59895d4d1e9a2d8443c1ec8e06 (diff)
downloadninja-0f31002ca696c1ef303d2926a504afd27305e94f.tar.gz
Added Flag material
-rw-r--r--assets/shaders/Flag.vert.glsl19
-rwxr-xr-xjs/lib/geom/rectangle.js46
-rw-r--r--js/lib/geom/shape-primitive.js130
-rw-r--r--js/lib/rdge/materials/flag-material.js6
-rwxr-xr-xjs/lib/rdge/materials/material.js5
-rw-r--r--js/lib/rdge/materials/twist-vert-material.js1
-rw-r--r--js/lib/rdge/texture.js527
-rwxr-xr-xjs/models/materials-model.js4
8 files changed, 437 insertions, 301 deletions
diff --git a/assets/shaders/Flag.vert.glsl b/assets/shaders/Flag.vert.glsl
index 9da0ee1c..a5e8b2f7 100644
--- a/assets/shaders/Flag.vert.glsl
+++ b/assets/shaders/Flag.vert.glsl
@@ -27,22 +27,21 @@ uniform mat4 u_worldMatrix;
27// varying variables 27// varying variables
28varying vec2 v_uv; 28varying vec2 v_uv;
29 29
30
30void main() 31void main()
31{ 32{
32 float pi = 3.14159; 33 float pi = 3.14159;
33 float angle = mod(u_time, pi)*2.0; 34 float angle = u_time;
34 35
35 vec3 v = a_pos;
36 v_uv = texcoord; 36 v_uv = texcoord;
37 37
38 vec2 pos = texcoord; 38 float x = 2.0*pi*texcoord.x/u_waveWidth;
39 float tmp = pos.x; pos.x = pos.y; pos.y = tmp; 39 float y = 2.0*pi*texcoord.y;
40 pos.x = pos.x * 1.0*pi * u_waveWidth;
41 pos.y = pos.y * 1.0*pi * u_waveWidth;
42 40
43 v.z = sin( pos.x + angle); 41 vec3 v = a_pos;
44 v.z += sin( pos.y/2.0 + angle); 42 v.z = sin( x + angle ) - 2.0*u_waveHeight;
45 v.z *= v.y * 0.09 * u_waveHeight; 43 v.z += sin( 0.2*y + angle);
44 v.z *= x * 0.09 * u_waveHeight;
46 45
47 gl_Position = u_projMatrix * u_mvMatrix * vec4(v,1.0) ; 46 gl_Position = u_projMatrix * u_mvMatrix * vec4(v,1.0) ;
48} 47}
diff --git a/js/lib/geom/rectangle.js b/js/lib/geom/rectangle.js
index e511d5f4..51947ff1 100755
--- a/js/lib/geom/rectangle.js
+++ b/js/lib/geom/rectangle.js
@@ -943,13 +943,13 @@ RectangleFill.create = function( rectCtr, width, height, tlRad, blRad, brRad,
943 } 943 }
944 944
945 //refine the mesh for vertex deformations 945 //refine the mesh for vertex deformations
946// if (material) { 946 if (material) {
947// if (material.hasVertexDeformation()) { 947 if (material.hasVertexDeformation()) {
948// var paramRange = material.getVertexDeformationRange(); 948 var paramRange = material.getVertexDeformationRange();
949// var tolerance = material.getVertexDeformationTolerance(); 949 var tolerance = material.getVertexDeformationTolerance();
950// nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance ); 950 nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance );
951// } 951 }
952// } 952 }
953 953
954 // create the RDGE primitive 954 // create the RDGE primitive
955 return ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); 955 return ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices);
@@ -1175,13 +1175,13 @@ RectangleStroke.create = function( rectCtr, width, height, strokeWidth, tlRad,
1175 } 1175 }
1176 1176
1177 //refine the mesh for vertex deformations 1177 //refine the mesh for vertex deformations
1178// if (material) { 1178 if (material) {
1179// if (material.hasVertexDeformation()) { 1179 if (material.hasVertexDeformation()) {
1180// var paramRange = material.getVertexDeformationRange(); 1180 var paramRange = material.getVertexDeformationRange();
1181// var tolerance = material.getVertexDeformationTolerance(); 1181 var tolerance = material.getVertexDeformationTolerance();
1182// nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance ); 1182 nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance );
1183// } 1183 }
1184// } 1184 }
1185 1185
1186 // create the RDGE primitive 1186 // create the RDGE primitive
1187 return ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); 1187 return ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices);
@@ -1259,15 +1259,15 @@ RectangleGeometry.create = function( ctr, width, height, material ) {
1259 RectangleGeometry.pushIndices( 0, 3, 2 ); 1259 RectangleGeometry.pushIndices( 0, 3, 2 );
1260 1260
1261 //refine the mesh for vertex deformations 1261 //refine the mesh for vertex deformations
1262// if (material) 1262 if (material)
1263// { 1263 {
1264// if (material.hasVertexDeformation()) 1264 if (material.hasVertexDeformation())
1265// { 1265 {
1266// var paramRange = material.getVertexDeformationRange(); 1266 var paramRange = material.getVertexDeformationRange();
1267// var tolerance = material.getVertexDeformationTolerance(); 1267 var tolerance = material.getVertexDeformationTolerance();
1268// nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance ); 1268 nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance );
1269// } 1269 }
1270// } 1270 }
1271 1271
1272 // create the RDGE primitive 1272 // create the RDGE primitive
1273 return ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); 1273 return ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices);
diff --git a/js/lib/geom/shape-primitive.js b/js/lib/geom/shape-primitive.js
index bf0087b2..44a7aa52 100644
--- a/js/lib/geom/shape-primitive.js
+++ b/js/lib/geom/shape-primitive.js
@@ -49,6 +49,136 @@ ShapePrimitive.create = function(coords, normals, uvs, indices, primType, ver
49 return prim; 49 return prim;
50}; 50};
51 51
52ShapePrimitive.getMeshBounds = function( verts, nVerts )
53{
54 if (!verts || (nVerts <= 0)) return null;
55
56 var bounds = [verts[0], verts[1], verts[2], verts[0], verts[1], verts[2]];
57 var index = 3;
58 for (var i=1; i<nVerts; i++)
59 {
60 var x = verts[index], y = verts[index+1], z = verts[index+2];
61 index += 3;
62
63 if (x < bounds[0]) bounds[0] = x;
64 else if (x > bounds[3]) bounds[3] = x;
65 if (y < bounds[1]) bounds[1] = y;
66 else if (y > bounds[4]) bounds[4] = y;
67 if (z < bounds[2]) bounds[2] = z;
68 else if (z > bounds[5]) bounds[5] = z;
69 }
70
71 return bounds;
72};
73
74ShapePrimitive.refineMesh = function( verts, norms, uvs, indices, nVertices, paramRange, tolerance )
75{
76 var oldVrtCount = nVertices;
77
78 // get the param range
79 var pUMin = paramRange[0], pVMin = paramRange[1],
80 pUMax = paramRange[2], pVMax = paramRange[3];
81 var iTriangle = 0;
82 var nTriangles = indices.length/3;
83 var index = 0;
84 while (iTriangle < nTriangles)
85 {
86 // get the indices of the 3 vertices
87 var i0 = indices[index],
88 i1 = indices[index+1],
89 i2 = indices[index+2];
90
91 // get the uv values
92 //var vrtIndex = 3*iTriangle;
93 var iuv0 = 2 * i0,
94 iuv1 = 2 * i1,
95 iuv2 = 2 * i2;
96 var u0 = uvs[iuv0], v0 = uvs[iuv0+1],
97 u1 = uvs[iuv1], v1 = uvs[iuv1+1],
98 u2 = uvs[iuv2], v2 = uvs[iuv2+1];
99
100 // find the u and v range
101 var uMin = u0, vMin = v0;
102 if (u1 < uMin) uMin = u1; if (v1 < vMin) vMin = v1;
103 if (u2 < uMin) uMin = u2; if (v2 < vMin) vMin = v2;
104 var uMax = u0, vMax = v0;
105 if (u1 > uMax) uMax = u1; if (v1 > vMax) vMax = v1;
106 if (u2 > uMax) uMax = u2; if (v2 > vMax) vMax = v2;
107
108 // if the parameter range of the triangle is outside the
109 // desired parameter range, advance to the next polygon and continue
110 if ((uMin > pUMax) || (uMax < pUMin) || (vMin > pVMax) || (vMax < pVMin))
111 {
112 // go to the next triangle
113 iTriangle++;
114 index += 3;
115 }
116 else
117 {
118 // check thesize of the triangle in uv space. If small enough, advance
119 // to the next triangle. If not small enough, split the triangle into 3;
120 var du = uMax - uMin, dv = vMax - vMin;
121 if ((du < tolerance) && (dv < tolerance))
122 {
123 iTriangle++;
124 index += 3;
125 }
126 else // split the triangle into 4 parts
127 {
128 //calculate the position of the new vertex
129 var iPt0 = 3 * i0,
130 iPt1 = 3 * i1,
131 iPt2 = 3 * i2;
132 var x0 = verts[iPt0], y0 = verts[iPt0+1], z0 = verts[iPt0+2],
133 x1 = verts[iPt1], y1 = verts[iPt1+1], z1 = verts[iPt1+2],
134 x2 = verts[iPt2], y2 = verts[iPt2+1], z2 = verts[iPt2+2];
135
136 // calculate the midpoints of the edges
137 var xA = (x0 + x1)/2.0, yA = (y0 + y1)/2.0, zA = (z0 + z1)/2.0,
138 xB = (x1 + x2)/2.0, yB = (y1 + y2)/2.0, zB = (z1 + z2)/2.0,
139 xC = (x2 + x0)/2.0, yC = (y2 + y0)/2.0, zC = (z2 + z0)/2.0;
140
141 // calculate the uv values of the new coordinates
142 var uA = (u0 + u1)/2.0, vA = (v0 + v1)/2.0,
143 uB = (u1 + u2)/2.0, vB = (v1 + v2)/2.0,
144 uC = (u2 + u0)/2.0, vC = (v2 + v0)/2.0;
145
146 // calculate the normals for the new points
147 var nx0 = norms[iPt0], ny0 = norms[iPt0+1], nz0 = norms[iPt0+2],
148 nx1 = norms[iPt1], ny1 = norms[iPt1+1], nz1 = norms[iPt1+2],
149 nx2 = norms[iPt2], ny2 = norms[iPt2+1], nz2 = norms[iPt2+2];
150 var nxA = (nx0 + nx1), nyA = (ny0 + ny1), nzA = (nz0 + nz1); var nrmA = VecUtils.vecNormalize(3, [nxA, nyA, nzA], 1.0 ),
151 nxB = (nx1 + nx2), nyB = (ny1 + ny2), nzB = (nz1 + nz2); var nrmB = VecUtils.vecNormalize(3, [nxB, nyB, nzB], 1.0 ),
152 nxC = (nx2 + nx0), nyC = (ny2 + ny0), nzC = (nz2 + nz0); var nrmC = VecUtils.vecNormalize(3, [nxC, nyC, nzC], 1.0 );
153