Hology
Hology
Hology
  • 👋Welcome to Hology Docs
  • Getting started
    • Introduction to Hology
    • The first steps
    • Editor basics
      • Flying in scenes
      • Placing objects
      • Select objects
      • Transform
      • Grouping objects
      • Duplicate
    • Starter project - Third person shooter
  • Tutorials
    • Rolling ball - Gameplay programming
    • Character movement programming
    • Character AI behavior
  • Release
    • Distribution
      • Discord Activities
      • Facebook Instant Games
      • Upload to Itch.io
      • Host with GitHub Pages
      • Publishing to Steam
      • iOS and Android
  • Assets
    • 3D Models
      • Custom collision shapes
      • Material slots
    • Materials
    • Textures
    • Prefabs
  • Gameplay
    • Actors
      • Creating actor classes
      • Actor parameters
      • Actor components
      • Actor lifecycle
      • Spawning actors
      • Moving actors
    • Services
      • Load assets
    • Player input
    • Collision detection
    • Physics
      • Physics body types
      • Applying forces
      • Ray casting
    • Trigger volumes
    • Character movement
    • Pointer events
    • Animation
      • Animation system
      • Character Animation
      • Animation State Machine
    • Sound
      • Audio parameter
    • World
    • Navigation
  • Shaders
    • Introduction to shaders
    • Creating shaders
    • Shader parameters
    • Typescript shaders
      • Types
      • Math functions
      • Attributes
      • Varying
      • Uniforms
      • Textures
      • Arrays
      • Select
      • Lighting
    • Painted materials
    • Water shader tutorial
  • Level design
    • Landscape sculpting
    • Landscape painting
    • Grass
  • User Interfaces
    • Creating user interfaces
    • Using React
    • VR
  • Visual Effects
    • Introduction to VFX
    • VFX Assets
  • Integrations
    • Arcweave
Powered by GitBook
On this page
  1. Gameplay

Sound

PreviousAnimation State MachineNextAudio parameter

Last updated 12 months ago

Playing sound is supported in Hology Engine using ThreeJS. See their documentation for more detail on how to use and .

Playing sound

In the code below we can see how we can utilize Audio instances, load audio files and play sound.

The following numbered points refer to different parts of the code below.

  1. Each sound that needs to be played needs its own instance of Audio. You can reuse audio instances but to play multiple sounds at the same time, you will need separate instances.

  2. Load an audio file directly from the public directory. These don't have to be imported in the editor before.

  3. To use the loaded audio, use the setBuffer function. You can also adjust the volume and other settings which you can read more about in the .

  4. Play the sound. If the sound is already playing however, you need to stop it first.

import { inject, Service, World, GameInstance, ViewController, AssetLoader } from '@hology/core/gameplay';
import * as THREE from 'three';

@Service()
class Game extends GameInstance {
  private world = inject(World)
  private view = inject(ViewController)
  private assetLoader = inject(AssetLoader)

  // 1
  private sound = new THREE.Audio(this.view.audioListener);

  async onStart() {    
    // 2
    const buffer = await this.assetLoader.getAudioAtPath('data/asset-resources/impactWood_heavy_001.ogg')
    // 3
    this.sound.setBuffer(buffer).setVolume(0.5)
  }

  playSound() {
    if (this.sound.isPlaying) {
      this.sound.stop()
    }
    // 4
    this.sound.play();
  }
}

export default Game

You can see a complete example for playing sound and run it locally to it out yourself here.

Audio
Positional Audio
ThreeJS docs
LogoGitHub - hologyengine/audio-tutorial: How to use audio with Hology EngineGitHub