Browse Source

+progress with vertex buffers and geometry

Vicente Penades 6 years ago
parent
commit
b5de6a1c4d

+ 13 - 0
src/glTF2Sharp.DOM/Geometry/VertexBuffer.cs

@@ -93,6 +93,19 @@ namespace glTF2Sharp.Geometry
             }
         }
 
+        public void CopyTo(IReadOnlyDictionary<string,Schema2.Accessor> vertexAccessors)
+        {
+            foreach(var key in vertexAccessors.Keys)
+            {
+                var dim = GetDimensions(key);
+                if (dim == 1) Memory.EncodedArrayUtils.CopyTo(this.GetScalarColumn(key), vertexAccessors[key].AsScalarArray());
+                if (dim == 2) Memory.EncodedArrayUtils.CopyTo(this.GetVector2Column(key), vertexAccessors[key].AsVector2Array());
+                if (dim == 3) Memory.EncodedArrayUtils.CopyTo(this.GetVector3Column(key), vertexAccessors[key].AsVector3Array());
+                if (dim == 4) Memory.EncodedArrayUtils.CopyTo(this.GetVector4Column(key), vertexAccessors[key].AsVector4Array());
+            }           
+            
+        }
+
         #endregion
 
         #region static API

+ 12 - 12
src/glTF2Sharp.DOM/Schema2/gltf.AccessorSparse.cs

@@ -39,7 +39,7 @@ namespace glTF2Sharp.Schema2
         public Memory.SparseArray<Single> GetScalarArray(Accessor baseAccessor)
         {
             var bot = baseAccessor.AsScalarArray(false);
-            var top = this._values.GetScalarArray(baseAccessor.LogicalParent, baseAccessor.Encoding, baseAccessor.Normalized);
+            var top = this._values.GetScalarArray(baseAccessor.LogicalParent, _count, baseAccessor.Encoding, baseAccessor.Normalized);
             var idx = this._indices.GetIndicesArray(baseAccessor.LogicalParent);
 
             return new Memory.SparseArray<Single>(bot, top, idx);
@@ -48,7 +48,7 @@ namespace glTF2Sharp.Schema2
         public Memory.SparseArray<Vector2> GetVector2Array(Accessor baseAccessor)
         {
             var bot = baseAccessor.AsVector2Array(false);
-            var top = this._values.GetVector2Array(baseAccessor.LogicalParent, baseAccessor.Encoding, baseAccessor.Normalized);
+            var top = this._values.GetVector2Array(baseAccessor.LogicalParent, _count, baseAccessor.Encoding, baseAccessor.Normalized);
             var idx = this._indices.GetIndicesArray(baseAccessor.LogicalParent);
 
             return new Memory.SparseArray<Vector2>(bot, top, idx);
@@ -57,7 +57,7 @@ namespace glTF2Sharp.Schema2
         public Memory.SparseArray<Vector3> GetVector3Array(Accessor baseAccessor)
         {
             var bot = baseAccessor.AsVector3Array(false);
-            var top = this._values.GetVector3Array(baseAccessor.LogicalParent, baseAccessor.Encoding, baseAccessor.Normalized);
+            var top = this._values.GetVector3Array(baseAccessor.LogicalParent, _count, baseAccessor.Encoding, baseAccessor.Normalized);
             var idx = this._indices.GetIndicesArray(baseAccessor.LogicalParent);
 
             return new Memory.SparseArray<Vector3>(bot, top, idx);
@@ -66,7 +66,7 @@ namespace glTF2Sharp.Schema2
         public Memory.SparseArray<Vector4> GetVector4Array(Accessor baseAccessor)
         {
             var bot = baseAccessor.AsVector4Array(false);
-            var top = this._values.GetVector4Array(baseAccessor.LogicalParent, baseAccessor.Encoding, baseAccessor.Normalized);
+            var top = this._values.GetVector4Array(baseAccessor.LogicalParent, _count, baseAccessor.Encoding, baseAccessor.Normalized);
             var idx = this._indices.GetIndicesArray(baseAccessor.LogicalParent);
 
             return new Memory.SparseArray<Vector4>(bot, top, idx);
@@ -107,28 +107,28 @@ namespace glTF2Sharp.Schema2
             this._byteOffset = byteOffset.AsNullable(_byteOffsetDefault);            
         }
 
-        public Memory.ScalarArray GetScalarArray(ROOT root, ComponentType encoding, Boolean normalized)
+        public Memory.ScalarArray GetScalarArray(ROOT root, int count, ComponentType encoding, Boolean normalized)
         {
             var srcBuffer = root.LogicalBufferViews[this._bufferView];
-            return srcBuffer.CreateScalarArray(this._byteOffset ?? 0, encoding, normalized);
+            return srcBuffer.CreateScalarArray(this._byteOffset ?? 0, count, encoding, normalized);
         }
 
-        public Memory.Vector2Array GetVector2Array(ROOT root, ComponentType encoding, Boolean normalized)
+        public Memory.Vector2Array GetVector2Array(ROOT root, int count, ComponentType encoding, Boolean normalized)
         {
             var srcBuffer = root.LogicalBufferViews[this._bufferView];
-            return srcBuffer.CreateVector2Array(this._byteOffset ?? 0, encoding, normalized);
+            return srcBuffer.CreateVector2Array(this._byteOffset ?? 0, count, encoding, normalized);
         }
 
-        public Memory.Vector3Array GetVector3Array(ROOT root, ComponentType encoding, Boolean normalized)
+        public Memory.Vector3Array GetVector3Array(ROOT root, int count, ComponentType encoding, Boolean normalized)
         {
             var srcBuffer = root.LogicalBufferViews[this._bufferView];
-            return srcBuffer.CreateVector3Array(this._byteOffset ?? 0, encoding, normalized);
+            return srcBuffer.CreateVector3Array(this._byteOffset ?? 0, count, encoding, normalized);
         }
 
-        public Memory.Vector4Array GetVector4Array(ROOT root, ComponentType encoding, Boolean normalized)
+        public Memory.Vector4Array GetVector4Array(ROOT root, int count, ComponentType encoding, Boolean normalized)
         {
             var srcBuffer = root.LogicalBufferViews[this._bufferView];
-            return srcBuffer.CreateVector4Array(this._byteOffset ?? 0, encoding, normalized);
+            return srcBuffer.CreateVector4Array(this._byteOffset ?? 0, count, encoding, normalized);
         }
     }
 }

+ 4 - 4
src/glTF2Sharp.DOM/Schema2/gltf.Accessors.cs

@@ -157,7 +157,7 @@ namespace glTF2Sharp.Schema2
 
             if (this._sparse != null && useSparse) return this._sparse.GetScalarArray(this);
 
-            return SourceBufferView.CreateScalarArray(this.ByteOffset, this.Encoding, this.Normalized);            
+            return SourceBufferView.CreateScalarArray(this.ByteOffset, this.Count, this.Encoding, this.Normalized);            
         }
 
         public Memory.IEncodedArray<Vector2> AsVector2Array(bool useSparse = true)
@@ -166,7 +166,7 @@ namespace glTF2Sharp.Schema2
 
             if (this._sparse != null && useSparse) return this._sparse.GetVector2Array(this);
 
-            return SourceBufferView.CreateVector2Array(this.ByteOffset, this.Encoding, this.Normalized);
+            return SourceBufferView.CreateVector2Array(this.ByteOffset, this.Count, this.Encoding, this.Normalized);
         }
 
         public Memory.IEncodedArray<Vector3> AsVector3Array(bool useSparse = true)
@@ -175,7 +175,7 @@ namespace glTF2Sharp.Schema2
 
             if (this._sparse != null && useSparse) return this._sparse.GetVector3Array(this);
 
-            return SourceBufferView.CreateVector3Array(this.ByteOffset, this.Encoding, this.Normalized);
+            return SourceBufferView.CreateVector3Array(this.ByteOffset, this.Count, this.Encoding, this.Normalized);
         }
 
         public Memory.IEncodedArray<Vector4> AsVector4Array(bool useSparse = true)
@@ -184,7 +184,7 @@ namespace glTF2Sharp.Schema2
 
             if (this._sparse != null && useSparse) return this._sparse.GetVector4Array(this);
 
-            return SourceBufferView.CreateVector4Array(this.ByteOffset, this.Encoding, this.Normalized);
+            return SourceBufferView.CreateVector4Array(this.ByteOffset, this.Count, this.Encoding, this.Normalized);
         }
 
         public ArraySegment<Byte> TryGetVertexBytes(int vertexIdx)

+ 16 - 8
src/glTF2Sharp.DOM/Schema2/gltf.BufferView.cs

@@ -109,24 +109,32 @@ namespace glTF2Sharp.Schema2
             return new Memory.IntegerArray(this.Data.Slice(byteOffset), encoding);
         }        
 
-        public Memory.ScalarArray CreateScalarArray(int byteOffset, ENCODING encoding, Boolean normalized)
+        public Memory.ScalarArray CreateScalarArray(int byteOffset, int count, ENCODING encoding, Boolean normalized)
         {
-            return new Memory.ScalarArray(this.Data.Slice(byteOffset), this.ByteStride, encoding, normalized);            
+            var data = this.Data.Slice(byteOffset, count * this.ByteStride);
+
+            return new Memory.ScalarArray(data, this.ByteStride, encoding, normalized);            
         }
 
-        public Memory.Vector2Array CreateVector2Array(int byteOffset, ENCODING encoding, Boolean normalized)
+        public Memory.Vector2Array CreateVector2Array(int byteOffset, int count, ENCODING encoding, Boolean normalized)
         {
-            return new Memory.Vector2Array(this.Data.Slice(byteOffset), this.ByteStride, encoding, normalized);
+            var data = this.Data.Slice(byteOffset, count * this.ByteStride);
+
+            return new Memory.Vector2Array(data, this.ByteStride, encoding, normalized);
         }
 
-        public Memory.Vector3Array CreateVector3Array(int byteOffset, ENCODING encoding, Boolean normalized)
+        public Memory.Vector3Array CreateVector3Array(int byteOffset, int count, ENCODING encoding, Boolean normalized)
         {
-            return new Memory.Vector3Array(this.Data.Slice(byteOffset), this.ByteStride, encoding, normalized);
+            var data = this.Data.Slice(byteOffset, count * this.ByteStride);
+
+            return new Memory.Vector3Array(data, this.ByteStride, encoding, normalized);
         }
 
-        public Memory.Vector4Array CreateVector4Array(int byteOffset, ENCODING encoding, Boolean normalized)
+        public Memory.Vector4Array CreateVector4Array(int byteOffset, int count, ENCODING encoding, Boolean normalized)
         {
-            return new Memory.Vector4Array(this.Data.Slice(byteOffset), this.ByteStride, encoding, normalized);
+            var data = this.Data.Slice(byteOffset, count * this.ByteStride);
+
+            return new Memory.Vector4Array(data, this.ByteStride, encoding, normalized);
         }
 
         public Memory.QuaternionArray CreateQuaternionArray(int byteOffset, ENCODING encoding, Boolean normalized)

+ 7 - 2
src/glTF2Sharp.Tests/Geometry/VertexBufferTests.cs

@@ -55,8 +55,13 @@ namespace glTF2Sharp.Geometry
             var mdlmesh = mdl.CreateMesh();
             var mdlprim = mdlmesh.CreatePrimitive();
             mdlprim.SetVertexAccessors(mdlbv, vbs.ByteOffset, vbs.Count, vbs.Attributes);
-            Memory.EncodedArrayUtils.CopyTo(vbs.GetVector3Column("POSITION"), mdlprim.VertexAccessors["POSITION"].AsVector3Array());
-            Memory.EncodedArrayUtils.CopyTo(vbs.GetVector3Column("NORMAL"), mdlprim.VertexAccessors["NORMAL"].AsVector3Array());
+            vbs.CopyTo(mdlprim.VertexAccessors);
+
+            var mdlprim_pos = mdlprim.VertexAccessors["POSITION"].AsVector3Array();
+            var mdlprim_nrm = mdlprim.VertexAccessors["NORMAL"].AsVector3Array();
+
+            CollectionAssert.AreEqual(positions.Skip(1).Take(2), mdlprim_pos);
+            CollectionAssert.AreEqual(normals.Skip(1).Take(2), mdlprim_nrm);
 
             mdl.AttachToCurrentTest("result.glb");
             mdl.AttachToCurrentTest("result.gltf");