Jump to content
  • Advertisement
Sign in to follow this  
SpikeViper

Unity Strange get_mesh lagspikes (Unity)

This topic is 795 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone know what's causing this?

 

image.png

Relevant code:

    void RenderMesh()
    {
        if (firstrender != true)
        {
            mesh.Clear();
            colmesh.Clear();

        }


        meshData.vertices.TrimExcess();
        meshData.triangles.TrimExcess();
        meshData.uv.TrimExcess();

        mesh.vertices = meshData.vertices._items;
        mesh.triangles = meshData.triangles._items;
        mesh.uv = meshData.uv._items;

        colmesh.vertices = meshData.vertices._items;
        colmesh.triangles = meshData.triangles._items;
        colmesh.uv = meshData.uv._items;


        filter.mesh.RecalculateNormals();

        if (meshData.light == true)
        {

            for (int i = 0; i < meshData.lightpositions.Count; i++)
            {

                Vector3 position = meshData.lightpositions[i];

                int x = (int)position.x;
                int y = (int)position.y;
                int z = (int)position.z;

                if (lights[x, y, z] == null) //TODO: Optimize lights
                {


                    Color32 color = meshData.lightcolors[x, y, z];
                    float intensity = meshData.lightintensity[x, y, z];
                    float range = meshData.lightrange[x, y, z];

                    GameObject light = new GameObject("Light " + x + " " + y + " " + z);
                    Light lightComp = light.AddComponent<Light>();
                    lightComp.color = color;
                    lightComp.intensity = intensity;
                    lightComp.range = range;
                    light.transform.parent = this.transform;
                    light.transform.localPosition = new Vector3(x, y, z);

                    lights[x, y, z] = light;

                }

            }

        }

        filter.mesh = mesh;
        coll.sharedMesh = colmesh;
        meshData.Clear();

        firstrender = false;

    }

Share this post


Link to post
Share on other sites
Advertisement

Why are you using TrimExcess in the render. I has no effect on the data in the list but will trigger a reallocation of the internal structure of your list and a copy of the data. This could be a big overhead for long lists.

 

It also appears you rebuild the mesh every render from the mesh data if I read right. If your data does not change every frame why rebuild constantly? Just build it on change and then keep it until it changes again

Share this post


Link to post
Share on other sites

According to Unity reference, http://docs.unity3d.com/ScriptReference/MeshFilter-mesh.html ;

 

"If a mesh is assigned to the mesh filter already, then first query of mesh property will create a duplicate of it, and this copy will be returned. Further queries of meshproperty will return this duplicated mesh instance. Once mesh property is queried, link to the original shared mesh is lost and MeshFilter.sharedMesh property becomes an alias to mesh. If you want to avoid this automatic mesh duplication, use MeshFilter.sharedMesh instead."

 

I think this is what you are experiencing from filter.mesh.RecalculateNormals();.

Edited by tessm

Share this post


Link to post
Share on other sites

Why are you using TrimExcess in the render. I has no effect on the data in the list but will trigger a reallocation of the internal structure of your list and a copy of the data. This could be a big overhead for long lists.
 
It also appears you rebuild the mesh every render from the mesh data if I read right. If your data does not change every frame why rebuild constantly? Just build it on change and then keep it until it changes again


RenderMesh is only called when a chunk is edited, or when it is created.

Share this post


Link to post
Share on other sites

According to Unity reference, http://docs.unity3d.com/ScriptReference/MeshFilter-mesh.html ;
 
"[font='Open Sans', sans-serif]If a mesh is assigned to the mesh filter already, then first query of [/font]mesh[font='Open Sans', sans-serif] property will create a duplicate of it, and this copy will be returned. Further queries of [/font]mesh[font='Open Sans', sans-serif]property will return this duplicated mesh instance. Once [/font]mesh[font='Open Sans', sans-serif] property is queried, link to the original shared mesh is lost and MeshFilter.sharedMesh property becomes an alias to [/font]mesh[font='Open Sans', sans-serif]. If you want to avoid this automatic mesh duplication, use MeshFilter.sharedMesh instead."[/font]
 
I think this is what you are experiencing from filter.mesh.RecalculateNormals();.


Thanks! This looks like the problem, I'll test that out once I'm home.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!