-
Notifications
You must be signed in to change notification settings - Fork 48
/
MeshFeatures.ts
231 lines (207 loc) · 7.07 KB
/
MeshFeatures.ts
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
import { ExtensionProperty } from "@gltf-transform/core";
import { Texture } from "@gltf-transform/core";
import { TextureInfo } from "@gltf-transform/core";
import { IProperty } from "@gltf-transform/core";
import { PropertyType } from "@gltf-transform/core";
import { PropertyTable } from "./StructuralMetadata";
const NAME = "EXT_mesh_features";
//============================================================================
// Interfaces for the model classes
//
// These correspond to the classes that are defined as the actual extension
// properties, as
// export class MeshFeatures extends ExtensionProperty<IMeshFeatures> {...}
//
// The naming convention for these interfaces is that they start with `I...`.
// They all have to extend the `IProperty` interface.
//
// Note that the textures in these interfaces are using the actual
// `Texture` type of the public glTF-Transform API, and each `texture`
// has an associated `TextureInfo`. This is used internally by glTF-Transform
// for some deduplication magic or whatnot.
//
// More genenerally: The types of the properties in these interfaces
// are the model classes themself, and NOT the interface. So it is
// featureIds: FeatureId[];
// and not
// featureIds: IFeatureId[];
//
// These interfaces are NOT publicly visible. They only serve as the type
// pararameter for the `ExtensionProperty` class, which is the base
// for the actual "model" classes that are exposed to the user.
interface IMeshFeatures extends IProperty {
featureIds: FeatureId[];
}
interface IFeatureId extends IProperty {
featureCount: number;
nullFeatureId: number;
label: string;
attribute: FeatureIdAttribute;
texture: FeatureIdTexture;
propertyTable: PropertyTable;
}
type FeatureIdAttribute = number;
interface IFeatureIdTexture extends IProperty {
channels: number[];
texture: Texture;
textureInfo: TextureInfo;
}
//============================================================================
//============================================================================
// The actual model classes
//
// These are exported, and visible to users.
//
// They offer accessor methods for the properties that are defined in
// the model class interfaces. Depending on the type of the properties,
// these accesor methods come in different flavors:
//
// - For "primitive" property types (like `number`, `boolean` and `string`),
// the implementations use `this.get(...)`/`this.set(...)`
// - For property types that correspond to other "model" classes,
// the implementations use `this.getRef(...)`/`this.setRef(...)`.
// - For property types that correspond to ARRAYS of "model" classes,
// the implementations don't offer "getters/setters", but instead,
// they offer `add/remove/list` methods, implemented based on
// `this.addRef(...)`/`this.removeRef(...)`/`this.listRefs(...)`.
//
// A special case is that of textures:
//
// Each texture in these classes is modeled as a property with the
// type `Texture`, and an associated `TextureInfo`. The `TextureInfo`
// can only be accessed with a `get` method, but not explicitly
// set: It is managed internally by glTF-Transform. So the for
// an `exampleTextureInfo: TextureInfo` property, there will only
// be a getter, implemented as
// ```
// getExampleTextureInfo(): TextureInfo | null {
// return this.getRef("exampleTexture") ?
// this.getRef("exampleTextureInfo") : null;
// }
// ```
/**
* Main model class of `EXT_mesh_features`
*
* @internal
*/
export class MeshFeatures extends ExtensionProperty<IMeshFeatures> {
static override EXTENSION_NAME = NAME;
public declare extensionName: typeof NAME;
public declare propertyType: "MeshFeatures";
public declare parentTypes: [PropertyType.PRIMITIVE];
protected override init(): void {
this.extensionName = NAME;
this.propertyType = "MeshFeatures";
this.parentTypes = [PropertyType.PRIMITIVE];
}
protected override getDefaults() {
return Object.assign(super.getDefaults(), {
featureIds: [],
});
}
listFeatureIds(): FeatureId[] {
return this.listRefs("featureIds");
}
addFeatureId(featureId: FeatureId) {
return this.addRef("featureIds", featureId);
}
removeFeatureId(featureId: FeatureId) {
return this.removeRef("featureIds", featureId);
}
}
/**
* Implementation of a feature ID for `EXT_mesh_features`
*
* @internal
*/
export class FeatureId extends ExtensionProperty<IFeatureId> {
static override EXTENSION_NAME = NAME;
public declare extensionName: typeof NAME;
public declare propertyType: "FeatureId";
public declare parentTypes: ["MeshFeatures"];
protected override init(): void {
this.extensionName = NAME;
this.propertyType = "FeatureId";
this.parentTypes = ["MeshFeatures"];
}
protected override getDefaults() {
return Object.assign(super.getDefaults(), {});
}
getFeatureCount(): number {
return this.get("featureCount");
}
setFeatureCount(featureCount: number) {
return this.set("featureCount", featureCount);
}
getNullFeatureId(): number {
return this.get("nullFeatureId");
}
setNullFeatureId(nullFeatureId: number) {
return this.set("nullFeatureId", nullFeatureId);
}
getLabel(): string {
return this.get("label");
}
setLabel(label: string) {
return this.set("label", label);
}
getAttribute(): FeatureIdAttribute {
return this.get("attribute");
}
setAttribute(attribute: FeatureIdAttribute) {
return this.set("attribute", attribute);
}
getTexture(): FeatureIdTexture | null {
return this.getRef("texture");
}
setTexture(texture: FeatureIdTexture | null) {
return this.setRef("texture", texture);
}
getPropertyTable(): PropertyTable | null {
return this.getRef("propertyTable");
}
setPropertyTable(propertyTable: PropertyTable | null) {
return this.setRef("propertyTable", propertyTable);
}
}
/**
* Implementation of a feature ID texture for `EXT_mesh_features`
*
* @internal
*/
export class FeatureIdTexture extends ExtensionProperty<IFeatureIdTexture> {
static override EXTENSION_NAME = NAME;
public declare extensionName: typeof NAME;
public declare propertyType: "FeatureIdTexture";
public declare parentTypes: ["FeatureId"];
protected override init(): void {
this.extensionName = NAME;
this.propertyType = "FeatureIdTexture";
this.parentTypes = ["FeatureId"];
}
protected override getDefaults() {
const defaultTextureInfo = new TextureInfo(this.graph, "textureInfo");
defaultTextureInfo.setMinFilter(TextureInfo.MagFilter.NEAREST);
defaultTextureInfo.setMagFilter(TextureInfo.MagFilter.NEAREST);
return Object.assign(super.getDefaults(), {
channels: [0],
texture: null,
textureInfo: defaultTextureInfo,
});
}
getChannels(): number[] {
return this.get("channels");
}
setChannels(channels: number[]) {
return this.set("channels", channels);
}
getTexture(): Texture | null {
return this.getRef("texture");
}
setTexture(texture: Texture | null) {
return this.setRef("texture", texture);
}
getTextureInfo(): TextureInfo | null {
return this.getRef("texture") ? this.getRef("textureInfo") : null;
}
}