Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
52f93fb
added modules support. add LineWidth property to materials
soypat Aug 19, 2021
bc99be3
add Up to perspective camera
soypat Aug 31, 2021
f6312b5
update readme
soypat Aug 31, 2021
867a622
fix AddVertice type. add AddVertices
soypat Sep 15, 2021
714b1a8
add euler methods
soypat Sep 22, 2021
4f13484
add CylinderGeometry. go generate now is gofmt'd
soypat Nov 15, 2021
4f7cf1b
add arrow+polarGrid helpers
soypat Nov 15, 2021
1822fc8
add helpers, fix CylinderGeometry
soypat Nov 15, 2021
ebe9f22
fix case of Vector js fields
soypat Nov 16, 2021
fba941d
add textures
soypat Nov 16, 2021
da4a856
fix up materials/texture API
soypat Nov 16, 2021
8f02395
rewrite template generation. add PhongMaterial fields
soypat Nov 18, 2021
74db943
add Camera interface and Trackball Helper
soypat Nov 18, 2021
8954395
add trackball movement
soypat Nov 19, 2021
d151387
add undocumented properties for trackball and fix buffGeom
soypat Nov 19, 2021
dd32125
add Quaternion methods
soypat Nov 20, 2021
90f340a
add Matrix4 methods
soypat Nov 20, 2021
bc93b8f
update BufferGeom+applyMatrix API. Add compile-time checks
soypat Nov 20, 2021
6473815
add Points Object3D
soypat Nov 21, 2021
e5bde6f
update documentation. Fix BoxGeometry parameters
soypat Nov 21, 2021
ad54e79
cleanup example. add deprecated notices
soypat Nov 21, 2021
5e162a4
add earth visualization example
soypat Nov 22, 2021
b92311c
tidy up examples in README.md
soypat Nov 22, 2021
909e039
add Vector3 methods
soypat Nov 22, 2021
2a63031
rename package three->gthree
soypat Feb 10, 2022
41860a2
Update README.md
soypat Feb 10, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
/examples/**/*.js.map
!/examples/_vendor/*.js
examples/basic/basic

.vscode
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
## Archived
This package has been archived. I now favor developing with syscall/js package over gopherjs due to the near complete support it has for `unsafe` and other features of the Go language. If you are interested in a maintained, WASM-ready version please look at https://github.com/soypat/three

# three

GopherJS bindings for [three.js](https://threejs.org/). **Still a WIP.**

Keep in mind parts of these bindings are 4 years old and were not programmed by me :). I'm working hard-ish
to get everything back to speed with the latest version of three.js.

## Examples

* [examples directory](./examples) contains basic examples. There is a [earth with trackball controls](./examples/earth) example for camera manipulation.
* Example repo: https://github.com/soypat/threejs-golang-example of what package structure could look like for an gopherjs project.
* https://github.com/soypat/tiny-ahrsim: in the loop simulation of IMU attitude estimation on a Raspberry Pi Pico.
2 changes: 1 addition & 1 deletion buffer_attrbiute.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package three
package gthree

import "github.com/gopherjs/gopherjs/js"

Expand Down
36 changes: 36 additions & 0 deletions camera_method_generator/_template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions camera_method_generator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// The following directive is necessary to make the package coherent:
//go:build ignore
// +build ignore

package main

import (
_ "embed"
"flag"
"log"

"github.com/soypat/gthree/generator"
)

//go:embed _template.go
var cameraTemplate string

var (
typeName = flag.String("typeName", "", "Name of class that extends Camera e.g. PerspectiveCamera")
typeSlug = flag.String("typeSlug", "", "Slugified name of class e.g. perspective_camera")
)

func main() {
flag.Parse()

if *typeName == "" {
log.Fatal("a type name argument must be provided (e.g. -typeName PerspectiveCamera)")
}
if *typeSlug == "" {
log.Fatal("a type slug argument must be provided (e.g. -typeSlug perspective_camera)")
}
p := generator.Parameters{
FilePrefix: "gen_camera",
Template: cameraTemplate,
Slug: *typeSlug,
Type: *typeName,
}
err := generator.Execute(p)
if err != nil {
log.Fatal(err)
}
log.Printf("Generated file: %s", p.Filename())
}
21 changes: 15 additions & 6 deletions cameras_perspective_camera.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package three
package gthree

//go:generate go run camera_method_generator/main.go -typeName PerspectiveCamera -typeSlug perspective_camera

import "github.com/gopherjs/gopherjs/js"

Expand All @@ -8,20 +10,20 @@ type PerspectiveCameraPosition struct {

type PerspectiveCamera struct {
*js.Object

// Use Set method to set up vector.
Up Vector3 `js:"up"`
Position Vector3 `js:"position"`
MatrixAutoUpdate bool `js:"matrixAutoUpdate"`
Aspect float64 `js:"aspect"`
}

// Assert PerspectiveCamera implements Camera.
var _ Camera = PerspectiveCamera{}

func NewPerspectiveCamera(fov, aspect, near, far float64) PerspectiveCamera {
return PerspectiveCamera{Object: three.Get("PerspectiveCamera").New(fov, aspect, near, far)}
}

func (c PerspectiveCamera) Copy() PerspectiveCamera {
return PerspectiveCamera{Object: c.Object.Call("copy")}
}

func (c PerspectiveCamera) SetFocalLength(focalLength float64) {
c.Object.Call("setFocalLength", focalLength)
}
Expand All @@ -46,6 +48,13 @@ func (c PerspectiveCamera) SetViewOffset(fullWidth, fullHeight, x, y, width, hei
c.Object.Call("setViewOffset", fullWidth, fullHeight, x, y, width, height)
}

// SetUp sets the up direction for the camera.
//
// It is the equivalent to c.Up.Set(v.X, v.Y, v.Z)
func (c PerspectiveCamera) SetUp(v Vector3) {
c.Up.Set(v.X, v.Y, v.Z)
}

func (c PerspectiveCamera) ClearViewOffset() {
c.Object.Call("clearViewOffset")
}
Expand Down
95 changes: 95 additions & 0 deletions controls_trackball.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package gthree

import "github.com/gopherjs/gopherjs/js"

type TrackballControls struct {
*js.Object

DynamicDampingFactor float64 `js:"dynamicDampingFactor"`
Enabled bool `js:"enabled"`

// How far you can zoom out. Default is Infinity.
MaxDistance float64 `js:"maxDistance"`
// How far you can zoom in. Default is 0.
MinDistance float64 `js:"minDistance"`
// This object contains references to the mouse actions used by the controls.
// * LEFT is assigned with THREE.MOUSE.ROTATE
// * MIDDLE is assigned with THREE.MOUSE.ZOOM
// * RIGHT is assigned with THREE.MOUSE.PAN
MouseButtons *js.Object `js:"mouseButtons"`
NoPan bool `js:"noPan"`
NoRotate bool `js:"noRotate"`
NoZoom bool `js:"noZoom"`
Camera Camera `js:"object"`
PanSpeed float64 `js:"panSpeed"`
RotateSpeed float64 `js:"rotateSpeed"`
// Whether or not damping is disabled. Default is false.
StaticMoving bool `js:"staticMoving"`
ZoomSpeed float64 `js:"zoomSpeed"`
// This array holds keycodes for controlling interactions.
// When the first defined key is pressed, all mouse interactions (left, middle, right) performs orbiting.
// When the second defined key is pressed, all mouse interactions (left, middle, right) performs zooming.
// When the third defined key is pressed, all mouse interactions (left, middle, right) performs panning.
// Default is KeyA, KeyS, KeyD which represents A, S, D.
Keys []string `js:"keys"`
// WARNING: Not part of documentation. Use to set where zoom-in focuses on.
Target Vector3 `js:"target"`
// WARNING: Not part of documentation.
Target0 Vector3 `js:"target0"`
// WARNING: Not part of documentation. Current position of camera.
Position Vector3 `js:"position"`
}

// NewTrackballControls instances a TrackballControls. Requires TrackballControls to be
// added at a global level or on THREE. See https://github.com/turban/webgl-earth for a
// way of doing this easily using Eberhard Graether's http://egraether.com/ version.
func NewTrackballControls(camera Camera, domElement *js.Object) TrackballControls {
const namespace = "TrackballControls"
trackball := getModule(namespace)
if domElement == nil || domElement == js.Undefined {
panic("domElement must be defined")
}
return TrackballControls{
Object: trackball.New(camera, domElement),
}
}

// Ensures the controls stay in the range [minDistance, maxDistance]. Called by update().
func (t TrackballControls) CheckDistances() {
t.Object.Call("checkDistances")
}

// Should be called if the controls is no longer required.
func (t TrackballControls) Dispose() {
t.Object.Call("dispose")
}

// Should be called if the application window is resized.
func (t TrackballControls) HandleResizes() {
t.Object.Call("handleResizes")
}

// Performs panning if necessary. Called by update().
func (t TrackballControls) PanCamera() {
t.Object.Call("panCamera")
}

// Resets the controls to its initial state.
func (t TrackballControls) Reset() {
t.Object.Call("reset")
}

// Rotates the camera if necessary. Called by update().
func (t TrackballControls) RotateCamera() {
t.Object.Call("rotateCamera")
}

// Updates the controls. Usually called in the animation loop.
func (t TrackballControls) Update() {
t.Object.Call("update")
}

// Performs zooming if necessary. Called by update().
func (t TrackballControls) ZoomCamera() {
t.Object.Call("zoomCamera")
}
11 changes: 11 additions & 0 deletions core_camera.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gthree

import "github.com/gopherjs/gopherjs/js"

type Camera interface {
// Returns a Vector3 representing the world space direction
// in which the camera is looking. (Note: A camera looks down its local, negative z-axis).
GetWorldDirection(target Vector3) Vector3

getInternalObject() *js.Object
}
2 changes: 1 addition & 1 deletion core_face3.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package three
package gthree

import "github.com/gopherjs/gopherjs/js"

Expand Down
4 changes: 2 additions & 2 deletions core_geometry.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package three
package gthree

import "github.com/gopherjs/gopherjs/js"

type Geometry interface {
ApplyMatrix(matrix *Matrix4)
ApplyMatrix4(matrix *Matrix4)
RotateX()
RotateY()
RotateZ()
Expand Down
83 changes: 83 additions & 0 deletions core_material.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package gthree

import (
"github.com/gopherjs/gopherjs/js"
)

type Material interface {
OnBeforeCompile()
SetValues(values MaterialParameters)
ToJSON(meta interface{}) interface{}
Clone()
Copy(source Object3D)
Dispose()

getInternalObject() *js.Object
}

// Side defines which side of faces will be rendered - front, back or both. Default is FrontSide.
type Side float64

const (
FrontSide Side = 0
BackSide Side = 1
DoubleSide Side = 2
)

type Shading float64

const (
// SmoothShading is the default and linearly interpolates color between vertices.
SmoothShading Shading = 0

// FlatShading uses the color of the first vertex for every pixel in a face.
FlatShading Shading = 1
)

type MaterialParameters struct {
*js.Object

Color *Color `js:"color"`
Shading Shading `js:"shading"` // THREE.MeshLambertMaterial: .shading has been removed. Use the boolean .flatShading instead.
FlatShading bool `js:"flatShading"`
Side Side `js:"side"`
Transparent bool `js:"transparent"`
Opacity float64 `js:"opacity"`
Map *Texture `js:"map"`
LineWidth float64 `js:"linewidth"`

// Points
Size float64 `js:"size"`

// Phong Materials

BumpMap *Texture `js:"bumpMap"`
BumpScale float64 `js:"bumpScale"`
SpecularMap *Texture `js:"specularMap"`
Specular *Color `js:"specular"`

// Physical Materials

Clearcoat float64 `js:"clearcoat"`
ClearcoatMap *Texture `js:"clearcoatMap"`
ClearcoatNormalMap *Texture `js:"clearcoatNormalMap"`
ClearcoatNormalScale Vector2 `js:"clearcoatNormalScale"`
ClearcoatRoughness float64 `js:"clearcoatRoughness"`
ClearcoatRoughnessMap *Texture `js:"clearcoatRoughnessMap"`
// Index of refraction
IOR float64 `js:"ior"`
Reflectivity float64 `js:"reflectivity"`
Sheen float64 `js:"sheen"`
SheenRoughness float64 `js:"sheenRoughness"`
SheenRoughnessMap *Texture `js:"sheenRoughnessMap"`
SheenColor *Color `js:"sheenColor"`
SheenColorMap *Texture `js:"sheenColorMap"`
Transmission float64 `js:"transmission"`
TransmissionMap *Texture `js:"transmissionMap"`
}

func NewMaterialParameters() *MaterialParameters {
return &MaterialParameters{
Object: js.Global.Get("Object").New(),
}
}
4 changes: 2 additions & 2 deletions core_object3d.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package three
package gthree

import "github.com/gopherjs/gopherjs/js"

type Object3D interface {
ApplyMatrix(matrix *Matrix4)
ApplyMatrix4(matrix *Matrix4)
Add(Object3D)
Remove(*js.Object)
ToJSON() interface{}
Expand Down
5 changes: 5 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gthree

/*
File to include debugging functions.
*/
Loading