Browse Source

moving more APIs to toolkit

Vicente Penades 6 years ago
parent
commit
fcead7d292

+ 54 - 0
src/SharpGLTF.Toolkit/Schema2/LightExtensions.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Numerics;
+using System.Text;
+
+namespace SharpGLTF.Schema2
+{
+    public static partial class Toolkit
+    {
+        /// <summary>
+        /// Sets the cone angles for the <see cref="PunctualLightType.Spot" light/>.
+        /// </summary>
+        /// <param name="light">This <see cref="PunctualLight"/> instance.</param>
+        /// <param name="innerConeAngle">
+        /// Gets the Angle, in radians, from centre of spotlight where falloff begins.
+        /// Must be greater than or equal to 0 and less than outerConeAngle.
+        /// </param>
+        /// <param name="outerConeAngle">
+        /// Gets Angle, in radians, from centre of spotlight where falloff ends.
+        /// Must be greater than innerConeAngle and less than or equal to PI / 2.0.
+        /// </param>
+        /// <returns>This <see cref="PunctualLight"/> instance.</returns>
+        public static PunctualLight WithSpotCone(this PunctualLight light, float innerConeAngle, float outerConeAngle)
+        {
+            light.SetSpotCone(innerConeAngle, outerConeAngle);
+            return light;
+        }
+
+        /// <summary>
+        /// Defines the light color, intensity and range for the current <see cref="PunctualLight"/>.
+        /// </summary>
+        /// <param name="light">This <see cref="PunctualLight"/> instance.</param>
+        /// <param name="color">RGB value for light's color in linear space.</param>
+        /// <param name="intensity">
+        /// Brightness of light in. The units that this is defined in depend on the type of light.
+        /// point and spot lights use luminous intensity in candela (lm/sr) while directional
+        /// lights use illuminance in lux (lm/m2)
+        /// </param>
+        /// <param name="range">
+        /// Hint defining a distance cutoff at which the light's intensity may be considered
+        /// to have reached zero. Supported only for point and spot lights. Must be > 0.
+        /// When undefined, range is assumed to be infinite.
+        /// </param>
+        /// <returns>This <see cref="PunctualLight"/> instance.</returns>
+        public static PunctualLight WithColor(this PunctualLight light, Vector3 color, float intensity = 1, float range = 0)
+        {
+            light.Color = color;
+            light.Intensity = intensity;
+            light.Range = range;
+
+            return light;
+        }
+    }
+}

+ 33 - 0
src/SharpGLTF.Toolkit/Schema2/MaterialExtensions.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Numerics;
 using System.Text;
 
@@ -64,5 +65,37 @@ namespace SharpGLTF.Schema2
             material.InitializeUnlit();
             return material;
         }
+
+        /// <summary>
+        /// Creates or reuses an <see cref="Image"/> with the file set by <paramref name="filePath"/>
+        /// </summary>
+        /// <param name="root">The <see cref="ModelRoot"/> root instance.</param>
+        /// <param name="filePath">A valid file path pointing to a valid image</param>
+        /// <returns>A <see cref="Image"/> instance.</returns>
+        public static Image UseImageWithFile(this ModelRoot root, string filePath)
+        {
+            var content = System.IO.File.ReadAllBytes(filePath);
+
+            return root.UseImageWithContent(content);
+        }
+
+        /// <summary>
+        /// Creates or reuses an <see cref="Image"/> with the image content set by <paramref name="imageContent"/>
+        /// </summary>
+        /// <param name="root">The <see cref="ModelRoot"/> root instance.</param>
+        /// <param name="imageContent">A buffer containing the bytes of the image file.</param>
+        /// <returns>A <see cref="Image"/> instance.</returns>
+        public static Image UseImageWithContent(this ModelRoot root, Byte[] imageContent)
+        {
+            foreach (var img in root.LogicalImages)
+            {
+                var existingContent = img.GetImageContent();
+                if (Enumerable.SequenceEqual(existingContent, imageContent)) return img;
+            }
+
+            var image = root.CreateImage();
+            image.SetSatelliteContent(imageContent);
+            return image;
+        }
     }
 }

+ 1 - 5
src/SharpGLTF.Toolkit/SharpGLTF.Toolkit.csproj

@@ -58,9 +58,5 @@
   <ItemGroup>
     <ProjectReference Include="..\SharpGLTF\SharpGLTF.csproj" />
   </ItemGroup>
-
-  <ItemGroup>
-    <Folder Include="Debug\" />
-  </ItemGroup>
-
+  
 </Project>

+ 3 - 7
src/SharpGLTF/Schema2/glb.Images.cs

@@ -114,19 +114,17 @@ namespace SharpGLTF.Schema2
         /// Initializes this <see cref="Image"/> with an image loaded from a file.
         /// </summary>
         /// <param name="filePath">A valid path to an image file.</param>
-        /// <returns>This <see cref="Image"/> instance.</returns>
-        public Image WithSatelliteFile(string filePath)
+        public void SetSatelliteFile(string filePath)
         {
             var content = System.IO.File.ReadAllBytes(filePath);
-            return WithSatelliteContent(content);
+            SetSatelliteContent(content);
         }
 
         /// <summary>
         /// Initializes this <see cref="Image"/> with an image stored in a <see cref="Byte"/> array.
         /// </summary>
         /// <param name="content">A <see cref="Byte"/> array containing a PNG or JPEG image.</param>
-        /// <returns>This <see cref="Image"/> instance.</returns>
-        public Image WithSatelliteContent(Byte[] content)
+        public void SetSatelliteContent(Byte[] content)
         {
             Guard.NotNull(content, nameof(content));
 
@@ -141,8 +139,6 @@ namespace SharpGLTF.Schema2
             this._mimeType = null;
             this._bufferView = null;
             this._SatelliteImageContent = content;
-
-            return this;
         }
 
         /// <summary>

+ 2 - 7
src/SharpGLTF/Schema2/khr.lights.cs

@@ -67,8 +67,7 @@ namespace SharpGLTF.Schema2
         /// Gets Angle, in radians, from centre of spotlight where falloff ends.
         /// Must be greater than innerConeAngle and less than or equal to PI / 2.0.
         /// </param>
-        /// <returns>This <see cref="PunctualLight"/> instance.</returns>
-        public PunctualLight WithSpotCone(float innerConeAngle, float outerConeAngle)
+        public void SetSpotCone(float innerConeAngle, float outerConeAngle)
         {
             if (_spot == null) throw new InvalidOperationException($"Expected {PunctualLightType.Spot} but found {LightType}");
 
@@ -76,8 +75,6 @@ namespace SharpGLTF.Schema2
 
             _spot.InnerConeAngle = innerConeAngle;
             _spot.OuterConeAngle = outerConeAngle;
-
-            return this;
         }
 
         /// <summary>
@@ -94,13 +91,11 @@ namespace SharpGLTF.Schema2
         /// to have reached zero. Supported only for point and spot lights. Must be > 0.
         /// When undefined, range is assumed to be infinite.
         /// </param>
-        /// <returns>This <see cref="PunctualLight"/> instance.</returns>
-        public PunctualLight WithColor(Vector3 color, float intensity = 1, float range = 0)
+        public void SetColor(Vector3 color, float intensity = 1, float range = 0)
         {
             this.Color = color;
             this.Intensity = intensity;
             this.Range = range;
-            return this;
         }
 
         #endregion

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

@@ -175,7 +175,7 @@ namespace SharpGLTF.Schema2.Authoring
             // PBRMetallicRoughness has a "BaseColor" and a "Metallic" and a "Roughness" channels.
             primitive.Material
                 .FindChannel("BaseColor")
-                .SetTexture(0, model.CreateImage().WithSatelliteFile(imagePath) );
+                .SetTexture(0, model.UseImageWithFile(imagePath) );
             
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.gltf");