-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrenderer.go
More file actions
82 lines (70 loc) · 2.47 KB
/
renderer.go
File metadata and controls
82 lines (70 loc) · 2.47 KB
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
package vthree
import (
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/lngramos/three"
)
type webGLRenderer struct {
vecty.Core
opts WebGLOptions `vecty:"prop"`
markup []vecty.MarkupOrChild `vecty:"prop"`
canvas *vecty.HTML
renderer *three.WebGLRenderer
}
// Mount implements the vecty.Mounter interface.
func (r *webGLRenderer) Mount() {
r.renderer = newWebGLRenderer(&webGLRendererParameters{Canvas: r.canvas.Node()})
r.opts.Init(r.renderer)
}
// Unmount implements the vecty.Unmounter interface.
func (r *webGLRenderer) Unmount() {
if r.opts.Shutdown != nil {
r.opts.Shutdown(r.renderer)
}
}
// Render implements the vecty.Component interface.
func (r *webGLRenderer) Render() vecty.ComponentOrHTML {
r.canvas = elem.Canvas(r.markup...)
return r.canvas
}
// WebGLOptions represent options for the WebGLRenderer component.
type WebGLOptions struct {
// Init is called when the three.js WebGLRenderer has been created.
//
// This can happen multiple times during the lifecycle of an application
// if the Vecty WebGLRenderer component was unmounted and mounted again,
// e.g. due to navigating to a different page and back again.
Init func(r *three.WebGLRenderer)
// Shutdown is called before the canvas associated with the three.js
// WebGLRenderer will be destroyed. For example, when your Vecty
// application no longer renders the WebGLRenderer component and it is
// being unmounted.
Shutdown func(r *three.WebGLRenderer)
// TODO(slimsag): allow specifying other parameters like context, precision,
// etc. from three.js WebGLRenderer constructor here:
// https://threejs.org/docs/#api/renderers/WebGLRenderer
}
// WebGLRenderer returns a Vecty component that initializes a three.js WebGL renderer for
// use in a Vecty application.
func WebGLRenderer(opts WebGLOptions, markup ...vecty.MarkupOrChild) vecty.Component {
if opts.Init == nil {
panic("vthree: Renderer: must specify opts.Init")
}
return &webGLRenderer{
opts: opts,
markup: markup,
}
}
type webGLRendererParameters struct {
Canvas *js.Object
}
// Note: We can't use three.NewWebGLRenderer because it doesn't allow
// specifying any parameters yet. Easy enough to just call ourself, though.
func newWebGLRenderer(parameters *webGLRendererParameters) *three.WebGLRenderer {
return &three.WebGLRenderer{
Object: js.Global.Get("THREE").Get("WebGLRenderer").New(map[string]interface{}{
"canvas": parameters.Canvas,
}),
}
}