Render STL/MF3/OBJ objects. Camera, lights, material/color, background, and render settings https://lestrrat-3d.github.io
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
dependabot[bot] d501a6b25d
Bump actions/setup-go from 7.0.0 to 7.1.0 in the github-actions group (#46)
Bumps the github-actions group with 1 update: [actions/setup-go](https://github.com/actions/setup-go).


Updates `actions/setup-go` from 7.0.0 to 7.1.0
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](b7ad1dad31...d8cbb8045d)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-version: 7.1.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-11-21 12:45:26 +09:00
.github Bump actions/setup-go from 7.0.0 to 7.1.0 in the github-actions group (#46) 2026-11-21 12:45:26 +09:00
docs add highlight boost to depth of field (#45) 2026-11-20 13:10:48 +09:00
examples add depth of field with bokeh blur (#44) 2026-11-20 13:08:37 +09:00
internal/cmd/genimages add highlight boost to depth of field (#45) 2026-11-20 13:10:48 +09:00
.gitignore add headless mesh renderer 2026-08-31 08:29:41 +09:00
.golangci.yml bump golangci-lint to v2.15.0 (#38) 2026-10-26 12:18:28 +09:00
animation.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
animation_test.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
bounds.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
bounds_test.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
clip.go add model clip planes for cutaways (#40) 2026-10-27 17:23:00 +09:00
clip_test.go add model clip planes for cutaways (#40) 2026-10-27 17:23:00 +09:00
depth_of_field.go add highlight boost to depth of field (#45) 2026-11-20 13:10:48 +09:00
depth_of_field_test.go add highlight boost to depth of field (#45) 2026-11-20 13:10:48 +09:00
doc.go light both sides for decad sheet bodies 2026-09-23 23:34:00 +09:00
edges.go weld exact and keep zero-area triangles (#35) 2026-10-24 08:00:54 +09:00
edges_test.go weld exact and keep zero-area triangles (#35) 2026-10-24 08:00:54 +09:00
gallery_test.go add highlight boost to depth of field (#45) 2026-11-20 13:10:48 +09:00
gif.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
gif_palette.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
gif_test.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
go.mod Bump golang.org/x/image from 0.47.0 to 0.48.0 (#43) 2026-11-16 08:58:27 +09:00
go.sum Bump golang.org/x/image from 0.47.0 to 0.48.0 (#43) 2026-11-16 08:58:27 +09:00
import.go bump golangci-lint to v2.15.0 (#38) 2026-10-26 12:18:28 +09:00
import_benchmark_test.go add performance benchmarks 2026-09-26 21:26:34 +09:00
import_test.go add headless mesh renderer 2026-08-31 08:29:41 +09:00
LICENSE add license and contribution policy 2026-08-31 09:18:17 +09:00
lines.go add dotted 3D guide lines to scenes (#42) 2026-11-13 08:36:44 +09:00
lines_test.go add dotted 3D guide lines to scenes (#42) 2026-11-13 08:36:44 +09:00
mesh.go treat wide groups of slivers as faces (#28) 2026-10-22 15:31:22 +09:00
mesh_benchmark_test.go add performance benchmarks 2026-09-26 21:26:34 +09:00
mesh_render_cache.go ignore sliver facets when drawing edges (#25) 2026-10-22 13:05:05 +09:00
mesh_render_cache_test.go cache repeated mesh render work 2026-09-26 23:26:34 +09:00
orbit.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
orbit_test.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
pyramid_test.go add orbit camera and GIF turntable rendering (#41) 2026-11-04 14:35:14 +09:00
README.md add highlight boost to depth of field (#45) 2026-11-20 13:10:48 +09:00
render.go add depth of field with bokeh blur (#44) 2026-11-20 13:08:37 +09:00
render_benchmark_test.go cache repeated mesh render work 2026-09-26 23:26:34 +09:00
render_test.go fix depth test under perspective (#32) 2026-10-22 22:03:19 +09:00
scene.go add depth of field with bokeh blur (#44) 2026-11-20 13:08:37 +09:00
scene_test.go speed up rendering and edge drawing 2026-09-26 22:40:21 +09:00
section.go add plane section renderer (#39) 2026-10-27 14:30:15 +09:00
section_annotate.go add plane section renderer (#39) 2026-10-27 14:30:15 +09:00
section_paint.go add plane section renderer (#39) 2026-10-27 14:30:15 +09:00
section_raster.go add plane section renderer (#39) 2026-10-27 14:30:15 +09:00
section_test.go add plane section renderer (#39) 2026-10-27 14:30:15 +09:00

solidlens

A cyan torus, violet block, and coral cylinder rendered by solidlens against a deep navy background

solidlens is a pure-Go, headless raster renderer for triangle meshes. It loads STL, 3MF, and OBJ files, accepts *decad.Mesh through TriangleSource, and renders a configured scene to an image.RGBA or PNG stream.

The scene owns the camera, model materials, directional and point lights, and background. The renderer owns only output dimensions, so it is safe to call concurrently with independent scenes.

Install

go get github.com/lestrrat-3d/solidlens

API

Use ReadMesh when the filename selects the import format, or call ReadSTL, Read3MF, or ReadOBJ directly. Read3MF uses github.com/lestrrat-go/3mf, including build transforms, components, and model units. ReadSTL uses github.com/lestrrat-go/stl.

Use MeshFrom for native geometry. *decad.Mesh satisfies TriangleSource without an adapter because it provides Vertices() []r3.Vec and Triangles() [][3]int.

Build a Scene with Camera, Model, Material, DirectionalLight, PointLight, and Background, then call Render or RenderPNG. Mesh coordinates are treated as millimetres.

Set Model.Edges to draw lines along a model's edges. Edges carries Enabled, Color, and a Width in pixels, and it is off by default. Outline fills in the usual values for a given color. An edge is drawn when it borders the mesh, when the two faces meeting there differ by at least CreaseAngle degrees, or when it lies on the model's silhouette, so a tessellated curve keeps its outline without turning into a grid of facets. A negative CreaseAngle draws every shared edge, which gives a full wireframe. A facet narrower than half a pixel at the model's distance does not turn its neighbours' shared edge into a crease, so the gaps and slivers a CAD kernel leaves between smooth faces draw no line. A face built from many such facets, such as a flat flank cut into long needles, is wider than half a pixel as a whole, so it keeps its own creases. Set Edges.Tolerance to a length in millimetres to change that width, or to solidlens.Exact to draw the mesh as it is.

Edges are drawn after every model's surface, so they stay hidden behind nearer bodies.

Open meshes render with both sides lit. A decad sheet body, the result of WithSurfaceResult, tessellates to such a mesh, and passing its *decad.Mesh as Model.Mesh shows its inner walls as well as its outer ones. The front of a triangle is the side its vertices wind counter-clockwise on, which for a sheet is decad's positive side. Set Model.BackMaterial to shade the other side in a different material and make the sheet's orientation visible. When it is nil, both sides use Model.Material. Edge lines are drawn along a sheet's free edges, because those edges border the mesh.

The examples package contains a tested end-to-end render.

Orbit views and turntables

NewOrbit takes a set of models and returns an Orbit, which frames them and turns a view into a Scene. A view is a Yaw and Pitch in degrees plus a Zoom, so a tool that lets the user drag to rotate only has to change those and render again. The camera keeps Z up and the lights follow it, so the face being looked at stays lit.

RenderTurntableGIF writes a looping GIF of one full turn around the models. GitHub shows a GIF inline in a README, but it does not run scripts, so an animation is the closest it gets to a 3D view. Set Turntable.Width, Height, Frames, Delay, View, and Orbit to change the size, smoothness, speed, starting angle, and field of view or background. All are optional.

var out bytes.Buffer
err := solidlens.RenderTurntableGIF(ctx, &out, models, solidlens.Turntable{Width: 640, Height: 480})

A GIF holds 256 colours, so the palette is chosen from a sample of the frames and shared by all of them. Flat-shaded models keep their look, and a smooth gradient may show bands.

Orbit views and turntables

These pieces are separate, so you can use one without the others.

  • BoundsOf returns the box around any TriangleSources, with its Center and Radius.
  • FitOrbit takes bounds and returns an Orbit, a camera on a sphere around a target. Orbit is a plain value. A viewer that rotates on drag changes its Yaw and Pitch, one that zooms scales Distance, and Orbit.Camera turns it into the Camera for a Scene.
  • CameraLights returns a key and a fill light placed relative to a camera, so the visible side stays lit as the camera moves. It works with any camera, not only an Orbit.
  • SceneSource is a numbered list of scenes, one per animation frame. Scenes plays a list you built, and Turntable turns an Orbit once around a base scene, lighting it with CameraLights unless the base scene has lights.
  • RenderGIF renders any SceneSource as a GIF that loops forever. GitHub shows a GIF inline in a README, but it does not run scripts, so an animation is the closest a README gets to a 3D view.
bounds, _ := solidlens.BoundsOf(mesh)
turntable := solidlens.Turntable{
	Base:  solidlens.Scene{Models: models, Background: solidlens.RGB(0.02, 0.02, 0.03)},
	Orbit: solidlens.FitOrbit(bounds, 0),
}
err := solidlens.RenderGIF(ctx, &out, turntable, solidlens.GIFOptions{Width: 640, Height: 480})

A GIF holds 256 colours, so the palette is chosen from a sample of the frames and shared by all of them. Flat-shaded models keep their look, and a smooth gradient may show bands.

Plane sections

RenderSection draws how two or more solids meet on a cutting plane. Give a SectionView the plane (Origin, and orthonormal in-plane axes U and V, so a point's plane coordinates are (p-Origin).U and (p-Origin).V) and one SectionLayer per solid. A layer holds closed loops of 3D points that lie in the plane, such as decad's Body.SectionOutline returns: outer loops run counter-clockwise seen from the tip of U x V, and holes run clockwise.

img, report, err := solidlens.RenderSection(ctx, solidlens.SectionView{
	Plane: solidlens.SectionPlane{U: solidlens.Vec{X: 1}, V: solidlens.Vec{Y: 1}},
	Layers: []solidlens.SectionLayer{
		{Name: "gear", Loops: gearLoops, Fill: solidlens.RGBA(0.05, 0.4, 0.95, 0.55), Outline: solidlens.RGB(0.02, 0.2, 0.7)},
		{Name: "mate", Loops: mateLoops, Fill: solidlens.RGBA(0.95, 0.6, 0.05, 0.55), Outline: solidlens.RGB(0.7, 0.35, 0.02)},
	},
})

Where two layers cover the same area the overlap color, red by default, paints over both, and edges are anti-aliased by supersampling. The image gets a millimetre grid with labels, a scale bar, a legend, and an optional Caption. The window fits all layers unless Window is set. The returned SectionReport has each layer's exact area, a raster estimate of it, and the raster overlap area, along with the pixel and sample sizes that bound the estimate's precision.

Two toothed wheels on a plane with their overlap in red

Cutaways

Set Model.Clip to a ClipPlane to cut a model open before it is drawn. The half-space the plane's Normal points into is removed, so a camera on that side looks into the solid. A closed solid shows the inside of its own surface through the cut, and Cap shades it: an unlit material draws it as one flat colour, which reads as the solid's cut face. ClipMesh returns the clipped mesh directly.

model := solidlens.Model{
	Mesh:     mesh,
	Material: solidlens.Matte(color),
	Clip: &solidlens.ClipPlane{
		Normal: solidlens.Vec{Y: -1},
		Cap:    &solidlens.Material{Color: color, Unlit: true},
	},
}

A torus and a cylinder, each cut open along a plane so the flat cut faces show

Guide lines

Set Scene.Lines to draw world-space polylines over the render, such as the guides in an exploded view. A Line carries its Points, a Color, a Width in pixels (zero means one pixel) and a Dash pattern in pixels. Dash alternates the length of a dash and of the gap after it, so {6, 4} draws 6 pixel dashes 4 pixels apart. It runs along the whole polyline and carries on around corners. An empty Dash draws a solid line.

Lines are drawn on top of every model and its edges and are not depth tested, so a guide stays visible behind a surface. Width and dash lengths are screen pixels, so a line keeps its look at any distance.

Depth of field

Set Scene.DepthOfField to blur everything away from a focal plane. It takes a FocusDistance from the camera and an Aperture diameter, both in scene units, and a larger aperture blurs more. Each pixel spreads into a disc, or into a regular polygon when Blades is between 3 and 16, which gives the bokeh shape. MaxRadius caps the blur in pixels and defaults to 24. A nil DepthOfField renders everything sharp.

Sharp FocusDistance: 16, Aperture: 0.7, Blades: 6
Six spheres receding from the camera, all in focus The same spheres with only the cyan middle sphere sharp and the near and far ones blurred

A small bright spot that is out of focus fades into its surroundings, because the blur averages it with the dark pixels around it. Set Highlight to make pixels brighter than HighlightThreshold (default 0.8) count for 1+Highlight times as much in that average, so the spot stays visible as a disc or polygon. A highlight that lies behind a nearer surface does not glow over it.

A cyan sphere in focus in front of warm and cool lights, each blurred into a bright hexagon

bokehLightsScene in internal/cmd/genimages/depth_of_field.go builds this render with Highlight: 80.

The blur reads the finished depth buffer, so it cannot show surfaces hidden behind a blurred foreground edge. Render time grows with the square of the largest blur radius, so lower MaxRadius to speed up a heavily blurred render.

The gallery uses triangle meshes built in Go and rendered with RenderPNG. It shows directional and point lighting, per-model materials, edge lines, perspective camera placement, and a configured background. The faceted bodies show their facet edges, while the sphere keeps only its silhouette, because its neighbouring faces stay under the default crease angle.

Mechanical forms Primitive forms
A cyan wheel with raised spokes beside a gold faceted cylinder and dome A pink sphere, green block, and blue faceted cone

The committed STL and 3MF files in docs/models are the scene sources. Run go run ./internal/cmd/genimages to regenerate the checked-in images from those files and the section image from its in-code outlines. Run go run ./internal/cmd/genimages --models only when intentionally rebuilding the model assets.

License

This project is source-available, and is licensed under the PolyForm Noncommercial License 1.0.0.

  • Noncommercial use is free. Individuals, hobby and personal projects, research, education, nonprofits, and government may use, modify, and redistribute it at no cost, subject to the license terms.
  • Commercial / business use requires a separate license. Any use by or for a business, or for commercial advantage, is not permitted under the noncommercial license. To obtain a commercial license, reach out on Bluesky at @lestrrat.bsky.social.

Contributions

This repository does not accept external pull requests.