Skip to content

Commit b90ef62

Browse files
committed
Initial implementation for creating a triangle asset
1 parent 88a1fc6 commit b90ef62

File tree

11 files changed

+369
-0
lines changed

11 files changed

+369
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
################################################################################
2+
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
3+
################################################################################
4+
5+
/Source/.vs
6+
/Source/bin
7+
/Source/obj
8+
/Source/packages

Source/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

Source/AssetGenerator.csproj

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3742898F-26BA-466B-A100-869B86F19F1A}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>AssetGenerator</RootNamespace>
10+
<AssemblyName>AssetGenerator</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="glTFLoader, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
36+
<HintPath>packages\glTFLoader.2.0.0\lib\net452\glTFLoader.dll</HintPath>
37+
</Reference>
38+
<Reference Include="glTFLoader_Shared, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
39+
<HintPath>packages\glTFLoader.2.0.0\lib\net452\glTFLoader_Shared.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
42+
<HintPath>packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
43+
</Reference>
44+
<Reference Include="System" />
45+
<Reference Include="System.Core" />
46+
<Reference Include="System.Xml.Linq" />
47+
<Reference Include="System.Data.DataSetExtensions" />
48+
<Reference Include="Microsoft.CSharp" />
49+
<Reference Include="System.Data" />
50+
<Reference Include="System.Net.Http" />
51+
<Reference Include="System.Xml" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<Compile Include="Attributes.cs" />
55+
<Compile Include="Common.cs" />
56+
<Compile Include="Data.cs" />
57+
<Compile Include="Materials.cs" />
58+
<Compile Include="Program.cs" />
59+
<Compile Include="Properties\AssemblyInfo.cs" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<None Include="App.config" />
63+
<None Include="packages.config" />
64+
</ItemGroup>
65+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
66+
</Project>

Source/AssetGenerator.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26403.7
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetGenerator", "AssetGenerator.csproj", "{3742898F-26BA-466B-A100-869B86F19F1A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3742898F-26BA-466B-A100-869B86F19F1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3742898F-26BA-466B-A100-869B86F19F1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3742898F-26BA-466B-A100-869B86F19F1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3742898F-26BA-466B-A100-869B86F19F1A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

Source/Attributes.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace AssetGenerator
4+
{
5+
[AttributeUsage(AttributeTargets.Class)]
6+
class AssetGroupAttribute : Attribute
7+
{
8+
public string Folder { get; private set; }
9+
10+
public AssetGroupAttribute(string folder)
11+
{
12+
this.Folder = folder;
13+
}
14+
}
15+
16+
[AttributeUsage(AttributeTargets.Method)]
17+
class AssetAttribute : Attribute
18+
{
19+
public string Name { get; private set; }
20+
21+
public AssetAttribute(string name)
22+
{
23+
this.Name = name;
24+
}
25+
}
26+
}

Source/Common.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using glTFLoader.Schema;
2+
using System.Collections.Generic;
3+
4+
namespace AssetGenerator
5+
{
6+
internal class Common
7+
{
8+
public static void SingleTriangle(Gltf gltf, Data geometryData)
9+
{
10+
var positions = new[]
11+
{
12+
new Vector3( 0.0f, 0.0f, 0.0f),
13+
new Vector3(-1.0f, 0.0f, 0.0f),
14+
new Vector3( 0.0f, 1.0f, 0.0f),
15+
};
16+
17+
geometryData.Writer.Write(positions);
18+
19+
gltf.Buffers = new[]
20+
{
21+
new Buffer
22+
{
23+
Uri = geometryData.Name,
24+
ByteLength = sizeof(float) * 3 * positions.Length,
25+
}
26+
};
27+
28+
gltf.BufferViews = new[]
29+
{
30+
new BufferView
31+
{
32+
Buffer = 0,
33+
ByteLength = sizeof(float) * 3 * positions.Length,
34+
}
35+
};
36+
37+
gltf.Accessors = new[]
38+
{
39+
new Accessor
40+
{
41+
BufferView = 0,
42+
ComponentType = Accessor.ComponentTypeEnum.FLOAT,
43+
Count = positions.Length,
44+
Type = Accessor.TypeEnum.VEC3,
45+
Max = new[] { 1.0f, 1.0f, 0.0f },
46+
Min = new[] { 0.0f, 0.0f, 0.0f },
47+
}
48+
};
49+
50+
gltf.Meshes = new[]
51+
{
52+
new Mesh
53+
{
54+
Primitives = new[]
55+
{
56+
new MeshPrimitive
57+
{
58+
Attributes = new Dictionary<string, int>
59+
{
60+
{ "POSITION", 0 },
61+
}
62+
}
63+
},
64+
}
65+
};
66+
67+
gltf.Nodes = new[]
68+
{
69+
new Node
70+
{
71+
Mesh = 0
72+
}
73+
};
74+
75+
gltf.Scenes = new[]
76+
{
77+
new Scene
78+
{
79+
Nodes = new[] { 0 }
80+
}
81+
};
82+
83+
gltf.Scene = 0;
84+
}
85+
}
86+
}

Source/Data.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace AssetGenerator
6+
{
7+
internal class Data
8+
{
9+
public string Name { get; private set; }
10+
public BinaryWriter Writer { get; private set; }
11+
12+
public Data(string name)
13+
{
14+
this.Name = name;
15+
this.Writer = new BinaryWriter(new MemoryStream());
16+
}
17+
}
18+
19+
internal struct Vector3
20+
{
21+
public float x;
22+
public float y;
23+
public float z;
24+
25+
public Vector3(float x, float y, float z)
26+
{
27+
this.x = x;
28+
this.y = y;
29+
this.z = z;
30+
}
31+
}
32+
33+
internal static class BinaryWriterExtensions
34+
{
35+
public static void Write(this BinaryWriter writer, Vector3 value)
36+
{
37+
writer.Write(value.x);
38+
writer.Write(value.y);
39+
writer.Write(value.z);
40+
}
41+
42+
public static void Write(this BinaryWriter writer, IEnumerable<Vector3> values)
43+
{
44+
values.ForEach(value => writer.Write(value));
45+
}
46+
}
47+
48+
internal static class EnumerableExtensions
49+
{
50+
public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
51+
{
52+
foreach (var value in values)
53+
{
54+
action(value);
55+
}
56+
}
57+
}
58+
}

Source/Materials.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using glTFLoader.Schema;
2+
using System.Collections.Generic;
3+
4+
namespace AssetGenerator
5+
{
6+
[AssetGroup("Materials")]
7+
internal static class Materials
8+
{
9+
[Asset("Test")]
10+
public static void Test(string name, Gltf gltf, List<Data> dataList)
11+
{
12+
var geometryData = new Data(name + ".bin");
13+
dataList.Add(geometryData);
14+
15+
Common.SingleTriangle(gltf, geometryData);
16+
17+
gltf.Materials = new[]
18+
{
19+
new Material
20+
{
21+
PbrMetallicRoughness = new MaterialPbrMetallicRoughness
22+
{
23+
BaseColorFactor = new[] { 1.0f, 0.0f, 0.0f, 0.0f },
24+
}
25+
}
26+
};
27+
28+
gltf.Meshes[0].Primitives[0].Material = 0;
29+
}
30+
}
31+
}

Source/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using glTFLoader.Schema;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Reflection;
5+
6+
namespace AssetGenerator
7+
{
8+
internal class Program
9+
{
10+
private static void Main(string[] args)
11+
{
12+
var executingAssembly = Assembly.GetExecutingAssembly();
13+
var executingAssemblyFolder = Path.GetDirectoryName(executingAssembly.Location);
14+
15+
foreach (var type in executingAssembly.GetTypes())
16+
{
17+
var assetGroupAttribute = type.GetCustomAttribute<AssetGroupAttribute>();
18+
if (assetGroupAttribute != null)
19+
{
20+
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
21+
{
22+
var assetAttribute = method.GetCustomAttribute<AssetAttribute>();
23+
if (assetAttribute != null)
24+
{
25+
var gltf = new Gltf
26+
{
27+
Asset = new Asset
28+
{
29+
Generator = "glTF Asset Generator " + Assembly.GetExecutingAssembly().GetName().Version,
30+
Version = "2.0",
31+
}
32+
};
33+
34+
var dataList = new List<Data>();
35+
36+
method.Invoke(null, new object[] { assetAttribute.Name, gltf, dataList });
37+
38+
var assetFolder = Path.Combine(executingAssemblyFolder, assetGroupAttribute.Folder);
39+
Directory.CreateDirectory(assetFolder);
40+
41+
var assetFile = Path.Combine(assetFolder, assetAttribute.Name + ".gltf");
42+
glTFLoader.Interface.SaveModel(gltf, assetFile);
43+
44+
foreach (var data in dataList)
45+
{
46+
data.Writer.Flush();
47+
48+
var dataFile = Path.Combine(assetFolder, data.Name);
49+
File.WriteAllBytes(dataFile, ((MemoryStream)data.Writer.BaseStream).ToArray());
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}

Source/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System.Reflection;
2+
3+
[assembly: AssemblyTitle("AssetGenerator.exe")]
4+
[assembly: AssemblyVersion("0.1.0.0")]

Source/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="glTFLoader" version="2.0.0" targetFramework="net452" />
4+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
5+
</packages>

0 commit comments

Comments
 (0)