-
Notifications
You must be signed in to change notification settings - Fork 9
/
GltfLoader.cs
228 lines (189 loc) · 7.63 KB
/
GltfLoader.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using VGltf;
using VGltf.Unity;
namespace VGltfExamples.GltfExamples
{
public sealed class GltfLoader : MonoBehaviour
{
[SerializeField] Dropdown filePathInput;
[SerializeField] Button loadButton;
[SerializeField] Button unloadButton;
[SerializeField] Button exportButton;
sealed class GltfResource : IDisposable
{
public IImporterContext Context;
public GameObject Go;
public void Dispose()
{
if (Go != null)
{
GameObject.Destroy(Go);
}
Context?.Dispose();
}
}
readonly List<GltfResource> _modelResources = new List<GltfResource>();
readonly List<string[]> _modelLocs = new List<string[]>
{
new string[]{"TextureLinearInterpolationTest", "glTF-Binary", "TextureLinearInterpolationTest.glb"},
new string[]{"TextureEncodingTest", "glTF-Binary", "TextureEncodingTest.glb"},
new string[]{"NormalTangentTest", "glTF-Binary", "NormalTangentTest.glb"},
new string[]{"NormalTangentMirrorTest", "glTF-Binary", "NormalTangentMirrorTest.glb"},
new string[]{"MetalRoughSpheres", "glTF-Binary", "MetalRoughSpheres.glb"},
new string[]{"AlphaBlendModeTest", "glTF-Binary", "AlphaBlendModeTest.glb"},
new string[]{"EmissiveStrengthTest", "glTF-Binary", "EmissiveStrengthTest.glb"},
new string[]{"WaterBottle", "glTF-Binary", "WaterBottle.glb"},
new string[]{"BoomBox", "glTF-Binary", "BoomBox.glb"},
new string[]{"NormalTest02", "normal_test_02.glb"},
};
void Start()
{
filePathInput.AddOptions(_modelLocs.Select(xs => new Dropdown.OptionData(xs.Last())).ToList());
loadButton.onClick.AddListener(UIOnLoadButtonClick);
unloadButton.onClick.AddListener(UIOnUnloadButtonClick);
exportButton.onClick.AddListener(UIOnExportButtonClick);
}
// Update is called once per frame
void Update()
{
}
void OnDestroy()
{
foreach (var disposable in _modelResources)
{
disposable.Dispose();
}
}
async UniTask<GltfResource> LoadGltf(string filePath, string name, System.Threading.CancellationToken ct)
{
// Read the glTF container.
// Task.Run is used to avoid blocking the main thread.
// GltfContainer is not dependent on Unity, so it can be used in other threads not only in the main thread.
var gltfContainer = await Task.Run(() =>
{
using (var fs = new FileStream(filePath, FileMode.Open))
{
return GltfContainer.FromGlb(fs);
}
}, ct);
// GltfResource is a wrapper of the resources only used in examples.
// It is used for avoid resource leaks when an exception occurs.
var res = new GltfResource();
try
{
// Create a GameObject that points to root nodes in the glTF scene.
// GameObjects of the glTF's child node will be created under the this object.
var go = new GameObject();
go.name = name;
res.Go = go;
// Create a TimeSlicer for Unity.
// TimeSlicer is used to control the time spent in the main thread.
var timeSlicer = new Common.TimeSlicer();
// Create a glTF Importer for Unity.
// The resources will be stored in the Context in this Importer.
// Resources can be released by calling Dispose of the Importer (or the Context).
using (var gltfImporter = new Importer(gltfContainer, timeSlicer))
{
// Load the Scene.
// ImportSceneNodes moves ownership of the Context from the Importer, so resources will not be released when the Importer is disposed.
// The Context must be disposed by the caller.
res.Context = await gltfImporter.ImportSceneNodes(go, ct);
}
}
catch (Exception)
{
res.Dispose();
throw;
}
return res;
}
UniTask ExportGltf(string filePath, GameObject go)
{
try
{
// Write the glTF container (unity-independent)
var gltfContainer = default(GltfContainer);
using (var gltfExporter = new Exporter(new Exporter.Config
{
IncludeRootObject = false,
}))
{
gltfExporter.ExportGameObjectAsScene(go);
gltfContainer = gltfExporter.IntoGlbContainer();
}
using (var fs = new FileStream(filePath, FileMode.Create))
{
GltfContainer.ToGlb(fs, gltfContainer);
}
Debug.Log($"Exported!: {filePath}");
}
catch (Exception e)
{
Debug.LogException(e);
}
return UniTask.CompletedTask;
}
// UI
public void UIOnLoadButtonClick()
{
UIOnLoadButtonClickAsync().Forget();
}
public async UniTaskVoid UIOnLoadButtonClickAsync()
{
var p0 = Common.MemoryProfile.Now;
DebugLogProfile(p0);
var loc = _modelLocs[filePathInput.value];
var filePath = SampleAssetPath(loc); // TODO: Support Android
var res = await LoadGltf(filePath, "glTF", System.Threading.CancellationToken.None);
_modelResources.Insert(0, res);
var p1 = Common.MemoryProfile.Now;
DebugLogProfile(p1, p0);
}
public void UIOnUnloadButtonClick()
{
if (_modelResources.Count == 0)
{
return;
}
var p0 = Common.MemoryProfile.Now;
DebugLogProfile(p0);
var head = _modelResources[0];
_modelResources.RemoveAt(0);
head.Dispose();
var p1 = Common.MemoryProfile.Now;
DebugLogProfile(p1, p0);
}
public void UIOnExportButtonClick()
{
if (_modelResources.Count == 0)
{
return;
}
var res = _modelResources[0];
ExportGltf("out.glb", res.Go).Forget();
}
void DebugLogProfile(Common.MemoryProfile now, Common.MemoryProfile prev = null)
{
Debug.Log($"----------");
Debug.Log($"(totalReservedMB, totalAllocatedMB, totalUnusedReservedMB)");
Debug.Log($"({now.TotalReservedMB}MB, {now.TotalAllocatedMB}MB, {now.TotalUnusedReservedMB}MB");
if (prev != null)
{
Debug.Log($"delta ({now.TotalReservedMB - prev.TotalReservedMB}MB, {now.TotalAllocatedMB - prev.TotalAllocatedMB}MB, {now.TotalUnusedReservedMB - prev.TotalUnusedReservedMB}MB)");
}
}
static string SampleAssetPath(params string[] names)
{
var a = new List<string> { Application.streamingAssetsPath, "SampleModels" };
a.AddRange(names);
return Path.Combine(a.ToArray());
}
}
}