Skip to content
loyality7Public

About

Code-driven pen-and-ink 3D animation engine in Go: ray-cast shapes, mirror water, rigs, physics, ink/ASCII looks, GIF and MP4 output

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

39 Commits

Folders and files

Repository files navigation

ln

ln

Draw and animate with code. Get hand-inked pictures, GIFs and video.

Language Output Looks Platform Status License

A stick man doing every move the engine has: wave, walk, run, jump, flips, punches, kicks, blocks, a knockout, a bow

One stick man, every move, drawn by go run ./examples/engine/stickman. No frames were drawn by hand.

Two stick men fight: block, duck, uppercut, flying kick, spin kick, double flip, clash, knockout
Stick fight
Two fighters, a dozen moves, comic effects, a follow camera.
examples/engine/stickfight
A pen-and-ink village with swaying trees, a moving sun and a gliding camera
Village
Swaying trees, a sun crossing the sky, cast shadows, a camera glide.
examples/ink/village
A robot walking through a village while the camera circles it
Walker
A jointed robot, a camera that follows and circles it, a moving sun.
examples/engine/walker
A cube drawn in by the pen, with a sphere orbiting it
Spin
The pen draws the scene in, a cube turns, a sphere orbits, the camera follows a spline.
examples/animation/spin
A camera tour of a black hole in white ink: zooming in, circling, the far side of the disk bent over the shadow
Black hole, ink lines
Light bent by gravity (ln/physics), drawn as lines. Zooms in, rises above the disk, skims it, pulls back.
examples/space/blackhole
The same camera tour of the black hole drawn as smooth glowing light: gold bands around the shadow, a thin photon ring, starlight streaking as the camera dives in
Black hole, glowing light
The same bent rays, drawn as light with bloom (ln/glow): heat from the disk's radius, a thin photon ring, starlight pulled into streaks.
examples/space/blackhole -glow

More examples. Every frame here is drawn by code, and nothing was animated by hand.


ln is a Go engine that turns a few lines of code into pen-and-ink art. You describe shapes, a camera, a light and some motion. It ray-casts the scene, works out where the shadows fall, and draws it as clean ink lines, hatching and solid blacks, with hand-drawn wobble. The output is a still, an SVG, a GIF or a video.

It began as a fork of fogleman/ln, a 3D line renderer for pen plotters. The renderer core is from there. Everything above it is new.

What you can make

Any shape, from code Boxes, spheres, tori, cones and capsules. Spin a curve into a vase (Lathe), lift an outline into a solid (Extrude), sweep a pipe along a path (Tube), or turn any formula f(u,v) into a surface. Every shape gets smooth shading and clean outlines automatically.
Worlds Terrain from a height function, forests scattered over it, mirror lakes with rippled reflections, and a sky model with a sun, moon and stars that moves through sunrise, noon, dusk and night with correct cast shadows. Any object can be a lamp.
Characters and motion Skeletons described as data. Moves written as keyframe tables or oscillators, then blended, jumped, flipped and spun with one-line modifiers. A complete stick man with 25 moves ships in the box.
Comic effects Dust puffs, impact stars, speed lines, smear arcs, screen shake and a follow camera, all triggered by named marks on a move.
Five looks Hatched ink, clean manga with solid blacks, loose sketch, precise technical, and ascii. Switch with one flag. The scene never changes.
Any output PNG, SVG, MP4, WebM and GIF. Video needs ffmpeg.

A scene in a few lines

package main

import . "github.com/loyality7/ln/components/studio"

func main() {
	Run(func(s *Scene) {
		s.Look("manga")                          // ink | manga | sketch | technical | ascii
		s.TimeOf("dusk")                         // dawn ... noon ... dusk ... night
		s.Camera(V(0, -12, 3), V(0, 0, 1), 40)   // where it stands, what it looks at, field of view

		s.Root.Box(10, 10, 0.2).At(0, 0, -0.1)   // a floor
		ball := s.Root.Sphere(1).At(-3, 0, 1)
		s.TL.Move(ball, V(3, 0, 1), 2, anim.OutBounce) // roll it across in 2 seconds
	})
}
go run .                  # a video, because the scene has motion
go run . -still 1         # one picture, at second 1
go run . -look ascii -sky night -fmt gif -out out/ball

The one-page cheat sheet covers everything you need to write a scene.

Characters in a few lines

A move is a table of poses. There is no motion code.

punch := rig.Keys(0.9,
	rig.K(0,    pose{"shoulderN": 72, "elbowN": 112}),   // guard
	rig.K(0.3,  pose{"shoulderN": -28, "elbowN": 130}),  // wind up
	rig.K(0.46, pose{"shoulderN": 90, "elbowN": 6})).    // strike
	Travel(0.35, 0, 0.5).                                // lunge forward
	Mark(rig.Mark{Kind: "impact", At: 0.5, X: 1.17, Z: 1.28}) // comic star where it lands

hero.Play(walk)
hero.Play(punch)                                          // blended, one after another
rig.Beat(s.TL, rig.Moves(a, punch), rig.Moves(b, block))  // two actors at once
fx.Install(s.Stage, fx.DefaultFollow(), a, b)             // dust, stars, shake, camera

The stick man above and the two-man fight in the grid are scripts of this kind: blocks, dodges, an uppercut, a flying kick, a spin kick, a double flip, a clash and a knockout, about 60 lines of choreography.

Video to art

Give ln any video and it redraws it. It finds the cuts, gives every shot its own colour palette, keeps shapes steady from frame to frame by carrying them along the footage's motion, and writes the result as ink lines and flat colour patches. No trained model is involved. The same footage, as it was filmed and drawn two ways:

The original footage: a fast montage of shots
Original
the footage we started from
The montage redrawn by ln as flat colour patches under thin ink lines
Drawn by ln, colour
-nofill=false
The montage redrawn by ln as thin black lines on white
Drawn by ln, lines only
the default
go run ./examples/video/art -in clip.mp4 -out out/art.mp4                    # black lines on white
go run ./examples/video/art -in clip.mp4 -out out/art.mp4 -nofill=false      # colour patches under the lines
go run ./examples/video/art -in clip.mp4 -out out/art.png -frame 30          # one frame, to tune with
go run ./examples/video/art -in clip.mp4 -out out/art.mp4 -gocode out/art.go # also the animation as plain Go code
go run ./examples/video/art -in clip.mp4 -out out/art.mp4 -check             # measure flicker and lost detail per frame

The ln command

Everything above also runs as one program, ln, with no Go needed. Download the file for your system from the Releases page (or the latest build under Actions), then:

ln install                                  # once: puts ln on your PATH, installs ffmpeg (Windows) and the Python packages
ln art clip.mp4                             # glowing lines over the footage's colours -> clip_glow-colour.mp4
ln art clip.mp4 -look lines                 # lines | colour | glow | glow-colour
ln art clip.mp4 -w 540 -code clip.go        # also write the animation as Go code
ln render scene.json                        # a 3D scene file to video (ln new writes a sample)
ln draw drawing.json                        # a 2D drawing, as ink or glow
ln help                                     # every command; ln help art for its options

Make the art as code, and run just the code

-code writes the whole animation as plain Go shapes (lines, colour patches, glow), so the video and Python are no longer needed:

ln art clip.mp4 -look glow-colour -w 540 -code clip.go
go run clip.go -out out/clip.mp4        # redraws the whole video from the code (needs ffmpeg)
go run clip.go -frame 40 -out f.png     # or one frame

Run clip.go from inside a Go module that requires github.com/loyality7/ln (for example this repo). It works for the lines, colour and glow-colour looks.

It needs Python with pip install opencv-python numpy scikit-image, and ffmpeg. The left clip is third-party footage, shown only so you can compare it with what ln drew; it belongs to its creators. Use your own clip for your own work.

How it works

 scene graph         shapes + lights          per-pixel buffers          a look            picture
 (nodes, timeline) -> (meshes, mirrors, sun) -> (depth, normal, object) -> (ink / manga /   -> PNG SVG
  rigs, clips                                    shadow, reflection        ASCII ...)          GIF MP4

Every arrow is an interface. A new look, a new light or a new kind of shape plugs in without touching the rest. The ink looks read the same per-pixel buffers, so any shape works with every look, reflections and shadows included.

Project layout

ln/ is the core: machinery that makes and draws shapes, with no specific objects in it.

Package Job
ln/geom ln/shapes ln/accel ln/model ln/scene Math, ray-cast shapes (with mirrors), BVH, meshes, scene
ln/solid The shape forge: lathe, extrude, tube, any f(u,v) surface, smooth shading, outlines
ln/light ln/buffer Lights behind one interface, and the visible world sampled per pixel
ln/ink ln/ascii The looks: hatched ink, manga, ASCII
ln/stage ln/anim Scene graph, timeline in seconds, easing, video and GIF export
ln/rig ln/fx Characters and motion as data; comic effects and the follow camera
ln/vision Reads shapes out of any video: cuts, palettes per shot, steady patches and lines, and a frame-by-frame checker (needs Python with OpenCV)
ln/physics General laws: RK4 integrators, gravity and orbits, light bending, Doppler and beaming, thermal radiation
components/ Things built on the core: studio (the simple front door: one import, no boilerplate), env (sky, sun, moon), nature (terrain, pines, rocks), stickman, mannequin, world, blackhole

Nothing in ln/ knows about any specific object. You can write your own components the same way.

Run the examples

The gallery at the top is built from these:

go run ./examples/engine/stickman      # every stick man move, with captions
go run ./examples/engine/stickfight    # two stick men fight
go run ./examples/ink/village          # the village
go run ./examples/engine/walker        # the walking robot
go run ./examples/animation/spin       # the spinning cube
go run ./examples/scenes/lake          # a mirror lake with a figure on the shore
go run ./examples/engine/daycycle      # one scene across a whole day
go run ./examples/forge/shapes         # every shape-forge function in one picture
go run ./examples/logo                 # draws the logo above with the engine itself

Needs Go and, for video, ffmpeg on your PATH. Output goes to out/.

Status

Solid The shape forge, lights and shadows, mirrors, the sky model, the ink and ASCII looks, the rig and clip system, the stick figure and its effects. All have tests.
Experimental The manga look (clean at a distance, rough on close-ups) and components/mannequin.
Not built yet Colour, boolean cuts on meshes, a scripting layer so scenes can be written without Go, scene files.

Full API: docs/engine.md.

Credits

Started from fogleman/ln by Michael Fogleman (MIT). MIT licensed, see LICENSE.md.

About

Code-driven pen-and-ink 3D animation engine in Go: ray-cast shapes, mirror water, rigs, physics, ink/ASCII looks, GIF and MP4 output

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages