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 9864a8e9..5a44deb3 100644 --- a/js/lib/geom/shape-primitive.js +++ b/js/lib/geom/shape-primitive.js | |||
@@ -49,6 +49,28 @@ 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 | |||
52 | ShapePrimitive.getBounds = function( prim ) | 74 | ShapePrimitive.getBounds = function( prim ) |
53 | { | 75 | { |
54 | var verts = prim.bufferStreams[0]; | 76 | var verts = prim.bufferStreams[0]; |
@@ -76,6 +98,114 @@ ShapePrimitive.getBounds = function( prim ) | |||
76 | return [xMin, yMin, zMin, xMax, yMax, zMax]; | 98 | return [xMin, yMin, zMin, xMax, yMax, zMax]; |
77 | }; | 99 | }; |
78 | 100 | ||
101 | ShapePrimitive.refineMesh = function( verts, norms, uvs, indices, nVertices, paramRange, tolerance ) | ||
102 | { | ||
103 | var oldVrtCount = nVertices; | ||
104 | |||
105 | // get the param range | ||
106 | var pUMin = paramRange[0], pVMin = paramRange[1], | ||
107 | pUMax = paramRange[2], pVMax = paramRange[3]; | ||
108 | var iTriangle = 0; | ||
109 | var nTriangles = indices.length/3; | ||
110 | var index = 0; | ||
111 | while (iTriangle < nTriangles) | ||
112 | { | ||
113 | // get the indices of the 3 vertices | ||
114 | var i0 = indices[index], | ||
115 | i1 = indices[index+1], | ||
116 | i2 = indices[index+2]; | ||
117 | |||
118 | // get the uv values | ||
119 | //var vrtIndex = 3*iTriangle; | ||
120 | var iuv0 = 2 * i0, | ||
121 | iuv1 = 2 * i1, | ||
122 | iuv2 = 2 * i2; | ||
123 | var u0 = uvs[iuv0], v0 = uvs[iuv0+1], | ||
124 | u1 = uvs[iuv1], v1 = uvs[iuv1+1], | ||
125 | u2 = uvs[iuv2], v2 = uvs[iuv2+1]; | ||
126 | |||
127 | // find the u and v range | ||
128 | var uMin = u0, vMin = v0; | ||
129 | if (u1 < uMin) uMin = u1; if (v1 < vMin) vMin = v1; | ||
130 | if (u2 < uMin) uMin = u2; if (v2 < vMin) vMin = v2; | ||
131 | var uMax = u0, vMax = v0; | ||
132 | if (u1 > uMax) uMax = u1; if (v1 > vMax) vMax = v1; | ||
133 | if (u2 > uMax) uMax = u2; if (v2 > vMax) vMax = v2; | ||
134 | |||
135 | // if the parameter range of the triangle is outside the | ||
136 | // desired parameter range, advance to the next polygon and continue | ||
137 | if ((uMin > pUMax) || (uMax < pUMin) || (vMin > pVMax) || (vMax < pVMin)) | ||
138 | { | ||
139 | // go to the next triangle | ||
140 | iTriangle++; | ||
141 | index += 3; | ||
142 | } | ||
143 | else | ||
144 | { | ||
145 | // check thesize of the triangle in uv space. If small enough, advance | ||
146 | // to the next triangle. If not small enough, split the triangle into 3; | ||
147 | var du = uMax - uMin, dv = vMax - vMin; | ||
148 | if ((du < tolerance) && (dv < tolerance)) | ||
149 | { | ||
150 | iTriangle++; | ||
151 | index += 3; | ||
152 | } | ||
153 | else // split the triangle into 4 parts | ||
154 | { | ||
155 | //calculate the position of the new vertex | ||
156 | var iPt0 = 3 * i0, | ||
157 | iPt1 = 3 * i1, | ||
158 | iPt2 = 3 * i2; | ||
159 | var x0 = verts[iPt0], y0 = verts[iPt0+1], z0 = verts[iPt0+2], | ||
160 | x1 = verts[iPt1], y1 = verts[iPt1+1], z1 = verts[iPt1+2], | ||
161 | x2 = verts[iPt2], y2 = verts[iPt2+1], z2 = verts[iPt2+2]; | ||
162 | |||
163 | // calculate the midpoints of the edges | ||
164 | var xA = (x0 + x1)/2.0, yA = (y0 + y1)/2.0, zA = (z0 + z1)/2.0, | ||
165 | xB = (x1 + x2)/2.0, yB = (y1 + y2)/2.0, zB = (z1 + z2)/2.0, | ||
166 | xC = (x2 + x0)/2.0, yC = (y2 + y0)/2.0, zC = (z2 + z0)/2.0; | ||
167 | |||
168 | // calculate the uv values of the new coordinates | ||
169 | var uA = (u0 + u1)/2.0, vA = (v0 + v1)/2.0, | ||
170 | uB = (u1 + u2)/2.0, vB = (v1 + v2)/2.0, | ||
171 | uC = (u2 + u0)/2.0, vC = (v2 + v0)/2.0; | ||
172 | |||
173 | // calculate the normals for the new points | ||
174 | var nx0 = norms[iPt0], ny0 = norms[iPt0+1], nz0 = norms[iPt0+2], | ||
175 | nx1 = norms[iPt1], ny1 = norms[iPt1+1], nz1 = norms[iPt1+2], | ||
176 | nx2 = norms[iPt2], ny2 = norms[iPt2+1], nz2 = norms[iPt2+2]; | ||
177 | var nxA = (nx0 + nx1), nyA = (ny0 + ny1), nzA = (nz0 + nz1); var nrmA = VecUtils.vecNormalize(3, [nxA, nyA, nzA], 1.0 ), | ||
178 | nxB = (nx1 + nx2), nyB = (ny1 + ny2), nzB = (nz1 + nz2); var nrmB = VecUtils.vecNormalize(3, [nxB, nyB, nzB], 1.0 ), | ||
179 | nxC = (nx2 + nx0), nyC = (ny2 + ny0), nzC = (nz2 + nz0); var nrmC = VecUtils.vecNormalize(3, [nxC, nyC, nzC], 1.0 ); | ||
180 | |||
181 | // push everything | ||
182 | verts.push(xA); verts.push(yA); verts.push(zA); | ||
183 | verts.push(xB); verts.push(yB); verts.push(zB); | ||
184 | verts.push(xC); verts.push(yC); verts.push(zC); | ||
185 | uvs.push(uA), uvs.push(vA); | ||
186 | uvs.push(uB), uvs.push(vB); | ||
187 | uvs.push(uC), uvs.push(vC); | ||
188 | norms.push(nrmA[0]); norms.push(nrmA[1]); norms.push(nrmA[2]); | ||
189 | norms.push(nrmB[0]); norms.push(nrmB[1]); norms.push(nrmB[2]); | ||
190 | norms.push(nrmC[0]); norms.push(nrmC[1]); norms.push(nrmC[2]); | ||
191 | |||
192 | // split the current triangle into 4 | ||
193 | indices[index+1] = nVertices; indices[index+2] = nVertices+2; | ||
194 | indices.push(nVertices); indices.push(i1); indices.push(nVertices+1); nTriangles++; | ||
195 | indices.push(nVertices+1); indices.push(i2); indices.push(nVertices+2); nTriangles++; | ||
196 | indices.push(nVertices); indices.push(nVertices+1); indices.push(nVertices+2); nTriangles++; | ||
197 | nVertices += 3; | ||
198 | |||
199 | // by not advancing 'index', we examine the first of the 3 triangles generated above | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | |||
204 | console.log( "refine mesh vertex count " + oldVrtCount + " => " + nVertices ); | ||
205 | return nVertices; | ||
206 | }; | ||
207 | |||
208 | |||
79 | if (typeof exports === "object") { | 209 | if (typeof exports === "object") { |
80 | exports.ShapePrimitive = ShapePrimitive; | 210 | exports.ShapePrimitive = ShapePrimitive; |
81 | } \ No newline at end of file | 211 | } \ No newline at end of file |