spt

package module
v0.0.0-...-a091fb7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2020 License: MIT Imports: 17 Imported by: 0

README

SDF Path Tracer

A signed distance function path tracer, adapted from the ubiquitous Ray Tracing in One Weekend book, extended to include:

  • various 2D and 3D SDFs
  • SDF bounding spheres to allow fast(er) ray intersection and elimination
  • multi-node cluster rendering via RPC
  • invisible shadow-catcher material

It seems pretty quick, at least in the ballpark of other similar efforts. The clustering feature seems less common; heaps of fun to spin up a few AWS burstable instances as render slaves and hammer lots of cores!

A shout-out to the author of pt is deserved. Very useful to have a Go-based reference implementation of a path tracer for debugging.

See spt_test.go for this example:

shapes.png

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	White  = Color{1.0, 1.0, 1.0}
	Black  = Color{0.001, 0.001, 0.001}
	Naught = Color{}
	Red    = Color{1.0, 0.0, 0.0}
	Blue   = Color{0.0, 0.0, 1.0}
	Green  = Color{0.0, 1.0, 0.0}
)
View Source
var (
	Zero2 = Vec2{}
	Zero3 = Vec3{}
	X2    = Vec2{X: 1}
	Y2    = Vec2{Y: 1}
	X3    = Vec3{X: 1}
	Y3    = Vec3{Y: 1}
	Z3    = Vec3{Z: 1}
)
View Source
var (
	Steel     = Metal(Color{0.4, 0.4, 0.4}, 0.95)
	Stainless = Metal(Color{0.4, 0.4, 0.4}, 0.3)
	Gold      = Metal(Color{0.93, 0.78, 0.31}, 0.0)
	Copper    = Metal(Color{0.68, 0.45, 0.41}, 0.8)
	Brass     = Metal(Color{0.80, 0.58, 0.45}, 0.9)
)
View Source
var Transparent = color.Transparent

Functions

func Render

func Render(scene Scene, renderers []Renderer) chan image.Image

func RenderSave

func RenderSave(out string, scene Scene, renderers []Renderer)

func RenderServeRPC

func RenderServeRPC(stop chan struct{}, port int) error

func SavePNG

func SavePNG(img image.Image, path string)

Types

type Camera

type Camera struct {
	Origin   Vec3
	U        Vec3
	V        Vec3
	W        Vec3
	M        float64
	Focus    float64
	Aperture float64
}

func NewCamera

func NewCamera(lookFrom, lookAt, vup Vec3, vfov float64, focus Vec3, aperture float64) Camera

func (Camera) CastRay

func (c Camera) CastRay(imageX, imageY, imageW, imageH int, jitterU, jitterV float64, rnd Random) Ray

type Color

type Color struct {
	R, G, B float64
}

func Hex

func Hex(x int) Color

func RGB

func RGB(r, g, b uint8) Color

func (Color) Add

func (c Color) Add(c2 Color) Color

func (Color) Brightness

func (c Color) Brightness() float64

func (Color) Div

func (c Color) Div(c2 Color) Color

func (Color) Min

func (c Color) Min(c2 Color) Color

func (Color) Mul

func (c Color) Mul(c2 Color) Color

func (Color) Opacity

func (c Color) Opacity(a float64) color.RGBA

func (Color) RGBA

func (c Color) RGBA() (uint32, uint32, uint32, uint32)

func (Color) Scale

func (c Color) Scale(t float64) Color

type CompressedRaster

type CompressedRaster struct {
	Buf []byte
}

type Dielectric

type Dielectric struct {
	Nothing
	Color
	RefractiveIndex float64
}

func (Dielectric) Scatter

func (mat Dielectric) Scatter(r Ray, thing *Thing, hit Vec3, depth int) (Ray, Color, bool)

type Diffuse

type Diffuse struct {
	Nothing
	Color
}

func (Diffuse) Scatter

func (mat Diffuse) Scatter(r Ray, thing *Thing, hit Vec3, depth int) (Ray, Color, bool)

type Emitter

type Emitter struct {
	Nothing
	Color
}

func (Emitter) Light

func (mat Emitter) Light() (Color, bool)

type Hit

type Hit struct {
	Thing    *Thing
	Position Vec3
	Normal   Vec3
}

type Invisible

type Invisible struct {
	Diffuse
}

type LocalRenderer

type LocalRenderer struct{}

func (LocalRenderer) Render

func (r LocalRenderer) Render(scene Scene) (Raster, error)

type Material

type Material interface {
	Light() (Color, bool)
	Scatter(Ray, *Thing, Vec3, int) (Ray, Color, bool)
}

func Glass

func Glass(color Color, refInd float64) Material

func Light

func Light(c Color) Material

func Matt

func Matt(c Color) Material

func Metal

func Metal(c Color, roughness float64) Material

func ShadowsOnly

func ShadowsOnly() Material

type Matrix44

type Matrix44 struct {
	X00, X01, X02, X03 float64
	X10, X11, X12, X13 float64
	X20, X21, X22, X23 float64
	X30, X31, X32, X33 float64
}

func Rotation

func Rotation(v Vec3, a float64) Matrix44

func Translation

func Translation(v Vec3) Matrix44

func (Matrix44) Determinant

func (a Matrix44) Determinant() float64

func (Matrix44) Inverse

func (a Matrix44) Inverse() Matrix44

func (Matrix44) MulVec3

func (a Matrix44) MulVec3(b Vec3) Vec3

type Metallic

type Metallic struct {
	Nothing
	Color
	Roughness float64
}

func (Metallic) Scatter

func (mat Metallic) Scatter(r Ray, thing *Thing, hit Vec3, depth int) (Ray, Color, bool)

type Nothing

type Nothing struct{}

func (Nothing) Light

func (mat Nothing) Light() (Color, bool)

func (Nothing) Scatter

func (mat Nothing) Scatter(r Ray, thing *Thing, hit Vec3, depth int) (Ray, Color, bool)

type Pixel

type Pixel struct {
	Color Color
	Rays  int32   // encoding/gob won't send a slice of pixels using a platform-dependent int size
	Alpha float64 // candidate for invisible shadows-only surface
}

type RPCRenderer

type RPCRenderer struct {
	// contains filtered or unexported fields
}

func (RPCRenderer) Render

func (r RPCRenderer) Render(scene Scene) (Raster, error)

type Random

type Random interface {
	Float64() float64
}

type Raster

type Raster []Pixel

type Ray

type Ray struct {
	Origin    Vec3
	Direction Vec3
	// contains filtered or unexported fields
}

func (Ray) PathTrace

func (r Ray) PathTrace(scene *Scene, depth int, bypass *Thing) (Color, int, float64)

type RenderRPC

type RenderRPC struct{}

func (RenderRPC) Render

func (srv RenderRPC) Render(in Scene, out *CompressedRaster) error

type Renderer

type Renderer interface {
	Render(Scene) (Raster, error)
}

func NewLocalRenderer

func NewLocalRenderer() Renderer

func NewRPCRenderer

func NewRPCRenderer(address string) Renderer

type SDF2

type SDF2 interface {
	SDF() func(Vec2) float64
	Circle() (Vec2, float64)
}

func Circle

func Circle(radius float64) SDF2

func Hexagram

func Hexagram(r float64) SDF2

func Parabola

func Parabola(w, h float64) SDF2

width on x-axis at height on y-axis

func Polygon

func Polygon(n int, r float64) SDF2

func Rectangle

func Rectangle(x, y float64) SDF2

func Stadium

func Stadium(h, r1, r2 float64) SDF2

func Triangle

func Triangle(p0, p1, p2 Vec2) SDF2

type SDF3

type SDF3 interface {
	SDF() func(Vec3) float64
	Sphere() (Vec3, float64)
}

func Capsule

func Capsule(h, r1, r2 float64) SDF3

func Cone

func Cone(h, r float64) SDF3

func Cube

func Cube(x, y, z float64) SDF3

func CubeR

func CubeR(x, y, z, r float64) SDF3

func Cylinder

func Cylinder(h, r float64) SDF3

func CylinderR

func CylinderR(h, r, ro float64) SDF3

func Difference

func Difference(items ...SDF3) SDF3

func Distort

func Distort(factor Vec3, sdf SDF3) SDF3

func Ellipsoid

func Ellipsoid(x, y, z float64) SDF3

func Elongate

func Elongate(x, y, z float64, sdf SDF3) SDF3

func Extrude

func Extrude(h float64, sdf SDF2) SDF3

func GearWheel

func GearWheel() SDF3

func Hollow

func Hollow(thickness float64, sdf SDF3) SDF3

func Intersection

func Intersection(items ...SDF3) SDF3

func Mirror

func Mirror(mul Vec3, sdf SDF3) SDF3

func MirrorX

func MirrorX(sdf SDF3) SDF3

func MirrorY

func MirrorY(sdf SDF3) SDF3

func MirrorZ

func MirrorZ(sdf SDF3) SDF3

func Pyramid

func Pyramid(h, w float64) SDF3

func Repeat

func Repeat(cx, cy, cz, ox, oy, oz float64, sdf SDF3) SDF3

func Revolve

func Revolve(o float64, sdf SDF2) SDF3

func Rotate

func Rotate(v Vec3, deg float64, sdf SDF3) SDF3

func RotateX

func RotateX(deg float64, sdf SDF3) SDF3

func RotateY

func RotateY(deg float64, sdf SDF3) SDF3

func RotateZ

func RotateZ(deg float64, sdf SDF3) SDF3

func Round

func Round(radius float64, sdf SDF3) SDF3

func Scale

func Scale(factor float64, sdf SDF3) SDF3

func Sphere

func Sphere(r float64) SDF3

func Torus

func Torus(x, y float64) SDF3

func Translate

func Translate(v Vec3, sdf SDF3) SDF3

func TranslateX

func TranslateX(n float64, sdf SDF3) SDF3

func TranslateY

func TranslateY(n float64, sdf SDF3) SDF3

func TranslateZ

func TranslateZ(n float64, sdf SDF3) SDF3

func TriPrism

func TriPrism(h, w float64) SDF3

func Union

func Union(items ...SDF3) SDF3

type SDFCircle

type SDFCircle struct {
	Radius float64
}

func (SDFCircle) Circle

func (s SDFCircle) Circle() (Vec2, float64)

func (SDFCircle) SDF

func (s SDFCircle) SDF() func(Vec2) float64

type SDFCone

type SDFCone struct {
	X, Y, H, R float64
}

func (SDFCone) SDF

func (s SDFCone) SDF() func(Vec3) float64

func (SDFCone) Sphere

func (s SDFCone) Sphere() (Vec3, float64)

type SDFCube

type SDFCube struct {
	X, Y, Z float64
}

func (SDFCube) SDF

func (s SDFCube) SDF() func(Vec3) float64

func (SDFCube) Sphere

func (s SDFCube) Sphere() (Vec3, float64)

type SDFDifference

type SDFDifference struct {
	Items []SDF3
}

func (SDFDifference) SDF

func (s SDFDifference) SDF() func(Vec3) float64

func (SDFDifference) Sphere

func (s SDFDifference) Sphere() (Vec3, float64)

type SDFDistort

type SDFDistort struct {
	SDF3
	Factor Vec3
}

non-uniform scaling

func (SDFDistort) SDF

func (s SDFDistort) SDF() func(Vec3) float64

func (SDFDistort) Sphere

func (s SDFDistort) Sphere() (Vec3, float64)

type SDFEllipsoid

type SDFEllipsoid struct {
	R Vec3
}

func (SDFEllipsoid) SDF

func (s SDFEllipsoid) SDF() func(Vec3) float64

func (SDFEllipsoid) Sphere

func (s SDFEllipsoid) Sphere() (Vec3, float64)

type SDFElongate

type SDFElongate struct {
	H Vec3
	SDF3
}

func (SDFElongate) SDF

func (s SDFElongate) SDF() func(Vec3) float64

func (SDFElongate) Sphere

func (s SDFElongate) Sphere() (Vec3, float64)

type SDFExtrude

type SDFExtrude struct {
	H float64
	SDF2
}

func (SDFExtrude) SDF

func (s SDFExtrude) SDF() func(Vec3) float64

func (SDFExtrude) Sphere

func (s SDFExtrude) Sphere() (Vec3, float64)

type SDFHexagram

type SDFHexagram struct {
	R float64
}

func (SDFHexagram) Circle

func (s SDFHexagram) Circle() (Vec2, float64)

func (SDFHexagram) SDF

func (s SDFHexagram) SDF() func(Vec2) float64

type SDFHollow

type SDFHollow struct {
	Thickness float64
	SDF3
}

func (SDFHollow) SDF

func (s SDFHollow) SDF() func(Vec3) float64

func (SDFHollow) Sphere

func (s SDFHollow) Sphere() (Vec3, float64)

type SDFIntersection

type SDFIntersection struct {
	Items []SDF3
}

func (SDFIntersection) SDF

func (s SDFIntersection) SDF() func(Vec3) float64

func (SDFIntersection) Sphere

func (s SDFIntersection) Sphere() (Vec3, float64)

type SDFMirror

type SDFMirror struct {
	SDF3
	Mul Vec3
}

func (SDFMirror) SDF

func (s SDFMirror) SDF() func(Vec3) float64

func (SDFMirror) Sphere

func (s SDFMirror) Sphere() (Vec3, float64)

type SDFParabola

type SDFParabola struct {
	M, H float64
}

func (SDFParabola) Circle

func (s SDFParabola) Circle() (Vec2, float64)

func (SDFParabola) SDF

func (s SDFParabola) SDF() func(Vec2) float64

type SDFPolygon

type SDFPolygon struct {
	N int
	R float64
}

func (SDFPolygon) Circle

func (s SDFPolygon) Circle() (Vec2, float64)

func (SDFPolygon) SDF

func (s SDFPolygon) SDF() func(Vec2) float64

type SDFRectangle

type SDFRectangle struct {
	X, Y float64
}

func (SDFRectangle) Circle

func (s SDFRectangle) Circle() (Vec2, float64)

func (SDFRectangle) SDF

func (s SDFRectangle) SDF() func(Vec2) float64

type SDFRepeat

type SDFRepeat struct {
	Count  Vec3
	Offset Vec3
	SDF3
}

func (SDFRepeat) SDF

func (s SDFRepeat) SDF() func(Vec3) float64

func (SDFRepeat) Sphere

func (s SDFRepeat) Sphere() (Vec3, float64)

type SDFRevolve

type SDFRevolve struct {
	O float64
	SDF2
}

func (SDFRevolve) SDF

func (s SDFRevolve) SDF() func(Vec3) float64

func (SDFRevolve) Sphere

func (s SDFRevolve) Sphere() (Vec3, float64)

type SDFRounded

type SDFRounded struct {
	Radius float64
	SDF3
}

func (SDFRounded) SDF

func (s SDFRounded) SDF() func(Vec3) float64

func (SDFRounded) Sphere

func (s SDFRounded) Sphere() (Vec3, float64)

type SDFScale

type SDFScale struct {
	SDF3
	Factor float64
}

func (SDFScale) SDF

func (s SDFScale) SDF() func(Vec3) float64

func (SDFScale) Sphere

func (s SDFScale) Sphere() (Vec3, float64)

type SDFSphere

type SDFSphere struct {
	R float64
}

func (SDFSphere) SDF

func (s SDFSphere) SDF() func(Vec3) float64

func (SDFSphere) Sphere

func (s SDFSphere) Sphere() (Vec3, float64)

type SDFStadium

type SDFStadium struct {
	H, R1, R2 float64
}

func (SDFStadium) Circle

func (s SDFStadium) Circle() (Vec2, float64)

func (SDFStadium) SDF

func (s SDFStadium) SDF() func(Vec2) float64

type SDFTorus

type SDFTorus struct {
	V Vec2
}

func (SDFTorus) SDF

func (s SDFTorus) SDF() func(Vec3) float64

func (SDFTorus) Sphere

func (s SDFTorus) Sphere() (Vec3, float64)

type SDFTransform

type SDFTransform struct {
	SDF3
	M Matrix44
	I Matrix44
}

func (SDFTransform) SDF

func (s SDFTransform) SDF() func(Vec3) float64

func (SDFTransform) Sphere

func (s SDFTransform) Sphere() (Vec3, float64)

type SDFTriangle

type SDFTriangle struct {
	P0, P1, P2 Vec2
}

func (SDFTriangle) Circle

func (s SDFTriangle) Circle() (Vec2, float64)

func (SDFTriangle) SDF

func (s SDFTriangle) SDF() func(Vec2) float64

type SDFUnion

type SDFUnion struct {
	Items []SDF3
}

func (SDFUnion) SDF

func (s SDFUnion) SDF() func(Vec3) float64

func (SDFUnion) Sphere

func (s SDFUnion) Sphere() (Vec3, float64)

type Scene

type Scene struct {
	Seed      int64   // Optional
	Camera    Camera  // Required
	Stuff     []Thing // Required
	Width     int     // in pixels
	Height    int     // in pixels
	Passes    int     // number of render passes
	Samples   int     // number of jittered samples per pixel
	Bounces   int     // max shadow ray bounces
	Horizon   float64 // max scene distance from 0,0,0 to limit marching rays
	Threshold float64 // distance from SDF considered close enough to be a hit
	Ambient   Color   // color when rays stop before reaching a light
	ShadowH   float64 // shadow alpha upper limit on invisible surfaces (dark center)
	ShadowL   float64 // shadow alpha lower limit on invisible surfaces (prenumbra cut-off)
	ShadowD   float64 // shadow darkness (light brightness multipler)
	ShadowR   float64 // shadow sharpness (light radius multipler)
	Raster    Raster  // summed samples per pixel
}

func (*Scene) At

func (scene *Scene) At(x, y int) color.Color

func (*Scene) Bounds

func (scene *Scene) Bounds() image.Rectangle

func (*Scene) ColorModel

func (scene *Scene) ColorModel() color.Model

func (*Scene) Merge

func (scene *Scene) Merge(raster Raster)

func (Scene) Render

func (scene Scene) Render() Raster

type Thing

type Thing struct {
	Mat Material
	SDF3
	// contains filtered or unexported fields
}

func Object

func Object(mat Material, sdf SDF3) Thing

func SpaceTime

func SpaceTime(size float64) Thing

func WorkBench

func WorkBench(size float64) Thing

func (*Thing) BoundingDistance

func (o *Thing) BoundingDistance(pos Vec3) float64

func (*Thing) Distance

func (o *Thing) Distance(pos Vec3) float64

func (*Thing) Material

func (o *Thing) Material() Material

func (*Thing) Normal

func (o *Thing) Normal(pos Vec3) Vec3

func (*Thing) Prepare

func (o *Thing) Prepare()

func (*Thing) SDF

func (o *Thing) SDF() func(Vec3) float64

func (*Thing) Sphere

func (o *Thing) Sphere() (Vec3, float64)

type Vec2

type Vec2 struct {
	X, Y float64
}

func V2

func V2(x, y float64) Vec2

func (Vec2) Abs

func (v Vec2) Abs() Vec2

func (Vec2) Add

func (v Vec2) Add(v2 Vec2) Vec2

func (Vec2) Div

func (v Vec2) Div(v2 Vec2) Vec2

func (Vec2) Dot

func (v1 Vec2) Dot(v2 Vec2) float64

func (Vec2) Length

func (v Vec2) Length() float64

func (Vec2) Max

func (v Vec2) Max(v1 Vec2) Vec2

func (Vec2) Min

func (v Vec2) Min(v1 Vec2) Vec2

func (Vec2) Mul

func (v Vec2) Mul(v2 Vec2) Vec2

func (Vec2) Scale

func (v Vec2) Scale(t float64) Vec2

Multiply by scalar

func (Vec2) Sub

func (v Vec2) Sub(v2 Vec2) Vec2

func (Vec2) Unit

func (v Vec2) Unit() Vec2

type Vec3

type Vec3 struct {
	X, Y, Z float64
}

func SDF3Normal

func SDF3Normal(sdf func(Vec3) float64, p Vec3) Vec3

func V3

func V3(x, y, z float64) Vec3

func (Vec3) Abs

func (v Vec3) Abs() Vec3

func (Vec3) Add

func (v Vec3) Add(v2 Vec3) Vec3

func (Vec3) Cross

func (v1 Vec3) Cross(v2 Vec3) Vec3

func (Vec3) Div

func (v Vec3) Div(v2 Vec3) Vec3

func (Vec3) Dot

func (v1 Vec3) Dot(v2 Vec3) float64

func (Vec3) Length

func (v Vec3) Length() float64

func (Vec3) Max

func (v Vec3) Max(v1 Vec3) Vec3

func (Vec3) Min

func (v Vec3) Min(v1 Vec3) Vec3

func (Vec3) Mul

func (v Vec3) Mul(v2 Vec3) Vec3

func (Vec3) Neg

func (v Vec3) Neg() Vec3

func (Vec3) Reflect

func (v Vec3) Reflect(n Vec3) Vec3

func (Vec3) Refract

func (v Vec3) Refract(n Vec3, niOverNt float64) (Vec3, bool)

func (Vec3) Scale

func (v Vec3) Scale(t float64) Vec3

func (Vec3) Sub

func (v Vec3) Sub(v2 Vec3) Vec3

func (Vec3) Unit

func (v Vec3) Unit() Vec3

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL