-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
player-info.js
204 lines (175 loc) · 7.42 KB
/
player-info.js
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
import { injectCustomShaderChunks } from "../utils/media-utils";
import { AVATAR_TYPES } from "../utils/avatar-utils";
import { registerComponentInstance, deregisterComponentInstance } from "../utils/component-utils";
import defaultAvatar from "../assets/models/DefaultAvatar.glb";
import { MediaDevicesEvents } from "../utils/media-devices-utils";
import { createHeadlessModelForSkinnedMesh } from "../utils/three-utils";
import { Layers } from "../camera-layers";
function ensureAvatarNodes(json) {
const { nodes } = json;
if (!nodes.some(node => node.name === "Head")) {
// If the avatar model doesn't have a Head node. The user has probably chosen a custom GLB.
// So, we need to construct a suitable hierarchy for avatar functionality to work.
// We re-parent the original root node to the Head node and set the scene root to a new AvatarRoot.
// Note: We assume that the first node in the primary scene is the one we care about.
const originalRoot = json.scenes[json.scene].nodes[0];
nodes.push({ name: "LeftEye", extensions: { MOZ_hubs_components: {} } });
nodes.push({ name: "RightEye", extensions: { MOZ_hubs_components: {} } });
nodes.push({
name: "Head",
children: [originalRoot, nodes.length - 1, nodes.length - 2],
extensions: { MOZ_hubs_components: { "scale-audio-feedback": "" } }
});
nodes.push({ name: "Neck", children: [nodes.length - 1] });
nodes.push({ name: "Spine", children: [nodes.length - 1] });
nodes.push({ name: "Hips", children: [nodes.length - 1] });
nodes.push({ name: "AvatarRoot", children: [nodes.length - 1] });
json.scenes[json.scene].nodes[0] = nodes.length - 1;
}
return json;
}
AFRAME.registerComponent("player-info", {
schema: {
avatarSrc: { type: "string" },
avatarType: { type: "string", default: AVATAR_TYPES.SKINNABLE },
muted: { default: false },
isSharingAvatarCamera: { default: false }
},
init() {
this.applyProperties = this.applyProperties.bind(this);
this.handleModelError = this.handleModelError.bind(this);
this.handleRemoteModelError = this.handleRemoteModelError.bind(this);
this.update = this.update.bind(this);
this.onPresenceUpdated = this.onPresenceUpdated.bind(this);
this.onMicStateChanged = this.onMicStateChanged.bind(this);
this.onAvatarModelLoaded = this.onAvatarModelLoaded.bind(this);
this.isLocalPlayerInfo = this.el.id === "avatar-rig";
this.playerSessionId = null;
if (!this.isLocalPlayerInfo) {
NAF.utils.getNetworkedEntity(this.el).then(networkedEntity => {
this.playerSessionId = NAF.utils.getCreator(networkedEntity);
const playerPresence = window.APP.hubChannel.presence.state[this.playerSessionId];
if (playerPresence) {
this.permissions = playerPresence.metas[0].permissions;
}
});
}
registerComponentInstance(this, "player-info");
},
remove() {
const avatarEl = this.el.querySelector("[avatar-audio-source]");
APP.isAudioPaused.delete(avatarEl);
deregisterComponentInstance(this, "player-info");
},
onAvatarModelLoaded(e) {
this.applyProperties(e);
const modelEl = this.el.querySelector(".model");
if (this.isLocalPlayerInfo && e.target === modelEl) {
let isSkinnedAvatar = false;
modelEl.object3D.traverse(function (o) {
if (o.isSkinnedMesh) {
const headlessMesh = createHeadlessModelForSkinnedMesh(o);
if (headlessMesh) {
isSkinnedAvatar = true;
o.parent.add(headlessMesh);
}
}
});
// This is to support using arbitrary models as avatars.
// TODO We can drop support for this when we go full VRM, or at least handle it earlier in the process.
if (!isSkinnedAvatar) {
modelEl.object3D.traverse(function (o) {
if (o.isMesh) o.layers.set(Layers.CAMERA_LAYER_THIRD_PERSON_ONLY);
});
}
}
},
play() {
this.el.addEventListener("model-loaded", this.onAvatarModelLoaded);
this.el.sceneEl.addEventListener("presence_updated", this.onPresenceUpdated);
if (this.isLocalPlayerInfo) {
this.el.querySelector(".model").addEventListener("model-error", this.handleModelError);
} else {
this.el.querySelector(".model").addEventListener("model-error", this.handleRemoteModelError);
}
window.APP.store.addEventListener("statechanged", this.update);
this.el.sceneEl.addEventListener("stateadded", this.update);
this.el.sceneEl.addEventListener("stateremoved", this.update);
if (this.isLocalPlayerInfo) {
APP.dialog.on("mic-state-changed", this.onMicStateChanged);
}
},
pause() {
this.el.removeEventListener("model-loaded", this.onAvatarModelLoaded);
this.el.sceneEl.removeEventListener("presence_updated", this.onPresenceUpdated);
if (this.isLocalPlayerInfo) {
this.el.querySelector(".model").removeEventListener("model-error", this.handleModelError);
} else {
this.el.querySelector(".model").removeEventListener("model-error", this.handleRemoteModelError);
}
this.el.sceneEl.removeEventListener("stateadded", this.update);
this.el.sceneEl.removeEventListener("stateremoved", this.update);
window.APP.store.removeEventListener("statechanged", this.update);
if (this.isLocalPlayerInfo) {
APP.dialog.off("mic-state-changed", this.onMicStateChanged);
}
},
onPresenceUpdated(e) {
this.updateFromPresenceMeta(e.detail);
},
updateFromPresenceMeta(presenceMeta) {
if (!this.playerSessionId && this.isLocalPlayerInfo) {
this.playerSessionId = NAF.clientId;
}
if (!this.playerSessionId || this.playerSessionId !== presenceMeta.sessionId) return;
this.permissions = presenceMeta.permissions;
},
update(oldData) {
if (this.data.muted !== oldData.muted) {
this.el.emit("remote_mute_updated", { muted: this.data.muted });
}
this.applyProperties();
},
can(perm) {
return !!this.permissions && this.permissions[perm];
},
applyProperties(e) {
const modelEl = this.el.querySelector(".model");
if (this.data.avatarSrc && modelEl) {
modelEl.components["gltf-model-plus"].jsonPreprocessor = ensureAvatarNodes;
modelEl.setAttribute("gltf-model-plus", "src", this.data.avatarSrc);
}
if (!e || e.target === modelEl) {
const uniforms = injectCustomShaderChunks(this.el.object3D);
this.el.querySelectorAll("[hover-visuals]").forEach(el => {
el.components["hover-visuals"].uniforms = uniforms;
});
}
const videoTextureTargets = modelEl.querySelectorAll("[video-texture-target]");
const sessionId = this.isLocalPlayerInfo ? NAF.clientId : this.playerSessionId;
for (const el of Array.from(videoTextureTargets)) {
el.setAttribute("video-texture-target", {
src: this.data.isSharingAvatarCamera ? `hubs://clients/${sessionId}/video` : ""
});
if (this.isLocalPlayerInfo) {
el.setAttribute("emit-scene-event-on-remove", `event:${MediaDevicesEvents.VIDEO_SHARE_ENDED}`);
}
}
const avatarEl = this.el.querySelector("[avatar-audio-source]");
if (this.data.muted) {
APP.isAudioPaused.add(avatarEl);
} else {
APP.isAudioPaused.delete(avatarEl);
}
},
handleModelError() {
window.APP.store.resetToRandomDefaultAvatar();
},
handleRemoteModelError() {
this.data.avatarSrc = defaultAvatar;
this.applyProperties();
},
onMicStateChanged({ enabled }) {
this.el.setAttribute("player-info", { muted: !enabled });
}
});