Fix WebGL initialization order issue

This commit is contained in:
Ivan Danyliuk 2018-10-23 08:28:14 +02:00
parent c3acd329c7
commit 1d2651c4ef
No known key found for this signature in database
GPG Key ID: 97ED33CE024E1DBF
4 changed files with 22 additions and 7 deletions

View File

@ -29,6 +29,9 @@ func (p *Page) UpdateGraph() {
// and creates all new objects based on current data and positions.
// It doesn't do any calculations of changes to the layout or graph data.
func (p *Page) RecreateObjects() {
p.loaded = true
p.loader.Reset()
p.webgl.RemoveObjects()
p.webgl.CreateObjects(p.layout.Positions(), p.layout.Links())

View File

@ -39,7 +39,7 @@ func NewPage() *Page {
}
page.forceEditor = widgets.NewForceEditor(page.onForcesApply)
page.network = widgets.NewNetworkSelector(page.onNetworkChange)
page.webgl = NewWebGLScene()
page.webgl = NewWebGLScene(page.onWebGLReady)
page.simulationWidget = widgets.NewSimulation("http://localhost:8084", page.startSimulation, page.replaySimulation)
page.statsWidget = widgets.NewStats()
return page
@ -128,6 +128,7 @@ func (p *Page) onNetworkChange(network *network.Network) {
// set forced positions if found in network
if network.Positions != nil {
fmt.Println("Using precalculated positions")
p.layout.SetPositions(network.Positions)
go p.RecreateObjects()
return
@ -194,3 +195,8 @@ func (p *Page) header() *vecty.HTML {
),
)
}
// onWebGLReady is executed when WebGL context is up and ready to render scene.
func (p *Page) onWebGLReady() {
p.onNetworkChange(p.network.Current())
}

View File

@ -33,12 +33,15 @@ type WebGLScene struct {
lines []*Line
rt *RenderThrottler // used as a helper to reduce rendering calls when animation is not needed (experimental)
initFn func() // function to run on initialization
}
// NewWebGLScene inits and returns new WebGL scene and canvas.
func NewWebGLScene() *WebGLScene {
func NewWebGLScene(initFn func()) *WebGLScene {
w := &WebGLScene{
rt: NewRenderThrottler(),
rt: NewRenderThrottler(),
initFn: initFn,
}
w.WebGLRenderer = vthree.NewWebGLRenderer(vthree.WebGLOptions{
Init: w.init,
@ -61,6 +64,9 @@ func (w *WebGLScene) init(renderer *three.WebGLRenderer) {
w.InitScene(windowWidth, windowHeight)
if w.initFn != nil {
w.initFn()
}
w.animate()
}

View File

@ -139,6 +139,10 @@ func (n *NetworkSelector) onUpload(json []byte) {
n.networks[net.Name] = net
n.setCurrentNetwork(net)
if n.handler != nil {
go n.handler(n.current)
}
vecty.Rerender(n)
}
@ -150,8 +154,4 @@ func (n *NetworkSelector) Current() *network.Network {
// setCurrentNetwork changes current network and runs needed update handlers.
func (n *NetworkSelector) setCurrentNetwork(net *network.Network) {
n.current = net
if n.handler != nil {
go n.handler(n.current)
}
}