Browse Source

Added aditional information to Runtime rendering.

Vicente Penades 5 years ago
parent
commit
c2da3bd5a7

+ 2 - 2
examples/SharpGLTF.Runtime.MonoGame/MonoGameModelInstance.cs

@@ -58,9 +58,9 @@ namespace SharpGLTF.Runtime
         /// <param name="world">The world matrix.</param>
         public void Draw(Matrix projection, Matrix view, Matrix world)
         {
-            foreach (var d in _Controller.DrawableReferences)
+            foreach (var d in _Controller.DrawableInstances)
             {
-                Draw(_Template._Meshes[d.MeshIndex], projection, view, world, d.Transform);
+                Draw(_Template._Meshes[d.Template.LogicalMeshIndex], projection, view, world, d.Transform);
             }
         }
 

+ 2 - 2
examples/SharpGLTF.Runtime.MonoGame/MonoGameModelTemplate.cs

@@ -117,9 +117,9 @@ namespace SharpGLTF.Runtime
 
             var bounds = default(BoundingSphere);
 
-            foreach (var d in instance.DrawableReferences)
+            foreach (var d in instance.DrawableInstances)
             {
-                var b = _Meshes[d.MeshIndex].BoundingSphere;
+                var b = _Meshes[d.Template.LogicalMeshIndex].BoundingSphere;
 
                 if (d.Transform is Transforms.RigidTransform statXform) b = b.Transform(statXform.WorldMatrix.ToXna());
 

+ 20 - 0
src/SharpGLTF.Core/Runtime/DrawableInstance.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SharpGLTF.Runtime
+{
+    [System.Diagnostics.DebuggerDisplay("{Template.Name} {MeshIndex}")]
+    public readonly struct DrawableInstance
+    {
+        internal DrawableInstance(IDrawableTemplate t, Transforms.IGeometryTransform xform)
+        {
+            Template = t;
+            Transform = xform;
+        }
+
+        public readonly IDrawableTemplate Template;
+
+        public readonly Transforms.IGeometryTransform Transform;
+    }
+}

+ 19 - 6
src/SharpGLTF.Core/Runtime/Drawables.cs → src/SharpGLTF.Core/Runtime/DrawableTemplate.cs

@@ -5,28 +5,41 @@ using System.Text;
 
 namespace SharpGLTF.Runtime
 {
+    public interface IDrawableTemplate
+    {
+        string NodeName { get; }
+
+        int LogicalMeshIndex { get; }
+    }
+
     /// <summary>
     /// Defines a reference to a drawable mesh
     /// </summary>
-    abstract class DrawableReference
+    abstract class DrawableTemplate : IDrawableTemplate
     {
         #region lifecycle
 
-        protected DrawableReference(Schema2.Node node)
+        protected DrawableTemplate(Schema2.Node node)
         {
             _LogicalMeshIndex = node.Mesh.LogicalIndex;
+
+            _NodeName = node.Name;
         }
 
         #endregion
 
         #region data
 
+        private readonly String _NodeName;
+
         private readonly int _LogicalMeshIndex;
 
         #endregion
 
         #region properties
 
+        public String NodeName => _NodeName;
+
         /// <summary>
         /// Gets the index of a <see cref="Schema2.Mesh"/> in <see cref="Schema2.ModelRoot.LogicalMeshes"/>
         /// </summary>
@@ -46,11 +59,11 @@ namespace SharpGLTF.Runtime
     /// <summary>
     /// Defines a reference to a drawable rigid mesh
     /// </summary>
-    sealed class RigidDrawableReference : DrawableReference
+    sealed class RigidDrawableTemplate : DrawableTemplate
     {
         #region lifecycle
 
-        internal RigidDrawableReference(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
+        internal RigidDrawableTemplate(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
             : base(node)
         {
             _NodeIndex = indexFunc(node);
@@ -83,11 +96,11 @@ namespace SharpGLTF.Runtime
     /// <summary>
     /// Defines a reference to a drawable skinned mesh
     /// </summary>
-    sealed class SkinnedDrawableReference : DrawableReference
+    sealed class SkinnedDrawableTemplate : DrawableTemplate
     {
         #region lifecycle
 
-        internal SkinnedDrawableReference(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
+        internal SkinnedDrawableTemplate(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
             : base(node)
         {
             var skin = node.Skin;

+ 38 - 2
src/SharpGLTF.Core/Runtime/SceneInstance.cs

@@ -17,7 +17,7 @@ namespace SharpGLTF.Runtime
     {
         #region lifecycle
 
-        internal SceneInstance(NodeTemplate[] nodeTemplates, DrawableReference[] drawables, Collections.NamedList<float> tracks)
+        internal SceneInstance(NodeTemplate[] nodeTemplates, DrawableTemplate[] drawables, Collections.NamedList<float> tracks)
         {
             _AnimationTracks = tracks;
 
@@ -52,7 +52,7 @@ namespace SharpGLTF.Runtime
         private readonly NodeTemplate[] _NodeTemplates;
         private readonly NodeInstance[] _NodeInstances;
 
-        private readonly DrawableReference[] _DrawableReferences;
+        private readonly DrawableTemplate[] _DrawableReferences;
         private readonly IGeometryTransform[] _DrawableTransforms;
 
         private readonly Collections.NamedList<float> _AnimationTracks;
@@ -79,8 +79,14 @@ namespace SharpGLTF.Runtime
         /// <summary>
         /// Gets the number of drawable references.
         /// </summary>
+        [Obsolete("Use DrawableInstancesCount")]
         public int DrawableReferencesCount => _DrawableTransforms.Length;
 
+        /// <summary>
+        /// Gets the number of drawable instances.
+        /// </summary>
+        public int DrawableInstancesCount => _DrawableTransforms.Length;
+
         /// <summary>
         /// Gets a collection of drawable references, where:
         /// <list type="bullet">
@@ -94,6 +100,7 @@ namespace SharpGLTF.Runtime
         /// </item>
         /// </list>
         /// </summary>
+        [Obsolete("Use DrawableInstances.")]
         public IEnumerable<(int MeshIndex, IGeometryTransform Transform)> DrawableReferences
         {
             get
@@ -105,6 +112,17 @@ namespace SharpGLTF.Runtime
             }
         }
 
+        public IEnumerable<DrawableInstance> DrawableInstances
+        {
+            get
+            {
+                for (int i = 0; i < _DrawableTransforms.Length; ++i)
+                {
+                    yield return GetDrawableInstance(i);
+                }
+            }
+        }
+
         #endregion
 
         #region API
@@ -191,6 +209,7 @@ namespace SharpGLTF.Runtime
         /// </summary>
         /// <param name="index">The index of the drawable reference, from 0 to <see cref="DrawableReferencesCount"/></param>
         /// <returns>A drawable reference</returns>
+        [Obsolete("Use GetDrawableInstance")]
         public (int MeshIndex, IGeometryTransform Transform) GetDrawableReference(int index)
         {
             var dref = _DrawableReferences[index];
@@ -200,6 +219,23 @@ namespace SharpGLTF.Runtime
             return (dref.LogicalMeshIndex, _DrawableTransforms[index]);
         }
 
+        /// <summary>
+        /// Gets a <see cref="DrawableInstance"/> object, where:
+        /// - Name is the name of this drawable instance. Originally, it was the name of <see cref="Schema2.Node"/>.
+        /// - MeshIndex is the logical Index of a <see cref="Schema2.Mesh"/> in <see cref="Schema2.ModelRoot.LogicalMeshes"/>.
+        /// - Transform is an <see cref="IGeometryTransform"/> that can be used to transform the <see cref="Schema2.Mesh"/> into world space.
+        /// </summary>
+        /// <param name="index">The index of the drawable reference, from 0 to <see cref="DrawableInstancesCount"/></param>
+        /// <returns><see cref="DrawableInstance"/> object.</returns>
+        public DrawableInstance GetDrawableInstance(int index)
+        {
+            var dref = _DrawableReferences[index];
+
+            dref.UpdateGeometryTransform(_DrawableTransforms[index], _NodeInstances);
+
+            return new DrawableInstance(dref, _DrawableTransforms[index]);
+        }
+
         #endregion
     }
 }

+ 5 - 5
src/SharpGLTF.Core/Runtime/SceneTemplate.cs

@@ -63,7 +63,7 @@ namespace SharpGLTF.Runtime
                 .Where(item => item.Mesh != null)
                 .ToList();
 
-            var drawables = new DrawableReference[instances.Count];
+            var drawables = new DrawableTemplate[instances.Count];
 
             for (int i = 0; i < drawables.Length; ++i)
             {
@@ -71,9 +71,9 @@ namespace SharpGLTF.Runtime
 
                 drawables[i] = srcInstance.Skin != null
                     ?
-                    (DrawableReference)new SkinnedDrawableReference(srcInstance, indexSolver)
+                    (DrawableTemplate)new SkinnedDrawableTemplate(srcInstance, indexSolver)
                     :
-                    (DrawableReference)new RigidDrawableReference(srcInstance, indexSolver);
+                    (DrawableTemplate)new RigidDrawableTemplate(srcInstance, indexSolver);
             }
 
             // gather animation durations.
@@ -93,7 +93,7 @@ namespace SharpGLTF.Runtime
             return new SceneTemplate(srcScene.Name, dstNodes, drawables, dstTracks);
         }
 
-        private SceneTemplate(string name, NodeTemplate[] nodes, DrawableReference[] drawables, Collections.NamedList<float> animTracks)
+        private SceneTemplate(string name, NodeTemplate[] nodes, DrawableTemplate[] drawables, Collections.NamedList<float> animTracks)
         {
             _Name = name;
             _NodeTemplates = nodes;
@@ -108,7 +108,7 @@ namespace SharpGLTF.Runtime
         private readonly String _Name;
 
         private readonly NodeTemplate[] _NodeTemplates;
-        private readonly DrawableReference[] _DrawableReferences;
+        private readonly DrawableTemplate[] _DrawableReferences;
 
         private readonly Collections.NamedList<float> _AnimationTracks;
 

+ 4 - 4
src/SharpGLTF.Toolkit/Schema2/SceneExtensions.cs

@@ -229,9 +229,9 @@ namespace SharpGLTF.Schema2
             var meshes = scene.LogicalParent.LogicalMeshes;
 
             return instance
-                .DrawableReferences
+                .DrawableInstances
                 .Where(item => item.Transform.Visible)
-                .SelectMany(item => meshes[item.MeshIndex].EvaluateTriangles(item.Transform));
+                .SelectMany(item => meshes[item.Template.LogicalMeshIndex].EvaluateTriangles(item.Transform));
         }
 
         /// <summary>
@@ -265,9 +265,9 @@ namespace SharpGLTF.Schema2
             var meshes = scene.LogicalParent.LogicalMeshes;
 
             return instance
-                .DrawableReferences
+                .DrawableInstances
                 .Where(item => item.Transform.Visible)
-                .SelectMany(item => meshes[item.MeshIndex].EvaluateTriangles<TvG, TvM, VertexEmpty>(item.Transform));
+                .SelectMany(item => meshes[item.Template.LogicalMeshIndex].EvaluateTriangles<TvG, TvM, VertexEmpty>(item.Transform));
         }
 
         public static Scenes.SceneBuilder ToSceneBuilder(this Scene srcScene)

+ 1 - 1
tests/SharpGLTF.Tests/Schema2/LoadAndSave/LoadSampleTests.cs

@@ -318,7 +318,7 @@ namespace SharpGLTF.Schema2.LoadAndSave
             {
                 instance.SetAnimationFrame(anim.LogicalIndex, t);
 
-                var nodexform = instance.GetDrawableReference(0).Transform;
+                var nodexform = instance.GetDrawableInstance(0).Transform;
 
                 TestContext.WriteLine($"Animation at {t}");