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
|
import React, {Component} from 'react';
import PropTypes from 'prop-types'
import ReactDOM from 'react-dom';
import * as THREE from 'three';
import STLLoader from './Loader'
import OrbitControlsModule from 'three-orbit-controls'
// import {ScaleLoader} from 'react-spinners';
// const STLLoader = STLLoaderModule(THREE);
const OrbitControls = OrbitControlsModule(THREE);
class STLViewer extends Component {
static propTypes = {
className: PropTypes.string,
url: PropTypes.string,
file: PropTypes.object,
width: PropTypes.number,
height: PropTypes.number,
backgroundColor: PropTypes.string,
modelMaterial: PropTypes.object,
sceneClassName: PropTypes.string,
onSceneRendered: PropTypes.func,
};
static defaultProps = {
backgroundColor: '#EAEAEA',
modelMaterial: { color: '#888888' },
height: 400,
width: 400,
rotate: true,
orbitControls: true,
sceneClassName: '',
};
componentDidMount() {
this.renderModel(this.props);
}
shouldComponentUpdate(nextProps, nextState) {
if (JSON.stringify(nextProps) === JSON.stringify(this.props)) {
return false
}
return true
}
componentDidUpdate(prevProps, nextState) {
this.renderModel(prevProps);
}
componentWillUnmount() {
cancelAnimationFrame(this.animationFrame)
}
componentDidCatch(error, info) {
console.log(error, info)
}
renderModel(props) {
let camera, scene, renderer, mesh, distance, controls;
const {url, file, width, height, modelMaterial, backgroundColor, orbitControls, sceneClassName, onSceneRendered} = props;
let xDims, yDims, zDims;
let component = this;
if (!url) return
scene = new THREE.Scene();
distance = 10000;
const directionalLight = new THREE.DirectionalLight(0xddeeff, 0.6);
directionalLight.position.x = 100;
directionalLight.position.y = 100;
directionalLight.position.z = 200;
directionalLight.position.normalize();
scene.add(directionalLight);
const directionalLight2 = new THREE.DirectionalLight(0xffffdd, 0.6);
directionalLight2.position.x = -75;
directionalLight2.position.y = 75;
directionalLight2.position.z = -200;
directionalLight2.position.normalize();
scene.add(directionalLight2);
const ambientLight = new THREE.AmbientLight(0x404040); // soft white light
scene.add(ambientLight);
const onLoad = geometry => {
geometry.computeFaceNormals();
geometry.computeVertexNormals();
geometry.center();
mesh = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial(modelMaterial)
);
geometry.computeBoundingBox();
xDims = geometry.boundingBox.max.x - geometry.boundingBox.min.x;
yDims = geometry.boundingBox.max.y - geometry.boundingBox.min.y;
zDims = geometry.boundingBox.max.z - geometry.boundingBox.min.z;
scene.add(mesh);
mesh.rotateX(this.props.transform.rotate.x)
mesh.rotateY(this.props.transform.rotate.y)
mesh.rotateZ(this.props.transform.rotate.z)
camera = new THREE.PerspectiveCamera(30, width / height, 1, distance);
camera.position.set(0, 0, Math.max(xDims * 3, yDims * 3, zDims * 3));
scene.add(camera);
renderer = new THREE.WebGLRenderer({
preserveDrawingBuffer: true,
antialias: true,
alpha: true,
});
renderer.setSize(width, height);
renderer.setClearColor(backgroundColor, 0);
renderer.domElement.className = sceneClassName;
if (orbitControls) {
controls = new OrbitControls(camera, ReactDOM.findDOMNode(component));
controls.enableKeys = false;
controls.addEventListener('change', orbitRender);
}
ReactDOM.findDOMNode(this).replaceChild(renderer.domElement,
ReactDOM.findDOMNode(this).firstChild);
render();
animate();
if (typeof onSceneRendered === "function") {
onSceneRendered(ReactDOM.findDOMNode(renderer.domElement))
}
};
const onProgress = (xhr) => {
if (xhr.lengthComputable) {
let percentComplete = xhr.loaded / xhr.total * 100;
}
};
const loader = new STLLoader();
if (file) {
loader.loadFile(file, onLoad, onProgress);
} else {
loader.load(url, onLoad, onProgress);
}
const render = () => {
renderer.render(scene, camera);
};
const orbitRender = () => {
render();
};
const animate = () => {
if (!mesh) return
this.animationFrame = requestAnimationFrame(animate)
mesh.rotateZ(Math.PI * 0.001)
render()
}
}
render() {
return (
<div
className={this.props.className}
style={{
position: 'fixed',
width: Math.round(this.props.width) + "px",
height: Math.round(this.props.height) + "px",
overflow: 'hidden',
...this.props.style,
}}
>
<div style={{
height: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}>
</div>
</div>
);
};
};
export default STLViewer;
|