Exemplo n.º 1
0
        private static Attach FromNoWeight(Mesh mesh)
        {
            List <BufferMesh> result = new(mesh.Primitives.Count);

            IColorConverter <LinearRGBColor, RGBColor> converter
                = new ConverterBuilder().FromLinearRGB().ToRGB(RGBWorkingSpaces.sRGB).Build();

            foreach (var primitive in mesh.Primitives)
            {
                // read vertices
                primitive.VertexAccessors.TryGetValue("POSITION", out Accessor positions);
                primitive.VertexAccessors.TryGetValue("NORMAL", out Accessor normals);

                var positionArray = positions.AsVector3Array();
                var normalArray   = normals?.AsVector3Array();

                BufferVertex[] vertices = new BufferVertex[positionArray.Count];
                for (int i = 0; i < vertices.Length; i++)
                {
                    var pos = positionArray[i];
                    var nrm = normalArray?[i] ?? Vector3.UnitY;
                    vertices[i] = new(pos, nrm, (ushort)i);
                }

                // read corners
                primitive.VertexAccessors.TryGetValue("TEXCOORD_0", out Accessor uvs);
                primitive.VertexAccessors.TryGetValue("COLOR_0", out Accessor colors);

                var uvArray    = uvs?.AsVector2Array();
                var colorArray = colors?.AsColorArray();

                BufferCorner[] corners = new BufferCorner[vertices.Length];
                for (int i = 0; i < corners.Length; i++)
                {
                    Vector2 uv  = uvArray?[i] ?? default;
                    Vector4 col = colorArray?[i] ?? Vector4.UnitW;

                    var linearCol = converter.Convert(new(col.X, col.Y, col.Z));

                    corners[i] = new((ushort)i, new((float)linearCol.R, (float)linearCol.G, (float)linearCol.B, col.W), uv);
                }

                // Read indices
                uint[] indices = GetIndices(primitive.IndexAccessor, positions.Count, primitive.DrawPrimitiveType);

                // convert material
                result.Add(new(vertices, false, corners, indices, GetMaterial(primitive.Material)));
            }

            Attach atc = new(result.ToArray());

            atc.Name = mesh.Name;
            return(atc);
        }
Exemplo n.º 2
0
 public CachedVertex(BufferVertex vtx)
 {
     position = new(vtx.Position, 1);
     normal   = vtx.Normal;
 }