- Notifications
You must be signed in to change notification settings - Fork2
SDF Viewer integration for Go libraries
License
Apache-2.0, MIT licenses found
Licenses found
yeicor/sdf-viewer-go
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
SDF Viewer integration for Go libraries.This allows you to design 3D models in Go using Signed Distance Functions while visualizing the results as fast aspossible.Check out theSDF Viewer App's README for more information.
Go modules included in this repository:
- sdf-viewer-go: Pure Go implementation of the SDF ViewerAPI.Usage example.
- sdf-viewer-go-auto: Utilities for the automatic implementation ofthesdf-viewer-go API.
- sdf-viewer-go-sdfx:SDFX implementation of the SDF ViewerAPI.Usage example.
- sdf-viewer-go-sdf:SDF (fork of SDFX by soypat)implementation of the SDF Viewer API.Usage example.
This project usesTinyGo to build the WebAssembly binary (for now) as the Go Compiler can't exportWebAssembly functions. This means that it won't "be able to compile every Go program out there", but should begood enough for most projects. It is also slower to build (and run) than the Go Compiler.
Write the followingmain.go
file:
package mainimport ("fmt" sdfviewergo"github.com/Yeicor/sdf-viewer-go/sdf-viewer-go" sdfviewergosdfx"github.com/Yeicor/sdf-viewer-go/sdf-viewer-go-sdfx" ."github.com/deadsy/sdfx/sdf" v3"github.com/deadsy/sdfx/vec/v3")//export initfuncinit() {// This is the only function you need to call to initialize the SDF Viewer.sdfviewergo.SetRootSDF(sceneSDF())}//placeholder main to allow compilationfuncmain() {fmt.Println("This is not an executable. Compile this with `"+"tinygo build -o example.wasm -target wasi -opt 2 -x -no-debug ."+"` and use the SDF Viewer app (github.com/Yeicor/sdf-viewer) to visualize the SDF.")}// sceneSDF returns the root SDF of the scene.funcsceneSDF() sdfviewergo.SDF {// Simple scene:box,_:=Box3D(v3.Vec{X:1.,Y:1.,Z:1.},0.25)cyl,_:=Cylinder3D(1.5,0.25,0.25)sdfxSDF:=Union3D(box,cyl)cyl2,_:=Cylinder3D(1.5,0.25,0.25)cyl2rot:=Transform3D(cyl2,RotateY(DtoR(90)))sdfxSDF=Difference3D(sdfxSDF,cyl2rot)returnsdfviewergosdfx.NewSDF(sdfxSDF)}
The build command is:
tinygo build -o example.wasm -target wasi -opt 2 -x -no-debug.
Then just open the generated WebAssembly file withSDF Viewer App:
sdf-viewer app url relative/path/to/example.wasm
Or, on a web browser that also publishes the WebAssembly file:
http://localhost:8080/?cliurl=relative/path/to/example.wasm&envdark
If you want to automate reloading the object when the source code changes
Set up a server process that will automatically build the WebAssembly filewhen the Go file changes, and send the updated file to the SDF Viewer App:
sdf-viewer server -s relative/path/to/example.wasm -w relative/path/to/main.go -b /bin/sh -b\-c -b"cd relative/path/to/ && tinygo build -o example.wasm -target wasi -opt 2 -x -no-debug ."
Connect the app to this server process:
sdf-viewer app url http://localhost:8080/relative/path/to/example.wasm
Or, on a web browser with access to the local server:
http://localhost:8080/?cliurl=http://localhost:8080/relative/path/to/example.wasm&envdark
This is the SDFX example generated fromthis source file.View it onlineordownload the WebAssembly file to view it on thedesktop app.
The demo was recorded at a lower framerate, but it runs in real time on an integrated graphics card.
About
SDF Viewer integration for Go libraries