#How are you calling CombineMeshes now

1 messages · Page 1 of 1 (latest)

hidden smelt
#

So this is my current code:

public static void Run2(in Mesh prefab, IMeshDeformPath path, out Mesh result)
{
    result = new Mesh();
    result.name = prefab.name;

    var verts = prefab.vertices;
    var newVerts = new Vector3[verts.Length];
    List<CombineInstance> combines = new List<CombineInstance>();

    for (int i = 0; i < path.Length; i++)
    {
        //new mesh for each step along the path
        var m = Object.Instantiate(prefab);

        //deform to conform to the path
        for (var v = 0; v < verts.Length; v++)
            newVerts[v] = Update(verts[v], i, path);

        //set the vertices
        m.vertices = newVerts;

        //add its submesh data to the list
        for (int s = 0; s < prefab.subMeshCount; s++)
            combines.Add(new CombineInstance() { mesh = m, subMeshIndex = s });
    }

    result.CombineMeshes(combines.ToArray(), false, false, false);
    result.RecalculateBounds();
    result.RecalculateNormals();
}
tranquil lotus
#

So from my understanding you want to call CombineMeshes three times

hidden smelt
#

or one per submesh plus the final combine?

tranquil lotus
#

Yes

hidden smelt
#

hm i see

tranquil lotus
#

So first two call with merging

#

And last call without merging

#

I assume performance is not big issue here

hidden smelt
#

well it is for run time

#

but the vert manipulation is in a job system

tranquil lotus
#

I guess you might want to improve the performance then, but making working code first would be good

hidden smelt
#

so i need something Dictionary<submeshID,List<CombineInstance>>()

tranquil lotus
#

Yeah if ID is consistent

hidden smelt
#

yeh it is

#

since im copy pasting the prefab each time the submeshes will always be the same index

tranquil lotus
#

Or sharedMaterial could be key I guess 🤔, whichever is comfortable

hidden smelt
#

hm trying to tihnk how to correctly combine them before applying the final combine

tranquil lotus
#

Shouldn’t be too complex

hidden smelt
#
        public static void Run2(in Mesh prefab, IMeshDeformPath path, out Mesh result)
        {
            result = new Mesh();
            result.name = prefab.name;

            var verts = prefab.vertices;
            var newVerts = new Vector3[verts.Length];
            List<CombineInstance>[] combines = new List<CombineInstance>[prefab.subMeshCount];
            Mesh[] meshes = new Mesh[prefab.subMeshCount];
            CombineInstance[] finalCombines = new CombineInstance[prefab.subMeshCount];

            for (int i = 0; i < path.Length; i++)
            {
                //new mesh for each step along the path
                var m = Object.Instantiate(prefab);

                //deform to conform to the path
                for (var v = 0; v < verts.Length; v++)
                    newVerts[v] = Update(verts[v], i, path);

                //set the vertices
                m.vertices = newVerts;

                //add its submesh data to the list
                for (int s = 0; s < prefab.subMeshCount; s++)
                {
                    if (combines[s] == null)
                        combines[s] = new List<CombineInstance>();

                    combines[s].Add(new CombineInstance() { mesh = m, subMeshIndex = s });
                }
            }

            for (int i = 0; i < combines.Length; i++)
            {
                meshes[i] = new Mesh();
                meshes[i].CombineMeshes(combines[i].ToArray(), true, false, false);
                finalCombines[i] = new CombineInstance() { mesh = meshes[i], subMeshIndex = i };
            }

            result.CombineMeshes(finalCombines, false, false, false);
            result.RecalculateBounds();
            result.RecalculateNormals();
        }
``` seem to get only one submesh output all the rest are culled
tranquil lotus
#

The submeshIndex for last combine should be 0

#

Both mesh is merged down to single submesh

hidden smelt
#

for result i set it to false so there should be two submeshes no?

tranquil lotus
#

I mean finalCombines[1] has single submesh

#

So it’s CombineInstance should have 0 for submeshIndex

hidden smelt
#

yeh it shows one

#

but it should have a second one

#

ill do a step through see if its actually populating correctly

tranquil lotus
#

I mean this line

finalCombines[i] = { … submeshIndex = i }
#

Should set submeshIndex = 0

#

Not i

hidden smelt
#

but then all submeshes have index 0

#

when one should be 0 and the other 1

tranquil lotus
#

No that’s not final index for submesh

#

That’s submesh index for the mesh you are giving

#

Put it 0 and see

hidden smelt
#

oh you so smart blushie

#

thanks ! i had been over complicating it after all

tranquil lotus
#

Yay! Glad it worked

hidden smelt
#

i dont fully follow why it had to 0 but ill take it ! 😄

#

i presume because a mesh with 1 submesh but the index starting at i>0 would basically be ignored?

tranquil lotus
#

Because each meshes you trying to combine now has 1 submesh

#

Yeah

#

That would be nothing

hidden smelt
#

tis a pain having to create multiple new meshes just to throw them away again

#

unity really could do with cleaner api on that

tranquil lotus
#

Yeah you might need custom algorithm when you optimize it

#

But enjoy for now