-
Notifications
You must be signed in to change notification settings - Fork 5
/
ModelLoader.cs
294 lines (243 loc) · 10.3 KB
/
ModelLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ObjLoader.Loader.Loaders;
using UkooLabs.FbxSharpie;
using Voltium.Core.Devices.Shaders;
using Silk.NET;
using Silk.NET.Assimp;
using Voltium.Common;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using SimpScene = Silk.NET.Assimp.Scene;
using SimpMesh = Silk.NET.Assimp.Mesh;
using SimpFace = Silk.NET.Assimp.Face;
using SimpReturn = Silk.NET.Assimp.Return;
using Node = SharpGLTF.Schema2.Node;
using ModelRoot = SharpGLTF.Schema2.ModelRoot;
using System;
using System.Buffers;
namespace Voltium.ModelLoading
{
[ShaderInput]
[StructLayout(LayoutKind.Sequential)]
public partial struct TexturedVertex
{
public TexturedVertex(Vector3 position, Vector3 normal, Vector3 tangent, Vector2 texC)
{
Position = position;
Normal = normal;
Tangent = tangent;
TexC = texC;
}
public TexturedVertex(
float vertexX, float vertexY, float vertexZ,
float normalX, float normalY, float normalZ,
float tangentX, float tangentY, float tangentZ,
float texU, float texV
)
{
Position = new(vertexX, vertexY, vertexZ);
Normal = new(normalX, normalY, normalZ);
Tangent = new(tangentX, tangentY, tangentZ);
TexC = new(texU, texV);
}
public Vector3 Position;
public Vector3 Normal;
public Vector3 Tangent;
[InputLayout(Name = "TexCoord")]
public Vector2 TexC;
}
[ShaderInput]
[StructLayout(LayoutKind.Sequential)]
public partial struct ColorVertex
{
public ColorVertex(Vector3 position, Vector3 normal, Vector4 color)
{
Position = position;
Normal = normal;
Color = color;
}
public ColorVertex(
float vertexX, float vertexY, float vertexZ,
float normalX, float normalY, float normalZ,
float tangentX, float tangentY, float tangentZ,
float colorR, float colorG, float colorB, float colorA
)
{
Position = new(vertexX, vertexY, vertexZ);
Normal = new(normalX, normalY, normalZ);
Color = new(colorR, colorG, colorB, colorA);
}
public Vector3 Position;
public Vector3 Normal;
public Vector4 Color;
}
public struct Material
{
public Vector4 DiffuseAlbedo;
public Vector4 ReflectionFactor;
public float Shininess;
}
public readonly struct Scene
{
}
public struct Mesh<TVertex>
{
public readonly TVertex[] Vertices;
public readonly uint[] Indices;
public Material Material;
public Matrix4x4 World;
public Mesh(TVertex[] vertices, uint[] indices, in Material material = default, in Matrix4x4 world = default)
{
Vertices = vertices;
Indices = indices;
Material = material;
World = world;
if (World == default)
{
World = Matrix4x4.Identity;
}
}
}
/// <summary>
/// The type used for loading of 2 and 3D models
/// </summary>
public static class ModelLoader
{
private static readonly ObjLoaderFactory _factory = new ObjLoaderFactory();
private static readonly IObjLoader _loader = _factory.Create(new MaterialStreamProvider());
private static readonly Assimp _assimp = Assimp.GetApi();
private static MemoryHandle DiffuseKey = StringHelper.MarshalToUnmanagedAscii(Assimp.MaterialColorDiffuseBase);
private static MemoryHandle ShininessKey = StringHelper.MarshalToUnmanagedAscii(Assimp.MaterialShininessBase);
private static MemoryHandle ReflectionFactorKey = StringHelper.MarshalToUnmanagedAscii(Assimp.MaterialColorReflectiveBase);
// TODO improve
public static unsafe Mesh<TexturedVertex>[] Load(string filename)
{
using var ascii = StringHelper.MarshalToUnmanagedAscii(filename);
SimpScene* pScene = _assimp.ImportFile((byte*)ascii.Pointer, 0);
var meshes = new ArrayBuilder<Mesh<TexturedVertex>>();
for (var i = 0; i < pScene->MNumMeshes; i++)
{
SimpMesh* pMesh = pScene->MMeshes[i];
var vertices = new TexturedVertex[pMesh->MNumVertices];
var material = new Material();
var simpMaterial = pScene->MMaterials[pMesh->MMaterialIndex];
ThrowIfFailed(_assimp.GetMaterialColor(simpMaterial, (byte*)DiffuseKey.Pointer, 0, 0, &material.DiffuseAlbedo));
ThrowIfFailed(_assimp.GetMaterialFloatArray(simpMaterial, (byte*)ShininessKey.Pointer, 0, 0, &material.Shininess, null));
ThrowIfFailed(_assimp.GetMaterialColor(simpMaterial, (byte*)ReflectionFactorKey.Pointer, 0, 0, &material.ReflectionFactor));
for (var k = 0; k < pMesh->MNumVertices; k++)
{
var vertex = new TexturedVertex(
pMesh->MVertices[k],
pMesh->MNormals[k],
pMesh->MTangents[k],
ToVector2(pMesh->MTextureCoords_0[k])
);
vertices[k] = vertex;
}
var indices = new ArrayBuilder<uint>();
for (var j = 0; j < pMesh->MNumFaces; j++)
{
SimpFace /* <- insert mug of dylan */ pFace = pMesh->MFaces[j];
indices.Add(new Span<uint>(pFace.MIndices, (int)pFace.MNumIndices));
}
meshes.Add(new Mesh<TexturedVertex>(vertices, indices.MoveTo(), material));
}
return meshes.MoveTo();
static void ThrowIfFailed(SimpReturn @return)
{
if (@return == SimpReturn.ReturnSuccess)
{
return;
}
if (@return == SimpReturn.ReturnOutofmemory)
{
ThrowHelper.ThrowInsufficientMemoryException("'ReturnOutofmemory' returned by ASSIMP");
}
else
{
ThrowHelper.ThrowExternalException("ASSIMP returned a generic error code");
}
}
}
private static Vector2 ToVector2(Vector3 vec) => Unsafe.As<Vector3, Vector2>(ref vec);
public static Mesh<TexturedVertex>[] LoadGl_Old(string filename)
{
var m = ModelRoot.Load(filename);
var texturedObjs = new List<Mesh<TexturedVertex>>(m.LogicalMeshes.Count);
//Hemi.001
//Hemi
//Point.001
//Point
//Cube.002
//Gun
//hulle
//bullet
//bullet_light
var scene = m.DefaultScene;
var nodes = scene.VisualChildren;
IEnumerable<Node> Flatten(IEnumerable<Node> e) => e.SelectMany(c => Flatten(c.VisualChildren)).Concat(e);
var flat = Flatten(nodes).Where(n => !n.Name.Contains("Plane") && n.Mesh is not null);
foreach (var node in flat)
{
var world = node.WorldMatrix;
foreach (var mesh in node.Mesh.Primitives)
{
var glMat = mesh.Material;
Material mat = default;
float metal = 0;
foreach (var channel in glMat.Channels)
{
//BaseColor
//MetallicRoughness
//Normal
//Occlusion
//Emissive
switch (channel.Key)
{
case "BaseColor":
mat.DiffuseAlbedo = channel.Parameter;
break;
case "MetallicRoughness":
mat.Shininess = channel.Parameter.Y;
metal = channel.Parameter.Z;
break;
}
mat.ReflectionFactor = new Vector4(Vector3.Lerp(new(0.4f), Unsafe.As<Vector4, Vector3>(ref mat.DiffuseAlbedo), metal), 0);
mat.ReflectionFactor.W = mat.ReflectionFactor.X;
}
var positions = mesh.GetVertexAccessor("POSITION")?.AsVector3Array();
var normals = mesh.GetVertexAccessor("NORMAL")?.AsVector3Array();
var texCoords = mesh.GetVertexAccessor("TEXCOORD_0")?.AsVector2Array();
var colors = mesh.GetVertexAccessor("COLOR")?.AsColorArray();
var tangents = mesh.GetVertexAccessor("TANGENT")?.AsVector4Array();
var triangleIndices = mesh.GetTriangleIndices()?.ToArray();
var indices = triangleIndices?.SelectMany(i => new[] { i.A, i.B, i.C }).ToArray();
if (texCoords is not null)
{
var vertices = new TexturedVertex[positions?.Count ?? 0];
for (var i = 0; i < vertices.Length; i++)
{
Vector4 tangent = default; // tangents![i];
vertices![i] = new TexturedVertex(positions![i], normals![i], Unsafe.As<Vector4, Vector3>(ref tangent), texCoords![i]);
}
texturedObjs.Add(new(vertices, Unsafe.As<uint[]>(indices!), mat, world));
}
}
}
return texturedObjs.ToArray();
//foreach (var texture in m.LogicalTextures)
//{
// var image = texture.PrimaryImage;
// if (!image.Content.IsDds)
// {
// ThrowHelper.ThrowNotSupportedException("DDS only supported as of now");
// }
// var dds = TextureLoader.CreateTexture(image.Content.Content.ToArray() /* have to copy to get a ROM<byte> */, TexType.Dds);
//}
}
}
}