-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoredImage.ts
361 lines (314 loc) · 11.8 KB
/
StoredImage.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { movePlayerTo } from "@decentraland/RestrictedActions";
import { recordEvent } from "../analytics";
import { sdkImageFlippedDimension, sdkImagesAreFlipped, sdkImagesFace, vlmImagesFace } from "../helpers/defaults";
import { getEntityByName } from "../helpers/entity";
import { IEmission, ITexture, ITransform } from "../interfaces/index";
import { sceneFeatures } from "../sceneData";
import { imageInstances, imageMaterials } from "../storage";
import { EClickEventType, TClickEvent, TImageInstanceConfig, TImageMaterialConfig, TTransform } from "../types/index";
import { StoredEntityInstance } from "./StoredEntity";
export class StoredImageMaterial extends Material implements ITexture, IEmission {
id: string;
customId?: string;
customRendering: boolean;
albedoTexture: Texture;
alphaTexture: Texture;
emissiveColor = Color3.White();
emissiveIntensity: number;
emissiveTexture: Texture;
parent?: string;
show: boolean;
instanceIds: string[] | any = [];
imageLink: string;
roughness: number = 1.0;
specularIntensity: number = 0;
metallic: number = 0;
withCollisions: boolean;
isTransparent: boolean;
clickEvent?: TClickEvent;
constructor(_config: TImageMaterialConfig) {
super();
this.id = _config.id;
this.customId = _config.customId;
this.customRendering = !!_config.customRendering;
this.parent = _config.parent;
this.show = _config.show;
this.emissiveIntensity = _config.emission || 1;
this.imageLink = _config.imageLink;
this.withCollisions = _config.withCollisions;
this.isTransparent = _config.isTransparent;
this.clickEvent = _config.clickEvent;
this.updateTexture(this.imageLink);
imageMaterials[this.id] = this;
if (this.customId) {
imageMaterials[this.customId] = imageMaterials[this.id];
}
if (this.customRendering || _config.instances.length < 1) {
return;
}
_config.instances.forEach((instance: TImageInstanceConfig) => {
this.createInstance(instance);
});
}
// deletes the material record AND removes the instances from the engine
delete: CallableFunction = () => {
delete imageMaterials[this.id];
[...this.instanceIds].forEach((instanceId: string) => {
imageInstances[instanceId].delete();
});
};
// just removes the instances from the engine, keeps the material record and instance records so we can bring stuff back
remove: CallableFunction = () => {
[...this.instanceIds].forEach((instanceId: string) => {
imageInstances[instanceId].remove();
});
};
showAll: CallableFunction = () => {
[...this.instanceIds].forEach((instanceId: string) => {
const visible = imageInstances[instanceId].show,
parent = imageInstances[instanceId].parent || this.parent;
if (!visible) {
return;
} else if (parent) {
imageInstances[instanceId].updateParent(parent);
} else {
imageInstances[instanceId].add();
}
});
};
updateParent: CallableFunction = (parent: string) => {
[...this.instanceIds].forEach((instanceId: string) => {
if (imageInstances[instanceId].parent === this.parent) {
imageInstances[instanceId].updateParent(parent);
}
});
this.parent = parent;
};
updateCustomId: CallableFunction = (customId: string) => {
if (this.customId && imageMaterials[this.customId]) {
delete imageMaterials[this.customId];
}
imageMaterials[customId] = imageMaterials[this.id];
this.customId = customId;
};
updateTexture: CallableFunction = (url?: string) => {
if (url) {
this.imageLink = url;
}
const texture = new Texture(this.imageLink, {hasAlpha: this.isTransparent});
this.albedoTexture = texture;
this.emissiveTexture = texture;
this.alphaTexture = texture;
if (this.isTransparent) {
this.transparencyMode = TransparencyMode.ALPHA_TEST;
} else {
this.transparencyMode = TransparencyMode.OPAQUE;
}
};
updateBrightness: CallableFunction = (brightness: number) => {
this.emissiveIntensity = brightness;
};
updateTransparency: CallableFunction = (isTransparent: boolean) => {
this.isTransparent = isTransparent;
this.updateTexture();
};
updateClickEvent: CallableFunction = (clickEvent: TClickEvent) => {
this.clickEvent = clickEvent;
[...this.instanceIds].forEach((instanceId: string) => {
imageInstances[instanceId].updateDefaultClickEvent(this.clickEvent);
});
};
createInstance: CallableFunction = (_config: TImageInstanceConfig) => {
this.instanceIds.push(_config.id);
new StoredImageInstance(this, _config);
imageInstances[_config.id].add();
};
deleteInstance: CallableFunction = (instanceId: string) => {
this.instanceIds = this.instanceIds.filter((id: string) => id !== instanceId);
imageInstances[instanceId].delete();
};
removeInstance: CallableFunction = (instanceId: string) => {
imageInstances[instanceId].remove();
};
addInstance: CallableFunction = (instanceId: string) => {
imageInstances[instanceId].add();
};
}
export class StoredImageInstance extends StoredEntityInstance implements ITransform {
id: string;
materialId: string;
parent: string;
position: TTransform;
scale: TTransform;
rotation: TTransform;
modifiedTransform: { position: TTransform; scale: TTransform; rotation: TTransform };
withCollisions: boolean;
constructor(_material: StoredImageMaterial, _instance: TImageInstanceConfig) {
super(_material, _instance);
this.id = _instance.id;
this.customId = _instance.customId;
this.parent = _instance.parent || _material.parent;
this.customRendering = _instance.customRendering;
this.position = _instance.position;
this.scale = _instance.scale;
this.rotation = _instance.rotation;
this.materialId = _material.id;
this.show = _instance.show;
this.clickEvent = _instance.clickEvent;
this.defaultClickEvent = _material.clickEvent;
imageInstances[this.id] = this;
const shape = new PlaneShape();
shape.withCollisions = typeof _instance.withCollisions === "boolean" ? _instance.withCollisions : _material.withCollisions;
this.addComponent(shape);
this.addComponent(_material);
this.updateTransform(this.position, this.scale, this.rotation);
this.updateDefaultClickEvent(_material.clickEvent);
if (this.parent && this.show && !this.customRendering) {
this.updateParent(this.parent);
} else if (this.show) {
this.add();
}
if (this.customId) {
imageInstances[this.customId] = imageInstances[this.id];
}
}
add: CallableFunction = () => {
engine.addEntity(this);
};
delete: CallableFunction = () => {
delete imageInstances[this.id];
if (this.customId) {
delete imageInstances[this.customId];
}
engine.removeEntity(this);
};
remove: CallableFunction = () => {
engine.removeEntity(this);
};
updateParent: CallableFunction = (parent: string) => {
if (parent) {
this.parent = parent;
const instanceParent = getEntityByName(parent);
this.setParent(instanceParent);
} else {
this.setParent(null);
}
};
updateCustomId: CallableFunction = (customId: string) => {
if (this.customId && imageInstances[this.customId]) {
delete imageInstances[this.customId];
}
imageInstances[customId] = imageInstances[this.id];
this.customId = customId;
};
updateTransform: CallableFunction = (newPosition?: TTransform, newScale?: TTransform, newRotation?: TTransform) => {
this.applyCustomTransforms(newPosition, newScale, newRotation);
const { position, scale, rotation } = this.modifiedTransform;
this.addComponentOrReplace(
new Transform({
position: new Vector3(position.x, position.y, position.z),
scale: new Vector3(scale.x, scale.y, scale.z),
rotation: Quaternion.Euler(rotation.x, rotation.y, rotation.z)
})
);
};
updateCollider: CallableFunction = (instanceConfig: TImageInstanceConfig) => {
this.withCollisions = instanceConfig.withCollisions;
const shape = new PlaneShape();
shape.withCollisions = this.withCollisions;
this.addComponentOrReplace(shape);
};
applyCustomTransforms: CallableFunction = (originalPosition: TTransform, originalScale: TTransform, originalRotation: TTransform) => {
this.position = originalPosition || this.position;
this.scale = originalScale || this.scale;
this.rotation = originalRotation || this.rotation;
this.modifiedTransform = { position: { ...this.position }, scale: { ...this.scale }, rotation: { ...this.rotation } };
if (sdkImagesAreFlipped) {
this.modifiedTransform.rotation[sdkImageFlippedDimension] += 180;
}
const imageRotationDegree = (vlmImagesFace - sdkImagesFace) * 90;
this.modifiedTransform.rotation.y += imageRotationDegree;
};
trackClickEvent: CallableFunction = (clickEvent: TClickEvent, id: string) => {
const trackingId = clickEvent.trackingId || id;
if (clickEvent.hasTracking && sceneFeatures.analytics) {
recordEvent(trackingId);
}
};
updateDefaultClickEvent: CallableFunction = (newDefaultClickEvent: TClickEvent) => {
this.defaultClickEvent = newDefaultClickEvent;
this.updateClickEvent();
};
updateClickEvent: CallableFunction = (newClickEvent?: TClickEvent) => {
if (typeof newClickEvent !== "undefined") {
this.clickEvent = newClickEvent;
}
if (this.clickEvent && this.clickEvent.synced) {
this.clickEvent = this.defaultClickEvent;
}
const clickEvent = this.clickEvent;
let pointerDownEvent,
showFeedback = clickEvent && clickEvent.showFeedback,
hoverText = clickEvent && clickEvent.hoverText,
instanceId = this.id;
if (!clickEvent || !imageInstances[instanceId]) {
return;
}
switch (clickEvent.type) {
case EClickEventType.NONE: //no click event
if (imageInstances[instanceId].getComponentOrNull(OnPointerDown)) {
imageInstances[instanceId].removeComponent(OnPointerDown);
}
return;
case EClickEventType.TRACKING_ONLY: //tracking clicks only
pointerDownEvent = new OnPointerDown(
() => {
this.trackClickEvent(clickEvent, `click-event-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
);
break;
case EClickEventType.EXTERNAL: //external link
pointerDownEvent = new OnPointerDown(
() => {
openExternalURL(clickEvent.externalLink);
this.trackClickEvent(clickEvent, `click-event-(external-link)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
);
break;
case EClickEventType.SOUND: //play a sound
const clip = new AudioClip(clickEvent.sound);
const source = new AudioSource(clip);
pointerDownEvent = new OnPointerDown(
() => {
source.playOnce();
this.trackClickEvent(clickEvent, `click-event-(sound)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
);
break;
case EClickEventType.MOVE: // move player
pointerDownEvent = new OnPointerDown(
() => {
movePlayerTo(clickEvent.moveTo.position, (clickEvent.moveTo.setCameraTarget ? clickEvent.moveTo.cameraTarget : null));
this.trackClickEvent(clickEvent, `click-event-(move-player)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
);
break;
case EClickEventType.TELEPORT: // teleport player
pointerDownEvent = new OnPointerDown(
() => {
teleportTo(clickEvent.teleportTo);
this.trackClickEvent(clickEvent, `click-event-(teleport-player)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
);
break;
}
if (pointerDownEvent) {
imageInstances[instanceId].addComponentOrReplace(pointerDownEvent);
}
};
}