Explorar el Código

improving buffer builder

Vicente Penades hace 6 años
padre
commit
182c4ee80b
Se han modificado 2 ficheros con 102 adiciones y 11 borrados
  1. 80 1
      src/MeshBuffers/VertexBuffer.cs
  2. 22 10
      src/MeshBuffers/VertexDeclaration.cs

+ 80 - 1
src/MeshBuffers/VertexBuffer.cs

@@ -48,7 +48,7 @@ namespace MeshBuffers
         public void Add(Vertex vertex)
         {
             if (!VertexDeclaration.AreEqual(vertex._Declaration, this._Declaration)) throw new ArgumentException(nameof(vertex));
-            if (vertex._Data.Count == _Stride) throw new ArgumentException(nameof(vertex));
+            if (vertex._Data.Count != _Stride) throw new ArgumentException(nameof(vertex));
             _Data.AddRange(vertex._Data);
         }
 
@@ -83,5 +83,84 @@ namespace MeshBuffers
         }
 
         #endregion
+
+        #region extra API
+
+        public Single[] GetScalarColumn(string attribute)
+        {
+            var offset = _Declaration.GetOffset(attribute);
+            if (offset < 0) throw new ArgumentException(nameof(attribute));
+
+            var dst = new Single[this.Count];
+
+            for (int i = 0; i < dst.Length; ++i)
+            {
+                dst[i] = _Data[i * _Stride + offset];
+            }
+
+            return dst;
+        }
+
+        public Vector2[] GetVector2Column(string attribute)
+        {
+            var offset = _Declaration.GetOffset(attribute);
+            if (offset < 0) throw new ArgumentException(nameof(attribute));
+
+            var dst = new Vector2[this.Count];
+
+            for (int i = 0; i < dst.Length; ++i)
+            {
+                dst[i] = new Vector2
+                    (
+                    _Data[i * _Stride + offset + 0],
+                    _Data[i * _Stride + offset + 1]                    
+                    );
+            }
+
+            return dst;
+        }
+
+        public Vector3[] GetVector3Column(string attribute)
+        {
+            var offset = _Declaration.GetOffset(attribute);
+            if (offset < 0) throw new ArgumentException(nameof(attribute));
+
+            var dst = new Vector3[this.Count];
+
+            for(int i=0; i < dst.Length; ++i)
+            {
+                dst[i] = new Vector3
+                    (
+                    _Data[i * _Stride + offset + 0],
+                    _Data[i * _Stride + offset + 1],
+                    _Data[i * _Stride + offset + 2]
+                    );
+            }
+
+            return dst;
+        }
+
+        public Vector4[] GetVector4Column(string attribute)
+        {
+            var offset = _Declaration.GetOffset(attribute);
+            if (offset < 0) throw new ArgumentException(nameof(attribute));
+
+            var dst = new Vector4[this.Count];
+
+            for (int i = 0; i < dst.Length; ++i)
+            {
+                dst[i] = new Vector4
+                    (
+                    _Data[i * _Stride + offset + 0],
+                    _Data[i * _Stride + offset + 1],
+                    _Data[i * _Stride + offset + 2],
+                    _Data[i * _Stride + offset + 3]
+                    );
+            }
+
+            return dst;
+        }
+
+        #endregion
     }
 }

+ 22 - 10
src/MeshBuffers/VertexDeclaration.cs

@@ -38,15 +38,9 @@ namespace MeshBuffers
 
             _HashCode = _Elements.Select(item => item.GetHashCode()).Aggregate((a, b) => (a * 17) ^ b);
 
-            _PositionV3 = _Offset("POSITION");
-            _NormalV3 = _Offset("NORMAL");
-        }
-
-        private int _Offset(string attribute)
-        {
-            var element = _Elements.FirstOrDefault(item => item._Attribute == attribute);
-            return element._Attribute == null ? -1 : element._Offset;
-        }
+            _PositionV3 = GetOffset("POSITION");
+            _NormalV3 = GetOffset("NORMAL");
+        }        
 
         public VertexDeclaration WithSingle(string attribute)
         {
@@ -99,10 +93,28 @@ namespace MeshBuffers
 
         #endregion
 
-        #region API
+        #region properties
 
         public int Stride => _Stride;
 
+        public IEnumerable<string> Attributes => _Elements.Select(item => item._Attribute);
+
+        #endregion
+
+        #region API
+
+        public int GetOffset(string attribute)
+        {
+            var idx = Array.FindIndex(_Elements, item => item._Attribute == attribute);
+            return idx < 0 ? -1 : _Elements[idx]._Offset;
+        }
+
+        public int GetDimensions(string attribute)
+        {
+            var idx = Array.FindIndex(_Elements, item => item._Attribute == attribute);
+            return idx < 0 ? -1 : _Elements[idx]._Dimensions;
+        }
+
         public Vertex CreateVertex() { return new Vertex(this); }
 
         internal Vector3 GetPosition(ROW vertex)