-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.js
More file actions
253 lines (206 loc) · 7.28 KB
/
scene.js
File metadata and controls
253 lines (206 loc) · 7.28 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
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
/**
* Scene Setup Module
* Handles Three.js scene, camera, renderer, axes, and grid initialization
*/
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
// Default build volume dimensions (mm) – matches the Ender3 preset.
export const AXIS_LENGTH = 220;
// Scene state
export let scene, camera, renderer, controls;
export let axesLines;
export let gridHelper = null;
/**
* Initialize the Three.js scene, camera, renderer, and controls.
*/
export function initScene() {
// Create scene.
scene = new THREE.Scene();
scene.background = new THREE.Color(0x1a1a1a);
// Use Z-up coordinate system to match G-code expectations and model slicing
THREE.Object3D.DEFAULT_UP.set(0, 0, 1);
// Create camera.
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.up.set(0, 0, 1);
camera.position.set(300, 300, 150);
camera.lookAt(0, 0, 0);
// Create renderer.
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.getElementById('canvas-container').appendChild(renderer.domElement);
// Add orbit controls.
controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.screenSpacePanning = false;
controls.minDistance = 10;
controls.maxDistance = 500;
// Add lights.
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.4);
directionalLight.position.set(10, 10, 5);
scene.add(directionalLight);
// Add custom axes.
createAxes();
// Add grid helper.
createGridHelper();
}
/**
* Create custom axes with proper colors and thickness.
* @param {number} sizeX - Length of the X axis in mm.
* @param {number} sizeY - Length of the Y axis in mm.
* @param {number} sizeZ - Length of the Z axis in mm.
*/
function createAxes(sizeX = AXIS_LENGTH, sizeY = AXIS_LENGTH, sizeZ = AXIS_LENGTH) {
const axisThickness = 3;
// Create X axis (red)
const xGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(sizeX, 0, 0),
]);
const xMaterial = new THREE.LineBasicMaterial({
color: 0xff0000,
linewidth: axisThickness,
});
const xAxis = new THREE.Line(xGeometry, xMaterial);
scene.add(xAxis);
// Create Y axis (green)
const yGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, sizeY, 0),
]);
const yMaterial = new THREE.LineBasicMaterial({
color: 0x00ff00,
linewidth: axisThickness,
});
const yAxis = new THREE.Line(yGeometry, yMaterial);
scene.add(yAxis);
// Create Z axis (blue)
const zGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, sizeZ),
]);
const zMaterial = new THREE.LineBasicMaterial({
color: 0x0000ff,
linewidth: axisThickness,
});
const zAxis = new THREE.Line(zGeometry, zMaterial);
scene.add(zAxis);
axesLines = [xAxis, yAxis, zAxis];
}
/**
* Create grid helper on the XY plane.
* @param {number} sizeX - Width of the grid in mm (X direction).
* @param {number} sizeY - Length of the grid in mm (Y direction).
*/
function createGridHelper(sizeX = AXIS_LENGTH, sizeY = AXIS_LENGTH) {
const divisions = 20;
const colorCenterLine = 0x888888;
const colorGrid = 0x444444;
const group = new THREE.Group();
const materialCenter = new THREE.LineBasicMaterial({ color: colorCenterLine });
const materialGrid = new THREE.LineBasicMaterial({ color: colorGrid });
// Step size based on divisions
const stepX = sizeX / divisions;
const stepY = sizeY / divisions;
// Vertical lines (parallel to Y axis)
for (let x = 0; x <= sizeX + 1e-4; x += stepX) {
const points = [];
points.push(new THREE.Vector3(x, 0, 0));
points.push(new THREE.Vector3(x, sizeY, 0));
const geom = new THREE.BufferGeometry().setFromPoints(points);
const isCenter = Math.abs(x) < 1e-6;
const line = new THREE.Line(geom, isCenter ? materialCenter : materialGrid);
group.add(line);
}
// Horizontal lines (parallel to X axis)
for (let y = 0; y <= sizeY + 1e-4; y += stepY) {
const points = [];
points.push(new THREE.Vector3(0, y, 0));
points.push(new THREE.Vector3(sizeX, y, 0));
const geom = new THREE.BufferGeometry().setFromPoints(points);
const isCenter = Math.abs(y) < 1e-6;
const line = new THREE.Line(geom, isCenter ? materialCenter : materialGrid);
group.add(line);
}
gridHelper = group;
scene.add(gridHelper);
}
/**
* Update the scene axes and grid to match new printer build volume dimensions.
* Updates the existing Three.js objects in-place so that all references held by
* event listeners (axis visibility toggles) continue to work correctly.
*
* @param {number} width - Build plate width in mm (X axis).
* @param {number} length - Build plate length in mm (Y axis).
* @param {number} height - Build volume height in mm (Z axis).
*/
export function updateBuildVolume(width, length, height) {
if (axesLines) {
// Update each axis line's endpoint position in-place.
const xPos = axesLines[0].geometry.attributes.position;
xPos.setXYZ(1, width, 0, 0);
xPos.needsUpdate = true;
const yPos = axesLines[1].geometry.attributes.position;
yPos.setXYZ(1, 0, length, 0);
yPos.needsUpdate = true;
const zPos = axesLines[2].geometry.attributes.position;
zPos.setXYZ(1, 0, 0, height);
zPos.needsUpdate = true;
}
if (gridHelper) {
// Dispose existing child geometries and remove them from the group.
while (gridHelper.children.length > 0) {
const child = gridHelper.children[0];
child.geometry.dispose();
gridHelper.remove(child);
}
// Rebuild the grid for the new build plate dimensions.
const divisions = 20;
const colorCenterLine = 0x888888;
const colorGrid = 0x444444;
const materialCenter = new THREE.LineBasicMaterial({ color: colorCenterLine });
const materialGrid = new THREE.LineBasicMaterial({ color: colorGrid });
const stepX = width / divisions;
const stepY = length / divisions;
for (let x = 0; x <= width + 0.001; x += stepX) {
const geom = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, 0, 0),
new THREE.Vector3(x, length, 0),
]);
const isCenter = Math.abs(x) < 1e-6;
gridHelper.add(new THREE.Line(geom, isCenter ? materialCenter : materialGrid));
}
for (let y = 0; y <= length + 0.001; y += stepY) {
const geom = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, y, 0),
new THREE.Vector3(width, y, 0),
]);
const isCenter = Math.abs(y) < 1e-6;
gridHelper.add(new THREE.Line(geom, isCenter ? materialCenter : materialGrid));
}
}
}
/**
* Handle window resize.
*/
export function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
/**
* Animation loop.
*/
export function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}