/
view.go
295 lines (270 loc) · 7.54 KB
/
view.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package evev
//go:generate core generate -add-types
import (
"fmt"
"image"
"cogentcore.org/core/colors"
"cogentcore.org/core/errors"
"cogentcore.org/core/tree"
"cogentcore.org/core/xyz"
"github.com/emer/eve/v2/eve"
)
// View connects a Virtual World with a Xyz Scene to visualize the world,
// including ability to render offscreen
type View struct {
// the root Group node of the virtual world
World *eve.Group
// the scene object for visualizing
Scene *xyz.Scene
// the root Group node in the Scene under which the world is rendered
Root *xyz.Group
}
// NewView returns a new View that links given world with given scene and root group
func NewView(world *eve.Group, sc *xyz.Scene, root *xyz.Group) *View {
vw := &View{World: world, Scene: sc, Root: root}
return vw
}
// InitLibrary initializes Scene library with basic Solid shapes
// based on bodies in the virtual world. More complex visualizations
// can be configured after this.
func (vw *View) InitLibrary() {
vw.InitLibraryBody(vw.World, vw.Scene)
}
// Sync synchronizes the view to the world
func (vw *View) Sync() bool {
rval := vw.SyncNode(vw.World, vw.Root, vw.Scene)
return rval
}
// UpdatePose updates the view pose values only from world tree.
// Essential that both trees are already synchronized.
func (vw *View) UpdatePose() {
vw.UpdatePoseNode(vw.World, vw.Root)
vw.Scene.NeedsUpdate()
}
// UpdateBodyView updates the display properties of given body name
// recurses the tree until this body name is found.
func (vw *View) UpdateBodyView(bodyNames []string) {
vw.UpdateBodyViewNode(bodyNames, vw.World, vw.Root)
vw.Scene.NeedsUpdate()
}
// RenderOffNode does an offscreen render using given node
// for the camera position and orientation.
// Current scene camera is saved and restored
func (vw *View) RenderOffNode(node eve.Node, cam *Camera) error {
sc := vw.Scene
sc.UpdateNodesIfNeeded()
camnm := "eve-view-renderoff-save"
sc.SaveCamera(camnm)
defer sc.SetCamera(camnm)
sc.Camera.FOV = cam.FOV
sc.Camera.Near = cam.Near
sc.Camera.Far = cam.Far
nb := node.AsNodeBase()
sc.Camera.Pose.Pos = nb.Abs.Pos
sc.Camera.Pose.Quat = nb.Abs.Quat
sc.Camera.Pose.Scale.Set(1, 1, 1)
sz := sc.Geom.Size
sc.Geom.Size = cam.Size
sc.Frame.SetSize(sc.Geom.Size) // nop if same
ok := sc.Render()
sc.Geom.Size = sz
if !ok {
return fmt.Errorf("could not render scene")
}
return nil
}
// Image returns the current rendered image
func (vw *View) Image() (*image.RGBA, error) {
return vw.Scene.ImageCopy()
}
// DepthImage returns the current rendered depth image
func (vw *View) DepthImage() ([]float32, error) {
return vw.Scene.DepthImage()
}
///////////////////////////////////////////////////////////////
// Sync, Config
// InitLibraryBody initializes Scene library with basic Solid shapes
// based on bodies in the virtual world. More complex visualizations
// can be configured after this.
func (vw *View) InitLibraryBody(wn eve.Node, sc *xyz.Scene) {
bod := wn.AsBody()
if bod != nil {
vw.InitLibSolid(bod, sc)
}
for idx := range *wn.Children() {
wk := wn.Child(idx).(eve.Node)
vw.InitLibraryBody(wk, sc)
}
}
// InitLibSolid initializes Scene library with Solid for given body
func (vw *View) InitLibSolid(bod eve.Body, sc *xyz.Scene) {
nm := bod.Name()
bb := bod.AsBodyBase()
if bb.Vis == "" {
bb.Vis = nm
}
if _, has := sc.Library[nm]; has {
return
}
lgp := sc.NewInLibrary(nm)
sld := xyz.NewSolid(lgp, nm)
wt := bod.NodeType().ShortName()
switch wt {
case "eve.Box":
mnm := "eveBox"
bm := sc.MeshByName(mnm)
if bm == nil {
bm = xyz.NewBox(sc, mnm, 1, 1, 1)
}
sld.SetMeshName(mnm)
case "eve.Cylinder":
mnm := "eveCylinder"
cm := sc.MeshByName(mnm)
if cm == nil {
cm = xyz.NewCylinder(sc, mnm, 1, 1, 32, 1, true, true)
}
sld.SetMeshName(mnm)
case "eve.Capsule":
mnm := "eveCapsule"
cm := sc.MeshByName(mnm)
if cm == nil {
cm = xyz.NewCapsule(sc, mnm, 1, .2, 32, 1)
}
sld.SetMeshName(mnm)
case "eve.Sphere":
mnm := "eveSphere"
sm := sc.MeshByName(mnm)
if sm == nil {
sm = xyz.NewSphere(sc, mnm, 1, 32)
}
sld.SetMeshName(mnm)
}
}
// ConfigBodySolid configures a solid for a body with current values
func (vw *View) ConfigBodySolid(bod eve.Body, sld *xyz.Solid) {
wt := bod.NodeType().ShortName()
switch wt {
case "eve.Box":
bx := bod.(*eve.Box)
sld.Pose.Scale = bx.Size
if bx.Color != "" {
sld.Mat.Color = errors.Log1(colors.FromString(bx.Color))
}
case "eve.Cylinder":
cy := bod.(*eve.Cylinder)
sld.Pose.Scale.Set(cy.BotRad, cy.Height, cy.BotRad)
if cy.Color != "" {
sld.Mat.Color = errors.Log1(colors.FromString(cy.Color))
}
case "eve.Capsule":
cp := bod.(*eve.Capsule)
sld.Pose.Scale.Set(cp.BotRad/.2, cp.Height/1.4, cp.BotRad/.2)
if cp.Color != "" {
sld.Mat.Color = errors.Log1(colors.FromString(cp.Color))
}
case "eve.Sphere":
sp := bod.(*eve.Sphere)
sld.Pose.Scale.SetScalar(sp.Radius)
if sp.Color != "" {
sld.Mat.Color = errors.Log1(colors.FromString(sp.Color))
}
}
}
// ConfigView configures the view node to properly display world node
func (vw *View) ConfigView(wn eve.Node, vn xyz.Node, sc *xyz.Scene) {
wb := wn.AsNodeBase()
vb := vn.(*xyz.Group)
vb.Pose.Pos = wb.Rel.Pos
vb.Pose.Quat = wb.Rel.Quat
bod := wn.AsBody()
if bod == nil {
return
}
if !vb.HasChildren() {
sc.AddFromLibrary(bod.AsBodyBase().Vis, vb)
}
bgp := vb.Child(0)
if bgp.HasChildren() {
sld, has := bgp.Child(0).(*xyz.Solid)
if has {
vw.ConfigBodySolid(bod, sld)
}
}
}
// SyncNode updates the view tree to match the world tree, using
// ConfigChildren to maximally preserve existing tree elements
// returns true if view tree was modified (elements added / removed etc)
func (vw *View) SyncNode(wn eve.Node, vn xyz.Node, sc *xyz.Scene) bool {
nm := wn.Name()
vn.SetName(nm) // guaranteed to be unique
skids := *wn.Children()
tnl := make(tree.Config, 0, len(skids))
for _, skid := range skids {
tnl.Add(xyz.GroupType, skid.Name())
}
mod := vn.ConfigChildren(tnl)
modall := mod
for idx := range skids {
wk := wn.Child(idx).(eve.Node)
vk := vn.Child(idx).(xyz.Node)
vw.ConfigView(wk, vk, sc)
if wk.HasChildren() {
kmod := vw.SyncNode(wk, vk, sc)
if kmod {
modall = true
}
}
}
if modall {
sc.NeedsUpdate()
}
return modall
}
///////////////////////////////////////////////////////////////
// UpdatePose
// UpdatePoseNode updates the view pose values only from world tree.
// Essential that both trees are already synchronized.
func (vw *View) UpdatePoseNode(wn eve.Node, vn xyz.Node) {
skids := *wn.Children()
for idx := range skids {
wk := wn.Child(idx).(eve.Node)
vk := vn.Child(idx).(xyz.Node)
wb := wk.AsNodeBase()
vb := vk.AsNode()
vb.Pose.Pos = wb.Rel.Pos
vb.Pose.Quat = wb.Rel.Quat
vw.UpdatePoseNode(wk, vk)
}
}
// UpdateBodyViewNode updates the body view info for given name(s)
// Essential that both trees are already synchronized.
func (vw *View) UpdateBodyViewNode(bodyNames []string, wn eve.Node, vn xyz.Node) {
skids := *wn.Children()
for idx := range skids {
wk := wn.Child(idx).(eve.Node)
vk := vn.Child(idx).(xyz.Node)
match := false
if _, isBod := wk.(eve.Body); isBod {
for _, nm := range bodyNames {
if wk.Name() == nm {
match = true
break
}
}
}
if match {
wb := wk.(eve.Body)
bgp := vk.Child(0)
if bgp.HasChildren() {
sld, has := bgp.Child(0).(*xyz.Solid)
if has {
vw.ConfigBodySolid(wb, sld)
}
}
}
vw.UpdateBodyViewNode(bodyNames, wk, vk)
}
}