-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportSamples.cs
132 lines (105 loc) · 4.38 KB
/
ExportSamples.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
// SPDX-FileCopyrightText: 2024 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0
using System.Threading.Tasks;
using UnityEngine.Serialization;
namespace GLTFast.Documentation.Examples
{
using UnityEngine;
using GLTFast;
using Export;
using Logging;
class ExportSamples : MonoBehaviour
{
[FormerlySerializedAs("path")]
[SerializeField]
string destinationFilePath;
async Task AdvancedExport()
{
#region AdvancedExport
// CollectingLogger lets you programmatically go through
// errors and warnings the export raised
var logger = new CollectingLogger();
// ExportSettings and GameObjectExportSettings allow you to configure the export
// Check their respective source for details
// ExportSettings provides generic export settings
var exportSettings = new ExportSettings
{
Format = GltfFormat.Binary,
FileConflictResolution = FileConflictResolution.Overwrite,
// Export everything except cameras or animation
ComponentMask = ~(ComponentType.Camera | ComponentType.Animation),
// Boost light intensities
LightIntensityFactor = 100f,
// Ensure mesh vertex attributes colors and texture coordinate (channels 1 through 8) are always
// exported, even if they are not used/referenced.
PreservedVertexAttributes = VertexAttributeUsage.AllTexCoords | VertexAttributeUsage.Color,
};
// GameObjectExportSettings provides settings specific to a GameObject/Component based hierarchy
var gameObjectExportSettings = new GameObjectExportSettings
{
// Include inactive GameObjects in export
OnlyActiveInHierarchy = false,
// Also export disabled components
DisabledComponents = true,
// Only export GameObjects on certain layers
LayerMask = LayerMask.GetMask("Default", "MyCustomLayer"),
};
// GameObjectExport lets you create glTFs from GameObject hierarchies
var export = new GameObjectExport(exportSettings, gameObjectExportSettings, logger: logger);
// Example of gathering GameObjects to be exported (recursively)
var rootLevelNodes = GameObject.FindGameObjectsWithTag("ExportMe");
// Add a scene
export.AddScene(rootLevelNodes, "My new glTF scene");
// Async glTF export
var success = await export.SaveToFileAndDispose(destinationFilePath);
if (!success)
{
Debug.LogError("Something went wrong exporting a glTF");
// Log all exporter messages
logger.LogAll();
}
#endregion
}
void ExportSettingsDraco()
{
#region ExportSettingsDraco
// ExportSettings provides generic export settings
var exportSettings = new ExportSettings
{
// Enable Draco compression
Compression = Compression.Draco,
// Optional: Tweak the Draco compression settings
DracoSettings = new DracoExportSettings
{
positionQuantization = 12
}
};
#endregion
}
async void Start()
{
await LocalTransform();
}
async Task LocalTransform()
{
#region LocalTransform
var export = new GameObjectExport();
// Add a scene
export.AddScene(
// Only the current gameObject as root node.
new[] { gameObject },
// The worldToLocalMatrix counter-acts any world-space transform, effectively moving the resulting
// root node to the origin.
gameObject.transform.worldToLocalMatrix,
"Node at origin glTF scene"
);
#endregion
// Async glTF export
var success = await export.SaveToFileAndDispose(destinationFilePath);
if (!success)
{
Debug.LogError("Something went wrong exporting a glTF");
}
}
}
}