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