• Stars
    star
    377
  • Rank 113,012 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An optimised raindrop effect on glass with WebGL2

Raindrop FX

GitHub GitHub Workflow Status GitHub Workflow Status GitHub tag (latest by date) npm GitHub file size in bytes

Optimised raindrop effect on glass with WebGL2

Inspired by https://github.com/codrops/RainEffect

Check the live demo: https://sardinefish.github.io/raindrop-fx/

Background image from https://www.pixiv.net/artworks/84765992

Usage

Use as npm package

You might need some bundle tools to run it in browser. eg. esbuild or webpack

$ npm install raindrop-fx
const RaindropFX = require("raindrop-fx");

const canvas = document.querySelector("#canvas");
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width;
canvas.height = rect.height;

const raindropFx = new RaindropFX({
    canvas: canvas,
    background: "path/to/background/image",
});

window.onresize = () =>
{
    const rect = canvas.getBoundingClientRect();
    raindropFx.resize(rect.width, rect.height);
}

raindropFx.start();

In Browser

For directly use in browser, download the pre-bundled script at /bundle/index.js and embed with your Web page.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Raindrop Effect</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <script src="./bundle/index.js"></script>
</head>
<body>
    <div id="root">
        <canvas id="canvas"></canvas>
    </div>
    <script src="./script.js"></script>
</body>
</html>

style.css

body {
    margin: 0;
}

#root {
    position: absolute;
    width: 100vw;
    height: 100vh;
}

#canvas {
    width: 100%;
    height: 100%;
}

script.js

const canvas = document.querySelector("#canvas");

// Set canvas size to fit the real size
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width;
canvas.height = rect.height;

const raindropFx = new RaindropFX({
    canvas: canvas,
    background: "path/to/backgroundImage",
});
raindropFx.start();

// Resize the effect renderer
window.onresize = () =>
{
    const rect = canvas.getBoundingClientRect();
    raindropFx.resize(rect.width, rect.height);
}

Configuration

All options except canvas element are optional at initialize.

// Minimal startup script with a dark opacity background
const raindropFx = new RaindropFX({
    canvas: canvas,
});

All options can be set dynamically by raindropFx.options.xxx=xx except renderer size and background image Example:

// At initialize
const raindropFx = new RaindropFX({
    canvas: canvas,
    spawnSize = [30, 80],
    spawnInterval = [0.1, 0.2],
    mistBlurStep: 5,
    dropletsPerSecond: 1000,
});

await raindropFx.start();

// Or set directly
raindropFx.options.spawnSize = [30, 150];
raindropFx.options.spawnInterval = [0.01, 0.05];
raindropFx.options.mistBlurStep = 5;
raindropFx.options.dropletsPerSecond = 1000;

Resize

Call raindropFx.resize to properly reszie.

raindropFx.resize(someWidth, someHeight);

Background

Background image must be set by calling to raindropFx.setBackground.

Background image source could be a url string, HTMLCanvasElement, HTMLImageElement, ArrayBuffer and other types that implemented interface TexImageSource and ArrayBufferView.

await raindropFx.setBackground("background image url");

// Set background image from HTMLImageElement
const img = new Image();
// ... 
await raindropFx.setBackground(img);

// Set background image from another HTMLCanvasElement
const anotherCanvas = document.querySelector("#another-canvas");
await raindropFx.setBackground(anotherCanvas);

Raindrop Simulation Options

interface SimulationOptions
{
    spawnInterval: [number, number],
    /**
     * Random size range when spawn a new raindrop
     */
    spawnSize: [number, number],
    /**
     * Maximal amount of spawned raindrops.
     * Recommend less than 2000
     */
    spawnLimit: number,
    /**
     * Recommend in range (0..1), other value should be ok.
     */
    slipRate: number,
    /**
     * Describe how often a raindrop change its motion state
     */
    motionInterval: [number, number],
    /**
     * Random velocity x relative to velocity y. 
     * Recommend in range (0..0.1)
     */
    xShifting: [number, number],
    /**
     * Relative size for collision check. 
     * Recommend in range (0.6..1.2)
     */
    colliderSize: number,
    /**
     * Mass density of the slipping trail raindrop. 
     * Recommend in range (0.1..0.3)
     * 
     * A large value cause a raindrop quickly lose its size during slipping.
     */
    trailDropDensity: number,
    /**
     * Random size range of slipping trail drop. 
     * Recommend in range (0.3..0.5)
     */
    trailDropSize: [number, number],
    /**
     * Random distance range between two slipping trail drop. 
     * Recommend in range (20..40)
     */
    trailDistance: [number, number],
    /**
     * Vertical spread of a new spawned slipping trail drop. 
     * Recommend in range (0.4..0.6)
     */
    trailSpread: number,
    /**
     * Spread rate when a new spawned raindrop hit the screen. 
     * Recommend in range (0.4..0.7)
     */
    initialSpread: number,
    /**
     * Spread shrink rate per seconds. 
     * Recommend in range (0.01..0.02)
     */
    shrinkRate: number,
    /**
     * Spread rate by velocity Y. 
     * Recommend in range (0.2..0.4)
     * 
     * Raindrop with higher fall speed looks more narrow.
     */
    velocitySpread: number,
    /**
     * Mass lose per second. 
     * Recommend in range (10..30)
     */
    evaporate: number,
    /**
     * Gravity acceleration in pixels/s. 
     * Recommend 2400
     */
    gravity: number,
}

Rendering Options

interface RenderingOptions
{
    /**
     * Background blur steps used for background & raindrop refract image.
     * Value should be integer from 0 to log2(backgroundSize).
     * Recommend 3 or 4
     */
    backgroundBlurSteps: number,
    /**
     * WrapMode for background texture used during sampling raindrop refrection.
     */
    backgroundWrapMode: "clamp" | "repeat" | "mirror";
    /**
     * Enable blurry mist effect
     */
    mist: boolean,
    /**
     * [r, g, b, a] mist color, each component in range (0..1). 
     * The alpha is used for whole mist layer.
     * Recommend [0.01, 0.01, 0.01, 1]
     */
    mistColor: [number, number, number, number],
    /**
     * Describe how long takes mist alpha from 0 to 1
     */
    mistTime: number,
    /**
     * Background blur steps used for mist.
     * Value should be integer from 0 to log2(backgroundSize).
     * Recommended value = backgroundBlurSteps + 1
     */
    mistBlurStep: number,
    /**
     * Tiny droplet spawn rate.
     */
    dropletsPerSeconds: number,
    /**
     * Random size range of tiny drplets.
     * Recommend [10, 30]
     */
    dropletSize: [number, number],
    /**
     * Smooth range [a, b] of raindrop edge.
     * Recommend [0.96, 1.0]
     * 
     * Larger range of (b - a) have smoother edge.
     * 
     * Lower value b makes raindrop larger with a distort edge
     */
    smoothRaindrop: [number, number],
    /**
     * Refract uv scale of minimal raindrop.
     * Recommend in range (0.2, 0.6)
     */
    refractBase: number,
    /**
     * Refract uv scaled by raindrop size.
     * Rocommend in range (0.4, 0.8)
     */
    refractScale: number,
    /**
     * Describe how raindrops are composed.
     * 
     * - `smoother` compose raindrops normal with 'exclusion' blend mode
     * 
     * - `harder` compose raindrops normal with 'normal' blend mode
     */
    raindropCompose: "smoother" | "harder"
    /**
     * Screen space (0..1) light direction or position.
     * Recommend [-1, 1, 2, 0]
     * 
     * - Use [x, y, z, 0] to indicate a direction
     * 
     * - Use [x, y, z, 1] to indicate a position
     */
    raindropLightPos: [number, number, number, number],
    /**
     * Lambertian diffuse lighting Color.
     * Recommend [0.3, 0.3, 0.3]
     */
    raindropDiffuseLight: [number, number, number],
    /**
     * Higher value makes more shadow.
     * Recommend in range (0.6..0.8)
     */
    raindropShadowOffset: number,
    /**
     * Similar to `smoothRaindrop`. Used to erase droplets & mist.
     * Recommended value [0.93, 1.0]
     */
    raindropEraserSize: [number, number],
    /**
     * Specular light clor.
     * Recommend disable it with [0, 0, 0] :)
     */
    raindropSpecularLight: [number, number, number],
    /**
     * Blinn-Phong exponent representing shininess.
     * Value from 0 to 1024 is ok
     */
    raindropSpecularShininess: number,
    /**
     * Will apply to calculate screen space normal for lighting.
     * Larger value makes raindrops looks more flat.
     * Recommend in range (0.3..1)
     */
    raindropLightBump: number,
}

Performance

  • On Windows Chrome 88. It takes about 6ms to update each frame with 2000 raindrops.

  • On Android (Mi 10) Chrome 87. It takes about 6.5ms to update each frame with 2000 raindrops.

By default settins on a 1920x1080 screen, there are up to 600 raindrops on the screen, which taks 2~3ms to update each frame.

More Repositories

1

Unity2DLighting

A 2D lighting system implement in Unity.
C#
146
star
2

SRP-Demos

Demos for custom Unity Scriptable Render Pipeline.
C#
136
star
3

Post-processing-Demo

Demo for post-processing implement in Unity.
ShaderLab
73
star
4

WFC-Demo

2D & 3D procedural content genration with tile-based Wave Function Collapse in Unity
C#
42
star
5

MAJIKA

A 2D pixel art platform ARPG game.
C#
41
star
6

SimpleSRP

A simple Scriptable Render Pipeline implement.
C#
30
star
7

zogra-renderer

A simple WebGL2 renderer.
TypeScript
23
star
8

UnityShaderSupport

An extension for VS Code to provide ShaderLab language support
TypeScript
22
star
9

PBR-Demo

Demo of PBR shader implement in Unity & MERL BRDF rendering.
ShaderLab
14
star
10

snake-remake

A simple snake game demo with WebGL
TypeScript
11
star
11

nscn-scanner

Security risk scanner for network assets, my diploma project.
Rust
5
star
12

code-formatter

A simple code formatter following the compilers principles.
C
5
star
13

Sardine-Blog

The repo of my blog server written in Rust. http://www.sardinefish.com/
JavaScript
4
star
14

SarEngine2D

A simple 2D game engine based on HTML Canvas
JavaScript
4
star
15

bongocat-unity

A desktop Bongo Cat made with Unity
C#
4
star
16

Raytrace-2D

An implement of ray-trace in 2D space based on HTML Canvas.
TypeScript
4
star
17

SDF-2D

A demo to test signed distance field on 2D canvas.
JavaScript
3
star
18

WindowsLoop

Project for Bingyan-Memo-Unique Game Jam.
C#
3
star
19

netfilter-firewall

A linux kernel module firewall implement with Netfilter in Rust.
Rust
3
star
20

WinCtxMenuEditor

Create Windows cascading context menu definded in JSON
C#
3
star
21

Procool

A 2D top-down A-AVG game in a procedually generated city.
C#
2
star
22

MonumentValley-CPY

A replica of the Monument Valley
C#
2
star
23

IK-Unity3D

An Implement of IK in Unity 3D
C#
2
star
24

UniqueHackDay2019

C#
2
star
25

Sar-CodeLab

Just a playground to test some amazing code.
JavaScript
2
star
26

rust_kernel_module

A tiny example for linux kernel module built with C & Rust, tested in kernel 5.4.0
Makefile
2
star
27

SpriteSheet

A console program generating image sequence by piece specific image files.
C#
1
star
28

pixel-particle-system

A simple pixel-art particle system generator & renderer.
TypeScript
1
star
29

RobotBattle

A FPS game demo
C#
1
star
30

tetris-auto

Rust
1
star
31

TheLastOne

A Game in develop
C#
1
star
32

home-ipv6

An IPv6 SLAAC Client for my Home Server
Rust
1
star
33

sar-push-service

A notification push service currently used for my blog comment system in Rust.
Rust
1
star
34

Duality

Game for Global Game Jam 2020
C#
1
star