vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

VertexColors and color per vertex #61

Closed duvet86 closed 3 years ago

duvet86 commented 4 years ago

Hi and thanks for the awesome library.

I am trying to assign a color per vertex but I have no idea how to do it. This is what I've been guessing so far.

                var colorObj = ColorTranslator.FromHtml(color);

                var material = new MaterialBuilder()
                    .WithBaseColor(new Vector4(colorObj.R / 255f, colorObj.G / 255f, colorObj.B / 255f, 1));

                var mesh = new MeshBuilder<VERTEX>($"mesh_{++localEntityId}");
                var prim = mesh.UsePrimitive(material);

                var (indexes, vertices) = geometry.GetVertices();

                for (int i = 2; i < indexes.Length; i += 3)
                {
                    var idx0 = indexes[i - 2];
                    var idx1 = indexes[i - 1];
                    var idx2 = indexes[i - 0];

                    var a = vertices[idx0];
                    var b = vertices[idx1];
                    var c = vertices[idx2];

                    prim.AddTriangle(a, b, c);
                }

                var scene = new SharpGLTF.Scenes.SceneBuilder();
                scene.AddRigidMesh(mesh, Matrix4x4.Identity);

                var model = scene.ToGltf2();

Thanks for your help.

vpenades commented 4 years ago

Use this:

var mesh = new MeshBuilder<VertexPosition, VertexColor1, VertexEmpty>($"mesh_{++localEntityId}");

Notice that MeshBuilder uses 3 templated components; with these components you can setup almost all the vertex types supported by glTF.

duvet86 commented 4 years ago

Thanks for the quick answer and sorry but I'm still struggling with it.

           ```
            var colorObj = ColorTranslator.FromHtml(color);

            //var material = new MaterialBuilder()
            //WithBaseColor(new Vector4(colorObj.R / 255f, colorObj.G / 255f, colorObj.B / 255f, 1));

            var material = MaterialBuilder.CreateDefault();

            //var mesh = new MeshBuilder<VERTEX>($"mesh_{++localEntityId}");
            var mesh = new MeshBuilder<VERTEX, VERTEXCOLOR, VERTEXEMPTY>($"mesh_{++localEntityId}");
            var prim = mesh.UsePrimitive(material);

            var prim2 = mesh.UsePrimitive(material, 1);

            var (indexes, vertices) = geometry.GetVertices();

            for (int i = 2; i < indexes.Length; i += 3)
            {
                var idx0 = indexes[i - 2];
                var idx1 = indexes[i - 1];
                var idx2 = indexes[i - 0];

                var a = vertices[idx0];
                var b = vertices[idx1];
                var c = vertices[idx2];

                prim.AddTriangle(a, b, c);
            }

            prim2.AddPoint(new VERTEX(colorObj.R / 255f, colorObj.G / 255f, colorObj.B / 255f));

            var scene = new SharpGLTF.Scenes.SceneBuilder();
            scene.AddRigidMesh(mesh, Matrix4x4.Identity);

            var model = scene.ToGltf2();
vpenades commented 4 years ago

prim.AddTriangle( (position0, color0), (position1,color1), (position2,color2) );

There's some examples in the project's code and tests that showcase how to pass different vertex layouts.

duvet86 commented 4 years ago

Thanks, now I have a better understanding of how to use the library. I've looked through the source code and I couldn't find examples.

I'd like to help and if you want to I could make a pull request improving the documentation on how to create a mesh and add vertex colors.

            var colorObj = ColorTranslator.FromHtml(color);
            var vertexColor = new VERTEXCOLOR(new Vector4(colorObj.R / 255f, colorObj.G / 255f, colorObj.B / 255f, 1));

            var mesh = new MeshBuilder<VERTEX, VERTEXCOLOR>("VertexColorMesh");
            var prim = mesh.UsePrimitive(MaterialBuilder.CreateDefault());

            var (indexes, vertices) = geometry.GetVertices();

            for (int i = 2; i < indexes.Length; i += 3)
            {
                var idx0 = indexes[i - 2];
                var idx1 = indexes[i - 1];
                var idx2 = indexes[i - 0];

                var a = vertices[idx0];
                var b = vertices[idx1];
                var c = vertices[idx2];

                prim.AddTriangle((a, vertexColor ), (b, vertexColor) , (c, vertexColor));
            }

            var scene = new SharpGLTF.Scenes.SceneBuilder();
            scene.AddRigidMesh(mesh, Matrix4x4.Identity);

            var model = scene.ToGltf2();
vpenades commented 4 years ago

There's plenty of examples on how to create meshes.

There's the Examples folder, where you can find a few examples that use different vertex types, The "InfiniteSkinnedTentacle" example uses the composited vertex.

There's also the Unit Tests, where you can find lots of examples on how to create meshes, from basic meshes to complex ones. There's even an example on how to create a mesh with a custom vertex attribute.

I've tried to add some minimal information but certainly it's not enough, for example:

But I agree with you that the documentation is missing or outdated in many places, so... more than adding docs to the code, what's missing is more "how to do..." tutorials... and I believe it could be easier for you, and I have no problem adding a tutorials section.