forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter-sfx.js
206 lines (185 loc) · 7.23 KB
/
character-sfx.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
import * as THREE from 'three';
import Avatar from './avatars/avatars.js';
import {
idleFactorSpeed,
walkFactorSpeed,
runFactorSpeed,
narutoRunTimeFactor,
} from './avatars/constants.js';
import {
crouchMaxTime,
} from './constants.js';
import {
mod,
loadJson,
loadAudioBuffer,
} from './util.js';
const localVector = new THREE.Vector3();
let walkSoundFiles;
let runSoundFiles;
let jumpSoundFiles;
let landSoundFiles;
let narutoRunSoundFiles;
let soundFileAudioBuffer;
const loadPromise = (async () => {
await Avatar.waitForLoad();
const audioContext = Avatar.getAudioContext();
const [
soundFileSpecs,
_soundFileAudioBuffer,
] = await Promise.all([
loadJson(`/sounds/sound-files.json`),
loadAudioBuffer(audioContext, '/sounds/sounds.mp3'),
]);
// console.log('got specs', soundFileSpecs);
walkSoundFiles = soundFileSpecs.filter(f => /^walk\//.test(f.name));
runSoundFiles = soundFileSpecs.filter(f => /^run\//.test(f.name));
jumpSoundFiles = soundFileSpecs.filter(f => /^jump\//.test(f.name));
landSoundFiles = soundFileSpecs.filter(f => /^land\//.test(f.name));
narutoRunSoundFiles = soundFileSpecs.filter(f => /^narutoRun\//.test(f.name));
soundFileAudioBuffer = _soundFileAudioBuffer;
// console.log('loaded audio', soundFileSpecs, soundFileAudioBuffer);
})();
const waitForLoad = () => loadPromise;
// HACK: this is used to dynamically control the step offset for a particular animation
// it is useful during development to adjust sync between animations and sound
// the key listener part of this is in io-manager.js
/* if (typeof window.lol !== 'number') {
window.lol = 0.06;
window.lol2 = 0.18;
} */
const sneakingOffset = -0.14;
const walkingOffset = 0.13;
const walkingBackwardOffset = 0.18;
const runningBackwardOffset = 0.06;
const strafeWalkingOffset = 0.24;
const offsets = {
'Sneaking Forward.fbx': sneakingOffset,
'walking.fbx': walkingOffset,
'walking backwards.fbx': walkingBackwardOffset,
'Fast Run.fbx': 0,
'left strafe walking.fbx': strafeWalkingOffset,
'right strafe walking.fbx': strafeWalkingOffset,
'running backwards.fbx': runningBackwardOffset,
};
class CharacterSfx {
constructor(player) {
this.player = player;
this.lastJumpState = false;
this.lastStepped = [false, false];
this.lastWalkTime = 0;
}
update(timestamp, timeDiffS) {
if (!this.player.avatar) {
return;
}
const timeSeconds = timestamp/1000;
const currentSpeed = localVector.set(this.player.avatar.velocity.x, 0, this.player.avatar.velocity.z).length();
const idleWalkFactor = Math.min(Math.max((currentSpeed - idleFactorSpeed) / (walkFactorSpeed - idleFactorSpeed), 0), 1);
const walkRunFactor = Math.min(Math.max((currentSpeed - walkFactorSpeed) / (runFactorSpeed - walkFactorSpeed), 0), 1);
const crouchFactor = Math.min(Math.max(1 - (this.player.avatar.crouchTime / crouchMaxTime), 0), 1);
// jump
const _playSound = audioSpec => {
const {offset, duration} = audioSpec;
const audioContext = Avatar.getAudioContext();
const audioBufferSourceNode = audioContext.createBufferSource();
audioBufferSourceNode.buffer = soundFileAudioBuffer;
audioBufferSourceNode.connect(audioContext.destination);
audioBufferSourceNode.start(0, offset, duration);
};
{
if (this.player.avatar.jumpState && !this.lastJumpState) {
const audioSpec = jumpSoundFiles[Math.floor(Math.random() * jumpSoundFiles.length)];
_playSound(audioSpec);
} else if (this.lastJumpState && !this.player.avatar.jumpState) {
const audioSpec = landSoundFiles[Math.floor(Math.random() * landSoundFiles.length)];
_playSound(audioSpec);
}
this.lastJumpState = this.player.avatar.jumpState;
}
// step
if (idleWalkFactor > 0.7 && !this.player.avatar.jumpState && !this.player.avatar.flyState) {
const isRunning = walkRunFactor > 0.5;
const isCrouching = crouchFactor > 0.5;
const isNarutoRun = this.player.avatar.narutoRunState;
const walkRunAnimationName = (() => {
if (isNarutoRun) {
return 'naruto run.fbx';
} else {
const animationAngles = isCrouching ?
Avatar.getClosest2AnimationAngles('crouch', this.player.avatar.getAngle())
:
(isRunning ?
Avatar.getClosest2AnimationAngles('run', this.player.avatar.getAngle())
:
Avatar.getClosest2AnimationAngles('walk', this.player.avatar.getAngle())
);
return animationAngles[0].name;
}
})();
const soundFiles = (() => {
if (isNarutoRun) {
return narutoRunSoundFiles;
} else if (isCrouching) {
return walkSoundFiles;
} else if (isRunning) {
return runSoundFiles;
} else {
return walkSoundFiles;
}
})();
const animations = Avatar.getAnimations();
const animation = animations.find(a => a.name === walkRunAnimationName);
const animationStepIndices = Avatar.getAnimationStepIndices();
const animationIndices = animationStepIndices.find(i => i.name === walkRunAnimationName);
const {leftStepIndices, rightStepIndices} = animationIndices;
const offset = offsets[walkRunAnimationName] ?? 0; // ?? window.lol;
const _getStepIndex = timeSeconds => {
const timeMultiplier = walkRunAnimationName === 'naruto run.fbx' ? narutoRunTimeFactor : 1;
const walkTime = (timeSeconds * timeMultiplier + offset) % animation.duration;
const walkFactor = walkTime / animation.duration;
const stepIndex = Math.floor(mod(walkFactor, 1) * leftStepIndices.length);
return stepIndex;
};
const startIndex = _getStepIndex(this.lastWalkTime);
const endIndex = _getStepIndex(timeSeconds);
for (let i = startIndex;; i++) {
i = i % leftStepIndices.length;
if (i !== endIndex) {
if (leftStepIndices[i] && !this.lastStepped[0]) {
const candidateAudios = soundFiles//.filter(a => a.paused);
if (candidateAudios.length > 0) {
/* for (const a of candidateAudios) {
!a.paused && a.pause();
} */
const audioSpec = candidateAudios[Math.floor(Math.random() * candidateAudios.length)];
_playSound(audioSpec);
}
}
this.lastStepped[0] = leftStepIndices[i];
if (rightStepIndices[i] && !this.lastStepped[1]) {
const candidateAudios = soundFiles// .filter(a => a.paused);
if (candidateAudios.length > 0) {
/* for (const a of candidateAudios) {
!a.paused && a.pause();
} */
const audioSpec = candidateAudios[Math.floor(Math.random() * candidateAudios.length)];
_playSound(audioSpec);
}
}
this.lastStepped[1] = rightStepIndices[i];
} else {
break;
}
}
this.lastWalkTime = timeSeconds;
}
}
destroy() {
// nothing
}
}
export {
waitForLoad,
CharacterSfx,
};