Browse Source

support for Punctual Lights almost completed.

Vicente Penades 6 years ago
parent
commit
56870378aa

+ 46 - 0
src/SharpGLTF/Schema2/gltf.ExtensionsFactory.cs

@@ -75,4 +75,50 @@ namespace SharpGLTF.Schema2
 
         #endregion
     }
+
+    partial class ModelRoot
+    {
+        private IEnumerable<glTFProperty> _GetAllExtensionContainers()
+        {
+            IEnumerable<glTFProperty> containers = new glTFProperty[] { this, this.Asset };
+
+            containers = containers.Concat(this.LogicalAccessors);
+            containers = containers.Concat(this.LogicalAnimations);
+            containers = containers.Concat(this.LogicalBuffers);
+            containers = containers.Concat(this.LogicalBufferViews);
+            containers = containers.Concat(this.LogicalCameras);
+            containers = containers.Concat(this.LogicalImages);
+            containers = containers.Concat(this.LogicalMaterials);
+            containers = containers.Concat(this.LogicalMeshes);
+            containers = containers.Concat(this.LogicalNodes);
+            containers = containers.Concat(this.LogicalPunctualLights);
+            containers = containers.Concat(this.LogicalSamplers);
+            containers = containers.Concat(this.LogicalScenes);
+            containers = containers.Concat(this.LogicalSkins);
+            containers = containers.Concat(this.LogicalTextures);
+            return containers;
+        }
+
+        internal void UpdateExtensionsSupport()
+        {
+            var usedExtensions = new HashSet<string>();
+
+            foreach (var c in _GetAllExtensionContainers())
+            {
+                var ids = c.Extensions.Select(item => ExtensionsFactory.Identify(c.GetType(), item.GetType()));
+
+                foreach (var id in ids) usedExtensions.Add(id);
+            }
+        }
+
+        internal void UsingExtension(Type parentType, Type extensionType)
+        {
+            var id = ExtensionsFactory.Identify(parentType, extensionType);
+            if (string.IsNullOrWhiteSpace(id)) return;
+
+            if (this._extensionsUsed.Contains(id)) return;
+
+            this._extensionsUsed.Add(id);
+        }
+    }
 }

+ 9 - 0
src/SharpGLTF/Schema2/gltf.LogicalChildOfRoot.cs

@@ -49,5 +49,14 @@ namespace SharpGLTF.Schema2
         }
 
         #endregion
+
+        #region API
+
+        internal void UsingExtension(Type extensionType)
+        {
+            LogicalParent.UsingExtension(this.GetType(), extensionType);
+        }
+
+        #endregion
     }
 }

+ 2 - 2
src/SharpGLTF/Schema2/gltf.Scene.cs

@@ -138,7 +138,7 @@ namespace SharpGLTF.Schema2
         #region properties - content
 
         /// <summary>
-        /// Gets or sets the <see cref="Mesh"/> of this <see cref="Node"/>.
+        /// Gets or sets the <see cref="Schema2.Mesh"/> of this <see cref="Node"/>.
         /// </summary>
         public Mesh Mesh
         {
@@ -152,7 +152,7 @@ namespace SharpGLTF.Schema2
         }
 
         /// <summary>
-        /// Gets or sets the <see cref="Skin"/> of this <see cref="Node"/>.
+        /// Gets or sets the <see cref="Schema2.Skin"/> of this <see cref="Node"/>.
         /// </summary>
         public Skin Skin
         {

+ 28 - 1
src/SharpGLTF/Schema2/khr.lights.cs

@@ -61,7 +61,24 @@ namespace SharpGLTF.Schema2
         public PunctualLightType LightType
         {
             get => (PunctualLightType)Enum.Parse(typeof(PunctualLightType), _type, true);
-            set => this._type = value.ToString().ToLower();
+            set
+            {
+                this._type = value.ToString().ToLower();
+                if (value != PunctualLightType.Spot) this._spot = null;
+                else if (this._spot == null) this._spot = new PunctualLightSpot();
+            }
+        }
+
+        public float InnerConeAngle
+        {
+            get => this._spot == null ? 0 : (float)this._spot.InnerConeAngle;
+            set { if (this._spot != null) this._spot.InnerConeAngle = value; }
+        }
+
+        public float OuterConeAngle
+        {
+            get => this._spot == null ? 0 : (float)this._spot.OuterConeAngle;
+            set { if (this._spot != null) this._spot.OuterConeAngle = value; }
         }
     }
 
@@ -110,6 +127,8 @@ namespace SharpGLTF.Schema2
             var ext = this.GetExtension<KHR_lights_punctualglTFextension>();
             if (ext == null)
             {
+                this.UsingExtension(typeof(ModelRoot), typeof(KHR_lights_punctualglTFextension));
+
                 ext = new KHR_lights_punctualglTFextension(this);
                 this.SetExtension(ext);
             }
@@ -133,6 +152,12 @@ namespace SharpGLTF.Schema2
 
     partial class Node
     {
+        /// <summary>
+        /// Gets or sets the <see cref="Schema2.PunctualLight"/> of this <see cref="Node"/>.
+        /// </summary>
+        /// <remarks>
+        /// This is part of <see cref="https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual"/> extension.
+        /// </remarks>
         public PunctualLight PunctualLight
         {
             get
@@ -148,6 +173,8 @@ namespace SharpGLTF.Schema2
 
                 Guard.MustShareLogicalParent(this, value, nameof(value));
 
+                this.UsingExtension(typeof(KHR_lights_punctualnodeextension));
+
                 var ext = new KHR_lights_punctualnodeextension(this);
                 ext.LightIndex = value.LogicalIndex;
 

+ 15 - 0
tests/SharpGLTF.Tests/Schema2/Authoring/CreateModelTests.cs

@@ -67,6 +67,21 @@ namespace SharpGLTF.Schema2.Authoring
                 );
         }
 
+        [Test(Description ="Creates a scene with lights")]
+        public void CreateSceneWithLights()
+        {
+            TestContext.CurrentContext.AttachShowDirLink();
+            TestContext.CurrentContext.AttachGltfValidatorLink();
+
+            var root = ModelRoot.CreateModel();
+            var scene = root.UseScene("Empty Scene");            
+            var node = scene.CreateNode();
+            node.PunctualLight = root.CreatePunctualLight("Directional Light");
+            node.PunctualLight.LightType = PunctualLightType.Directional;
+
+            root.AttachToCurrentTest("sceneWithLight.gltf");
+        }
+
         [Test(Description ="Creates a model with a triangle mesh")]
         public void CreateSolidTriangleScene()
         {