vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
454 stars 72 forks source link

Polygons flicker when you rotate the screen. #177

Closed EugeneNIghtingale closed 1 month ago

EugeneNIghtingale commented 1 year ago

Hello vpenades! Thank you for you job/ Part of volumetric objects with a closed contour, when the distance between polygons is small, is not displayed and flickers. This can be verified with code that builds a small box::

        static void Main(string[] args)
        {
            VERTEX[] arrayVertex = new VERTEX[] {
                new VERTEX(8297641, 4115375, (float)66.7979),
                new VERTEX(8297640, 4115375, (float)66.7979),
                new VERTEX(8297638, 4115374, (float)66.7979),

                new VERTEX(8297638, 4115374, (float)66.7979),
                new VERTEX(8297640, 4115375, (float)66.7979),
                new VERTEX(8297638, 4115375, (float)66.7979),

                new VERTEX(8297641, 4115375, (float)66.77165),
                new VERTEX(8297638, 4115374, (float)66.77165),
                new VERTEX(8297640, 4115375, (float)66.77165),

                new VERTEX(8297640, 4115375, (float)66.77165),
                new VERTEX(8297638, 4115374, (float)66.77165),
                new VERTEX(8297638, 4115375, (float)66.77165),

                new VERTEX(8297638, 4115375, (float)66.77165),
                new VERTEX(8297638, 4115374, (float)66.77165),
                new VERTEX(8297638, 4115375, (float)66.7979),

                new VERTEX(8297638, 4115375, (float)66.7979),
                new VERTEX(8297638, 4115374, (float)66.77165),
                new VERTEX(8297638, 4115374, (float)66.7979),

                new VERTEX(8297638, 4115374, (float)66.77165),
                new VERTEX(8297641, 4115375, (float)66.77165),
                new VERTEX(8297638, 4115374, (float)66.7979),

                new VERTEX(8297638, 4115374, (float)66.7979),
                new VERTEX(8297641, 4115375, (float)66.77165),
                new VERTEX(8297641, 4115375, (float)66.7979),

                new VERTEX(8297641, 4115375, (float)66.77165),
                new VERTEX(8297640, 4115375, (float)66.77165),
                new VERTEX(8297641, 4115375, (float)66.7979),

                new VERTEX(8297641, 4115375, (float)66.7979),
                new VERTEX(8297640, 4115375, (float)66.77165),
                new VERTEX(8297640, 4115375, (float)66.7979),

                new VERTEX(8297640, 4115375, (float)66.77165),
                new VERTEX(8297638, 4115375, (float)66.77165),
                new VERTEX(8297640, 4115375, (float)66.7979),

                new VERTEX(8297640, 4115375, (float)66.7979),
                new VERTEX(8297638, 4115375, (float)66.77165),
                new VERTEX(8297638, 4115375, (float)66.7979)};

            var material1 = new MaterialBuilder()
                .WithDoubleSide(true)
                .WithMetallicRoughnessShader()
                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 0, 1));

            var mesh = new MeshBuilder<VERTEX>("mesh");
            var prim = mesh.UsePrimitive(material1);
            for (int i = 0; i < arrayVertex.Count()-3; i=i+3)
            {
                prim.AddTriangle(arrayVertex[i], arrayVertex[i+1], arrayVertex[i+2]);
            }

            var scene = new SharpGLTF.Scenes.SceneBuilder();
            scene.AddRigidMesh(mesh, Matrix4x4.Identity);
            var model = scene.ToGltf2();
            model.SaveGLTF("mesh.gltf");
        }
vpenades commented 1 year ago

I haven't tried your code, but from your code, I suspect the problem is the vertex coordinates you're using.

You must understand how floating point values work: floating values allow for very small numbers, and very large numbers at the expense of precission , so the larger the number, the more precission is lost.

So you have vertices that are very large in the X and Y axis, and very small in the Z axis. This is no good.

The recomendation would be to create self-centered meshes, and then move the meshes to the final location using matrix transforms in the nodes.