diff options
Diffstat (limited to 'js/helper-classes/RDGE/GLRectangle.js')
-rw-r--r-- | js/helper-classes/RDGE/GLRectangle.js | 165 |
1 files changed, 154 insertions, 11 deletions
diff --git a/js/helper-classes/RDGE/GLRectangle.js b/js/helper-classes/RDGE/GLRectangle.js index 8535a683..f6f5a899 100644 --- a/js/helper-classes/RDGE/GLRectangle.js +++ b/js/helper-classes/RDGE/GLRectangle.js | |||
@@ -72,13 +72,14 @@ function GLRectangle() | |||
72 | this._materialSpecular = [0.4, 0.4, 0.4, 1.0]; | 72 | this._materialSpecular = [0.4, 0.4, 0.4, 1.0]; |
73 | 73 | ||
74 | if(strokeMaterial) | 74 | if(strokeMaterial) |
75 | { | ||
76 | this._strokeMaterial = strokeMaterial; | 75 | this._strokeMaterial = strokeMaterial; |
77 | } | 76 | else |
77 | this._strokeMaterial = new FlatMaterial(); | ||
78 | |||
78 | if(fillMaterial) | 79 | if(fillMaterial) |
79 | { | ||
80 | this._fillMaterial = fillMaterial; | 80 | this._fillMaterial = fillMaterial; |
81 | } | 81 | else |
82 | this._fillMaterial = new FlatMaterial(); | ||
82 | } | 83 | } |
83 | 84 | ||
84 | /////////////////////////////////////////////////////////////////////// | 85 | /////////////////////////////////////////////////////////////////////// |
@@ -430,7 +431,7 @@ function GLRectangle() | |||
430 | this.createStroke = function(ctr, width, height, strokeWidth, tlRad, blRad, brRad, trRad, material) | 431 | this.createStroke = function(ctr, width, height, strokeWidth, tlRad, blRad, brRad, trRad, material) |
431 | { | 432 | { |
432 | // create the geometry | 433 | // create the geometry |
433 | var prim = RectangleStroke.create( ctr, width, height, strokeWidth, tlRad, blRad, brRad, trRad) | 434 | var prim = RectangleStroke.create( ctr, width, height, strokeWidth, tlRad, blRad, brRad, trRad, material) |
434 | return prim; | 435 | return prim; |
435 | } | 436 | } |
436 | 437 | ||
@@ -440,9 +441,9 @@ function GLRectangle() | |||
440 | // special the (common) case of no rounded corners | 441 | // special the (common) case of no rounded corners |
441 | var prim | 442 | var prim |
442 | if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0)) | 443 | if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0)) |
443 | prim = RectangleGeometry.create( ctr, width, height ); | 444 | prim = RectangleGeometry.create( ctr, width, height, material ); |
444 | else | 445 | else |
445 | prim = RectangleFill.create( ctr, width, height, tlRad, blRad, brRad, trRad); | 446 | prim = RectangleFill.create( ctr, width, height, tlRad, blRad, brRad, trRad, material); |
446 | 447 | ||
447 | return prim; | 448 | return prim; |
448 | } | 449 | } |
@@ -691,7 +692,7 @@ function GLRectangle() | |||
691 | } | 692 | } |
692 | 693 | ||
693 | RectangleFill = {}; | 694 | RectangleFill = {}; |
694 | RectangleFill.create = function( rectCtr, width, height, tlRad, blRad, brRad, trRad) | 695 | RectangleFill.create = function( rectCtr, width, height, tlRad, blRad, brRad, trRad, material) |
695 | { | 696 | { |
696 | var x = rectCtr[0], y = rectCtr[1], z = 0.0; | 697 | var x = rectCtr[0], y = rectCtr[1], z = 0.0; |
697 | var hw = 0.5*width, hh = 0.5*height; | 698 | var hw = 0.5*width, hh = 0.5*height; |
@@ -778,6 +779,17 @@ RectangleFill.create = function( rectCtr, width, height, tlRad, blRad, brRad, | |||
778 | j++; | 779 | j++; |
779 | } | 780 | } |
780 | 781 | ||
782 | //refine the mesh for vertex deformations | ||
783 | if (material) | ||
784 | { | ||
785 | if (material.hasVertexDeformation()) | ||
786 | { | ||
787 | var paramRange = material.getVertexDeformationRange(); | ||
788 | var tolerance = material.getVertexDeformationTolerance(); | ||
789 | nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance ); | ||
790 | } | ||
791 | } | ||
792 | |||
781 | // create the RDGE primitive | 793 | // create the RDGE primitive |
782 | var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); | 794 | var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); |
783 | return prim; | 795 | return prim; |
@@ -850,7 +862,7 @@ RectangleFill.getRoundedCorner = function(ctr, startPt, vertices) | |||
850 | 862 | ||
851 | 863 | ||
852 | RectangleStroke = {}; | 864 | RectangleStroke = {}; |
853 | RectangleStroke.create = function( rectCtr, width, height, strokeWidth, tlRad, blRad, brRad, trRad) | 865 | RectangleStroke.create = function( rectCtr, width, height, strokeWidth, tlRad, blRad, brRad, trRad, material) |
854 | { | 866 | { |
855 | var x = rectCtr[0], y = rectCtr[1], z = 0.0; | 867 | var x = rectCtr[0], y = rectCtr[1], z = 0.0; |
856 | var hw = 0.5*width, hh = 0.5*height, sw = strokeWidth; | 868 | var hw = 0.5*width, hh = 0.5*height, sw = strokeWidth; |
@@ -1041,8 +1053,20 @@ RectangleStroke.create = function( rectCtr, width, height, strokeWidth, tlRad, | |||
1041 | k++; | 1053 | k++; |
1042 | } | 1054 | } |
1043 | 1055 | ||
1056 | //refine the mesh for vertex deformations | ||
1057 | if (material) | ||
1058 | { | ||
1059 | if (material.hasVertexDeformation()) | ||
1060 | { | ||
1061 | var paramRange = material.getVertexDeformationRange(); | ||
1062 | var tolerance = material.getVertexDeformationTolerance(); | ||
1063 | nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance ); | ||
1064 | } | ||
1065 | } | ||
1066 | |||
1044 | // create the RDGE primitive | 1067 | // create the RDGE primitive |
1045 | var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); | 1068 | var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); |
1069 | //var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.LINES, nVertices); | ||
1046 | return prim; | 1070 | return prim; |
1047 | } | 1071 | } |
1048 | 1072 | ||
@@ -1085,7 +1109,7 @@ RectangleStroke.getUV = RectangleFill.getUV; | |||
1085 | 1109 | ||
1086 | // Helper function for generating Three.js geometry | 1110 | // Helper function for generating Three.js geometry |
1087 | RectangleGeometry = {}; | 1111 | RectangleGeometry = {}; |
1088 | RectangleGeometry.create = function( ctr, width, height ) | 1112 | RectangleGeometry.create = function( ctr, width, height, material ) |
1089 | { | 1113 | { |
1090 | var x = ctr[0], y = ctr[1], z = 0.0; | 1114 | var x = ctr[0], y = ctr[1], z = 0.0; |
1091 | var hw = 0.5*width, hh = 0.5*height; | 1115 | var hw = 0.5*width, hh = 0.5*height; |
@@ -1123,8 +1147,20 @@ RectangleGeometry.create = function( ctr, width, height ) | |||
1123 | RectangleGeometry.pushIndices( 2, 1, 0 ); | 1147 | RectangleGeometry.pushIndices( 2, 1, 0 ); |
1124 | RectangleGeometry.pushIndices( 0, 3, 2 ); | 1148 | RectangleGeometry.pushIndices( 0, 3, 2 ); |
1125 | 1149 | ||
1150 | //refine the mesh for vertex deformations | ||
1151 | if (material) | ||
1152 | { | ||
1153 | if (material.hasVertexDeformation()) | ||
1154 | { | ||
1155 | var paramRange = material.getVertexDeformationRange(); | ||
1156 | var tolerance = material.getVertexDeformationTolerance(); | ||
1157 | nVertices = ShapePrimitive.refineMesh( this.vertices, this.normals, this.uvs, this.indices, nVertices, paramRange, tolerance ); | ||
1158 | } | ||
1159 | } | ||
1160 | |||
1126 | // create the RDGE primitive | 1161 | // create the RDGE primitive |
1127 | var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); | 1162 | var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.TRIANGLES, nVertices); |
1163 | //var prim = ShapePrimitive.create(this.vertices, this.normals, this.uvs, this.indices, g_Engine.getContext().renderer.LINES, nVertices); | ||
1128 | return prim; | 1164 | return prim; |
1129 | } | 1165 | } |
1130 | 1166 | ||
@@ -1193,4 +1229,111 @@ ShapePrimitive.create = function(coords, normals, uvs, indices, primType, ver | |||
1193 | renderer.createPrimitive(prim, vertexCount); | 1229 | renderer.createPrimitive(prim, vertexCount); |
1194 | 1230 | ||
1195 | return prim; | 1231 | return prim; |
1196 | } \ No newline at end of file | 1232 | } |
1233 | |||
1234 | |||
1235 | ShapePrimitive.refineMesh = function( verts, norms, uvs, indices, nVertices, paramRange, tolerance ) | ||
1236 | { | ||
1237 | // get the param range | ||
1238 | var pUMin = paramRange[0], pVMin = paramRange[1], | ||
1239 | pUMax = paramRange[2], pVMax = paramRange[3]; | ||
1240 | var iTriangle = 0; | ||
1241 | var nTriangles = indices.length/3; | ||
1242 | var index = 0; | ||
1243 | while (iTriangle < nTriangles) | ||
1244 | { | ||
1245 | // get the indices of the 3 vertices | ||
1246 | var i0 = indices[index], | ||
1247 | i1 = indices[index+1], | ||
1248 | i2 = indices[index+2]; | ||
1249 | |||
1250 | // get the uv values | ||
1251 | //var vrtIndex = 3*iTriangle; | ||
1252 | var iuv0 = 2 * i0, | ||
1253 | iuv1 = 2 * i1, | ||
1254 | iuv2 = 2 * i2; | ||
1255 | var u0 = uvs[iuv0], v0 = uvs[iuv0+1], | ||
1256 | u1 = uvs[iuv1], v1 = uvs[iuv1+1], | ||
1257 | u2 = uvs[iuv2], v2 = uvs[iuv2+1]; | ||
1258 | |||
1259 | // find the u and v range | ||
1260 | var uMin = u0, vMin = v0; | ||
1261 | if (u1 < uMin) uMin = u1; if (v1 < vMin) vMin = v1; | ||
1262 | if (u2 < uMin) uMin = u2; if (v2 < vMin) vMin = v2; | ||
1263 | var uMax = u0, vMax = v0; | ||
1264 | if (u1 > uMax) uMax = u1; if (v1 > vMax) vMax = v1; | ||
1265 | if (u2 > uMax) uMax = u2; if (v2 > vMax) vMax = v2; | ||
1266 | |||
1267 | // if the parameter range of the triangle is outside the | ||
1268 | // desired parameter range, advance to the next polygon and continue | ||
1269 | if ((uMin > pUMax) || (uMax < pUMin) || (vMin > pVMax) || (vMax < pVMin)) | ||
1270 | { | ||
1271 | // go to the next triangle | ||
1272 | iTriangle++; | ||
1273 | index += 3; | ||
1274 | } | ||
1275 | else | ||
1276 | { | ||
1277 | // check thesize of the triangle in uv space. If small enough, advance | ||
1278 | // to the next triangle. If not small enough, split the triangle into 3; | ||
1279 | var du = uMax - uMin, dv = vMax - vMin; | ||
1280 | if ((du < tolerance) && (dv < tolerance)) | ||
1281 | { | ||
1282 | iTriangle++; | ||
1283 | index += 3; | ||
1284 | } | ||
1285 | else // split the triangle into 4 parts | ||
1286 | { | ||
1287 | //calculate the position of the new vertex | ||
1288 | var iPt0 = 3 * i0, | ||
1289 | iPt1 = 3 * i1, | ||
1290 | iPt2 = 3 * i2; | ||
1291 | var x0 = verts[iPt0], y0 = verts[iPt0+1], z0 = verts[iPt0+2], | ||