Sparkles
<Sparkles /> makes sparkles on your geometry's vertices – optionally guided by a directional light.
Usage
Basic
<script setup lang="ts">
import { OrbitControls, Sparkles, TorusKnot } from '@tresjs/cientos'
import { TresCanvas } from '@tresjs/core'
</script>
<template>
<TresCanvas clear-color="#333">
<TresPerspectiveCamera :position="[0, 0, 5]" />
<TorusKnot :args="[1, 0.25, 128, 16]">
<TresMeshBasicMaterial color="#222" />
<Sparkles />
</TorusKnot>
<OrbitControls />
</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 { OrbitControls, Sparkles, Sphere, Torus } from '@tresjs/cientos'
import { TresCanvas, useRenderLoop } from '@tresjs/core'
import { shallowRef } from 'vue'
const lightRef = shallowRef()
useRenderLoop().onLoop(({ elapsed }) => {
if (lightRef.value) {
lightRef.value.position.x = Math.cos(elapsed) * 2.5
lightRef.value.position.y = Math.sin(elapsed) * 2.5
}
})
</script>
<template>
<TresCanvas clear-color="#333">
<TresPerspectiveCamera :position="[0, 0, 8]" />
<TresDirectionalLight ref="lightRef">
<Sphere
color="white"
:scale="0.1"
/>
</TresDirectionalLight>
<Torus :args="[1, 0.25, 16, 48]">
<TresMeshBasicMaterial color="#222" />
<Sparkles :directional-light="lightRef" />
</Torus>
<OrbitControls />
</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, wherenumberis a gradient "stop" from0to1.
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 { OrbitControls, Sparkles, Sphere } from '@tresjs/cientos'
import { TresCanvas } from '@tresjs/core'
</script>
<template>
<TresCanvas clear-color="#333">
<TresPerspectiveCamera :position="[0, 0, 8]" />
<Sphere>
<TresMeshBasicMaterial color="#222" />
<Sparkles
:sequence-alpha="[[0., 0.], [0.6, 1.0], [0.7, 0.0], [1.0, 1.0]]"
:sequence-color="['yellow', 'white', 'orange', 'red', 'black']"
:sequence-offset="[[0.7, [0, 0, 0]], [0.75, [0, 0.1, 0]], [1.0, [0, 0.5, 0]]]"
:sequence-size="[[0.0, 0.0], [0.7, 1.0]]"
:sequence-surface-distance="[[0.0, 0.0], [0.7, 1.0]]"
:lifetime-sec="2.0"
:size="2"
:surface-distance="0.8"
:mix-color="1.0"
/>
</Sphere>
<OrbitControls />
</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 is0.0, 'progress' through the:sequence-is determined entirely by the light shining on the surface of the sparkling mesh.1 - If the
:mix-prop is1.0, 'progress' through the:sequence-is determined entirely by the particle's lifetime.
1) 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 { Sparkles, Sphere } from '@tresjs/cientos'
import { TresCanvas, useRenderLoop } from '@tresjs/core'
import { TresLeches, useControls } from '@tresjs/leches'
import { shallowRef } from 'vue'
import '@tresjs/leches/styles'
const lightRef = shallowRef()
const { value: mix } = useControls({
mix: { value: 0, min: 0, max: 1, step: 0.01 },
})
useRenderLoop().onLoop(({ elapsed }) => {
if (lightRef.value) {
lightRef.value.position.x = Math.cos(elapsed) * 3
lightRef.value.position.y = Math.sin(elapsed) * 3
}
})
</script>
<template>
<TresLeches class="top-0 important-left-4" />
<TresCanvas clear-color="#333">
<TresPerspectiveCamera :position="[-2.5, 0, 8]" />
<TresDirectionalLight ref="lightRef">
<Sphere
color="white"
:scale="0.1"
/>
</TresDirectionalLight>
<Sphere :args="[1, 16, 16]">
<TresMeshBasicMaterial color="#222" />
<Sparkles
:directional-light="lightRef"
:mix-alpha="mix"
:mix-color="mix"
:mix-offset="mix"
:mix-size="mix"
:mix-surface-distance="mix"
:lifetime-sec="2"
:sequence-alpha="[0.1, 1.0]"
:sequence-surface-distance="[0.1, 0.5]"
/>
</Sphere>
</TresCanvas>
</template>Props
| Name | Description |
|---|---|
| map | Type: Texture | stringDefault: 'https://raw.githubusercontent.com/Tresjs/asset...Texture or image path for individual sparkles |
| geometry | Type: Object3D | BufferGeometryDefault: undefinedVertices 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.
|
| directionalLight | Type: Object3DDefault: undefinedParticles "light up" when their normal "faces" the light. If no directionalLight is provided, the default "up" vector will be used. |
| lifetimeSec | Type: numberDefault: 0.4Particle lifetime in seconds |
| cooldownSec | Type: numberDefault: 2.0Particle cooldown in seconds – time between lifetime end and respawn |
| normalThreshold | Type: numberDefault: 0.7Number from 0-1 indicating how closely the particle needs to be faced towards the light to "light up". (Lower == more flexible) |
| noiseScale | Type: numberDefault: 3.0Scale of the noise period (lower == more slowly cycling noise) |
| scaleNoise | Type: numberDefault: 1.0Noise coefficient applied to particle scale |
| offsetNoise | Type: numberDefault: 0.1Noise coefficient applied to particle offset |
| lifetimeNoise | Type: numberDefault: 0.0Noise coefficient applied to particle lifetime |
| size | Type: numberDefault: 1.0Particle scale multiplier |
| alpha | Type: numberDefault: 1.0Opacity multiplier |
| offset | Type: numberDefault: 1.0Offset multiplier |
| surfaceDistance | Type: numberDefault: 1.0Surface distance multiplier |
| sequenceColor | Type: 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 |
| sequenceAlpha | Type: Gradient<number>Default: [[0.0, 0.0], [0.10, 1.0], [0.5, 1.0], [0.9, 0.0]]Opacity sequence as particles progress |
| sequenceOffset | Type: Gradient<[number, number, number]>Default: [0.0, 0.0, 0.0]Distance sequence as particles progress |
| sequenceNoise | Type: Gradient<[number, number, number]>Default: [0.1, 0.1, 0.1]Noise sequence as particles progress |
| sequenceSize | Type: Gradient<number>Default: [0.0, 1.0]Size sequence as particles progress |
| sequenceSurfaceDistance | Type: Gradient<number>Default: [0.05, 0.08, 0.1]Distance from surface (along normal) as particles progress |
| mixColor | Type: numberDefault: 0.5'mix*' props: A particle "progresses" with a mix of two factors:
How is a particle's progress for color calculated? (0: normal, 1: particle lifetime) |
| mixAlpha | Type: numberDefault: 1.How is a particle's progress for alpha calculated? (0: normal, 1: particle lifetime) |
| mixOffset | Type: numberDefault: 1.How is a particle's progress for offset calculated? (0: normal, 1: particle lifetime) |
| mixSize | Type: numberDefault: 0.How is a particle's progress for size calculated? (0: normal, 1: particle lifetime) |
| mixSurfaceDistance | Type: numberDefault: 1.How is a particle's progress for surface distance calculated? (0: normal, 1: particle lifetime) |
| mixNoise | Type: numberDefault: 1.How is a particle's progress for lifetime calculated? (0: normal, 1: particle lifetime) |
| blending | Type: BlendingDefault: AdditiveBlendingMaterial blending |
| transparent | Type: booleanDefault: trueMaterial transparency |
| depthWrite | Type: booleanDefault: falseMaterial depth write |