forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
character-fx.js
309 lines (285 loc) · 10.2 KB
/
character-fx.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
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
/*
this file implements avatar transformation effects/shaders.
*/
import * as THREE from 'three';
import metaversefile from 'metaversefile';
import * as metaverseModules from './metaverse-modules.js';
import {sceneLowPriority} from './renderer.js';
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localVector3 = new THREE.Vector3();
const _makeKiHairMaterial = () => {
let wVertex = THREE.ShaderLib["standard"].vertexShader;
let wFragment = THREE.ShaderLib["standard"].fragmentShader;
// let wUniforms = THREE.UniformsUtils.clone(THREE.ShaderLib["standard"].uniforms);
let wUniforms = {
iTime: {
value: 0,
needsUpdate: false,
},
uHeadCenter: {
value: new THREE.Vector3(0, 0, 0),
needsUpdate: false,
},
};
wVertex = wVertex.replace(`#include <clipping_planes_pars_vertex>`, `\
#include <clipping_planes_pars_vertex>
varying vec2 vUv;
varying vec3 vWorldPosition;
`);
wVertex = wVertex.replace(`}`, `\
vUv = uv;
}
`);
wVertex = wVertex.replace(`#include <skinning_vertex>`, `\
#include <skinning_vertex>
vWorldPosition = transformed;
`);
wFragment = wFragment.replace(`#include <clipping_planes_pars_fragment>`, `\
#include <clipping_planes_pars_fragment>
uniform float iTime;
uniform vec3 uHeadCenter;
varying vec2 vUv;
varying vec3 vWorldPosition;
// #define PI 3.1415926535897932384626433832795
`);
wFragment = wFragment.replace(`}`, `\
// float f = dot(vNormal, normalize(vec3(1.)));
// vec3 colorX = mix(color1, color2, f);
// vec3 colorY = mix(color1, color2, vUv.y);
// gl_FragColor.rgb = colorX * colorY;
// gl_FragColor.rgb = colorX;
gl_FragColor.rb = vUv;
float distanceToCenter = length(vWorldPosition - uHeadCenter);
float glowRadius = iTime * 0.15;
float distanceToGlowRadius = abs(distanceToCenter - glowRadius);
distanceToGlowRadius = mod(distanceToGlowRadius, 0.1);
gl_FragColor.rgb *= 0.3;
if (distanceToGlowRadius < 0.01) {
gl_FragColor.rgb *= 2.;
}
float glowFactor = 1. + sin(iTime * PI * 2. * 3.) * 0.5;
gl_FragColor.rgb *= 0.5 + glowFactor * 0.5;
gl_FragColor.a = 1.;
}
`);
const material = new THREE.ShaderMaterial({
uniforms: wUniforms,
vertexShader: wVertex,
fragmentShader: wFragment,
extensions: {
derivatives: true,
},
// side: THREE.DoubleSide,
});
return material;
};
class CharacterFx {
constructor(player) {
this.player = player;
// this.lastJumpState = false;
// this.lastStepped = [false, false];
// this.lastWalkTime = 0;
this.kiMesh = null;
this.sonicBoom = null;
this.healEffect = null;
this.hairMeshes = null;
this.lastSSS = false;
}
update(timestamp, timeDiffS) {
if (!this.player.avatar) {
return;
}
const timeS = timestamp/1000;
const powerupAction = this.player.getAction('dance');
const isPowerup = !!powerupAction && powerupAction.animation === 'powerup';
const sssAction = this.player.getAction('sss');
const isSSS = !!sssAction;
const _initHairMeshes = () => {
this.hairMeshes = [];
this.player.avatar.object.scene.traverse(o => {
// console.log(o.name, o.isMesh);
if (o.isSkinnedMesh) {
const {geometry, skeleton} = o;
const skeletonBoneHariBooleans = skeleton.bones.map(bone => /hair/i.test(bone.name));
const {attributes, index: indexAttribute} = geometry;
const indices = indexAttribute.array;
const {skinIndex, skinWeight} = attributes;
const skinIndices = skinIndex.array;
const skinWeights = skinWeight.array;
const {itemSize} = skinIndex;
let done = false;
for (let i = 0; i < indices.length; i++) {
const index = indices[i];
for (let j = 0; j < itemSize; j++) {
const skinIndex = skinIndices[index * itemSize + j];
const skinWeight = skinWeights[index * itemSize + j];
if (skinWeight !== 0 && skeletonBoneHariBooleans[skinIndex]) {
this.hairMeshes.push(o);
done = true;
break;
}
}
if (done) {
break;
}
}
}
});
for (const hairMesh of this.hairMeshes) {
hairMesh.kiOriginalMaterial = hairMesh.material;
const kiHairMaterial = _makeKiHairMaterial();
hairMesh.kiMaterial = kiHairMaterial;
/* kiHairMaterial.uniforms.color1.value.setHex(0xfdeb44);
kiHairMaterial.uniforms.color1.needsUpdate = true;
kiHairMaterial.uniforms.color2.value.setHex(0xf6b01d);
kiHairMaterial.uniforms.color2.needsUpdate = true; */
/* hairMesh.material.defines = kiHairMaterial.defines;
hairMesh.material.vertexShader = kiHairMaterial.vertexShader;
hairMesh.material.fragmentShader = kiHairMaterial.fragmentShader; */
}
for (const springBones of this.player.avatar.springBoneManager.springBoneGroupList) {
for (const o of springBones) {
o.kiOriginalGravityDir = o.gravityDir.clone();
o.kiOriginalGravityPower = o.gravityPower;
}
}
};
const _enableHairMeshes = () => {
for (const hairMesh of this.hairMeshes) {
hairMesh.material = hairMesh.kiMaterial;
}
};
const _disableHairMeshes = () => {
for (const hairMesh of this.hairMeshes) {
hairMesh.material = hairMesh.kiOriginalMaterial;
}
for (const springBones of this.player.avatar.springBoneManager.springBoneGroupList) {
for (const o of springBones) {
o.gravityDir.copy(o.kiOriginalGravityDir);
o.gravityPower = o.kiOriginalGravityPower;
}
}
};
const _updateHair = () => {
if (isSSS && !this.lastSSS) {
if (!this.hairMeshes) {
_initHairMeshes();
}
_enableHairMeshes();
} else if (this.lastSSS && !isSSS) {
_disableHairMeshes();
}
if (isSSS) {
for (const hairMesh of this.hairMeshes) {
hairMesh.material.uniforms.iTime.value = timeS;
hairMesh.material.uniforms.iTime.needsUpdate = true;
hairMesh.material.uniforms.uHeadCenter.value.setFromMatrixPosition(this.player.avatar.modelBoneOutputs.Head.matrixWorld);
hairMesh.material.uniforms.uHeadCenter.needsUpdate = true;
}
if (this.player.avatar.springBoneManager) {
const headPosition = localVector.setFromMatrixPosition(this.player.avatar.modelBoneOutputs.Head.matrixWorld);
const octave = Math.sin(timeS * Math.PI * 2 * 4) // +
// Math.sin(timeS * Math.PI * 2 * 4) +
// Math.sin(timeS * Math.PI * 2 * 8);
const gravityPower = 0.4 + (1 + octave)*0.5 * 0.5;
for (const springBones of this.player.avatar.springBoneManager.springBoneGroupList) {
for (const o of springBones) {
const gravityDir = localVector2.setFromMatrixPosition(o.bone.matrixWorld)
.sub(headPosition)
.normalize()
.lerp(localVector3.set(0, 1, 0), 0.9);
o.gravityDir.copy(gravityDir);
o.gravityPower = gravityPower;
}
}
}
}
};
_updateHair();
const _updateKiMesh = () => {
if (isPowerup && !this.kiMesh) {
this.kiMesh = metaversefile.createApp();
(async () => {
const {modules} = metaversefile.useDefaultModules();
const m = modules['ki'];
await this.kiMesh.addModule(m);
})();
sceneLowPriority.add(this.kiMesh);
}
if (this.kiMesh) {
this.kiMesh.visible = isPowerup;
}
};
_updateKiMesh();
this.lastSSS = isSSS;
const _updateSonicBoomMesh = () => {
if (!this.sonicBoom && !this.player.isNpcPlayer) {
this.sonicBoom = metaversefile.createApp();
this.sonicBoom.setComponent('player', this.player);
(async () => {
const {modules} = metaversefile.useDefaultModules();
const m = modules['sonicBoom'];
await this.sonicBoom.addModule(m);
})();
sceneLowPriority.add(this.sonicBoom);
}
};
_updateSonicBoomMesh();
const _updateNameplate = () => {
if(!this.nameplate && !this.player.isNpcPlayer && !this.player.isLocalPlayer){
(async () => {
this.nameplate = metaversefile.createApp();
this.nameplate.setComponent('player', this.player);
const {modules} = metaversefile.useDefaultModules();
const m = modules['nameplate'];
await this.nameplate.addModule(m);
sceneLowPriority.add(this.nameplate);
})();
}
};
_updateNameplate();
const _updateHealEffectMesh = () => {
if(this.player.hasAction('cure')){
if (!this.healEffect) {
this.healEffect = metaversefile.createApp();
(async () => {
const {modules} = metaversefile.useDefaultModules();
const m = modules['healEffect'];
await this.healEffect.addModule(m);
this.healEffect.playEffect(this.player);
this.player.removeAction('cure')
})();
sceneLowPriority.add(this.healEffect);
}
else{
this.healEffect.playEffect(this.player);
this.player.removeAction('cure')
}
}
};
_updateHealEffectMesh();
}
destroy() {
if (this.kiMesh) {
sceneLowPriority.remove(this.kiMesh);
this.kiMesh = null;
}
if (this.sonicBoom) {
sceneLowPriority.remove(this.sonicBoom);
this.sonicBoom.destroy();
this.sonicBoom = null;
}
if (this.nameplate) {
sceneLowPriority.remove(this.nameplate);
this.nameplate = null;
}
if (this.healEffect) {
sceneLowPriority.remove(this.healEffect);
this.healEffect = null;
}
}
}
export {
CharacterFx,
};