Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meshinstance Not Drawing vertices at correct location and a possible performance issue #17307

Closed
Ron5s opened this issue Mar 6, 2018 · 4 comments

Comments

@Ron5s
Copy link

Ron5s commented Mar 6, 2018

Godot version:
Godot 3.0.2 Mono Stable

OS/device including version:
Windows 10 and 7

Issue description:
I rewrote the 2D Linebuilder code in 3D using MeshInstance and C#. It looks like it does not draw the vertices of the mesh in the correct location. If this geometry is draw with ImmediateGeometry it is draw correctly. The attached pictures show this. The green circles show where the location of the vertices are located. In addition to this it appears that ImmediateGeometry has a pretty big performance hit.

Steps to reproduce:
MeshInstance Function Code:

    public List<MeshInstance> Draw(List<Vector3[]> Features, List<Color> featureColor, List<string> featureName, SurfaceTool surfaceTool)
    {
        int i = 0;
        meshInstanceList.Clear();

        foreach (Vector3[] feature in Features)
        {
            SpatialMaterial material = new SpatialMaterial();
            material.AlbedoColor = featureColor[i];
            string name = featureName[i];
            i++;

            if (feature != null)
            {
                if (feature.Length > 2)
                {
                    MeshInstance meshInstance = new MeshInstance();
                    ArrayMesh arrayMesh = new ArrayMesh();
                    material.FlagsUnshaded = true;
                    material.SetCullMode(SpatialMaterial.CullMode.Disabled);
                    meshInstance.SetName(name);
                    meshInstance.SetMaterialOverride(material);
                    surfaceTool.Begin(Mesh.PrimitiveType.Triangles);

                    for (int j = 2; j < feature.Length; j = j + 3)
                    {
                        surfaceTool.AddVertex(feature[j - 2]);
                        surfaceTool.AddVertex(feature[j - 1]);
                        surfaceTool.AddVertex(feature[j]);
                    }
                    
                    surfaceTool.GenerateNormals();
                    surfaceTool.Index();
                    surfaceTool.Commit(arrayMesh);
                    meshInstance.SetMesh(arrayMesh);
                    meshInstanceList.Add(meshInstance);
                }
            }
            surfaceTool.Clear();
        }

        return meshInstanceList;
    }

ImmediateGeometry Code:

    public List<ImmediateGeometry> Draw(List<Vector3[]> Features, List<Color> featureColor, List<string> featureName)
    {
        int i = 0;
        ImmediateGeometryList.Clear();

        foreach (Vector3[] feature in Features)
        {
            SpatialMaterial material = new SpatialMaterial();
            material.AlbedoColor = featureColor[i];
            material.FlagsUnshaded = true;
            material.SetCullMode(SpatialMaterial.CullMode.Disabled);
            string name = featureName[i];
            i++;

            if (feature != null)
            {
                if (feature.Length > 2)
                {
                    ImmediateGeometry immediateGeometry = new ImmediateGeometry();
                    immediateGeometry.Begin(Mesh.PrimitiveType.Triangles);
                    immediateGeometry.SetName(name);
                    immediateGeometry.SetMaterialOverride(material);

                    for (int j = 2; j < feature.Length; j = j + 3)
                    {
                        immediateGeometry.AddVertex(feature[j - 2]);
                        immediateGeometry.AddVertex(feature[j - 1]);
                        immediateGeometry.AddVertex(feature[j]);
                    }
                    ImmediateGeometryList.Add(immediateGeometry);
                    immediateGeometry.End();
                }
            }
        }
        return ImmediateGeometryList;
    }

immediategeometry
meshinstance

@neikeq neikeq added this to the 3.1 milestone Mar 10, 2018
@K9Kraken
Copy link

K9Kraken commented Apr 3, 2018

I am suffering from the same bug using SurfaceTool. The further you draw something from the position 0,0,0 the worse the distortion gets. I get the same distortion results using SurfaceTool's add_vertex function and the append_from function.
I tested by not using SurfaceTool and added the shape by instancing a mesh and there is no distortion.

I am drawing a terrain using hexagons, around the 0,0,0 position the hexagons look normal.

This is what the hexagons look like when x position is around 300:
godot_st_distortion_1

This is what the hexagons are looking like when x position is around 500:
godot_st_distortion_2

I am on Manjaro Linux 17.1.7
Using Godot version 3.0.2

@K9Kraken
Copy link

K9Kraken commented May 4, 2018

Here is a scene that will draw shapes with ImmediateGeometry and SurfaceTool every frame while increasing the distance from position 0,0.

bug.tscn.zip

Shapes drawn with ImmediateGeometry have no issues drawing correctly.
Shapes drawn with SurfaceTool increasingly distort the further it is drawn from 0,0.

In this scene the top 3 shapes are drawn with ImmediateGeometry.
The bottom 3 shapes are drawn with SurfaceTool.

1
2
3

@K9Kraken
Copy link

K9Kraken commented May 8, 2018

Did some testing and imported models in Godot suffer from this exact bug: #17820

@reduz
Copy link
Member

reduz commented Sep 6, 2018

This was fixed recently, It had to do with vertex compression to 16 bits being enabled by default. This was removed, so it should no longer happen.

@reduz reduz closed this as completed Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants