Browse Source

fleshing out SceneBuilder...

Vicente Penades 6 years ago
parent
commit
addda0c17a

+ 84 - 3
src/SharpGLTF.Toolkit/Animations/Curves.cs

@@ -13,6 +13,24 @@ namespace SharpGLTF.Animations
         T GetSample(float offset);
     }
 
+    public interface ICurveWriter<T>
+        where T : struct
+    {
+        void RemoveKey(float key);
+
+        void SetControlPoint(float key, T value);
+    }
+
+    public interface ICubicCurveWriter<T> : ICurveWriter<T>
+        where T : struct
+    {
+        void SetControlPointIn(float key, T value);
+        void SetControlPointOut(float key, T value);
+
+        void SetTangentIn(float key, T value);
+        void SetTangentOut(float key, T value);
+    }
+
     public abstract class Curve<Tin, Tout> : ICurveSampler<Tout>
         where Tin : struct
         where Tout : struct
@@ -31,6 +49,12 @@ namespace SharpGLTF.Animations
 
         #region API
 
+        public void RemoveKey(float key) { _Keys.Remove(key); }
+
+        protected Tin? GetKey(float key) { return _Keys.TryGetValue(key, out Tin value) ? value : (Tin?)null; }
+
+        protected void SetKey(float key, Tin value) { _Keys[key] = value; }
+
         protected (Tin, Tin, float) FindSample(float offset)
         {
             return _FindSample(_Keys, offset);
@@ -89,18 +113,29 @@ namespace SharpGLTF.Animations
         #endregion
     }
 
-    class Vector3LinearCurve : Curve<Vector3, Vector3>
+    class Vector3LinearCurve : Curve<Vector3, Vector3>, ICurveWriter<Vector3>
     {
+        #region API
+
         public override Vector3 GetSample(float offset)
         {
             var sample = FindSample(offset);
 
             return Vector3.Lerp(sample.Item1, sample.Item2, sample.Item3);
         }
+
+        public void SetControlPoint(float offset, Vector3 value)
+        {
+            SetKey(offset, value);
+        }
+
+        #endregion
     }
 
-    class Vector3CubicCurve : Curve<(Vector3, Vector3, Vector3), Vector3>
+    class Vector3CubicCurve : Curve<(Vector3, Vector3, Vector3), Vector3>, ICubicCurveWriter<Vector3>
     {
+        #region API
+
         public override Vector3 GetSample(float offset)
         {
             var sample = FindSample(offset);
@@ -122,16 +157,62 @@ namespace SharpGLTF.Animations
 
             return (value1 * part1) + (value2 * part2) + (tangent1 * part3) + (tangent2 * part4);
         }
+
+        public void SetControlPoint(float key, Vector3 value)
+        {
+            var val = GetKey(key) ?? default;
+            val.Item2 = value;
+            SetKey(key, val);
+        }
+
+        public void SetControlPointIn(float key, Vector3 value)
+        {
+            var val = GetKey(key) ?? default;
+            val.Item1 = value - val.Item2;
+            SetKey(key, val);
+        }
+
+        public void SetControlPointOut(float key, Vector3 value)
+        {
+            var val = GetKey(key) ?? default;
+            val.Item3 = value + val.Item2;
+            SetKey(key, val);
+        }
+
+        public void SetTangentIn(float key, Vector3 value)
+        {
+            var val = GetKey(key) ?? default;
+            val.Item1 = value;
+            SetKey(key, val);
+        }
+
+        public void SetTangentOut(float key, Vector3 value)
+        {
+            var val = GetKey(key) ?? default;
+            val.Item3 = value;
+            SetKey(key, val);
+        }
+
+        #endregion
     }
 
-    class QuaternionLinearCurve : Curve<Quaternion, Quaternion>
+    class QuaternionLinearCurve : Curve<Quaternion, Quaternion>, ICurveWriter<Quaternion>
     {
+        #region API
+
         public override Quaternion GetSample(float offset)
         {
             var sample = FindSample(offset);
 
             return Quaternion.Slerp(sample.Item1, sample.Item2, sample.Item3);
         }
+
+        public void SetControlPoint(float offset, Quaternion value)
+        {
+            SetKey(offset, value);
+        }
+
+        #endregion
     }
 
     class QuaternionCubicCurve : Curve<(Quaternion, Quaternion, Quaternion), Quaternion>

+ 59 - 0
src/SharpGLTF.Toolkit/Scenes/Content.cs

@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.Numerics;
+using System.Text;
+
+namespace SharpGLTF.Scenes
+{
+    interface IContentRoot { }
+
+    interface IContent { }
+
+    interface IRenderableContent : IContent { }
+
+    class StaticController : IContentRoot
+    {
+        private IContent _target;// Can be either a morphController or a mesh, or light or camera
+
+        private Matrix4x4 _Transform;
+    }
+
+    class TransformController : IContentRoot
+    {
+        private IContent _target;// Can be either a morphController or a mesh, or light or camera
+
+        private NodeBuilder _Node;
+    }
+
+    class SkinController : IContentRoot
+    {
+        private IRenderableContent _Target; // Can be either a morphController or a mesh
+
+        // condition: all NodeBuilder objects must have the same root.
+        private readonly List<NodeBuilder> _Joints = new List<NodeBuilder>();
+    }
+
+    class MorphModifier : IRenderableContent // must be a child of a controller, and the parent of a mesh
+    {
+        private IRenderableContent _Target; // must be a mesh
+
+        // morph targets here
+
+        private readonly Animations.Animatable<Vector4> _MorphWeights = new Animations.Animatable<Vector4>();
+    }
+
+    class MeshContent : IRenderableContent
+    {
+        private Geometry.IMeshBuilder<Materials.MaterialBuilder> _Geometry;
+    }
+
+    class LightContent : IContent
+    {
+
+    }
+
+    class CameraContent : IContent
+    {
+
+    }
+}

+ 26 - 0
src/SharpGLTF.Toolkit/Scenes/InstanceBuilder.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SharpGLTF.Scenes
+{
+    public class InstanceBuilder
+    {
+        #region lifecycle
+
+        internal InstanceBuilder(SceneBuilder parent)
+        {
+            _Parent = parent;
+        }
+
+        #endregion
+
+        #region data
+
+        private readonly SceneBuilder _Parent;
+
+        private IContentRoot _Content;
+
+        #endregion
+    }
+}

+ 32 - 0
src/SharpGLTF.Toolkit/Scenes/SceneBuilder.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SharpGLTF.Scenes
+{
+    public class SceneBuilder
+    {
+        #region data
+
+        private readonly List<InstanceBuilder> _Instances = new List<InstanceBuilder>();
+
+        #endregion
+
+        #region properties
+
+        public IReadOnlyList<InstanceBuilder> Instances => _Instances;
+
+        #endregion
+
+        #region API
+
+        public InstanceBuilder CreateInstance()
+        {
+            var inst = new InstanceBuilder(this);
+            _Instances.Add(inst);
+            return inst;
+        }
+
+        #endregion
+    }
+}

+ 1 - 1
tests/SharpGLTF.Tests/AnimationSamplingTests.cs

@@ -30,7 +30,7 @@ namespace SharpGLTF
         [Test]
         public void TestVector3CubicSplineSampling()
         {
-            var hermite = Transforms.AnimationSamplerFactory.Hermite(new Vector3(1, 0, 0), new Vector3(0, 2, 0), new Vector3(3, 0, 0), new Vector3(0, -2, 0), 0.5f);
+            var hermite = Transforms.AnimationSamplerFactory.Hermite(new Vector3(1, 0, 0), new Vector3(1, 2, 0), new Vector3(3, 0, 0), new Vector3(3, -2, 0), 0.5f);
 
             var sampler = Transforms.AnimationSamplerFactory.CreateCubicSamplerFunc(_TransAnim);