• Stars
    star
    3,269
  • Rank 13,741 (Top 0.3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 5 years ago

Reviews

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

Repository Details

3D line art engine.

ln The 3D Line Art Engine

ln is a vector-based 3D renderer written in Go. It is used to produce 2D vector graphics (think SVGs) depicting 3D scenes.

The output of an OpenGL pipeline is a rastered image. The output of ln is a set of 2D vector paths.

Examples

Motivation

I created this so I could plot 3D drawings with my Makeblock XY Plotter.

Here's one of my drawings from the plotter...

Example

Installation

go get github.com/fogleman/ln/ln

Features

  • Primitives
    • Sphere
    • Cube
    • Triangle
    • Cylinder
    • 3D Functions
  • Triangle Meshes
    • OBJ & STL
  • Vector-based "Texturing"
  • CSG (Constructive Solid Geometry) Operations
    • Intersection
    • Difference
    • Union
  • Output to PNG or SVG

How it Works

To understand how ln works, it's useful to start with the Shape interface:

type Shape interface {
	Paths() Paths
	Intersect(Ray) Hit
	Contains(Vector, float64) bool
	BoundingBox() Box
	Compile()
}

Each shape must provide some Paths which are 3D polylines on the surface of the solid. Ultimately anything drawn in the final image is based on these paths. These paths can be anything. For a sphere they could be lat/lng grid lines, a triangulated-looking surface, dots on the surface, etc. This is what we call vector-based texturing. Each built-in Shape ships with a default Paths function (e.g. a Cube simply draws the outline of a cube) but you can easily provide your own.

Each shape must also provide an Intersect method that lets the engine test for ray-solid intersection. This is how the engine knows what is visible to the camera and what is hidden.

All of the Paths are chopped up to some granularity and each point is tested by shooting a ray toward the camera. If there is no intersection, that point is visible. If there is an intersection, it is hidden and will not be rendered.

The visible points are then transformed into 2D space using transformation matrices. The result can then be rendered as PNG or SVG.

The Contains method is only needed for CSG (Constructive Solid Geometry) operations.

Hello World: A Single Cube

The Code

package main

import "github.com/fogleman/ln/ln"

func main() {
	// create a scene and add a single cube
	scene := ln.Scene{}
	scene.Add(ln.NewCube(ln.Vector{-1, -1, -1}, ln.Vector{1, 1, 1}))

	// define camera parameters
	eye := ln.Vector{4, 3, 2}    // camera position
	center := ln.Vector{0, 0, 0} // camera looks at
	up := ln.Vector{0, 0, 1}     // up direction

	// define rendering parameters
	width := 1024.0  // rendered width
	height := 1024.0 // rendered height
	fovy := 50.0     // vertical field of view, degrees
	znear := 0.1     // near z plane
	zfar := 10.0     // far z plane
	step := 0.01     // how finely to chop the paths for visibility testing

	// compute 2D paths that depict the 3D scene
	paths := scene.Render(eye, center, up, width, height, fovy, znear, zfar, step)

	// render the paths in an image
	paths.WriteToPNG("out.png", width, height)

	// save the paths as an svg
	paths.WriteToSVG("out.svg", width, height)
}

The Output

Cube

Custom Texturing

Suppose we want to draw cubes with vertical stripes on their sides, as shown in the skyscrapers example above. We can just define a new type and override the Paths() function.

type StripedCube struct {
	ln.Cube
	Stripes int
}

func (c *StripedCube) Paths() ln.Paths {
	var paths ln.Paths
	x1, y1, z1 := c.Min.X, c.Min.Y, c.Min.Z
	x2, y2, z2 := c.Max.X, c.Max.Y, c.Max.Z
	for i := 0; i <= c.Stripes; i++ {
		p := float64(i) / float64(c.Stripes)
		x := x1 + (x2-x1)*p
		y := y1 + (y2-y1)*p
		paths = append(paths, ln.Path{{x, y1, z1}, {x, y1, z2}})
		paths = append(paths, ln.Path{{x, y2, z1}, {x, y2, z2}})
		paths = append(paths, ln.Path{{x1, y, z1}, {x1, y, z2}})
		paths = append(paths, ln.Path{{x2, y, z1}, {x2, y, z2}})
	}
	return paths
}

Now StripedCube instances can be added to the scene.

Constructive Solid Geometry (CSG)

You can easily construct complex solids using Intersection, Difference, Union.

shape := ln.NewDifference(
	ln.NewIntersection(
		ln.NewSphere(ln.Vector{}, 1),
		ln.NewCube(ln.Vector{-0.8, -0.8, -0.8}, ln.Vector{0.8, 0.8, 0.8}),
	),
	ln.NewCylinder(0.4, -2, 2),
	ln.NewTransformedShape(ln.NewCylinder(0.4, -2, 2), ln.Rotate(ln.Vector{1, 0, 0}, ln.Radians(90))),
	ln.NewTransformedShape(ln.NewCylinder(0.4, -2, 2), ln.Rotate(ln.Vector{0, 1, 0}, ln.Radians(90))),
)

This is (Sphere & Cube) - (Cylinder | Cylinder | Cylinder).

Unfortunately, it's difficult to compute the joint formed at the boundaries of these combined shapes, so sufficient texturing is needed on the original solids for a decent result.

Example

More Repositories

1

primitive

Reproducing images with geometric primitives.
Go
12,639
star
2

Craft

A simple Minecraft clone written in C using modern OpenGL (shaders).
C
10,374
star
3

nes

NES emulator written in Go.
Go
5,427
star
4

Minecraft

Simple Minecraft-inspired program using Python and Pyglet
Python
5,220
star
5

gg

Go Graphics - 2D rendering in Go with a simple API.
Go
4,372
star
6

pt

A path tracer written in Go.
Go
2,077
star
7

sdf

Simple SDF mesh generation in Python
Python
1,583
star
8

Quads

Computer art based on quadtrees.
Python
1,176
star
9

fauxgl

Software-only 3D renderer written in Go.
Go
864
star
10

physarum

Physarum polycephalum slime mold simulation
Go
857
star
11

hmm

Heightmap meshing utility.
C
568
star
12

Tiling

Tilings of regular polygons.
Python
482
star
13

rbgg

Isolate and remove the background gradient from images of paper.
Go
360
star
14

pack3d

Tightly pack 3D models.
Go
323
star
15

rush

Rush Hour puzzle goodies!
Go
287
star
16

axi

Library for working with the AxiDraw v3 pen plotter.
Python
274
star
17

PirateMap

Procedurally generate pirate treasure maps.
Python
263
star
18

simplify

3D mesh simplification in Go.
Go
247
star
19

ribbon

Ribbon diagrams of proteins in #golang.
Go
243
star
20

Punchcard

Generate GitHub-style punchcard charts with ease.
Python
240
star
21

terrarium

Some code for generating topographic contour maps.
Go
222
star
22

pg

Python OpenGL Graphics Framework
Python
209
star
23

dlaf

Diffusion-limited aggregation, fast.
C++
187
star
24

FeedNotifier

Feed Notifier is a Windows application that resides in the system tray and displays pop-up notifications on your desktop when new items arrive in your subscribed RSS or Atom feeds
Python
165
star
25

CellularForms

An implementation of Andy Lomas' Cellular Forms.
C++
155
star
26

MisterQueen

A chess engine written in C.
C
148
star
27

density

Render millions of points on a map.
Go
147
star
28

meshview

Performant 3D mesh viewer written in Go.
Go
133
star
29

delaunay

Fast Delaunay triangulation implemented in Go.
Go
115
star
30

GraphLayout

Graph drawing using simulated annealing for layout.
Python
114
star
31

Piet

Procedurally Generating Images in the Style of Piet Mondrian
Python
97
star
32

ease

Easing functions in #golang.
Go
92
star
33

AdventOfCode2018

My solutions to the Advent of Code 2018 problems.
Python
81
star
34

iMeme

iMeme is a popular meme generator for Mac OS X
Objective-C
72
star
35

DCPU-16

Python scripts for DCPU-16 emulation.
DCPU-16 ASM
70
star
36

xy

Various utilities for the Makeblock XY Plotter Robot Kit
Python
70
star
37

contourmap

Compute contour lines (isolines) for any 2D data in Go.
Go
69
star
38

demsphere

Generate 3D meshes of planets, moons, etc. from spherical DEMs. (WIP)
Go
66
star
39

GPS

Real-time GPS Satellite Positions in 3D
Python
65
star
40

slicer

Fast 3D mesh slicer written in Go.
Go
61
star
41

AllRGB

Scripts for creating AllRGB images.
Python
61
star
42

pixsort

Applying the traveling salesman problem to pixel art.
Go
59
star
43

mol

Render ball-and-stick models of molecules.
Go
58
star
44

Ricochet

Implementation of Ricochet Robot (Rasende Roboter) including a GUI and a solver
Python
54
star
45

domaincoloring

Domain coloring in Go.
Go
50
star
46

Scale

Beautiful Fractals for Mac OS X.
Objective-C
40
star
47

HelloFlask

A boiler-plate starting point for a Flask web application, including SQLAlchemy, WTForms and Bootstrap.
CSS
38
star
48

gorgb

Program to create "allRGB" images.
Go
38
star
49

Field

Creating computer art by simulating charged particle field lines.
Python
37
star
50

TWL06

Official Scrabble dictionary packaged into a convenient Python module.
Python
36
star
51

serve

Simple Go file server for command line development use, a la Python's SimpleHTTPServer.
Go
34
star
52

choppy

Chop 3D models in half with a user-defined slice plane.
Go
34
star
53

Sync

Code inspired by the book.
Python
31
star
54

tracer

Global illumination path tracer in C++
C++
30
star
55

maps

Work in progress. Render maps in #golang with a simple API.
Go
27
star
56

FutureBlocks

Tetris clone written in QBasic.
Visual Basic
26
star
57

HiRISE

Convert HiRISE PDS IMG files to 3D meshes with normal maps.
Python
25
star
58

AdventOfCode2019

My solutions for Advent of Code 2019.
Python
25
star
59

AdventOfCode2021

My solutions for Advent of Code 2021.
Python
24
star
60

lissaknot

Create 3D models of Lissajous knots.
Go
24
star
61

LoginServer

Online multiplayer game login server for secure user authentication.
Python
24
star
62

pyMeme

Cross-platform meme generator application.
Python
23
star
63

HelloGL

Basic project structure for an OpenGL application.
C
22
star
64

ShortUrl

Python module for generating tiny URLs.
Python
22
star
65

imview

Simple image viewer written in Go + OpenGL.
Go
21
star
66

AdventOfCode2020

My solutions for Advent of Code 2020
Python
20
star
67

PieSlice

Making simple wallpapers out of quarter circles.
Python
20
star
68

Manhattan

Rendering the buildings of Manhattan using OSM data and NYC shapefiles.
Python
20
star
69

GameFrame

Game Frame Simulator
Objective-C
19
star
70

Phrases

Source code for a simple website that generates random, two-word phrases.
HTML
18
star
71

poissondisc

Poisson Disc sampling in Go.
Go
16
star
72

Mazes

Maze generation and rendering using Python.
Python
16
star
73

SwtPacMan

Pac-Man clone written in 2005 using Java + SWT.
Java
16
star
74

Mapper

Web app for quickly plotting markers, polylines, polygons, heatmaps, etc. on a map.
JavaScript
15
star
75

mc

Marching cubes algorithm implemented in #golang.
Go
12
star
76

Poker

Python poker hand evaluator
Python
12
star
77

GrayScott

Simple Gray Scott Reaction Diffusion model implemented in C++ and OpenCL
C++
12
star
78

WangTiling

Weighted Wang tiling.
Python
12
star
79

thumbs

Go binary that watches a folder for images and generates thumbnails of them.
Go
11
star
80

DrMario

Dr Mario clone in Python and wxPython, including AI
Python
10
star
81

Fireflies

Synchronizing fireflies using JavaScript and D3.
10
star
82

go-airspy

Go wrapper for the libairspy library.
Go
10
star
83

MLX90640

Arduino & Python code to interface with the MLX90640 thermal camera
Python
10
star
84

go-embree

Simple golang wrapper for embree using cgo
C++
9
star
85

triangulate

Polygon triangulation via ear clipping in #golang.
Go
8
star
86

wxSnow

Falling snowflakes on your Windows desktop.
Python
8
star
87

WellPlate

Python + wxPython user interface for 96 and 384 well plates.
Python
8
star
88

platformer

It's happening!
Go
8
star
89

AdventOfCode2022

My solutions for Advent of Code 2022.
Python
8
star
90

AdventOfCode2023

My solutions to the Advent of Code 2023 puzzles.
Python
7
star
91

Yellow

Visualization for 2015 NYC Yellow Taxi Trips
JavaScript
7
star
92

motion

Constant acceleration motion planner written in Go.
Go
7
star
93

Turing

2-D Turing Machine
Python
7
star
94

Boggle

Web-based Boggle clone written using Python and Flask
Python
6
star
95

colormap

Colormaps for Go.
Go
6
star
96

go-maps

Utilities for rendering maps in Go.
Go
6
star
97

slices2stl

Go
6
star
98

CurveMesh

Python
6
star
99

StarRocket

Star Rocket is a cartoon-themed space game with over 120 action-packed levels!
Objective-C
6
star
100

visvalingam

Visvalingam-Whyatt line simplification algorithm in Go.
Go
6
star