-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththreejs.go
177 lines (150 loc) · 4.96 KB
/
threejs.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
package onthefly
import (
"fmt"
"log"
)
// For generating IDs
var (
geometryCounter = 0
materialCounter = 0
meshCounter = 0
)
// Unique prefixes when generating IDs
const (
geometryPrefix = "g"
materialPrefix = "ma"
meshPrefix = "m"
)
type (
// For Three.JS elements, like a mesh or material
Element struct {
ID string // name of the variable
JS string // javascript code for creating the element
}
// The Three.JS render function, where head and tail are standard
RenderFunc struct {
head, mid, tail string
}
// Different types of elements
Geometry Element
Material Element
Mesh Element
)
// Create a HTML5 page that links with Three.JS and sets up a scene
func NewThreeJS(args ...string) (*Page, *Tag) {
title := "Untitled"
if len(args) > 0 {
title = args[0]
}
page := NewHTML5Page(title)
// Style the page for showing a fullscreen canvas
page.FullCanvas()
if len(args) > 1 {
threeJSURL := args[1]
page.LinkToJSInBody(threeJSURL)
} else {
// Link to Three.JS
page.LinkToJSInBody("https://threejs.org/build/three.min.js")
}
// Add a scene
script, _ := page.AddScriptToBody("var scene = new THREE.Scene();")
// Return the script tag that can be used for adding additional
// javascript/Three.JS code
return page, script
}
// Create a HTML5 page that includes the given JavaScript code at the end of
// the <body> tag, before </body>. The given JS code must be the contents of
// the http://threejs.org/build/three.min.js script for this to work.
func NewThreeJSWithGiven(titleText, js string) (*Page, *Tag) {
page := NewHTML5Page(titleText)
// Style the page for showing a fullscreen canvas
page.FullCanvas()
// Add the given JavaScript to the body
page.AddScriptToBody(js)
// Add a scene
script, _ := page.AddScriptToBody("var scene = new THREE.Scene();")
// Return the script tag that can be used for adding additional
// javascript/Three.JS code
return page, script
}
// Add a camera with default settings
func (three *Tag) AddCamera() {
// TODO create an AddCustomCamera function
three.AddContent("var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);")
}
// Add a WebGL renderer with default settings
func (three *Tag) AddRenderer() {
three.AddContent("var renderer = new THREE.WebGLRenderer();")
three.AddContent("renderer.setSize(window.innerWidth, window.innerHeight);")
three.AddContent("document.body.appendChild(renderer.domElement);")
}
// Add a mesh to the current scene
func (three *Tag) AddToScene(mesh *Mesh) {
three.AddContent(mesh.JS)
three.AddContent("scene.add(" + mesh.ID + ");")
}
// Create a new mesh, given geometry and material.
// The geometry and material will be instanciated together with the mesh.
func NewMesh(geometry *Geometry, material *Material) *Mesh {
id := fmt.Sprintf("%s%d", meshPrefix, meshCounter)
meshCounter++
js := geometry.JS + material.JS
js += "var " + id + " = new THREE.Mesh(" + geometry.ID + ", " + material.ID + ");"
return &Mesh{id, js}
}
// Set the camera position. Axis must be "x", "y", or "z".
func (three *Tag) CameraPos(axis string, value int) {
if (axis != "x") && (axis != "y") && (axis != "z") {
log.Fatalln("camera axis must be x, y or z")
}
three.AddContent(fmt.Sprintf("camera.position.%s = %d;", axis, value))
}
// Very simple type of material
func NewMaterial(color string) *Material {
id := fmt.Sprintf("%s%d", materialPrefix, materialCounter)
materialCounter++
js := "var " + id + " = new THREE.MeshBasicMaterial({color: " + color + "});"
return &Material{id, js}
}
// Create a material which reflects the normals of the geometry
func NewNormalMaterial() *Material {
id := fmt.Sprintf("%s%d", materialPrefix, materialCounter)
materialCounter++
js := "var " + id + " = new THREE.MeshNormalMaterial();"
return &Material{id, js}
}
// Create geometry for a box
func NewBoxGeometry(w, h, d int) *Geometry {
id := fmt.Sprintf("%s%d", geometryPrefix, geometryCounter)
geometryCounter++
js := fmt.Sprintf("var %s = new THREE.BoxGeometry(%d, %d, %d);", id, w, h, d)
return &Geometry{id, js}
}
// Add a test cube to the scene
func (three *Tag) AddTestCube() *Mesh {
// TODO Create functions for adding geometry, material and creating meshes
//material := NewMaterial(color)
material := NewNormalMaterial()
geometry := NewBoxGeometry(1, 1, 1)
cube := NewMesh(geometry, material)
three.AddToScene(cube)
return cube
}
// Create a new render function, which is called at every animation frame
func NewRenderFunction() *RenderFunc {
head := "var render = function() { requestAnimationFrame(render);"
tail := "renderer.render(scene, camera); };"
return &RenderFunc{head, "", tail}
}
// Add javascript code to the body of a render function
func (r *RenderFunc) AddJS(s string) {
r.mid += s
}
// Add a render function.
// If call is true, the render function is called at the end of the script.
func (three *Tag) AddRenderFunction(r *RenderFunc, call bool) {
three.AddContent(r.head + r.mid + r.tail)
if call {
three.AddContent("render();")
}
}