This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
/
main.go
66 lines (49 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 04-discover-basic-elements-plane is a port to Go of the Babylon example located here:
// https://www.babylonjs-playground.com/#LXZPJK
// and linked from here:
// https://doc.babylonjs.com/babylon101/discover_basic_elements
package main
import (
"math"
"syscall/js"
"github.com/gmlewis/go-babylonjs/babylon"
)
func main() {
doc := js.Global().Get("document")
canvas := doc.Call("getElementById", "renderCanvas") // Get the canvas element
b := babylon.New()
engine := b.NewEngine(canvas, &babylon.NewEngineOpts{Antialias: Bool(true)}) // Generate the BABYLON 3D engine
/******* Add the create scene function ******/
createScene := func() *babylon.Scene {
// Create the scene space
scene := b.NewScene(engine, nil)
// Add a camera to the scene and attach it to the canvas
camera := b.NewArcRotateCamera("Camera", -math.Pi/2, math.Pi/2, 4, b.Vector3().Zero(), scene, nil)
camera.AttachControl(canvas, true, nil)
// Add lights to the scene
b.NewHemisphericLight("light1", b.NewVector3(1, 1, 0), scene)
b.NewPointLight("light2", b.NewVector3(0, 1, -1), scene)
// Add and manipulate meshes in the scene
b.MeshBuilder().CreatePlane("plane", &babylon.PlaneOpts{Height: Float64(2), Width: Float64(1)}, scene)
return scene
}
/******* End of the create scene function ******/
scene := createScene() //Call the createScene function
// Register a render loop to repeatedly render the scene
engine.RunRenderLoop(scene.RenderLoopFunc(nil))
// Watch for browser/canvas resize events
window := js.Global().Get("window")
// Note that engine.ResizeFunc is never released since it is needed for resizing.
window.Call("addEventListener", "resize", engine.ResizeFunc())
// prevent program from terminating
c := make(chan struct{}, 0)
<-c
}
// Bool returns the pointer to the provided bool.
func Bool(v bool) *bool {
return &v
}
// Float64 returns the pointer to the provided float64.
func Float64(v float64) *float64 {
return &v
}