Browse Source

MeshBuilder progress

Vicente Penades 6 years ago
parent
commit
1ac74ba2a7

+ 6 - 0
src/SharpGLTF.Core/Memory/MemoryAccessor.cs

@@ -38,9 +38,15 @@ namespace SharpGLTF.Memory
                 case "TEXCOORD_3": return new MemoryAccessInfo("TEXCOORD_3", 0, 0, 0, DIMENSIONS.VEC2);
                 case "TEXCOORD_3": return new MemoryAccessInfo("TEXCOORD_3", 0, 0, 0, DIMENSIONS.VEC2);
 
 
                 case "COLOR_0": return new MemoryAccessInfo("COLOR_0", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
                 case "COLOR_0": return new MemoryAccessInfo("COLOR_0", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
+                case "COLOR_1": return new MemoryAccessInfo("COLOR_1", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
+                case "COLOR_2": return new MemoryAccessInfo("COLOR_2", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
+                case "COLOR_3": return new MemoryAccessInfo("COLOR_3", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
 
 
                 case "JOINTS_0": return new MemoryAccessInfo("JOINTS_0", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE);
                 case "JOINTS_0": return new MemoryAccessInfo("JOINTS_0", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE);
+                case "JOINTS_1": return new MemoryAccessInfo("JOINTS_1", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE);
+
                 case "WEIGHTS_0": return new MemoryAccessInfo("WEIGHTS_0", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
                 case "WEIGHTS_0": return new MemoryAccessInfo("WEIGHTS_0", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
+                case "WEIGHTS_1": return new MemoryAccessInfo("WEIGHTS_1", 0, 0, 0, DIMENSIONS.VEC4, ENCODING.UNSIGNED_BYTE, true);
             }
             }
 
 
             throw new NotImplementedException();
             throw new NotImplementedException();

+ 2 - 0
src/SharpGLTF.Core/Transforms/AffineTransform.cs

@@ -5,6 +5,8 @@ using System.Text;
 
 
 namespace SharpGLTF.Transforms
 namespace SharpGLTF.Transforms
 {
 {
+    // https://github.com/KhronosGroup/glTF-Validator/issues/33
+
     /// <summary>
     /// <summary>
     /// Represents an affine transform in 3D space, defined by a <see cref="Quaternion"/> rotation, a <see cref="Vector3"/> scale and a <see cref="Vector3"/> translation.
     /// Represents an affine transform in 3D space, defined by a <see cref="Quaternion"/> rotation, a <see cref="Vector3"/> scale and a <see cref="Vector3"/> translation.
     /// </summary>
     /// </summary>

+ 28 - 0
src/SharpGLTF.Toolkit/Geometry/MeshBuilder.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Text;
 using System.Text;
 using System.Linq;
 using System.Linq;
+using System.Numerics;
 
 
 namespace SharpGLTF.Geometry
 namespace SharpGLTF.Geometry
 {
 {
@@ -167,6 +168,25 @@ namespace SharpGLTF.Geometry
             }
             }
         }
         }
 
 
+        public void AddPrimitive(PrimitiveBuilder<TMaterial, TvP, TvM, TvJ> primitive, Matrix4x4 transform)
+        {
+            if (primitive == null) throw new ArgumentNullException(nameof(primitive));
+
+            foreach (var t in primitive.Triangles)
+            {
+                var a = primitive.Vertices[t.Item1];
+                var b = primitive.Vertices[t.Item2];
+                var c = primitive.Vertices[t.Item3];
+
+                var aa = a.Item1; aa.Transform(transform); a.Item1 = aa;
+                var bb = b.Item1; bb.Transform(transform); b.Item1 = bb;
+                var cc = c.Item1; cc.Transform(transform); c.Item1 = cc;
+
+                AddTriangle(a, b, c);
+            }
+
+        }
+
         public void Validate()
         public void Validate()
         {
         {
             foreach (var v in _Vertices)
             foreach (var v in _Vertices)
@@ -265,6 +285,14 @@ namespace SharpGLTF.Geometry
             return new int[0];
             return new int[0];
         }
         }
 
 
+        public void AddMesh(MeshBuilder<TMaterial, TvP, TvM, TvJ> mesh, Matrix4x4 transform)
+        {
+            foreach (var p in mesh.Primitives)
+            {
+                UsePrimitive(p.Material).AddPrimitive(p, transform);
+            }
+        }
+
         public void Validate()
         public void Validate()
         {
         {
             foreach (var p in _Primitives.Values)
             foreach (var p in _Primitives.Values)

+ 53 - 0
src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexColumns.cs

@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using System.Numerics;
+using System.Text;
+
+namespace SharpGLTF.Geometry.VertexTypes
+{
+    class VertexColumns
+    {
+        #region lifecycle
+
+        public VertexColumns(IReadOnlyDictionary<string, Accessor> vertexAccessors)
+        {
+            if (vertexAccessors.ContainsKey("POSITION")) Positions = vertexAccessors["POSITION"].AsVector3Array();
+            if (vertexAccessors.ContainsKey("NORMAL")) Normals = vertexAccessors["NORMAL"].AsVector3Array();
+            if (vertexAccessors.ContainsKey("TANGENT")) Tangents = vertexAccessors["TANGENT"].AsVector4Array();
+
+            if (vertexAccessors.ContainsKey("COLOR_0")) Colors0 = vertexAccessors["COLOR_0"].AsVector4Array();
+            if (vertexAccessors.ContainsKey("COLOR_1")) Colors1 = vertexAccessors["COLOR_1"].AsVector4Array();
+
+            if (vertexAccessors.ContainsKey("TEXCOORD_0")) Textures0 = vertexAccessors["TEXCOORD_0"].AsVector2Array();
+            if (vertexAccessors.ContainsKey("TEXCOORD_1")) Textures1 = vertexAccessors["TEXCOORD_1"].AsVector2Array();
+
+            if (vertexAccessors.ContainsKey("JOINTS_0")) Joints0 = vertexAccessors["JOINTS_0"].AsVector4Array();
+            if (vertexAccessors.ContainsKey("JOINTS_1")) Joints1 = vertexAccessors["JOINTS_1"].AsVector4Array();
+
+            if (vertexAccessors.ContainsKey("WEIGHTS_0")) Weights0 = vertexAccessors["WEIGHTS_0"].AsVector4Array();
+            if (vertexAccessors.ContainsKey("WEIGHTS_1")) Weights1 = vertexAccessors["WEIGHTS_1"].AsVector4Array();
+        }
+
+        #endregion
+
+        #region columns
+
+        public Memory.IEncodedArray<Vector3> Positions { get; private set; }
+        public Memory.IEncodedArray<Vector3> Normals { get; private set; }
+        public Memory.IEncodedArray<Vector4> Tangents { get; private set; }
+
+        public Memory.IEncodedArray<Vector4> Colors0 { get; private set; }
+        public Memory.IEncodedArray<Vector4> Colors1 { get; private set; }
+
+        public Memory.IEncodedArray<Vector2> Textures0 { get; private set; }
+        public Memory.IEncodedArray<Vector2> Textures1 { get; private set; }
+
+        public Memory.IEncodedArray<Vector4> Joints0 { get; private set; }
+        public Memory.IEncodedArray<Vector4> Joints1 { get; private set; }
+
+        public Memory.IEncodedArray<Vector4> Weights0 { get; private set; }
+        public Memory.IEncodedArray<Vector4> Weights1 { get; private set; }
+
+        #endregion
+    }
+}

+ 23 - 5
src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexPosition.cs

@@ -7,6 +7,8 @@ namespace SharpGLTF.Geometry.VertexTypes
 {
 {
     public interface IVertexPosition
     public interface IVertexPosition
     {
     {
+        void Transform(Matrix4x4 xform);
+
         void Validate();
         void Validate();
     }
     }
 
 
@@ -33,6 +35,11 @@ namespace SharpGLTF.Geometry.VertexTypes
         [VertexAttribute("POSITION")]
         [VertexAttribute("POSITION")]
         public Vector3 Position;
         public Vector3 Position;
 
 
+        public void Transform(Matrix4x4 xform)
+        {
+            Position = Vector3.Transform(Position, xform);
+        }
+
         public void Validate()
         public void Validate()
         {
         {
             if (!Position._IsReal()) throw new NotFiniteNumberException(nameof(Position));
             if (!Position._IsReal()) throw new NotFiniteNumberException(nameof(Position));
@@ -62,6 +69,12 @@ namespace SharpGLTF.Geometry.VertexTypes
         [VertexAttribute("NORMAL")]
         [VertexAttribute("NORMAL")]
         public Vector3 Normal;
         public Vector3 Normal;
 
 
+        public void Transform(Matrix4x4 xform)
+        {
+            Position = Vector3.Transform(Position, xform);
+            Normal = Vector3.Normalize(Vector3.TransformNormal(Normal, xform));
+        }
+
         public void Validate()
         public void Validate()
         {
         {
             if (!Position._IsReal()) throw new NotFiniteNumberException(nameof(Position));
             if (!Position._IsReal()) throw new NotFiniteNumberException(nameof(Position));
@@ -90,16 +103,21 @@ namespace SharpGLTF.Geometry.VertexTypes
         [VertexAttribute("TANGENT")]
         [VertexAttribute("TANGENT")]
         public Vector4 Tangent;
         public Vector4 Tangent;
 
 
+        public void Transform(Matrix4x4 xform)
+        {
+            Position = Vector3.Transform(Position, xform);
+            Normal = Vector3.Normalize(Vector3.TransformNormal(Normal, xform));
+
+            // TODO: not sure if this is correct, must be checked. Most probably, if the xform handedness if negative, Tangent.W must be reversed.
+            var txyz = Vector3.Normalize(Vector3.TransformNormal(new Vector3(Tangent.X, Tangent.Y, Tangent.Z), xform));
+            Tangent = new Vector4(txyz, Tangent.W);
+        }
+
         public void Validate()
         public void Validate()
         {
         {
             if (!Position._IsReal()) throw new NotFiniteNumberException(nameof(Position));
             if (!Position._IsReal()) throw new NotFiniteNumberException(nameof(Position));
             if (!Normal._IsReal()) throw new NotFiniteNumberException(nameof(Normal));
             if (!Normal._IsReal()) throw new NotFiniteNumberException(nameof(Normal));
             if (!Tangent._IsReal()) throw new NotFiniteNumberException(nameof(Tangent));
             if (!Tangent._IsReal()) throw new NotFiniteNumberException(nameof(Tangent));
         }
         }
-
-        void IVertexPosition.Validate()
-        {
-            throw new NotImplementedException();
-        }
     }
     }
 }
 }