whispervis/objects.go

75 lines
1.9 KiB
Go
Raw Normal View History

2018-09-05 13:53:09 +00:00
package main
import (
"fmt"
2018-09-17 19:11:04 +00:00
"github.com/divan/graphx/graph"
"github.com/divan/graphx/layout"
2018-09-20 12:41:24 +00:00
"github.com/divan/three"
2018-09-05 13:53:09 +00:00
)
// CreateObjects creates WebGL primitives from layout/graphGroup data.
2018-09-17 19:11:04 +00:00
// TODO(divan): change positions and links types to something more clear and readable
func (w *WebGLScene) CreateObjects(positions map[string]*layout.Object, links []*graph.Link) {
w.graphGroup = three.NewGroup()
w.scene.Add(w.graphGroup)
2018-09-05 13:53:09 +00:00
w.nodesGroup = three.NewGroup()
w.graphGroup.Add(w.nodesGroup)
2018-09-05 13:53:09 +00:00
w.edgesGroup = three.NewGroup()
w.graphGroup.Add(w.edgesGroup)
2018-09-17 19:11:04 +00:00
w.createNodes(positions)
w.createEdges(positions, links)
2018-09-05 14:09:56 +00:00
}
2018-09-17 19:11:04 +00:00
func (w *WebGLScene) createNodes(positions map[string]*layout.Object) {
2018-09-05 14:09:56 +00:00
scale := 2.0
geometry := NewEthereumGeometry(scale)
material := NewNodeMaterial()
2018-09-17 19:11:04 +00:00
for _, node := range positions {
2018-09-05 13:53:09 +00:00
mesh := three.NewMesh(geometry, material)
mesh.Position.Set(node.X, node.Y, node.Z)
w.nodesGroup.Add(mesh)
w.nodes = append(w.nodes, mesh)
2018-09-05 13:53:09 +00:00
}
obj := w.nodesGroup.GetObjectById(100)
fmt.Println("Moving mesh", obj)
2018-09-05 14:09:56 +00:00
}
2018-09-05 13:53:09 +00:00
2018-09-17 19:11:04 +00:00
func (w *WebGLScene) createEdges(positions map[string]*layout.Object, links []*graph.Link) {
2018-09-05 14:09:56 +00:00
material := NewEdgeMatherial()
2018-09-17 19:11:04 +00:00
for _, link := range links {
2018-09-05 13:53:09 +00:00
from := link.From()
to := link.To()
2018-09-17 19:11:04 +00:00
start := positions[from]
end := positions[to]
2018-09-05 13:53:09 +00:00
var geom = three.NewBasicGeometry(three.BasicGeometryParams{})
geom.AddVertice(start.X, start.Y, start.Z)
geom.AddVertice(end.X, end.Y, end.Z)
2018-09-05 14:09:56 +00:00
line := three.NewLine(geom, material)
w.edgesGroup.Add(line)
w.lines = append(w.lines, line)
2018-09-05 13:53:09 +00:00
}
}
2018-09-17 19:11:04 +00:00
// RemoveObjects removes WebGL primitives, cleaning up scene.
func (w *WebGLScene) RemoveObjects() {
if w.nodesGroup != nil {
for _, child := range w.nodesGroup.Children {
w.nodesGroup.Remove(child)
2018-09-20 13:30:20 +00:00
}
}
if w.edgesGroup != nil {
for _, child := range w.edgesGroup.Children {
w.edgesGroup.Remove(child)
2018-09-20 13:30:20 +00:00
}
}
w.nodes, w.lines = nil, nil
w.graphGroup, w.nodesGroup, w.edgesGroup = nil, nil, nil
2018-09-17 19:11:04 +00:00
}