Browse Source

fixed accessor bounds for normalized values to match the Spec

Vicente Penades 6 years ago
parent
commit
8a81443108

+ 11 - 3
src/SharpGLTF/Memory/FloatingArrays.cs

@@ -656,14 +656,15 @@ namespace SharpGLTF.Memory
         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
         public int Count => _Accessor.Count;
 
+        [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
+        public int Dimensions => _Dimensions;
+
         public Single[] this[int index]
         {
             get
             {
                 var val = new Single[_Dimensions];
-
-                for (int i = 0; i < val.Length; ++i) val[i] = _Accessor[index, i];
-
+                CopyItemTo(index, val);
                 return val;
             }
 
@@ -679,6 +680,13 @@ namespace SharpGLTF.Memory
             }
         }
 
+        public void CopyItemTo(int index, Single[] dstItem)
+        {
+            var count = _Dimensions;
+
+            for (int i = 0; i < count; ++i) dstItem[i] = _Accessor[index, i];
+        }
+
         public void CopyTo(ArraySegment<Single[]> dst) { EncodedArrayUtils.Copy<Single[]>(this, dst); }
 
         public IEnumerator<Single[]> GetEnumerator() { return new EncodedArrayEnumerator<Single[]>(this); }

+ 15 - 52
src/SharpGLTF/Schema2/gltf.Accessors.cs

@@ -327,11 +327,15 @@ namespace SharpGLTF.Schema2
             return SourceBufferView.Content.Slice(this.ByteOffset + (vertexIdx * byteStride), byteSize);
         }
 
+        #endregion
+
+        #region API
+
         public void UpdateBounds()
         {
-            var count = this._type.DimCount();
+            var dimensions = this._type.DimCount();
 
-            var bounds = new double[count];
+            var bounds = new double[dimensions];
             this._min.Clear();
             this._min.AddRange(bounds);
             this._max.Clear();
@@ -340,60 +344,19 @@ namespace SharpGLTF.Schema2
             this._min.Fill(double.MaxValue);
             this._max.Fill(double.MinValue);
 
-            if (count == 1)
-            {
-                var minmax = this.AsScalarArray().GetBounds();
-
-                this._min[0] = minmax.Item1;
-                this._max[0] = minmax.Item2;
-            }
-
-            if (count == 2)
-            {
-                var minmax = this.AsVector2Array().GetBounds();
+            var array = new MultiArray(this.SourceBufferView.Content, this.ByteOffset, this.Count, this.SourceBufferView.ByteStride, dimensions, this.Encoding, false);
 
-                this._min[0] = minmax.Item1.X;
-                this._max[0] = minmax.Item2.X;
+            var current = new float[dimensions];
 
-                this._min[1] = minmax.Item1.Y;
-                this._max[1] = minmax.Item2.Y;
-            }
-
-            if (count == 3)
+            for (int i = 0; i < array.Count; ++i)
             {
-                var minmax = this.AsVector3Array().GetBounds();
-
-                this._min[0] = minmax.Item1.X;
-                this._max[0] = minmax.Item2.X;
+                array.CopyItemTo(i, current);
 
-                this._min[1] = minmax.Item1.Y;
-                this._max[1] = minmax.Item2.Y;
-
-                this._min[2] = minmax.Item1.Z;
-                this._max[2] = minmax.Item2.Z;
-            }
-
-            if (count == 4)
-            {
-                var minmax = this.AsVector4Array().GetBounds();
-
-                this._min[0] = minmax.Item1.X;
-                this._max[0] = minmax.Item2.X;
-
-                this._min[1] = minmax.Item1.Y;
-                this._max[1] = minmax.Item2.Y;
-
-                this._min[2] = minmax.Item1.Z;
-                this._max[2] = minmax.Item2.Z;
-
-                this._min[3] = minmax.Item1.W;
-                this._max[3] = minmax.Item2.W;
-            }
-
-            if (count > 4)
-            {
-                this._min.Clear();
-                this._max.Clear();
+                for (int j = 0; j < current.Length; ++j)
+                {
+                    this._min[j] = Math.Min(this._min[j], current[j]);
+                    this._max[j] = Math.Max(this._max[j], current[j]);
+                }
             }
         }
 

+ 1 - 1
tests/SharpGLTF.Tests/Schema2/Authoring/CreateModelTests.cs

@@ -372,7 +372,7 @@ namespace SharpGLTF.Schema2.Authoring
 
             model.CreateAnimation("Animation")
                 .CreateRotationChannel(jnode2, keyframes);
-
+            
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.gltf");
         }

+ 8 - 4
tests/SharpGLTF.Tests/TestUtils.cs

@@ -43,15 +43,19 @@ namespace SharpGLTF
         {
             // find the output path for the current test
             fileName = NUnit.Framework.TestContext.CurrentContext.GetAttachmentPath(fileName, true);
-            
-            if (fileName.ToLower().EndsWith(".glb"))
+
+            if (model.LogicalBuffers.Count > 1 || model.LogicalImages.Count > 0)
             {
                 // clone the model so merging the buffers will not affect the source model.
                 model = model.DeepClone();
 
-                // ensure the model has just one buffer
-                model.MergeImages();
+                if (fileName.ToLower().EndsWith(".glb")) model.MergeImages();
                 model.MergeBuffers();
+            }
+
+            
+            if (fileName.ToLower().EndsWith(".glb"))
+            {
                 model.SaveGLB(fileName);
             }
             else if (fileName.ToLower().EndsWith(".gltf"))