feat: implement pitch which was required for note blocks to work properly

This commit is contained in:
Vitaly Turovsky 2024-07-13 20:31:27 +03:00
commit 6c5f72e1f2
2 changed files with 4 additions and 3 deletions

View file

@ -32,7 +32,7 @@ export class Viewer {
this.world.camera = camera
}
constructor(public renderer: THREE.WebGLRenderer, worldConfig = defaultWorldRendererConfig) {
constructor (public renderer: THREE.WebGLRenderer, worldConfig = defaultWorldRendererConfig) {
// https://discourse.threejs.org/t/updates-to-color-management-in-three-js-r152/50791
THREE.ColorManagement.enabled = false
renderer.outputColorSpace = THREE.LinearSRGBColorSpace
@ -118,7 +118,7 @@ export class Viewer {
this.world.updateCamera(pos?.offset(0, yOffset, 0) ?? null, yaw, pitch)
}
playSound (position: Vec3, path: string, volume = 1) {
playSound (position: Vec3, path: string, volume = 1, pitch = 1) {
if (!this.audioListener) {
this.audioListener = new THREE.AudioListener()
this.camera.add(this.audioListener)
@ -134,6 +134,7 @@ export class Viewer {
sound.setBuffer(buffer)
sound.setRefDistance(20)
sound.setVolume(volume)
sound.setPlaybackRate(pitch) // set the pitch
this.scene.add(sound)
// set sound position
sound.position.set(position.x, position.y, position.z)

View file

@ -50,7 +50,7 @@ subscribeKey(miscUiState, 'gameLoaded', async () => {
const isMuted = options.mutedSounds.includes(soundKey) || options.volume === 0
if (position) {
if (!isMuted) {
viewer.playSound(position, url, soundVolume * Math.max(Math.min(volume, 1), 0) * (options.volume / 100))
viewer.playSound(position, url, soundVolume * Math.max(Math.min(volume, 1), 0) * (options.volume / 100), Math.max(Math.min(pitch ?? 1, 2), 0.5))
}
if (getDistance(bot.entity.position, position) < 4 * 16) {
lastPlayedSounds.lastServerPlayed[soundKey] ??= { count: 0, last: 0 }