forked from atteneder/glTFast
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathAnimationData.cs
More file actions
214 lines (197 loc) · 9.3 KB
/
AnimationData.cs
File metadata and controls
214 lines (197 loc) · 9.3 KB
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
// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0
#if UNITY_ANIMATION
using System;
using GLTFast.Schema;
using UnityEngine;
namespace GLTFast {
public enum TargetType {
Unknown = -1,
Camera,
Material,
Mesh,
Node,
}
public class AnimationData {
public TargetType TargetType = TargetType.Unknown;
public Type AnimationClipType;
public GltfAccessorAttributeType AccessorType;
public string TargetProperty;
public int TargetId = -1;
public string[] PropertyNames;
public static AnimationData TranslationData() {
var template = "localPosition.";
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(Transform),
AccessorType = GltfAccessorAttributeType.VEC3,
TargetProperty = "translationNative",
PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"}
};
}
public static AnimationData RotationData() {
var template = "localRotation.";
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(Transform),
AccessorType = GltfAccessorAttributeType.VEC4,
TargetProperty = "rotationNative",
PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z", $"{template}w"}
};
}
public static AnimationData ScaleData() {
var template = "localScale.";
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(Transform),
AccessorType = GltfAccessorAttributeType.VEC3,
TargetProperty = "scaleNative",
PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"}
};
}
public static AnimationData WeightData() {
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(MeshRenderer),
AccessorType = GltfAccessorAttributeType.SCALAR,
TargetProperty = "weights"
};
}
public static AnimationData GeneratePointerData(string pointerPath) {
var data = new AnimationData();
switch (pointerPath) {
case string p when p.StartsWith("/cameras/"):
data.TargetType = TargetType.Camera;
data.AnimationClipType = typeof(AnimationCameraGameObject);
data.TargetId = ParsePointerTargetId(pointerPath["/cameras/".Length..]);
data.TargetProperty = pointerPath[$"/cameras/{data.TargetId}/".Length..];
break;
case string p when p.StartsWith("/materials/"):
data.TargetType = TargetType.Material;
data.AnimationClipType = typeof(Renderer);
data.TargetId = ParsePointerTargetId(pointerPath["/materials/".Length..]);
data.TargetProperty = pointerPath[$"/materials/{data.TargetId}/".Length..];
break;
case string p when p.StartsWith("/meshes/"):
data.TargetType = TargetType.Mesh;
data.AnimationClipType = typeof(UnityEngine.MeshRenderer);
data.TargetId = ParsePointerTargetId(pointerPath["/meshes/".Length..]);
data.TargetProperty = pointerPath[$"/meshes/{data.TargetId}/".Length..];
break;
case string p when p.StartsWith("/nodes/"):
data.TargetType = TargetType.Node;
if (pointerPath[^7..].Equals("weights")) {
data.AnimationClipType = typeof(UnityEngine.MeshRenderer);
} else {
data.AnimationClipType = typeof(Transform);
}
data.TargetId = ParsePointerTargetId(pointerPath["/nodes/".Length..]);
data.TargetProperty = pointerPath[$"/nodes/{data.TargetId}/".Length..];
break;
}
string template;
switch(data.TargetProperty) {
// Core
case "rotation":
template = "localRotation.";
data.PropertyNames = new[] {$"{template}x", $"{template}y", $"{template}z", $"{template}w"};
data.AccessorType = GltfAccessorAttributeType.VEC4;
break;
case "scale":
template = "localScale.";
data.PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"};
data.AccessorType = GltfAccessorAttributeType.VEC3;
break;
case "translation":
template = "localPosition.";
data.PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"};
data.AccessorType = GltfAccessorAttributeType.VEC3;
break;
case "weights":
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/xmag":
data.PropertyNames = new [] {"xMag"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/ymag":
data.PropertyNames = new [] {"yMag"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/zfar":
data.PropertyNames = new [] {"farClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/znear":
data.PropertyNames = new [] {"nearClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "perspective/yfov":
data.PropertyNames = new [] {"fov"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "perspective/zfar":
data.PropertyNames = new [] {"farClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "perspective/znear":
data.PropertyNames = new [] {"nearClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "pbrMetallicRoughness/baseColorFactor":
template = "material.baseColorFactor.";
data.PropertyNames = new[] {$"{template}r", $"{template}g", $"{template}b", $"{template}a"};
data.AccessorType = GltfAccessorAttributeType.VEC4;
break;
case "pbrMetallicRoughness/metallicFactor":
data.PropertyNames = new[] {"material.metallicFactor"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "pbrMetallicRoughness/roughnessFactor":
data.PropertyNames = new[] {"material.roughnessFactor"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "alphaCutoff":
data.PropertyNames = new[] {"material.alphaCutoff"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "emissiveFactor":
template = "material.emissiveFactor.";
data.PropertyNames = new[] {$"{template}r", $"{template}g", $"{template}b"};
data.AccessorType = GltfAccessorAttributeType.VEC3;
break;
case "normalTexture/scale":
data.PropertyNames = new[] {"material.normalTexture_scale"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "occlusionTexture/strength":
data.PropertyNames = new[] {"material.occlusionTexture_strength"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
// KHR_materials_transmission
case "extensions/KHR_materials_transmission/transmissionFactor":
data.PropertyNames = new[] {"material.transmissionFactor"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
default:
#if DEBUG
Debug.LogWarning($"glTF animation pointer {pointerPath} is not supported.");
#endif
break;
}
return data;
}
public static int ParsePointerTargetId(string name) {
var split = name[..name.IndexOf("/")];
if(int.TryParse(split, out var targetId)) {
return targetId;
}
return -1;
}
}
}
#endif