Skip to content

Repository files navigation

MeltGL

MIT license Strict TypeScript WebGL 2 No third party dependencies

MeltGL melts images and video in the browser. The picture is not warped or scrolled downwards. It is treated as a body of material, and the material is run through a fluid solver on the GPU: heat lowers its viscosity, gravity pulls it down, surface tension rounds the drops, and the outline of the picture moves because the material under it moved.

That last part is the whole point. A warp can only push pixels around inside the shape it started with, so the silhouette never changes and nothing ever really drips. Here the silhouette is what the solver is solving for, which is why drops swell, neck, and let go on their own.

There is a live demo at 1etu.github.io/MeltGL. Drop a picture or a video of your own on the box and pull the sliders around. The experiments page has the test cases used while building the solver, one phenomenon per figure: a slab heated from above, six materials on one shape, four decades of viscosity, threads against drops, where the heat comes from, thin features going first, a hole closing, and the picture riding the material.

The melt

Eight seconds of the slime preset, recorded from the demo page at 20 frames per second. There is also an MP4 if you prefer that.

A teapot melting into green slime and pooling on the floor

Using it

import { createMelt } from 'meltgl'

const melt = await createMelt({
  target: document.querySelector('#poster'),
  source: 'teapot.png',
  material: 'wax',
  duration: 8
})

melt.play()
melt.on('complete', () => location.href = '/next')

The target is any element. MeltGL puts its own canvas over it and leaves the element alone, and the canvas runs past the bottom edge so the material has somewhere to fall. The source is an <img>, a <video>, a URL to either, or your own object that hands back frames. Materials are wax, honey, chocolate, tar, solder and slime, or a preset with any of its numbers changed.

Besides play there is reverse, pause, seek, reset, configure and dispose, and they all do what their names say. Scrubbing works because the solver keeps periodic snapshots and replays from the nearest one.

MeltGL is not on npm yet. Clone the repository and run pnpm install then pnpm build, or take the prebuilt module out of Website/ and load it straight from a page. pnpm dev opens the demo, which is the fastest way to get a feel for the parameters.

How it works

The engine is a two dimensional free surface Navier-Stokes solver, following Carlson and others from 2002. Temperature drives viscosity over about four decades, so the same code covers a solid that barely creeps and a liquid that runs off the screen. Each substep advects the fields, applies heat and gravity and a curvature force at the surface, solves viscosity implicitly because an explicit solve is not stable at that stiffness, projects the velocity to remove divergence with a multigrid V-cycle, and puts the level set that carries the outline back into shape.

The picture itself rides on a reference map, which is a second field holding the material coordinate each cell started from. The image is sampled through that map when the frame is shaded, so it stretches with the material and never turns to mush the way repeated resampling would.

A 360 by 360 melt holds 60 frames per second on a discrete GPU. Fields run at a fraction of the canvas resolution by default while the picture is always shaded at full resolution, which is where most of that headroom comes from.

Browsers without WebGL 2 and float render targets fall back to an SVG filter. It is a displacement warp of the source, not a simulation, and it honours only the material's viscosity, density and tension plus the noise and timing options. It looks like something melting from across the room. Do not expect it to match.

Credits

This is a small implementation of other people's ideas. It would not exist without these.

License

MIT. Reports and patches are welcome on the issue tracker.

About

Melts images and video in the browser with a real fluid simulation on the GPU

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages