Looks like that whole algorithm was broken. It was attempting to
naively split just on vertices but that doesn't work as an index later
could point to a vertex that was in the previous submesh. Also, the
indices don't necessarily correspond to the vertices in the same area
in the array. Here's a patch which fixes the whole thing. It loops on
indices rather than vertices and then makes sure that an entire tri
goes into the submesh before a new one is created.

Index: MeshHelper.as
===================================================================
--- MeshHelper.as       (revision 3100)
+++ MeshHelper.as       (working copy)
@@ -1,21 +1,22 @@
 package away3d.tools
 {
        import away3d.arcane;
+       import away3d.containers.ObjectContainer3D;
        import away3d.core.base.Geometry;
+       import away3d.core.base.Object3D;
        import away3d.core.base.SubGeometry;
+       import away3d.core.base.data.UV;
        import away3d.core.base.data.Vertex;
-       import away3d.core.base.data.UV;
-       import away3d.core.base.Object3D;
+       import away3d.entities.Entity;
+       import away3d.entities.Mesh;
        import away3d.loading.IResource;
        import away3d.loading.ResourceDependency;
-       import away3d.entities.Mesh;
-       import away3d.entities.Entity;
-       import away3d.containers.ObjectContainer3D;
        import away3d.materials.MaterialBase;
        import away3d.tools.utils.Bounds;
-       
+       
+       import flash.geom.Matrix3D;
        import flash.geom.Vector3D;
-       import flash.geom.Matrix3D;
+       import flash.utils.Dictionary;
        
        use namespace arcane;
        
@@ -292,7 +293,7 @@
                        if(name != "")
                                m.name = name;

-                       if(vertices.length<LIMIT){
+                       if(vertices.length < LIMIT){
                                sub_geom = new SubGeometry();
                                sub_geom.updateVertexData(vertices);
                                sub_geom.updateIndexData(indices);
@@ -303,45 +304,49 @@
                                
                        } else {
                                
-                               var nvertices:Vector.<Number> = new 
Vector.<Number>();
-                               var nindices:Vector.<uint> = new 
Vector.<uint>();
-                               var nuvs:Vector.<Number> = new 
Vector.<Number>();
-                               var submeshes:Array = [nvertices, nindices, 
nuvs];
+                               var newvertices:Vector.<Number> = new 
Vector.<Number>();
+                               var newIndices:Vector.<uint> = new 
Vector.<uint>();
+                               var newUvs:Vector.<Number> = new 
Vector.<Number>();
+                               var indicesUsed : Dictionary = new Dictionary();
+                               var submeshes:Array = [newvertices, newIndices, 
newUvs];
                                
-                               var index:uint;
-                               var induv:uint;
-                               var ind:uint;
-                               var nind:uint;
-                               var ninduv:uint;
-                               
-                               var i:uint;
+                               var indicesIndex:uint;
                                var j:uint;
                                
-                               for (i = 0;i<vertices.length;i+=3){
-                                       
-                                       if(index +3 > LIMIT ){
-                                               index =  nind =  induv = ind = 
0;
-                                               nvertices = new 
Vector.<Number>();
-                                               nindices = new Vector.<uint>();
-                                               nuvs = new Vector.<Number>();
-                                               submeshes.push(nvertices, 
nindices, nuvs);
+                               for (indicesIndex = 0; indicesIndex < 
indices.length;) {
+                                       //the next tri *may* cause us to go 
past the limit so stant a new submesh
+                                       if (newvertices.length + 9 > LIMIT) {
+                                               newIndices = new 
Vector.<uint>();
+                                               newvertices = new 
Vector.<Number>();
+                                               newUvs = new Vector.<Number>();
+                                               indicesUsed = new Dictionary();
+                                               
+                                               submeshes.push(newvertices, 
newIndices, newUvs);
                                        }
-                                       
-                                       nvertices[index++] = vertices[i];
-                                       nvertices[index++] = vertices[i+1];
-                                       nvertices[index++] = vertices[i+2];
-                                       
-                                       nindices[ind++] = indices[nind++];
-                                       
-                                       nuvs[ninduv++] = uvs[induv++];
-                                       nuvs[ninduv++] = uvs[induv++];
+                                       //loop by tri
+                                       for (j = 0; j < 3; ++j, ++indicesIndex) 
{
+                                               var index : * = 
indicesUsed[indices[indicesIndex]];
+                                               if (index === null || index === 
undefined) {
+                                                       index = 
indicesUsed[indices[indicesIndex]] = newvertices.length / 3;
+                                                       
+                                                       var VertexIndex: uint = 
indices[indicesIndex] * 3;
+                                                       
newvertices.push(vertices[VertexIndex++]);
+                                                       
newvertices.push(vertices[VertexIndex++]);
+                                                       
newvertices.push(vertices[VertexIndex]);
+                                                       
+                                                       var uvIndex : uint = 
indices[indicesIndex] * 2;
+                                                       
newUvs.push(uvs[uvIndex++]);
+                                                       
newUvs.push(uvs[uvIndex++]);
+                                               }
+                                               newIndices.push(index);
+                                       }
                                }
-                               
-                               for(i=0;i<submeshes.length;i+=3){
+
+                               for(j = 0; j < submeshes.length;){
                                        sub_geom = new SubGeometry();
-                                       sub_geom.updateVertexData(submeshes[i]);
-                                       
sub_geom.updateIndexData(submeshes[i+1]);
-                                       sub_geom.updateUVData(submeshes[i+2]);
+                                       
sub_geom.updateVertexData(submeshes[j++]);
+                                       
sub_geom.updateIndexData(submeshes[j++]);
+                                       sub_geom.updateUVData(submeshes[j++]);
                                        geometry.addSubGeometry(sub_geom);
                                }
                        }
@@ -400,4 +405,4 @@
                        return build(vertices, indices, uvs, m.name, 
m.material);
                }
        }
-}
\ No newline at end of file
+}

--
Justin Patrin
Developer Extraordinaire



On Sat, Mar 26, 2011 at 12:49 PM, Fabrice3D <[email protected]> wrote:
> thx Justin, fixed.
> On Mar 26, 2011, at 7:51 AM, Justin Patrin wrote:
>
>> I was looking through and it looks like nindv needs to be reset when a
>> new subgeometry is set and that induv is being incremented one more
>> than it should be.
>>
>> Index: src/away3d/tools/MeshHelper.as
>> ===================================================================
>> --- src/away3d/tools/MeshHelper.as    (revision 3075)
>> +++ src/away3d/tools/MeshHelper.as    (working copy)
>> @@ -322,6 +322,7 @@
>>                                       if(index >= LIMIT ){
>>                                               index = 0;
>>                                               nind = 0;
>> +                                             ninduv = 0;
>>                                               nvertices = new 
>> Vector.<Number>();
>>                                               nindices = new Vector.<uint>();
>>                                               nuvs = new Vector.<Number>();
>> @@ -332,13 +333,10 @@
>>                                       nvertices[index++] = vertices[i+1];
>>                                       nvertices[index++] = vertices[i+2];
>>
>> -                                     nindices[nind] = indices[ind];
>> +                                     nindices[nind++] = indices[ind++];
>>
>>                                       nuvs[ninduv++] = uvs[induv++];
>>                                       nuvs[ninduv++] = uvs[induv++];
>> -
>> -                                     ind++;
>> -                                     nind++;
>> -                                     induv++;
>>                               }
>>
>>                               for(i=0;i<submeshes.length;i+=3){
>>
>>
>> --
>> Justin Patrin
>> Developer Extraordinaire
>
>

Reply via email to