浏览代码

fleshing out SharpGLTF.Toolkit

Vicente Penades 6 年之前
父节点
当前提交
4bf2ce174d

+ 7 - 0
SharpGLTF.sln

@@ -23,6 +23,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{D7D51F42
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MeshBuffers", "src\MeshBuffers\MeshBuffers.csproj", "{1C93D96E-19DD-443B-8818-AAB14FEDA35D}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpGLTF.Toolkit", "src\SharpGLTF.Toolkit\SharpGLTF.Toolkit.csproj", "{41690879-1F91-4555-A40A-F67B01868D7E}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
 		{1C93D96E-19DD-443B-8818-AAB14FEDA35D}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{1C93D96E-19DD-443B-8818-AAB14FEDA35D}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{1C93D96E-19DD-443B-8818-AAB14FEDA35D}.Release|Any CPU.Build.0 = Release|Any CPU
+		{41690879-1F91-4555-A40A-F67B01868D7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{41690879-1F91-4555-A40A-F67B01868D7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{41690879-1F91-4555-A40A-F67B01868D7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{41690879-1F91-4555-A40A-F67B01868D7E}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -54,6 +60,7 @@ Global
 		{B1DA4F42-AB6A-4021-9989-674B1394E8A2} = {072B725F-773F-4751-9616-E9778897C1D2}
 		{4FCBB910-67D4-4628-9B2B-F5F2C8D92257} = {0CBF510D-D836-40BA-95EC-E93FDBB90632}
 		{1C93D96E-19DD-443B-8818-AAB14FEDA35D} = {0CBF510D-D836-40BA-95EC-E93FDBB90632}
+		{41690879-1F91-4555-A40A-F67B01868D7E} = {072B725F-773F-4751-9616-E9778897C1D2}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {1D7BBAD9-834C-4981-AC96-0AA5226FC43F}

+ 42 - 0
src/SharpGLTF.Toolkit/Collections/VertexColumn.cs

@@ -0,0 +1,42 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SharpGLTF.Collections
+{
+    public class VertexColumn<T> : IReadOnlyList<T>
+        where T : struct
+    {
+        #region data
+
+        private readonly List<T> _Vertices = new List<T>();
+        private readonly Dictionary<T, int> _VertexCache = new Dictionary<T, int>();
+
+        #endregion
+
+        #region API
+
+        public T this[int index] => _Vertices[index];
+
+        public int Count => _Vertices.Count;
+
+        public IEnumerator<T> GetEnumerator() { return _Vertices.GetEnumerator(); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return _Vertices.GetEnumerator(); }
+
+        public int Use(T v)
+        {
+            if (_VertexCache.TryGetValue(v, out int index)) return index;
+
+            index = _Vertices.Count;
+
+            _Vertices.Add(v);
+            _VertexCache[v] = index;
+
+            return index;
+        }
+
+        #endregion
+    }
+}

+ 16 - 9
tests/SharpGLTF.Tests/Schema2/Authoring/InterleavedMeshBuilder.cs → src/SharpGLTF.Toolkit/Geometry/InterleavedMeshBuilder.cs

@@ -3,9 +3,13 @@ using System.Collections.Generic;
 using System.Numerics;
 using System.Text;
 
-namespace SharpGLTF.Schema2.Authoring
+namespace SharpGLTF.Geometry
 {
-    class InterleavedMeshBuilder<TVertex, TMaterial> where TVertex : struct
+    using Collections;
+    using Schema2;
+
+    public class InterleavedMeshBuilder<TVertex, TMaterial>
+        where TVertex : struct
     {
         #region data
 
@@ -44,7 +48,7 @@ namespace SharpGLTF.Schema2.Authoring
             indices.Add(aa);
             indices.Add(bb);
             indices.Add(cc);
-        }        
+        }
 
         public void CopyToNode(Node dstNode, Func<TMaterial, Material> materialEvaluator)
         {
@@ -52,7 +56,7 @@ namespace SharpGLTF.Schema2.Authoring
             CopyToMesh(dstNode.Mesh, materialEvaluator);
         }
 
-        public void CopyToMesh(Mesh dstMesh, Func<TMaterial, Material> materialEvaluator)
+        public void CopyToMesh(Schema2.Mesh dstMesh, Func<TMaterial, Material> materialEvaluator)
         {
             var root = dstMesh.LogicalParent;
 
@@ -61,12 +65,14 @@ namespace SharpGLTF.Schema2.Authoring
 
             // create vertex buffer
             int byteStride = attributes[0].ByteStride;
-            var vbuffer = root.UseBufferView(new Byte[byteStride * _Vertices.Count], byteStride, BufferMode.ARRAY_BUFFER);
+            var vbytes = new Byte[byteStride * _Vertices.Count];
+
+            var vbuffer = root.UseBufferView(new ArraySegment<byte>(vbytes), byteStride, BufferMode.ARRAY_BUFFER);
 
             // create vertex accessors
             var vertexAccessors = new Dictionary<String, Accessor>();
 
-            foreach(var attribute in attributes)
+            foreach (var attribute in attributes)
             {
                 var accessor = root.CreateAccessor(attribute.Name);
 
@@ -77,12 +83,13 @@ namespace SharpGLTF.Schema2.Authoring
                 if (attributeType == typeof(Vector4)) accessor.WithVertexData(vbuffer, attribute.ByteOffset, _GetVector4Column(_Vertices, attribute.Name), attribute.Encoding, attribute.Normalized);
 
                 vertexAccessors[attribute.Name.ToUpper()] = accessor;
-            }            
+            }
 
             foreach (var kvp in _Indices)
             {
                 // create index buffer
-                var ibuffer = root.UseBufferView(new Byte[4 * kvp.Value.Count], 0, BufferMode.ELEMENT_ARRAY_BUFFER);
+                var ibytes = new Byte[4 * kvp.Value.Count];
+                var ibuffer = root.UseBufferView(new ArraySegment<byte>(ibytes), 0, BufferMode.ELEMENT_ARRAY_BUFFER);
 
                 var indices = root
                     .CreateAccessor("Indices")
@@ -182,5 +189,5 @@ namespace SharpGLTF.Schema2.Authoring
         }
 
         #endregion
-    }    
+    }
 }

+ 28 - 0
src/SharpGLTF.Toolkit/Geometry/VertexTypes/SkinnedVertices.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Numerics;
+using System.Text;
+
+namespace SharpGLTF.Geometry.VertexTypes
+{
+    public struct SkinnedPosition
+    {
+        public SkinnedPosition(float px, float py, float pz, int jointIndex)
+        {
+            Position = new Vector3(px, py, pz);
+            Joints_0 = new Vector4(jointIndex);
+            Weights_0 = Vector4.UnitX;
+        }
+
+        public SkinnedPosition(float px, float py, float pz, int jointIndex1, int jointIndex2)
+        {
+            Position = new Vector3(px, py, pz);
+            Joints_0 = new Vector4(jointIndex1, jointIndex2, 0, 0);
+            Weights_0 = new Vector4(0.5f, 0.5f, 0, 0);
+        }
+
+        public Vector3 Position;
+        public Vector4 Joints_0;
+        public Vector4 Weights_0;
+    }
+}

+ 19 - 0
src/SharpGLTF.Toolkit/Geometry/VertexTypes/StaticVertices.cs

@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Numerics;
+using System.Text;
+
+namespace SharpGLTF.Geometry.VertexTypes
+{
+    public struct StaticPositionNormal
+    {
+        public StaticPositionNormal(float px, float py, float pz, float nx, float ny, float nz)
+        {
+            Position = new Vector3(px, py, pz);
+            Normal = Vector3.Normalize(new Vector3(nx, ny, nz));
+        }
+
+        public Vector3 Position;
+        public Vector3 Normal;
+    }
+}

+ 58 - 0
src/SharpGLTF.Toolkit/SharpGLTF.Toolkit.csproj

@@ -0,0 +1,58 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netstandard2.0</TargetFramework>
+    <AssemblyName>SharpGLTF.Toolkit</AssemblyName>
+    <RootNamespace>SharpGLTF</RootNamespace>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
+    <PackageProjectUrl>https://github.com/vpenades/SharpGLTF</PackageProjectUrl>
+    <RepositoryUrl>https://github.com/vpenades/SharpGLTF</RepositoryUrl>
+    <RepositoryType>git</RepositoryType>
+    <Authors>Vicente Penades</Authors>
+    <Description>SharpGLTF.Toolkit extends SharpGLTF library with useful tools and helper classes.</Description>
+    <Copyright>Copyright (c) 2019 Vicente Penades</Copyright>
+    <PackageTags>C# glTF 3D</PackageTags>
+    <Version>1.0.0-alpha0005</Version>
+    <LangVersion>latest</LangVersion>
+    <DebugSymbols>true</DebugSymbols>
+    <PackageLicenseFile>LICENSE</PackageLicenseFile>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <None Include="..\..\LICENSE" Pack="true" PackagePath="" />
+  </ItemGroup>
+  
+  <ItemGroup>
+    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
+      <_Parameter1>SharpGLTF.Tests</_Parameter1>
+    </AssemblyAttribute>
+  </ItemGroup>
+
+  <PropertyGroup>
+    <DocumentationFile>bin\Docs\SharpGLTF.Toolkit.xml</DocumentationFile>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
+    <PackageReference Include="System.Memory" Version="4.5.2" />
+    <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
+    <PackageReference Include="StyleCop.Analyzers" Version="1.0.2">
+      <PrivateAssets>all</PrivateAssets>
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
+    </PackageReference>
+  </ItemGroup>
+
+  <PropertyGroup>
+    <CodeAnalysisRuleSet>..\..\SharpGLTF.ruleset</CodeAnalysisRuleSet>    
+    <PackageIconUrl>https://raw.githubusercontent.com/vpenades/SharpGLTF/master/build/Icons/glTF2Sharp.png</PackageIconUrl>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <AdditionalFiles Include="..\..\stylecop.json" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\SharpGLTF\SharpGLTF.csproj" />
+  </ItemGroup>
+
+</Project>

+ 34 - 57
tests/SharpGLTF.Tests/Schema2/Authoring/CreateModelTests.cs

@@ -5,8 +5,14 @@ using System.Text;
 
 using NUnit.Framework;
 
+
 namespace SharpGLTF.Schema2.Authoring
 {
+    using Geometry;
+
+    using STATICVERTEX = Geometry.VertexTypes.StaticPositionNormal;
+    using SKINNEDVERTEX = Geometry.VertexTypes.SkinnedPosition;
+
     [TestFixture]
     public class CreateModelTests
     {
@@ -210,17 +216,7 @@ namespace SharpGLTF.Schema2.Authoring
         }
 
 
-        struct mySimpleVertex
-        {
-            public mySimpleVertex(float px, float py, float pz, float nx, float ny, float nz)
-            {
-                Position = new Vector3(px, py, pz);
-                Normal = Vector3.Normalize(new Vector3(nx, ny, nz));
-            }
-
-            public Vector3 Position;
-            public Vector3 Normal;
-        }
+        
 
         [Test(Description = "Creates an interleaved scene using a mesh builder helper class")]
         public void CreateInterleavedMeshBuilderScene()
@@ -228,12 +224,12 @@ namespace SharpGLTF.Schema2.Authoring
             TestContext.CurrentContext.AttachShowDirLink();
             TestContext.CurrentContext.AttachGltfValidatorLink();
 
-            var meshBuilder = new InterleavedMeshBuilder<mySimpleVertex, Vector4>();
+            var meshBuilder = new InterleavedMeshBuilder<STATICVERTEX, Vector4>();
 
-            var v1 = new mySimpleVertex(-10, 10, 0, -10, 10, 15);
-            var v2 = new mySimpleVertex( 10, 10, 0, 10, 10, 15);
-            var v3 = new mySimpleVertex( 10,-10, 0, 10, -10, 15);
-            var v4 = new mySimpleVertex(-10,-10, 0, -10, -10, 15);            
+            var v1 = new STATICVERTEX(-10, 10, 0, -10, 10, 15);
+            var v2 = new STATICVERTEX( 10, 10, 0, 10, 10, 15);
+            var v3 = new STATICVERTEX( 10,-10, 0, 10, -10, 15);
+            var v4 = new STATICVERTEX(-10,-10, 0, -10, -10, 15);            
             meshBuilder.AddPolygon(new Vector4(1, 1, 1, 1), v1, v2, v3, v4);
 
             var model = ModelRoot.CreateModel();
@@ -261,12 +257,12 @@ namespace SharpGLTF.Schema2.Authoring
             TestContext.CurrentContext.AttachShowDirLink();
             TestContext.CurrentContext.AttachGltfValidatorLink();
 
-            var meshBuilder = new InterleavedMeshBuilder<mySimpleVertex, Vector4>();
+            var meshBuilder = new InterleavedMeshBuilder<STATICVERTEX, Vector4>();
 
-            var v1 = new mySimpleVertex(-10, 10, 0, -10, 10, 15);
-            var v2 = new mySimpleVertex(10, 10, 0, 10, 10, 15);
-            var v3 = new mySimpleVertex(10, -10, 0, 10, -10, 15);
-            var v4 = new mySimpleVertex(-10, -10, 0, -10, -10, 15);
+            var v1 = new STATICVERTEX(-10, 10, 0, -10, 10, 15);
+            var v2 = new STATICVERTEX(10, 10, 0, 10, 10, 15);
+            var v3 = new STATICVERTEX(10, -10, 0, 10, -10, 15);
+            var v4 = new STATICVERTEX(-10, -10, 0, -10, -10, 15);
             meshBuilder.AddPolygon(new Vector4(1, 1, 1, 1), v1, v2, v3, v4);
 
             var model = ModelRoot.CreateModel();
@@ -302,26 +298,7 @@ namespace SharpGLTF.Schema2.Authoring
             model.AttachToCurrentTest("result.gltf");
         }
 
-        struct mySkinnedVertex
-        {
-            public mySkinnedVertex(float px, float py, float pz, int jointIndex)
-            {
-                Position = new Vector3(px, py, pz);
-                Joints_0 = new Vector4(jointIndex);
-                Weights_0 = Vector4.UnitX;
-            }
-
-            public mySkinnedVertex(float px, float py, float pz, int jointIndex1, int jointIndex2)
-            {
-                Position = new Vector3(px, py, pz);
-                Joints_0 = new Vector4(jointIndex1, jointIndex2,0,0);
-                Weights_0 = new Vector4(0.5f, 0.5f, 0, 0);
-            }
-
-            public Vector3 Position;            
-            public Vector4 Joints_0;
-            public Vector4 Weights_0;
-        }
+        
 
         [Test(Description = "Creates a skinned animated scene using a mesh builder helper class")]
         public void CreateSkinnedAnimatedMeshBuilderScene()
@@ -346,22 +323,22 @@ namespace SharpGLTF.Schema2.Authoring
             snode.Skin.BindJoints(jnode1, jnode2, jnode3);
 
             // create the mesh
-            var meshBuilder = new InterleavedMeshBuilder<mySkinnedVertex, Vector4>();
-
-            var v1 = new mySkinnedVertex(-10, 0, +10, 0);
-            var v2 = new mySkinnedVertex(+10, 0, +10, 0);
-            var v3 = new mySkinnedVertex(+10, 0, -10, 0);
-            var v4 = new mySkinnedVertex(-10, 0, -10, 0);
-
-            var v5 = new mySkinnedVertex(-10, 40, +10, 0, 1);
-            var v6 = new mySkinnedVertex(+10, 40, +10, 0, 1);
-            var v7 = new mySkinnedVertex(+10, 40, -10, 0, 1);
-            var v8 = new mySkinnedVertex(-10, 40, -10, 0, 1);
-
-            var v9  = new mySkinnedVertex(-5, 80, +5, 2);
-            var v10 = new mySkinnedVertex(+5, 80, +5, 2);
-            var v11 = new mySkinnedVertex(+5, 80, -5, 2);
-            var v12 = new mySkinnedVertex(-5, 80, -5, 2);
+            var meshBuilder = new InterleavedMeshBuilder<SKINNEDVERTEX, Vector4>();
+
+            var v1 = new SKINNEDVERTEX(-10, 0, +10, 0);
+            var v2 = new SKINNEDVERTEX(+10, 0, +10, 0);
+            var v3 = new SKINNEDVERTEX(+10, 0, -10, 0);
+            var v4 = new SKINNEDVERTEX(-10, 0, -10, 0);
+
+            var v5 = new SKINNEDVERTEX(-10, 40, +10, 0, 1);
+            var v6 = new SKINNEDVERTEX(+10, 40, +10, 0, 1);
+            var v7 = new SKINNEDVERTEX(+10, 40, -10, 0, 1);
+            var v8 = new SKINNEDVERTEX(-10, 40, -10, 0, 1);
+
+            var v9  = new SKINNEDVERTEX(-5, 80, +5, 2);
+            var v10 = new SKINNEDVERTEX(+5, 80, +5, 2);
+            var v11 = new SKINNEDVERTEX(+5, 80, -5, 2);
+            var v12 = new SKINNEDVERTEX(-5, 80, -5, 2);
 
             meshBuilder.AddPolygon(new Vector4(1, 0, 1, 1), v1, v2, v6, v5);
             meshBuilder.AddPolygon(new Vector4(1, 0, 1, 1), v2, v3, v7, v6);

+ 2 - 0
tests/SharpGLTF.Tests/Schema2/Authoring/SimpleMeshBuilder.cs

@@ -4,6 +4,8 @@ using System.Numerics;
 using System.Text;
 using System.Linq;
 
+using SharpGLTF.Collections;
+
 namespace SharpGLTF.Schema2.Authoring
 {
     class SimpleSceneBuilder<TMaterial>

+ 1 - 0
tests/SharpGLTF.Tests/SharpGLTF.Tests.csproj

@@ -17,6 +17,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\..\src\MeshBuffers\MeshBuffers.csproj" />
+    <ProjectReference Include="..\..\src\SharpGLTF.Toolkit\SharpGLTF.Toolkit.csproj" />
     <ProjectReference Include="..\..\src\SharpGLTF\SharpGLTF.csproj" />
   </ItemGroup>
 

+ 3 - 35
tests/SharpGLTF.Tests/WavefrontWriter.cs

@@ -6,43 +6,11 @@ using System.Text;
 
 using static System.FormattableString;
 
-namespace SharpGLTF
-{
-
-    class VertexColumn<T> : IReadOnlyList<T>
-        where T:struct
-    {
-        #region data
-
-        private readonly List<T> _Vertices = new List<T>();
-        private readonly Dictionary<T, int> _VertexCache = new Dictionary<T, int>();
-
-        #endregion
-
-        #region API
-
-        public T this[int index] => _Vertices[index];
-
-        public int Count => _Vertices.Count;
 
-        public IEnumerator<T> GetEnumerator() { return _Vertices.GetEnumerator(); }
 
-        IEnumerator IEnumerable.GetEnumerator() { return _Vertices.GetEnumerator(); }
-
-        public int Use(T v)
-        {
-            if (_VertexCache.TryGetValue(v, out int index)) return index;
-
-            index = _Vertices.Count;
-
-            _Vertices.Add(v);
-            _VertexCache[v] = index;
-
-            return index;
-        }
-
-        #endregion
-    }
+namespace SharpGLTF
+{
+    using Collections;
 
     /// <summary>
     /// Tiny wavefront object writer