This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscene.js
186 lines (165 loc) · 3.97 KB
/
scene.js
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
/**
* @fileoverview ThreeJS scene management
*/
//Imports
import
{
Color,
GammaEncoding,
MOUSE,
Mesh,
MeshBasicMaterial,
PerspectiveCamera,
PlaneBufferGeometry,
Scene,
WebGLRenderer
} from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import utils from './utils';
//State
const state = {
camera: null,
canvas: null,
controls: null,
destroyed: false,
object: null,
plane: null,
renderer: null,
scene: null
};
//Window resize event handler
const resize = () =>
{
//Update renderer size
state.renderer.setSize(
state.canvas.clientWidth,
state.canvas.clientHeight
);
//Update camera aspect ratio
state.camera.aspect = state.canvas.clientWidth / state.canvas.clientHeight;
state.camera.updateProjectionMatrix();
};
/**
* @function Setup scene
* @param {Element} canvas HTML canvas element
* @param {Object} bed Bed dimensions
* @param {Object} theme Theme
* @param {String} gcode Raw GCODE
* @param {Object} position Position
* @param {Object} rotation Rotation
* @param {Object} scale Scale
*/
const setup = async (canvas, bed, theme, gcode, position, rotation, scale) =>
{
//Canvas
state.canvas = canvas;
//Renderer
state.renderer = new WebGLRenderer({antialias: true});
state.renderer.setPixelRatio(window.devicePixelRatio);
state.renderer.setSize(
state.canvas.clientWidth,
state.canvas.clientHeight
);
state.renderer.encoding = GammaEncoding;
state.renderer.outputEncoding = GammaEncoding;
state.renderer.shadowMap.enabled = true;
state.canvas.appendChild(state.renderer.domElement);
//Camera
state.camera = new PerspectiveCamera(
50,
state.canvas.clientWidth / state.canvas.clientHeight,
0.1,
200
);
state.camera.position.set(0, bed.X / 2, -bed.Y);
//Orbit controls
state.controls = new OrbitControls(state.camera, state.canvas);
state.controls.rotateSpeed = 0.7;
state.controls.minDistance = 1;
state.controls.maxDistance = 100;
state.controls.minPolarAngle = 0;
state.controls.maxPolarAngle = Math.PI;
state.controls.mouseButtons = {
LEFT: MOUSE.PAN,
MIDDLE: MOUSE.DOLLY,
RIGHT: MOUSE.ROTATE
};
//Scene
state.scene = new Scene();
state.scene.background = new Color(theme.backgroundColor);
//Plane
state.plane = new Mesh(
new PlaneBufferGeometry(),
new MeshBasicMaterial({color: new Color(theme.bedColor)})
);
state.plane.rotation.x = -Math.PI / 2;
state.plane.scale.set(bed.X, bed.Y, 1);
state.scene.add(state.plane);
//GCODE
if (gcode != null)
{
await utils.update.gcode(gcode, theme, state.scene);
utils.update.position(position);
utils.update.rotation(rotation);
utils.update.scale(scale);
}
//Subscribe to resize event
window.addEventListener('resize', resize);
//Start
const animate = () =>
{
if (!state.destroyed)
{
state.controls.update();
requestAnimationFrame(animate);
state.renderer.render(state.scene, state.camera);
}
};
animate();
//Environment
if (process.env.NODE_ENV == 'development' || process.env.NODE_ENV == 'testing')
{
window.getVueGcodeViewerState = () =>
{
return state;
};
}
};
/**
* @function Tear down scene
*/
const teardown = () =>
{
//Unsubscribe to resize event
window.removeEventListener('resize', resize);
//Clean everything in the scene
state.scene.children.forEach(object =>
{
//Geometry
if (
object.geometry != null &&
typeof object.geometry.dispose == 'function'
)
{
object.geometry.dispose();
}
//Material
if (
object.material != null &&
typeof object.material.dispose == 'function'
)
{
object.material.dispose();
}
});
//Disable animation loop
state.destroyed = true;
//Delete scene and camera
delete state.scene;
delete state.camera;
//Dispose of controls and renderer
state.controls.dispose();
state.renderer.dispose();
};
//Export
export {setup, teardown, state};