This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
/
main.go
106 lines (78 loc) · 3.19 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 21-universal-camera is a port to Go of the Babylon example located here:
// https://www.babylonjs-playground.com/#SRZRWV
// and linked from here:
// https://doc.babylonjs.com/babylon101/cameras
package main
import (
"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 {
scene := b.NewScene(engine, nil)
/********** UNIVERSAL CAMERA EXAMPLE **************************/
// This creates and positions a universal camera (non-mesh)
camera := b.NewUniversalCamera("camera", b.NewVector3(0, 0, -10), scene)
// This targets the camera to scene origin
camera.SetTarget(b.Vector3().Zero())
// This attaches the camera to the canvas
camera.AttachControl(canvas, true)
/**************************************************************/
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
b.NewHemisphericLight("light", b.NewVector3(0, 1, 0), scene)
//Materials
redMat := b.NewStandardMaterial("red", scene)
redMat.SetDiffuseColor(b.NewColor3(1, 0, 0))
redMat.SetEmissiveColor(b.NewColor3(1, 0, 0))
redMat.SetSpecularColor(b.NewColor3(1, 0, 0))
greenMat := b.NewStandardMaterial("green", scene)
greenMat.SetDiffuseColor(b.NewColor3(0, 1, 0))
greenMat.SetEmissiveColor(b.NewColor3(0, 1, 0))
greenMat.SetSpecularColor(b.NewColor3(0, 1, 0))
blueMat := b.NewStandardMaterial("blue", scene)
blueMat.SetDiffuseColor(b.NewColor3(0, 0, 1))
blueMat.SetEmissiveColor(b.NewColor3(0, 0, 1))
blueMat.SetSpecularColor(b.NewColor3(0, 0, 1))
mb := b.Mesh()
// Shapes
planeOpts := &babylon.MeshCreatePlaneOpts{Updatable: Bool(true), SideOrientation: Float64(mb.DOUBLESIDE())}
plane1 := mb.CreatePlane("plane1", 3, scene, planeOpts)
plane1.Position().SetX(-3)
plane1.Position().SetZ(0)
plane1.SetMaterial(redMat.Material)
plane2 := mb.CreatePlane("plane2", 3, scene, planeOpts)
plane2.Position().SetX(3)
plane2.Position().SetZ(-1.5)
plane2.SetMaterial(greenMat.Material)
plane3 := mb.CreatePlane("plane3", 3, scene, planeOpts)
plane3.Position().SetX(3)
plane3.Position().SetZ(1.5)
plane3.SetMaterial(blueMat.Material)
mb.CreateGround("ground1", 10, 10, 2, &babylon.MeshCreateGroundOpts{Scene: 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
}