aboutsummaryrefslogtreecommitdiff
path: root/js/lib/geom/shape-primitive.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/geom/shape-primitive.js')
-rw-r--r--js/lib/geom/shape-primitive.js181
1 files changed, 180 insertions, 1 deletions
diff --git a/js/lib/geom/shape-primitive.js b/js/lib/geom/shape-primitive.js
index 5a44deb3..57dedf4a 100644
--- a/js/lib/geom/shape-primitive.js
+++ b/js/lib/geom/shape-primitive.js
@@ -144,7 +144,7 @@ ShapePrimitive.refineMesh = function( verts, norms, uvs, indices, nVertices, pa
144 { 144 {
145 // check thesize of the triangle in uv space. If small enough, advance 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; 146 // to the next triangle. If not small enough, split the triangle into 3;
147 var du = uMax - uMin, dv = vMax - vMin; 147 var du = Math.abs(uMax) - uMin, dv = Math.abs(vMax - vMin);
148 if ((du < tolerance) && (dv < tolerance)) 148 if ((du < tolerance) && (dv < tolerance))
149 { 149 {
150 iTriangle++; 150 iTriangle++;
@@ -205,6 +205,185 @@ ShapePrimitive.refineMesh = function( verts, norms, uvs, indices, nVertices, pa
205 return nVertices; 205 return nVertices;
206}; 206};
207 207
208ShapePrimitive.convertTriangleStripToTriangles = function( indices )
209{
210 if (!indices || (indices.length < 3)) return;
211
212 var indOut = [];
213 var nInd = indices.length;
214 for (var i=2; i<nInd; i++)
215 {
216 indOut.push( indices[i-2] );
217 indOut.push( indices[i-1] );
218 indOut.push( indices[i] );
219 }
220
221 return indOut;
222};
223
224ShapePrimitive.subdivideOversizedMesh = function( vertices, normals, uvs, indices )
225{
226 var rtnArray;
227 var nVrtBytes = vertices.length*4,
228 nIndBytes = indices.length*4;
229
230 // only subdivide the input mesh if it exceeds limits
231 if ((nVrtBytes >= 65000) || (nIndBytes >= 65000))
232 {
233 var nVerts = vertices.length / 3;
234 var nVerts0 = 0, nVerts1 = 0;
235 var iSplitVrt = nVerts/2; // any triangle referencing vertex iSplitVrt or greater goes to the second half
236 var nTriangles = indices.length/3;
237 var v0 = [], v1 = [], n0 = [], n1 = [], uv0 = [], uv1 = [], i0 = [], i1 = [];
238 var map0 = [], map1 = [];
239 var index = 0;
240 for (var iTri=0; iTri<nTriangles; iTri++)
241 {
242 // determine which side to move the triangle into
243 var vDst, nDst, uvDst, iDst, mapDst, nOut;
244 var iVrts = [ indices[index], indices[index+1], indices[index+2] ];
245 if ( (iVrts[0] >= iSplitVrt) || (iVrts[1] >= iSplitVrt) || (iVrts[2] >= iSplitVrt) )
246 {
247 vDst = v0; nDst = n0; uvDst = uv0; iDst = i0; mapDst = map0; nOut = v0.length / 3;
248 }
249 else
250 {
251 vDst = v1; nDst = n1; uvDst = uv1; iDst = i1; mapDst = map1; nOut = v1.length / 3;
252 }
253
254 for (var i=0; i<3; i++)
255 {
256 var iVrt = iVrts[i];
257
258 // if this is the first time that the vertex has been encountered, copy it over to the output
259 var iOut = mapDst[iVrt];
260 if (!iOut)
261 {
262 mapDst[iVrt] = nOut;
263 vDst.push( vertices[3*iVrt] ); vDst.push( vertices[3*iVrt + 1] ); vDst.push( vertices[3*iVrt + 2] );
264 nDst.push( normals[3*iVrt] ); nDst.push( normals[3*iVrt + 1] ); nDst.push( normals[3*iVrt + 2] );
265 uvDst.push( uvs[2*iVrt] ); uvDst.push( uvs[2*iVrt + 1] );
266 iDst.push( nOut );
267 nOut++;
268 }
269 else
270 iDst.push( iOut );
271 }
272
273 index += 3;
274 }
275
276 // create objects out of the 2 halves
277 var obj1 =
278 {
279 vertices: v0,
280 normals: n0,
281 uvs: uv0,
282 indices: i0
283 },
284 obj2 =
285 {
286 vertices: v1,
287 normals: n1,
288 uvs: uv1,
289 indices: i1
290 };
291
292 console.log( "mesh split into 2 parts: " + obj1.vertices.length/3 + ", " + obj2.vertices.length/3 );
293
294 // recurse on the 2 halves in case they need subdivision
295 var arr1 = ShapePrimitive.subdivideOversizedMesh( obj1.vertices, obj1.normals, obj1.uvs, obj1.indices );
296 var arr2 = ShapePrimitive.subdivideOversizedMesh( obj2.vertices, obj2.normals, obj2.uvs, obj2.indices );
297 rtnArray = arr1.concat( arr2 );
298 }
299 else
300 {
301 rtnArray =
302 [
303 {
304 vertices: vertices,
305 normals: normals,
306 uvs: uvs,
307 indices: indices
308 }
309 ];
310 }
311
312 return rtnArray;
313};
314
315
316////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
317
318ShapePrimitive.convertTrianglesToLines = function( verts, norms, uvs, indices, vertsOut, normsOut, uvsOut, indicesOut )
319{
320 var iTriangle = 0;
321 var nTriangles = indices.length/3;
322 var index = 0;
323 var nVertices = 0;
324 while (iTriangle < nTriangles)
325 {
326 // get the indices of the 3 vertices
327 var i0 = indices[index],
328 i1 = indices[index+1],
329 i2 = indices[index+2];
330
331 // get the uv values
332 //var vrtIndex = 3*iTriangle;
333 var iuv0 = 2 * i0,
334 iuv1 = 2 * i1,
335 iuv2 = 2 * i2;
336 var u0 = uvs[iuv0], v0 = uvs[iuv0+1],
337 u1 = uvs[iuv1], v1 = uvs[iuv1+1],
338 u2 = uvs[iuv2], v2 = uvs[iuv2+1];
339
340 //calculate the position of the new vertex
341 var iPt0 = 3 * i0,
342 iPt1 = 3 * i1,
343 iPt2 = 3 * i2;
344 var x0 = verts[iPt0], y0 = verts[iPt0+1], z0 = verts[iPt0+2],
345 x1 = verts[iPt1], y1 = verts[iPt1+1], z1 = verts[iPt1+2],
346 x2 = verts[iPt2], y2 = verts[iPt2+1], z2 = verts[iPt2+2];
347
348 // calculate the normals for the new points
349 var nx0 = norms[iPt0], ny0 = norms[iPt0+1], nz0 = norms[iPt0+2],
350 nx1 = norms[iPt1], ny1 = norms[iPt1+1], nz1 = norms[iPt1+2],
351 nx2 = norms[iPt2], ny2 = norms[iPt2+1], nz2 = norms[iPt2+2];
352
353 // push everything
354 vertsOut.push( x0 ); vertsOut.push( y0 ); vertsOut.push( z0 );
355 vertsOut.push( x1 ); vertsOut.push( y1 ); vertsOut.push( z1 );
356 vertsOut.push( x1 ); vertsOut.push( y1 ); vertsOut.push( z1 );
357 vertsOut.push( x2 ); vertsOut.push( y2 ); vertsOut.push( z2 );
358 vertsOut.push( x2 ); vertsOut.push( y2 ); vertsOut.push( z2 );
359 vertsOut.push( x0 ); vertsOut.push( y0 ); vertsOut.push( z0 );
360 indicesOut.push( index ); indicesOut.push( index + 1 );
361 indicesOut.push( index + 1 ); indicesOut.push( index + 2 );
362 indicesOut.push( index + 2 ); indicesOut.push( index );
363
364 normsOut.push( nx0 ); normsOut.push( ny0 ); normsOut.push( nz0 );
365 normsOut.push( nx1 ); normsOut.push( ny1 ); normsOut.push( nz1 );
366 normsOut.push( nx1 ); normsOut.push( ny1 ); normsOut.push( nz1 );
367 normsOut.push( nx2 ); normsOut.push( ny2 ); normsOut.push( nz2 );
368 normsOut.push( nx2 ); normsOut.push( ny2 ); normsOut.push( nz2 );
369 normsOut.push( nx0 ); normsOut.push( ny0 ); normsOut.push( nz0 );
370
371 uvsOut.push( u0 ); uvsOut.push( v0 );
372 uvsOut.push( u1 ); uvsOut.push( v1 );
373 uvsOut.push( u1 ); uvsOut.push( v1 );
374 uvsOut.push( u2 ); uvsOut.push( v2 );
375 uvsOut.push( u2 ); uvsOut.push( v2 );
376 uvsOut.push( u0 ); uvsOut.push( v0 );
377
378 iTriangle++;
379 index += 3;
380 nVertices += 6;
381 }
382
383 return nVertices;
384};
385
386
208 387
209if (typeof exports === "object") { 388if (typeof exports === "object") {
210 exports.ShapePrimitive = ShapePrimitive; 389 exports.ShapePrimitive = ShapePrimitive;