threlte logo
@threlte/extras

<Stars>

webgl <Stars> works with WebGL only.

This component is a port of drei’s <Stars> component, which adds a blinking shader-based starfield to your scene.

<script lang="ts">
  import { Canvas } from '@threlte/core'
  import { Checkbox, Folder, Pane, Slider } from 'svelte-tweakpane-ui'
  import Scene from './Scene.svelte'

  let count = $state(5000)
  let radius = $state(50)
  let depth = $state(50)
  let factor = $state(6)
  let saturation = $state(1)
  let lightness = $state(0.8)
  let opacity = $state(1)
  let fade = $state(true)
  let rounded = $state(false)
  let speed = $state(0)
</script>

<Pane
  title="Stars"
  position="fixed"
>
  <Folder title="Distribution">
    <Slider
      label="count"
      bind:value={count}
      min={100}
      max={20000}
      step={100}
    />
    <Slider
      label="radius"
      bind:value={radius}
      min={1}
      max={200}
      step={1}
    />
    <Slider
      label="depth"
      bind:value={depth}
      min={1}
      max={200}
      step={1}
    />
  </Folder>
  <Folder title="Appearance">
    <Slider
      label="factor"
      bind:value={factor}
      min={0}
      max={20}
      step={0.1}
    />
    <Slider
      label="saturation"
      bind:value={saturation}
      min={0}
      max={1}
      step={0.01}
    />
    <Slider
      label="lightness"
      bind:value={lightness}
      min={0}
      max={1}
      step={0.01}
    />
    <Slider
      label="opacity"
      bind:value={opacity}
      min={0}
      max={1}
      step={0.01}
    />
    <Checkbox
      label="fade"
      bind:value={fade}
    />
    <Checkbox
      label="rounded"
      bind:value={rounded}
    />
  </Folder>
  <Folder title="Animation">
    <Slider
      label="speed"
      bind:value={speed}
      min={0}
      max={5}
      step={0.1}
    />
  </Folder>
</Pane>

<div>
  <Canvas>
    <Scene
      {count}
      {radius}
      {depth}
      {factor}
      {saturation}
      {lightness}
      {opacity}
      {fade}
      {rounded}
      {speed}
    />
  </Canvas>
</div>

<style>
  div {
    height: 100%;
  }
</style>
<script lang="ts">
  import { T } from '@threlte/core'
  import { OrbitControls, Grid, Stars } from '@threlte/extras'

  interface Props {
    count: number
    radius: number
    depth: number
    factor: number
    saturation: number
    lightness: number
    opacity: number
    fade: boolean
    rounded: boolean
    speed: number
  }

  let { ...rest }: Props = $props()
</script>

<Stars {...rest} />

<T.PerspectiveCamera
  makeDefault
  position.y={1}
  position.x={2}
  position.z={5}
  fov={90}
>
  <OrbitControls
    enableDamping
    enablePan={false}
    enableZoom={false}
    autoRotate
    autoRotateSpeed={0.3}
  />
</T.PerspectiveCamera>

<Grid
  infiniteGrid
  fadeOrigin={[0, 0, 0]}
  fadeDistance={10}
  cellColor="#dddddd"
  sectionColor="#ddd"
/>

Examples

Basic Example

Billboard.svelte
<script lang="ts">
  import { Stars } from '@threlte/extras'
</script>

<Stars />

Component Signature

<Stars> extends < T . Points > and supports all its props, snippets, bindings and events.

Props

name
type
required
default
description

count
number
no
5000
The number of stars.

depth
number
no
50
The thickness of the shell the stars are spread across, so they sit between radius and radius + depth from the center.

factor
number
no
6
The size of the stars. Each star gets a random size between half and all of it.

fade
boolean
no
true
Fades each star out toward its edges, drawing a soft glow instead of a hard shape.

lightness
number
no
0.8
The lightness of the stars' colors.

opacity
number
no
1
The opacity of the stars.

radius
number
no
50
The minimum distance of the stars from the center.

rounded
boolean
no
false
Renders each point as an antialiased circle instead of a square. Combine with fade for a soft halo, or disable fade for crisp filled discs.

saturation
number
no
1
The saturation of the stars' colors, which run through every hue.

speed
number
no
0
How fast the stars twinkle. 0 keeps them still.