-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
EntityMesh.ts
201 lines (174 loc) · 7.53 KB
/
EntityMesh.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
import { Scene } from "@babylonjs/core/scene";
import { AnimationGroup } from "@babylonjs/core/Animations/animationGroup";
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { Mesh } from "@babylonjs/core/Meshes/mesh";
import { ActionManager } from "@babylonjs/core/Actions/actionManager";
import { ExecuteCodeAction } from "@babylonjs/core/Actions/directActions";
import { Color3 } from "@babylonjs/core/Maths/math.color";
import { Vector3, Vector4 } from "@babylonjs/core/Maths/math";
import { Entity } from "../Entity";
import { Skeleton } from "@babylonjs/core/Bones/skeleton";
import { GameController } from "../../Controllers/GameController";
import { EquipmentSchema } from "../../../server/rooms/schema";
import { UserInterface } from "../../Controllers/UserInterface";
import { AbstractMesh } from "@babylonjs/core/Meshes/abstractMesh";
import { VatController } from "../../Controllers/VatController";
import { EquippableType } from "../../../shared/types";
export class EntityMesh {
private _entity: Entity;
private _scene: Scene;
private _ui: UserInterface;
private _loadedAssets;
private _room;
private _game: GameController;
private _entityData;
private _animationGroups: AnimationGroup[];
public skeleton: Skeleton;
public mesh: Mesh;
public playerMesh;
public debugMesh: Mesh;
public selectedMesh: Mesh;
public fakeShadow: Mesh;
public equipments;
constructor(entity: Entity) {
this._entity = entity;
this._scene = entity._scene;
this._ui = entity._ui;
this._game = entity._game;
this._loadedAssets = entity._game._loadedAssets;
this._room = entity._room;
this._entityData = entity.entityData;
this.equipments = new Map();
}
public createMesh() {
// load player mesh
let materialIndex = VatController.findMeshKey(this._entity.raceData, this._entity.sessionId);
const playerMesh = this._entityData.meshes.get(materialIndex).createInstance(this._entity.type + "" + this._entity.sessionId);
playerMesh.parent = this._entity;
playerMesh.isPickable = true;
playerMesh.rotationQuaternion = null; // You cannot use a rotationQuaternion followed by a rotation on the same mesh. Once a rotationQuaternion is applied any subsequent use of rotation will produce the wrong orientation, unless the rotationQuaternion is first set to null.
if (this._entity.rotationFix) {
playerMesh.rotation.set(0, this._entity.rotationFix, 0);
}
playerMesh.scaling.set(this._entity.scale, this._entity.scale, this._entity.scale);
playerMesh.instancedBuffers.bakedVertexAnimationSettingsInstanced = new Vector4(0, 0, 0, 0);
playerMesh.occlusionRetryCount = 1;
playerMesh.occlusionType = AbstractMesh.OCCLUSION_TYPE_STRICT;
this.mesh = playerMesh;
// set metadata
this.mesh.metadata = {
sessionId: this._entity.sessionId,
type: this._entity.type,
race: this._entity.race,
name: this._entity.name,
};
this.equipments.forEach((equipment) => {
equipment.setParent(this.mesh);
});
}
public async load() {
// create entity mesh
this.createMesh();
// selected circle
var material = this._scene.getMaterialByName("entity_selected");
const selectedMesh = MeshBuilder.CreateCylinder("entity_selected_" + this._entity.race, { diameter: 2, height: 0.01, tessellation: 8 }, this._scene);
selectedMesh.parent = this._entity;
selectedMesh.material = material;
selectedMesh.isVisible = false;
selectedMesh.isPickable = false;
selectedMesh.checkCollisions = false;
selectedMesh.position = new Vector3(0, 0.05, 0);
this.selectedMesh = selectedMesh;
// add cheap shadow
if (this._loadedAssets["DYNAMIC_shadow_01"]) {
let shadowMesh = this._loadedAssets["DYNAMIC_shadow_01"].createInstance("shadow_" + this._entity.sessionId);
shadowMesh.parent = this._entity;
shadowMesh.isPickable = false;
shadowMesh.checkCollisions = false;
shadowMesh.doNotSyncBoundingInfo = true;
shadowMesh.position = new Vector3(0, 0.04, 0);
this.fakeShadow = shadowMesh;
}
setTimeout(() => {
// check for any equipment changes
this._entity.entity.equipment.onAdd((e) => {
this.equipItem(e);
});
this._entity.entity.equipment.onRemove((e) => {
this.removeItem(e);
});
}, 300);
return true;
}
public deleteMeshes() {
// remove player mesh
this.mesh.dispose();
// remove any other mesh
if (this.equipments.length > 0) {
this.equipments.forEach((equipment) => {
equipment.dispose();
});
}
}
////////////////////////////////////////////////////
///////////// EQUIPMENT ////////////////////
////////////////////////////////////////////////////
removeItem(e) {
//
if (this.equipments.has(e.key)) {
this.equipments.get(e.key).dispose();
this.equipments.delete(e.key);
}
// refresh base mesh
let item = this._game.getGameData("item", e.key);
if (item && item.equippable) {
if (item.equippable.mesh && item.equippable.type === EquippableType.EMBEDDED) {
//this._game._vatController.refreshMesh(this._entity);
}
}
}
async equipItem(e) {
// if not already equipped
if (this.equipments.has(e.key)) return false;
// get item data
let item = this._game.getGameData("item", e.key);
if (item && item.equippable) {
// if mesh needs to be added
let equipOptions = item.equippable;
// if dynamic item
if (equipOptions && equipOptions.mesh && equipOptions.type === EquippableType.DYNAMIC) {
this._game._vatController.prepareItemForVat(this._entityData, e.key);
}
if (equipOptions && equipOptions.mesh && equipOptions.type === EquippableType.EMBEDDED) {
this._game._vatController.prepareEmbeddedItemForVat(this._entityData, e.key);
}
// create instance of mesh
let mesh = this._entityData.items.get(item.key);
let instance = mesh.createInstance("equip_" + this._entity.sessionId + "_" + e.key);
instance.instancedBuffers.bakedVertexAnimationSettingsInstanced = new Vector4(0, 0, 0, 0);
instance.isPickable = false;
// or like this(so we don't need to sync it every frame)
instance.setParent(this.mesh);
instance.position.setAll(0);
instance.rotationQuaternion = undefined;
instance.rotation.setAll(0);
// add
this.equipments.set(e.key, instance);
// refresh animation
this._entity.animatorController.refreshAnimation();
// if embedded item
if (equipOptions && equipOptions.mesh && equipOptions.type === EquippableType.EMBEDDED) {
//this._game._vatController.refreshMesh(this._entity);
}
}
}
public equipAllItems() {
// equip all items
this._entity.equipment.forEach((e: EquipmentSchema) => {
this.equipItem(e);
});
}
public getAnimation() {
return this._animationGroups;
}
}