# 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

```vue
<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.

```vue
<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)](#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']]`

```vue
<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.[<sup>

1

</sup>

](#precisely)
- If the `:mix-` prop is `1.0`, 'progress' through the `:sequence-` is determined entirely by the particle's lifetime.

[](undefined) 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.

```vue
<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

<table>
<thead>
  <tr>
    <th align="left">
      Name
    </th>
    
    <th align="left">
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td align="left">
      <strong>
        map
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Texture | string
      </code>
      
      <br />
      
      Default: <code>
        'https://raw.githubusercontent.com/Tresjs/asset...'
      </code>
      
      <br />
      
      <br />
      
      Texture or image path for individual sparkles
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        geometry
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Object3D | BufferGeometry
      </code>
      
      <br />
      
      Default: <code>
        undefined
      </code>
      
      <br />
      
      <br />
      
      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.<br />
      
      <ul>
        <li>
          If provided, the component will use the passed geometry.
        </li>
        
        <li>
          If no geometry is provided, the component will try to make a copy of the parent object's geometry.
        </li>
        
        <li>
          If no parent geometry exists, the component will create and use an IcosphereGeometry.
        </li>
      </ul>
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        directionalLight
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Object3D
      </code>
      
      <br />
      
      Default: <code>
        undefined
      </code>
      
      <br />
      
      <br />
      
      Particles "light up" when their normal "faces" the light. If no <code>
        directionalLight
      </code>
      
       is provided, the default "up" vector will be used.
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        lifetimeSec
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        0.4
      </code>
      
      <br />
      
      <br />
      
      Particle lifetime in seconds
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        cooldownSec
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        2.0
      </code>
      
      <br />
      
      <br />
      
      Particle cooldown in seconds – time between lifetime end and respawn
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        normalThreshold
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        0.7
      </code>
      
      <br />
      
      <br />
      
      Number from 0-1 indicating how closely the particle needs to be faced towards the light to "light up". (Lower == more flexible)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        noiseScale
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        3.0
      </code>
      
      <br />
      
      <br />
      
      Scale of the noise period (lower == more slowly cycling noise)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        scaleNoise
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.0
      </code>
      
      <br />
      
      <br />
      
      Noise coefficient applied to particle scale
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        offsetNoise
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        0.1
      </code>
      
      <br />
      
      <br />
      
      Noise coefficient applied to particle offset
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        lifetimeNoise
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        0.0
      </code>
      
      <br />
      
      <br />
      
      Noise coefficient applied to particle lifetime
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        size
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.0
      </code>
      
      <br />
      
      <br />
      
      Particle scale multiplier
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        alpha
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.0
      </code>
      
      <br />
      
      <br />
      
      Opacity multiplier
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        offset
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.0
      </code>
      
      <br />
      
      <br />
      
      Offset multiplier
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        surfaceDistance
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.0
      </code>
      
      <br />
      
      <br />
      
      Surface distance multiplier
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        sequenceColor
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Gradient<TresColor>
      </code>
      
      <br />
      
      Default: <code>
        [[0.7, '#82dbc5'], [0.8, '#fbb03b']]
      </code>
      
      <br />
      
      <br />
      
      '<em>
        Sequence' props: specify how a particle changes as it "progresses". See also "mix
      </em>
      
      " props.<br />
      
      Color sequence as particles progress
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        sequenceAlpha
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Gradient<number>
      </code>
      
      <br />
      
      Default: <code>
        [[0.0, 0.0], [0.10, 1.0], [0.5, 1.0], [0.9, 0.0]]
      </code>
      
      <br />
      
      <br />
      
      Opacity sequence as particles progress
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        sequenceOffset
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Gradient<[number, number, number]>
      </code>
      
      <br />
      
      Default: <code>
        [0.0, 0.0, 0.0]
      </code>
      
      <br />
      
      <br />
      
      Distance sequence as particles progress
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        sequenceNoise
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Gradient<[number, number, number]>
      </code>
      
      <br />
      
      Default: <code>
        [0.1, 0.1, 0.1]
      </code>
      
      <br />
      
      <br />
      
      Noise sequence as particles progress
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        sequenceSize
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Gradient<number>
      </code>
      
      <br />
      
      Default: <code>
        [0.0, 1.0]
      </code>
      
      <br />
      
      <br />
      
      Size sequence as particles progress
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        sequenceSurfaceDistance
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Gradient<number>
      </code>
      
      <br />
      
      Default: <code>
        [0.05, 0.08, 0.1]
      </code>
      
      <br />
      
      <br />
      
      Distance from surface (along normal) as particles progress
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        mixColor
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        0.5
      </code>
      
      <br />
      
      <br />
      
      'mix*' props: A particle "progresses" with a mix of two factors:<br />
      
      <ul>
        <li>
          its normal "facing" the directionalLight
        </li>
        
        <li>
          its lifetime
        </li>
      </ul>
      
      'mix*' props specify the relationship between the two factors.<br />
      
      How is a particle's progress for color calculated? (0: normal, 1: particle lifetime)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        mixAlpha
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.
      </code>
      
      <br />
      
      <br />
      
      How is a particle's progress for alpha calculated? (0: normal, 1: particle lifetime)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        mixOffset
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.
      </code>
      
      <br />
      
      <br />
      
      How is a particle's progress for offset calculated? (0: normal, 1: particle lifetime)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        mixSize
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        0.
      </code>
      
      <br />
      
      <br />
      
      How is a particle's progress for size calculated? (0: normal, 1: particle lifetime)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        mixSurfaceDistance
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.
      </code>
      
      <br />
      
      <br />
      
      How is a particle's progress for surface distance calculated? (0: normal, 1: particle lifetime)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        mixNoise
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        number
      </code>
      
      <br />
      
      Default: <code>
        1.
      </code>
      
      <br />
      
      <br />
      
      How is a particle's progress for lifetime calculated? (0: normal, 1: particle lifetime)
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        blending
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        Blending
      </code>
      
      <br />
      
      Default: <code>
        AdditiveBlending
      </code>
      
      <br />
      
      <br />
      
      Material blending
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        transparent
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        boolean
      </code>
      
      <br />
      
      Default: <code>
        true
      </code>
      
      <br />
      
      <br />
      
      Material transparency
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <strong>
        depthWrite
      </strong>
    </td>
    
    <td align="left">
      Type: <code>
        boolean
      </code>
      
      <br />
      
      Default: <code>
        false
      </code>
      
      <br />
      
      <br />
      
      Material depth write
    </td>
  </tr>
</tbody>
</table>
