Three Vue3Three Vue3
Guide
Demo
AI Agents
  • English
  • 简体中文
GitHub
Guide
Demo
AI Agents
  • English
  • 简体中文
GitHub
    • Getting Started
    • Scene
    • Parameter Bus
      • Default Usage
      • Methods
      • GlobalParameters
      • Custom instance
    • SkyBox
    • Model Loader

      • Component Loader
      • Function Loader
    • Popup
    • Callout
    • Movable Element
    • Animation
    • Model Rotator
    • Picker
    • Fit Camera
    • Mesh

      • Wave Circle Mesh
      • Flow Line Mesh
    • Effect

      • Nature

        • Rain
        • Snow
        • Water
        • Water Pool
        • Sandy Beach
      • Sky & Atmosphere

        • Sky Dome
        • HeightFog
        • Nebula Galaxy
      • Light

        • Bloom
        • Sweep Light
        • Light Gradient
        • Light Pillar
        • Radar Sweep
        • Signal Wave
        • Force Shield
      • Particles & Sprites

        • Fireworks
        • Text Fireworks
        • Glow Sprite
    • GIS

      • Geographic Reference
      • GeoObject
      • GeoJsonLayer
      • Tile Layer

type

Class

globalParameterBus is a tiny shared store for scene-wide values. Effects subscribe to a key and react to changes, so a single update reaches the sky dome, the lights, and anything else listening. sunDirection is the built-in key.

Default Usage

Click me to view the codes
<template>
  <div>
    <tv-scene class="scene" @created="created"></tv-scene>
    <label class="control">
      Sun elevation
      <input v-model.number="elevation" type="range" min="-5" max="70" step="1" />
    </label>
  </div>
</template>

<script lang="ts" setup>
import { onBeforeUnmount, ref, watch } from 'vue'
import * as THREE from 'three'
import { SkyDome, globalParameterBus } from 'three-vue3'

const elevation = ref(18)

let dome: SkyDome | undefined

const sunDirection = (degrees: number) => {
  const phi = THREE.MathUtils.degToRad(degrees)
  return new THREE.Vector3(Math.cos(phi), Math.sin(phi), 0.6).normalize()
}

const created = (scene, { camera }) => {
  camera.position.set(0, 2, 8)

  // No sunDirection is passed, so the dome follows the global parameter bus.
  dome = new SkyDome({ radius: 60 })
  scene.add(dome)

  globalParameterBus.set('sunDirection', sunDirection(elevation.value))
}

watch(elevation, (value) => {
  globalParameterBus.set('sunDirection', sunDirection(value))
})

onBeforeUnmount(() => {
  dome?.dispose()
  globalParameterBus.reset()
})
</script>

<style>
.scene {
  margin-top: 10px;
  width: 100%;
  height: 300px;
}

.control {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 14px;
}
</style>

Methods

NameParametersDescription
get<K>(key: K) => GlobalParameters[K]Returns the current value.
set<K>(key: K, value: GlobalParameters[K]) => voidWrites a value and notifies subscribers.
subscribe<K>(key: K, listener: (value) => void) => () => voidSubscribes to changes, returns an unsubscribe function.
reset() => voidRestores the default values.

GlobalParameters

NameTypeDefaultDescription
sunDirectionTHREE.Vector3(-0.78, 0.2, -0.6) normalizedShared sun direction used by the sky.

Custom instance

import { ParameterBus } from 'three-vue3'

const bus = new ParameterBus()
const unsubscribe = bus.subscribe('sunDirection', (direction) => {
  console.log(direction)
})
Edit this page on GitHub
Last Updated:
Contributors: Shing Rui
Prev
Scene
Next
SkyBox