5.4.0

Sparkles

Makes sparkles on geometry vertices, optionally guided by directional light.

<Sparkles /> makes sparkles on your geometry's vertices – optionally guided by a directional light.

Usage

Basic

<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { Sparkles } from '@tresjs/cientos'
</script>

<template>
  <TresCanvas>
    <TresPerspectiveCamera :position="[0, 3, 5]" />
    <TresMesh>
      <TresSphereGeometry />
      <Sparkles />
    </TresMesh>
    <TresAmbientLight />
  </TresCanvas>
</template>

With TresDirectionalLight

By default, sparkles appear on the up-facing vertices. However, you can pass a directional light to the component. Moving the directional light will cause "lit" vertices to emit sparkles.

<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { Sparkles } from '@tresjs/cientos'
import { shallowRef } from 'vue'

const directionalLightRef = shallowRef()
</script>

<template>
  <TresCanvas>
    <TresPerspectiveCamera :position="[0, 3, 5]" />
    <TresMesh>
      <TresSphereGeometry />
      <Sparkles :directional-light="directionalLightRef" />
    </TresMesh>
    <TresDirectionalLight
      ref="directionalLightRef"
      :position="[3, 3, 3]"
      :intensity="2"
    />
    <TresAmbientLight />
  </TresCanvas>
</template>

Sequences

All props beginning with :sequence- are used to define how a particle changes as it progresses (See also: Mixes). :sequence- props are of the type Gradient<T>, which can be any one of:

  • T: a single value
  • [T, T, T, ...]: an evenly distributed series of values
  • [[number, T], [number, T], ...]: an unevently distributed series of values, where number is a gradient "stop" from 0 to 1.

For example, all of these are acceptable values for Gradient<TresColor>:

  • 'red'
  • ['red', 'blue', 'green']
  • [[0.1, 'red'], [0.25, 'blue'], [0.5, 'green']]
<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { Sparkles } from '@tresjs/cientos'
</script>

<template>
  <TresCanvas>
    <TresPerspectiveCamera :position="[0, 3, 5]" />
    <TresMesh>
      <TresSphereGeometry />
      <Sparkles
        :sequence-color="['red', 'blue', 'green']"
        :sequence-alpha="[[0.0, 0.0], [0.10, 1.0], [0.5, 1.0], [0.9, 0.0]]"
        :sequence-size="[0.0, 1.0, 0.5]"
      />
    </TresMesh>
    <TresAmbientLight />
  </TresCanvas>
</template>

Mixes

All props beginning with :mix- allow you to specify how a particle "progresses" through a corresponding :sequence- prop. E.g., :mix-alpha affects :sequence-alpha.

  • If the :mix- prop is 0.0, 'progress' through the :sequence- is determined entirely by the light shining on the surface of the sparkling mesh.1
  • If the :mix- prop is 1.0, 'progress' through the :sequence- is determined entirely by the particle's lifetime.

More precisely, the value is determined by the dot product of the directionalLight's inverted normalized position and each of the sparkling mesh's vertex normals.

<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { Sparkles } from '@tresjs/cientos'
import { shallowRef } from 'vue'

const directionalLightRef = shallowRef()
</script>

<template>
  <TresCanvas>
    <TresPerspectiveCamera :position="[0, 3, 5]" />
    <TresMesh>
      <TresSphereGeometry />
      <Sparkles
        :directional-light="directionalLightRef"
        :mix-color="0.8"
        :mix-alpha="0.5"
        :mix-size="0.2"
      />
    </TresMesh>
    <TresDirectionalLight
      ref="directionalLightRef"
      :position="[3, 3, 3]"
    />
    <TresAmbientLight />
  </TresCanvas>
</template>

Props

NameDescription
mapType: Texture | string
Default: 'https://raw.githubusercontent.com/Tresjs/asset...'

Texture or image path for individual sparkles
geometryType: Object3D | BufferGeometry
Default: undefined

Vertices of the geometry will be used to emit sparkles. Geometry normals are used for sparkles' traveling direction and for responding to the directional light prop.
  • If provided, the component will use the passed geometry.
  • If no geometry is provided, the component will try to make a copy of the parent object's geometry.
  • If no parent geometry exists, the component will create and use an IcosphereGeometry.
directionalLightType: Object3D
Default: undefined

Particles "light up" when their normal "faces" the light. If no directionalLight is provided, the default "up" vector will be used.
lifetimeSecType: number
Default: 0.4

Particle lifetime in seconds
cooldownSecType: number
Default: 2.0

Particle cooldown in seconds – time between lifetime end and respawn
normalThresholdType: number
Default: 0.7

Number from 0-1 indicating how closely the particle needs to be faced towards the light to "light up". (Lower == more flexible)
noiseScaleType: number
Default: 3.0

Scale of the noise period (lower == more slowly cycling noise)
scaleNoiseType: number
Default: 1.0

Noise coefficient applied to particle scale
offsetNoiseType: number
Default: 0.1

Noise coefficient applied to particle offset
lifetimeNoiseType: number
Default: 0.0

Noise coefficient applied to particle lifetime
sizeType: number
Default: 1.0

Particle scale multiplier
alphaType: number
Default: 1.0

Opacity multiplier
offsetType: number
Default: 1.0

Offset multiplier
surfaceDistanceType: number
Default: 1.0

Surface distance multiplier
sequenceColorType: Gradient<TresColor>
Default: [[0.7, '#82dbc5'], [0.8, '#fbb03b']]

'Sequence' props: specify how a particle changes as it "progresses". See also "mix" props.
Color sequence as particles progress
sequenceAlphaType: Gradient<number>
Default: [[0.0, 0.0], [0.10, 1.0], [0.5, 1.0], [0.9, 0.0]]

Opacity sequence as particles progress
sequenceOffsetType: Gradient<[number, number, number]>
Default: [0.0, 0.0, 0.0]

Distance sequence as particles progress
sequenceNoiseType: Gradient<[number, number, number]>
Default: [0.1, 0.1, 0.1]

Noise sequence as particles progress
sequenceSizeType: Gradient<number>
Default: [0.0, 1.0]

Size sequence as particles progress
sequenceSurfaceDistanceType: Gradient<number>
Default: [0.05, 0.08, 0.1]

Distance from surface (along normal) as particles progress
mixColorType: number
Default: 0.5

'mix*' props: A particle "progresses" with a mix of two factors:
  • its normal "facing" the directionalLight
  • its lifetime
'mix*' props specify the relationship between the two factors.
How is a particle's progress for color calculated? (0: normal, 1: particle lifetime)
mixAlphaType: number
Default: 1.

How is a particle's progress for alpha calculated? (0: normal, 1: particle lifetime)
mixOffsetType: number
Default: 1.

How is a particle's progress for offset calculated? (0: normal, 1: particle lifetime)
mixSizeType: number
Default: 0.

How is a particle's progress for size calculated? (0: normal, 1: particle lifetime)
mixSurfaceDistanceType: number
Default: 1.

How is a particle's progress for surface distance calculated? (0: normal, 1: particle lifetime)
mixNoiseType: number
Default: 1.

How is a particle's progress for lifetime calculated? (0: normal, 1: particle lifetime)
blendingType: Blending
Default: AdditiveBlending

Material blending
transparentType: boolean
Default: true

Material transparency
depthWriteType: boolean
Default: false

Material depth write