Skip to content

Commit 6b84572

Browse files
committed
use three as modules
1 parent 5f1e628 commit 6b84572

File tree

14 files changed

+94
-61
lines changed

14 files changed

+94
-61
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import * as THREE from 'three';
2+
import { JSONLoader } from 'three/src/loaders/JSONLoader.js';
33
import { Interface } from 'doodle3d-slicer';
44
import fileURL from '!url-loader!./models/shape.json';
55
import { render } from 'react-dom';
@@ -20,7 +20,7 @@ const downloadGCode = ({ gcode: { gcode } }) => {
2020
fileSaver.saveAs(file);
2121
};
2222

23-
const jsonLoader = new THREE.JSONLoader();
23+
const jsonLoader = new JSONLoader();
2424
jsonLoader.load(fileURL, geometry => {
2525
render((
2626
<MuiThemeProvider>

package-lock.json

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
"react-dom": "^16.0.0",
2424
"react-jss": "^7.2.0",
2525
"react-resize-detector": "^1.1.0",
26-
"three": "^0.83.0"
26+
"three": "^0.88.0"
2727
},
2828
"devDependencies": {
29+
"raw-loader": "^0.5.1",
2930
"babel-plugin-inline-import": "^2.0.6",
3031
"babel-preset-stage-0": "^6.24.1",
3132
"babel-plugin-syntax-dynamic-import": "^6.18.0",

src/interface/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash';
22
import React from 'react';
3-
import * as THREE from 'three';
3+
import { Quaternion } from 'three/src/math/Quaternion.js';
4+
import { Vector3 } from 'three/src/math/Vector3.js';
45
import PropTypes from 'proptypes';
56
import { placeOnGround, createScene, fetchProgress, slice, TabTemplate } from './utils.js';
67
import injectSheet from 'react-jss';
@@ -161,16 +162,13 @@ class Interface extends React.Component {
161162
}
162163
};
163164

164-
rotateX = () => this.rotate(new THREE.Vector3(0, 0, 1), Math.PI / 2.0);
165-
rotateY = () => this.rotate(new THREE.Vector3(1, 0, 0), Math.PI / 2.0);
166-
rotateZ = () => this.rotate(new THREE.Vector3(0, 1, 0), Math.PI / 2.0);
165+
rotateX = () => this.rotate(new Vector3(0, 0, 1), Math.PI / 2.0);
166+
rotateY = () => this.rotate(new Vector3(1, 0, 0), Math.PI / 2.0);
167+
rotateZ = () => this.rotate(new Vector3(0, 1, 0), Math.PI / 2.0);
167168
rotate = (axis, angle) => {
168169
const { mesh, render } = this.state;
169170
if (mesh) {
170-
const quaternion = new THREE.Quaternion();
171-
quaternion.setFromAxisAngle(axis, angle);
172-
mesh.quaternion.premultiply(quaternion);
173-
mesh.updateMatrix();
171+
mesh.rotateOnWorldAxis(axis, angle);
174172
placeOnGround(mesh);
175173
render();
176174
}

src/interface/utils.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
import * as THREE from 'three';
2+
import { Box3 } from 'three/src/math/Box3.js';
3+
import { Matrix4 } from 'three/src/math/Matrix4.js';
4+
import { Scene } from 'three/src/scenes/Scene.js';
5+
import { PerspectiveCamera } from 'three/src/cameras/PerspectiveCamera.js';
6+
import { AmbientLight } from 'three/src/lights/AmbientLight.js';
7+
import { DirectionalLight } from 'three/src/lights/DirectionalLight.js';
8+
import { MeshPhongMaterial } from 'three/src/materials/MeshPhongMaterial.js';
9+
import { BoxGeometry } from 'three/src/geometries/BoxGeometry.js';
10+
import { Mesh } from 'three/src/objects/Mesh.js';
11+
import { BoxHelper } from 'three/src/helpers/BoxHelper.js';
12+
import { WebGLRenderer } from 'three/src/renderers/WebGLRenderer.js';
13+
import { DoubleSide } from 'three/src/constants.js';
214
import 'three/examples/js/controls/EditorControls';
315
import printerSettings from '../settings/printer.yml';
416
import materialSettings from '../settings/material.yml';
@@ -8,7 +20,7 @@ import React from 'react';
820
import PropTypes from 'prop-types';
921

1022
export function placeOnGround(mesh) {
11-
const boundingBox = new THREE.Box3().setFromObject(mesh);
23+
const boundingBox = new Box3().setFromObject(mesh);
1224

1325
mesh.position.y -= boundingBox.min.y;
1426
mesh.updateMatrix();
@@ -21,30 +33,30 @@ export function createScene(canvas, props, state) {
2133
// center geometry
2234
geometry.computeBoundingBox();
2335
const center = geometry.boundingBox.getCenter();
24-
geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-center.x, -center.y, -center.z));
36+
geometry.applyMatrix(new Matrix4().makeTranslation(-center.x, -center.y, -center.z));
2537

26-
const scene = new THREE.Scene();
38+
const scene = new Scene();
2739

28-
const camera = new THREE.PerspectiveCamera(50, 1, 1, 10000);
40+
const camera = new PerspectiveCamera(50, 1, 1, 10000);
2941
camera.position.set(0, 400, 300);
3042

31-
const directionalLightA = new THREE.DirectionalLight(0xa2a2a2);
43+
const directionalLightA = new DirectionalLight(0xa2a2a2);
3244
directionalLightA.position.set(1, 1, 1);
3345
scene.add(directionalLightA);
3446

35-
const directionalLightB = new THREE.DirectionalLight(0xa2a2a2);
47+
const directionalLightB = new DirectionalLight(0xa2a2a2);
3648
directionalLightB.position.set(-1, 1, -1);
3749
scene.add(directionalLightB);
3850

39-
const light = new THREE.AmbientLight(0x656565);
51+
const light = new AmbientLight(0x656565);
4052
scene.add(light);
4153

42-
const material = new THREE.MeshPhongMaterial({ color: 0x2194ce, side: THREE.DoubleSide, specular: 0xc5c5c5, shininess: 5 });
43-
const mesh = new THREE.Mesh(geometry, material);
54+
const material = new MeshPhongMaterial({ color: 0x2194ce, side: DoubleSide, specular: 0xc5c5c5, shininess: 5 });
55+
const mesh = new Mesh(geometry, material);
4456
placeOnGround(mesh);
4557
scene.add(mesh);
4658

47-
const box = new THREE.BoxHelper(new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1).applyMatrix(new THREE.Matrix4().makeTranslation(0, 0.5, 0))), 0x72bcd4);
59+
const box = new BoxHelper(new Mesh(new BoxGeometry(1, 1, 1).applyMatrix(new Matrix4().makeTranslation(0, 0.5, 0))), 0x72bcd4);
4860
scene.add(box);
4961

5062
const { dimensions } = settings;
@@ -66,7 +78,7 @@ export function createScene(canvas, props, state) {
6678
const updateCanvas = (canvas) => {
6779
if (!renderer || renderer.domElement !== canvas) {
6880
if (renderer) renderer.dispose();
69-
renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true });
81+
renderer = new WebGLRenderer({ canvas, alpha: true, antialias: true });
7082
renderer.setClearColor(0xffffff, 0);
7183
}
7284
if (!editorControls || editorControls.domElement !== canvas) {
@@ -106,14 +118,16 @@ const GCODE_SERVER_URL = 'https://gcodeserver.doodle3d.com';
106118
const CONNECT_URL = 'http://connect.doodle3d.com/';
107119

108120
export async function slice(name, mesh, settings, printers, quality, material, updateProgress) {
121+
if (!printers) throw new Error('Please select a printer');
122+
109123
const { dimensions } = settings;
110124
const centerX = dimensions.x / 2;
111125
const centerY = dimensions.y / 2;
112126

113127
const geometry = mesh.geometry.clone();
114128
mesh.updateMatrix();
115129

116-
const matrix = new THREE.Matrix4().makeTranslation(centerY, 0, centerX).multiply(mesh.matrix);
130+
const matrix = new Matrix4().makeTranslation(centerY, 0, centerX).multiply(mesh.matrix);
117131
const { gcode } = await sliceGeometry(settings, geometry, matrix, false, false, ({ progress }) => {
118132
updateProgress({
119133
action: progress.action,

src/sliceActions/calculateLayersIntersections.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as THREE from 'three';
1+
import { Vector2 } from 'three/src/math/Vector2.js';
22

33
export default function calculateLayersIntersections(lines, settings) {
44
const {
@@ -38,7 +38,7 @@ export default function calculateLayersIntersections(lines, settings) {
3838
z = line.end.z * alpha + line.start.z * alpha1;
3939
}
4040

41-
layerIntersectionPoints[layerIndex][lineIndex] = new THREE.Vector2(z, x);
41+
layerIntersectionPoints[layerIndex][lineIndex] = new Vector2(z, x);
4242
}
4343
}
4444
}

src/sliceActions/createLines.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import * as THREE from 'three';
1+
import { Line3 } from 'three/src/math/Line3.js';
2+
import { Vector2 } from 'three/src/math/Vector2.js';
23

34
function addLine(geometry, lineLookup, lines, a, b, isFlat) {
45
const index = lines.length;
56
lineLookup[`${a}_${b}`] = index;
67

78
lines.push({
8-
line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]),
9+
line: new Line3(geometry.vertices[a], geometry.vertices[b]),
910
connects: [],
1011
normals: [],
1112
isFlat
@@ -38,7 +39,7 @@ export default function createLines(geometry, settings) {
3839
lines[lineIndexB].connects.push(lineIndexC, lineIndexA);
3940
lines[lineIndexC].connects.push(lineIndexA, lineIndexB);
4041

41-
const normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize();
42+
const normal = new Vector2(face.normal.z, face.normal.x).normalize();
4243
lines[lineIndexA].normals.push(normal);
4344
lines[lineIndexB].normals.push(normal);
4445
lines[lineIndexC].normals.push(normal);

src/sliceActions/helpers/GCode.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as THREE from 'three';
1+
import { Vector2 } from 'three/src/math/Vector2.js';
22
import { PRECISION } from '../../constants.js';
33

44
export const MOVE = 'G';
@@ -16,7 +16,7 @@ export default class {
1616

1717
this._gcode = [];
1818
this._currentValues = {};
19-
this._nozzlePosition = new THREE.Vector2(0, 0);
19+
this._nozzlePosition = new Vector2(0, 0);
2020
this._extruder = 0.0;
2121
this._duration = 0.0;
2222
this._isRetracted = false;
@@ -47,7 +47,7 @@ export default class {
4747
}
4848

4949
moveTo(x, y, z, { speed }) {
50-
const newNozzlePosition = new THREE.Vector2(x, y).multiplyScalar(PRECISION);
50+
const newNozzlePosition = new Vector2(x, y).multiplyScalar(PRECISION);
5151
const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition);
5252

5353
this._duration += lineLength / speed;
@@ -66,7 +66,7 @@ export default class {
6666
}
6767

6868
lineTo(x, y, z, { speed, flowRate }) {
69-
const newNozzlePosition = new THREE.Vector2(x, y).multiplyScalar(PRECISION);
69+
const newNozzlePosition = new Vector2(x, y).multiplyScalar(PRECISION);
7070
const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition);
7171

7272
this._extruder += this._nozzleToFilamentRatio * lineLength * flowRate;

src/sliceActions/intersectionsToShapes.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as THREE from 'three';
1+
import { Vector2 } from 'three/src/math/Vector2.js';
22
import Shape from 'clipper-js';
33

44
export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) {
@@ -48,8 +48,8 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt
4848
if (typeof intersectionPoints[index] !== 'undefined') {
4949
const faceNormal = faceNormals[Math.floor(i / 2)];
5050

51-
const a = new THREE.Vector2(intersection.x, intersection.y);
52-
const b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y);
51+
const a = new Vector2(intersection.x, intersection.y);
52+
const b = new Vector2(intersectionPoints[index].x, intersectionPoints[index].y);
5353

5454
// can't calculate normal between points if distance is smaller as 0.0001
5555
if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) {
@@ -64,7 +64,7 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt
6464
index = -1;
6565
} else {
6666
// make sure the path goes the right direction
67-
// THREE.Vector2.normal is not yet implimented
67+
// Vector2.normal is not yet implimented
6868
// const normal = a.sub(b).normal().normalize();
6969
const normal = a.sub(b);
7070
normal.set(-normal.y, normal.x).normalize();
@@ -111,7 +111,7 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt
111111
if (openShape) {
112112
lineShapesOpen.push(shape);
113113
} else {
114-
lineShapesClosed.push(shape);
114+
lineShapesClosed.push(shape);
115115
}
116116
} else {
117117
fillShapes.push(shape);

src/sliceActions/optimizePaths.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as THREE from 'three';
1+
import { Vector2 } from 'three/src/math/Vector2.js';
22
import Shape from 'clipper-js';
33

44
export default function optimizePaths(slices, settings) {
5-
const start = new THREE.Vector2(0, 0);
5+
const start = new Vector2(0, 0);
66

77
for (let layer = 0; layer < slices.length; layer ++) {
88
const slice = slices[layer];
@@ -102,7 +102,7 @@ function optimizeShape(shape, start) {
102102

103103
if (shape.closed) {
104104
for (let j = 0; j < path.length; j += 1) {
105-
const point = new THREE.Vector2().copy(path[j]);
105+
const point = new Vector2().copy(path[j]);
106106
const length = point.sub(start).length();
107107
if (minLength === false || length < minLength) {
108108
minPath = path;
@@ -112,7 +112,7 @@ function optimizeShape(shape, start) {
112112
}
113113
}
114114
} else {
115-
const startPoint = new THREE.Vector2().copy(path[0]);
115+
const startPoint = new Vector2().copy(path[0]);
116116
const lengthToStart = startPoint.sub(start).length();
117117
if (minLength === false || lengthToStart < minLength) {
118118
minPath = path;
@@ -121,7 +121,7 @@ function optimizeShape(shape, start) {
121121
pathIndex = i;
122122
}
123123

124-
const endPoint = new THREE.Vector2().copy(path[path.length - 1]);
124+
const endPoint = new Vector2().copy(path[path.length - 1]);
125125
const lengthToEnd = endPoint.sub(start).length();
126126
if (lengthToEnd < minLength) {
127127
minPath = path;

0 commit comments

Comments
 (0)