-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathModelAsset.cs
64 lines (55 loc) · 2.25 KB
/
ModelAsset.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using DirectX12GameEngine.Core.Assets;
using DirectX12GameEngine.Graphics;
using DirectX12GameEngine.Rendering;
using DirectX12GameEngine.Rendering.Materials;
using Microsoft.Extensions.DependencyInjection;
namespace DirectX12GameEngine.Assets
{
[AssetContentType(typeof(Model))]
public class ModelAsset : AssetWithSource<Model>
{
public IList<Material> Materials { get; } = new List<Material>();
public async override Task CreateAssetAsync(Model model, IServiceProvider services)
{
IContentManager contentManager = services.GetRequiredService<IContentManager>();
GraphicsDevice device = services.GetRequiredService<GraphicsDevice>();
if (device is null) throw new InvalidOperationException();
string extension = Path.GetExtension(Source);
if (extension == ".glb")
{
model.Materials.Clear();
model.Meshes.Clear();
using Stream stream = await contentManager.FileProvider.OpenStreamAsync(Source, FileMode.Open, FileAccess.Read);
GltfModelLoader modelLoader = await GltfModelLoader.CreateAsync(device, stream);
var meshes = await modelLoader.GetMeshesAsync();
foreach (Mesh mesh in meshes)
{
model.Meshes.Add(mesh);
}
if (Materials.Count > 0)
{
foreach (Material material in Materials)
{
model.Materials.Add(material);
}
}
else
{
foreach (MaterialAttributes attributes in await modelLoader.GetMaterialAttributesAsync())
{
Material material = await Material.CreateAsync(device, new MaterialDescriptor { Id = Id, Attributes = attributes }, contentManager);
model.Materials.Add(material);
}
}
}
else
{
throw new NotSupportedException("This file type is not supported.");
}
}
}
}