Spatial Audio
3D positioned audio for immersive sound.
Basic Usage
import { Spatial } from '@gamebyte/framework';
// Set listener position (usually camera)
Spatial.setListener({
position: camera.position,
orientation: camera.quaternion
});
// Play sound at position
Spatial.play('explosion', {
position: { x: 10, y: 0, z: 5 }
});
🎮Spatial Audio Demo
Loading demo...
Theme Support
This demo automatically adapts to your selected theme. Try toggling the theme using the 🌙/☀️ button in the navigation bar!
Configuration
Spatial.play('footstep', {
position: { x: 5, y: 0, z: 3 },
volume: 1.0,
refDistance: 1, // Full volume distance
maxDistance: 50, // Sound cutoff
rolloffFactor: 1, // Attenuation curve
loop: false
});
Updating Listener
// Update every frame for moving listener
function update(deltaTime: number) {
Spatial.setListener({
position: player.position,
orientation: player.quaternion
});
}
Sound Sources
// Create persistent sound source
const ambience = Spatial.createSource('forest-ambience', {
position: { x: 0, y: 0, z: 0 },
loop: true,
volume: 0.5
});
ambience.play();
// Update position
ambience.setPosition({ x: 10, y: 0, z: 5 });
// Stop
ambience.stop();
2D Panning (For 2D Games)
import { Spatial } from '@gamebyte/framework';
// Enable 2D mode (left/right panning only)
Spatial.setMode('2d');
// Play with panning based on X position
Spatial.play('hit', {
position: { x: -100, y: 0 } // Left side
});