diff options
Diffstat (limited to 'js/lib/geom/shape-primitive.js')
-rw-r--r-- | js/lib/geom/shape-primitive.js | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/js/lib/geom/shape-primitive.js b/js/lib/geom/shape-primitive.js index 97873d32..380cf334 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 | ||
52 | ShapePrimitive.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 | |||
74 | ShapePrimitive.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 | |||
154 | // push everything | ||
155 | verts.push(xA); verts.push(yA); verts.push(zA); | ||
156 | verts.push(xB); verts.push(yB); verts.push(zB); | ||
157 | verts.push(xC); verts.push(yC); verts.push(zC); | ||
158 | uvs.push(uA), uvs.push(vA); | ||
159 | uvs.push(uB), uvs.push(vB); | ||
160 | uvs.push(uC), uvs.push(vC); | ||
161 | norms.push(nrmA[0]); norms.push(nrmA[1]); norms.push(nrmA[2]); | ||
162 | norms.push(nrmB[0]); norms.push(nrmB[1]); norms.push(nrmB[2]); | ||
163 | norms.push(nrmC[0]); norms.push(nrmC[1]); norms.push(nrmC[2]); | ||
164 | |||
165 | // split the current triangle into 4 | ||
166 | indices[index+1] = nVertices; indices[index+2] = nVertices+2; | ||
167 | indices.push(nVertices); indices.push(i1); indices.push(nVertices+1); nTriangles++; | ||
168 | indices.push(nVertices+1); indices.push(i2); indices.push(nVertices+2); nTriangles++; | ||
169 | indices.push(nVertices); indices.push(nVertices+1); indices.push(nVertices+2); nTriangles++; | ||
170 | nVertices += 3; | ||
171 | |||
172 | // by not advancing 'index', we examine the first of the 3 triangles generated above | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | |||
177 | console.log( "refine mesh vertex count " + oldVrtCount + " => " + nVertices ); | ||
178 | return nVertices; | ||
179 | }; | ||
180 | |||
181 | |||
52 | if (typeof exports === "object") { | 182 | if (typeof exports === "object") { |
53 | exports.ShapePrimitive = ShapePrimitive; | 183 | exports.ShapePrimitive = ShapePrimitive; |
54 | } \ No newline at end of file | 184 | } \ No newline at end of file |