whispervis/scene.go

109 lines
2.7 KiB
Go
Raw Normal View History

2018-09-05 16:53:09 +03:00
package main
2018-09-17 22:11:04 +03:00
import (
"fmt"
2018-10-12 22:34:30 +02:00
"github.com/divan/graphx/layout"
2018-09-20 15:41:24 +03:00
"github.com/divan/three"
2018-09-17 22:11:04 +03:00
"github.com/gopherjs/gopherjs/js"
2018-09-20 15:41:24 +03:00
"github.com/status-im/whispervis/vthree"
2018-09-17 22:11:04 +03:00
)
// WebGLScene represents WebGL part of app.
type WebGLScene struct {
*vthree.WebGLRenderer
scene *three.Scene
camera three.PerspectiveCamera
renderer *three.WebGLRenderer
graphGroup *three.Group
nodesGroup *three.Group
edgesGroup *three.Group
controls TrackBallControl
2018-09-17 22:11:04 +03:00
autoRotate bool
2018-10-12 22:34:30 +02:00
wobble bool
wobbling *Wobbling
2018-10-12 22:34:30 +02:00
positions map[string]*layout.Object
// these slices exist here because we have no good way to access three.Group children for now
// TODO(divan): as soon as three.js wrappers allow us to access children, get rid of it here
nodes []*Mesh
lines []*Line
rt *RenderThrottler // used as a helper to reduce rendering calls when animation is not needed (experimental)
2018-09-17 22:11:04 +03:00
}
// NewWebGLScene inits and returns new WebGL scene and canvas.
func NewWebGLScene() *WebGLScene {
w := &WebGLScene{
rt: NewRenderThrottler(),
}
2018-09-17 22:11:04 +03:00
w.WebGLRenderer = vthree.NewWebGLRenderer(vthree.WebGLOptions{
2018-10-22 14:46:27 +02:00
Init: w.init,
Shutdown: w.shutdown,
Antialias: true,
2018-09-17 22:11:04 +03:00
})
return w
}
func (w *WebGLScene) init(renderer *three.WebGLRenderer) {
fmt.Println("WebGL init")
2018-10-19 22:10:56 +02:00
windowWidth := js.Global.Get("innerWidth").Float() - 300 // TODO(divan): sync this with page layout
2018-09-17 22:11:04 +03:00
windowHeight := js.Global.Get("innerHeight").Float() - 20
w.renderer = renderer
w.renderer.SetSize(windowWidth, windowHeight, true)
devicePixelRatio := js.Global.Get("devicePixelRatio").Float()
w.renderer.SetPixelRatio(devicePixelRatio)
w.InitScene(windowWidth, windowHeight)
w.animate()
}
func (w *WebGLScene) shutdown(renderer *three.WebGLRenderer) {
fmt.Println("WebGL shutdown")
w.scene = nil
w.camera = three.PerspectiveCamera{}
w.renderer = nil
w.RemoveObjects()
}
// Reset resets state of WebGLScene.
func (w *WebGLScene) Reset() {
fmt.Println("Resetting WebGL")
w.RemoveObjects()
}
2018-09-05 16:53:09 +03:00
// InitScene inits a new scene, sets up camera, lights and all that.
2018-09-17 22:11:04 +03:00
func (w *WebGLScene) InitScene(width, height float64) {
w.camera = three.NewPerspectiveCamera(70, width/height, 1, 1000)
2018-10-22 14:40:41 +02:00
w.camera.Position.Set(0, 0, 100)
2018-09-05 16:53:09 +03:00
2018-09-17 22:11:04 +03:00
w.scene = three.NewScene()
2018-10-22 14:40:41 +02:00
w.scene.Background = three.NewColorRGB(0, 0, 17)
2018-09-05 16:53:09 +03:00
2018-09-17 22:11:04 +03:00
w.InitLights()
w.InitControls()
w.Reset()
2018-09-05 16:53:09 +03:00
}
// InitLights init lights for the scene.
2018-09-17 22:11:04 +03:00
func (w *WebGLScene) InitLights() {
2018-10-22 14:40:41 +02:00
ambLight := three.NewAmbientLight(three.NewColorHex(0xbbbbbb), 1)
ambLight.MatrixAutoUpdate = false
2018-09-17 22:11:04 +03:00
w.scene.Add(ambLight)
2018-09-05 16:53:09 +03:00
2018-10-22 14:40:41 +02:00
light := three.NewDirectionalLight(three.NewColor("white"), 1)
light.MatrixAutoUpdate = false
2018-09-17 22:11:04 +03:00
w.scene.Add(light)
2018-09-05 16:53:09 +03:00
}
// InitControls init controls for the scene.
2018-09-17 22:11:04 +03:00
func (w *WebGLScene) InitControls() {
w.controls = NewTrackBallControl(w.camera, w.renderer)
2018-09-05 16:53:09 +03:00
}