Three Vue3Three Vue3
Guide
Demo
AI Agents
  • English
  • 简体中文
GitHub
Guide
Demo
AI Agents
  • English
  • 简体中文
GitHub
    • Getting Started
    • Scene
    • Parameter Bus
    • 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
          • Default Usage
          • Collision & Splash
          • Props
          • Class usage
        • 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

Component

<tv-rain> renders a GPU rain field inside a <tv-scene>. Each drop is a billboard quad stretched into a streak and wrapped in a box around the camera, so the rain follows the view without spawning new geometry, and every drop lands on the surface it hits.

The shader is a TSL node material, so it renders on WebGPURenderer (the default) and on a classic WebGLRenderer through WebGLNodesHandler. Add renderer-type="webgl" to <tv-scene> to compare the two backends.

The same effect is available as the RainEffect class when you are not using the scene component.

Default Usage

Click me to view the codes
<template>
  <tv-scene class="scene" bg-color="rgb(40, 42, 54)" @created="created">
    <tv-rain :count="3000" :speed="0.8" :color="0x87ceeb" :range="30" :height="20" />
  </tv-scene>
</template>

<script lang="ts" setup>
const created = (_scene, { camera }) => {
  if (!camera) return
  camera.position.set(0, 0, 4)
  camera.lookAt(0, 0, 0)
}
</script>

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

Collision & Splash

Pass a grayscale heightMap and drops stop at the surface instead of falling through it. Each impact spawns an expanding ripple, so rain reads as hitting the ground. The map covers heightMapSize world units centred on the origin and is sampled like a plane's UVs (u = x / size + 0.5, v = 0.5 - z / size), so terrain displaced by the same image lines up exactly with the impacts.

The live example generates a deterministic 256 × 256 value-noise height field and shares it between the displaced terrain, rain collisions, and ripples.

<template>
  <tv-scene
    class="scene"
    bg-color="rgb(28, 30, 40)"
    :axes-helper="false"
    :grid-helper="false"
    @created="created"
  >
    <tv-rain
      v-if="heightMap"
      :count="5000"
      :range="SIZE"
      :height="HEIGHT"
      :height-map="heightMap"
      :height-map-size="SIZE"
      :height-map-amplitude="AMPLITUDE"
      :ground-height="GROUND_Y"
      :splash-size="1"
      :splash-duration="0.7"
    />
  </tv-scene>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import * as THREE from 'three'

const SIZE = 40
const HEIGHT = 24
const GROUND_Y = -HEIGHT / 2
const AMPLITUDE = HEIGHT * 0.4

const heightMap = shallowRef<THREE.CanvasTexture>()

const created = (scene, { camera, controls }) => {
  camera.position.set(0, 8, 27)
  controls.target.set(0, -3, 0)
  controls.update()

  // Any grayscale image works; this one is generated to avoid assets.
  const canvas = document.createElement('canvas')
  canvas.width = canvas.height = 128
  const ctx = canvas.getContext('2d')!
  const image = ctx.createImageData(128, 128)
  for (let y = 0; y < 128; y++) {
    for (let x = 0; x < 128; x++) {
      const value = (Math.sin(x / 11) * Math.cos(y / 13) * 0.5 + 0.5) * 255
      const i = (y * 128 + x) * 4
      image.data[i] = image.data[i + 1] = image.data[i + 2] = value
      image.data[i + 3] = 255
    }
  }
  ctx.putImageData(image, 0, 0)

  const texture = new THREE.CanvasTexture(canvas)
  texture.generateMipmaps = false
  texture.minFilter = THREE.LinearFilter
  heightMap.value = texture

  // Terrain displaced by the same map, so drops land on the visible surface.
  const geometry = new THREE.PlaneGeometry(SIZE, SIZE, 128, 128)
  geometry.rotateX(-Math.PI / 2)
  const position = geometry.attributes.position
  const uv = geometry.attributes.uv
  for (let i = 0; i < position.count; i++) {
    const col = Math.round(uv.getX(i) * 127)
    const row = Math.round((1 - uv.getY(i)) * 127)
    position.setY(i, GROUND_Y + (image.data[(row * 128 + col) * 4] / 255) * AMPLITUDE)
  }
  position.needsUpdate = true
  geometry.computeVertexNormals()
  scene.add(
    new THREE.Mesh(
      geometry,
      new THREE.MeshStandardMaterial({ color: '#39404f', roughness: 0.85 })
    )
  )
}
</script>

Set :splash="false" to keep the collision but skip the ripples, and use splashColor, splashSize and splashDuration to tune them.

Props

NameTypeDefaultDescription
countnumber4000optional Number of raindrops.
colorstring | number | Color0xb0c4deoptional Raindrop color.
speednumber1optional Falling speed multiplier.
rangenumber20optional Horizontal distribution range.
heightnumber15optional Falling height range.
windXnumber0.1optional Wind force on the X axis.
windZnumber0optional Wind force on the Z axis.
opacitynumber0.8optional Raindrop opacity.
heightMapstring | Texture-optional Grayscale height map the drops collide with.
heightMapSizenumberrangeoptional World size covered by the height map.
heightMapAmplitudenumberheight * 0.4optional Vertical extent added by the height map.
groundHeightnumber-height / 2optional Base ground Y of the height map.
splashbooleantrueoptional Draw an expanding ripple where each drop lands.
splashColorstring | number | Color0xd6ecffoptional Ripple color.
splashSizenumber0.9optional World radius of a ripple at its widest.
splashDurationnumber0.6optional Ripple lifetime in seconds.

Class usage

The standalone RainEffect is a lightweight point cloud: drops fall and drift with the wind inside a box and wrap at the edges, without a height map or ground splashes.

import { RainEffect } from 'three-vue3'

const rain = new RainEffect({ count: 2000, speed: 20, wind: [2, 0] })
scene.add(rain.mesh)

// Inside the render loop:
rain.update(delta)

// On teardown:
rain.dispose()
Edit this page on GitHub
Last Updated:
Contributors: Shing Rui, ruixinglong
Next
Snow