diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2021-04-05 19:15:39 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2021-04-05 19:15:39 +0200 |
| commit | bd2b25f9e63c0c0ebdb8ce63235e38876464af31 (patch) | |
| tree | 4916f52499f2cd30e183c6aaea06b7652157eecb /frontend/site/projects/museum/stl/STLViewer.js | |
| parent | 503dd94e6cd5f65aa306dfc8e50ecf46c485744d (diff) | |
update 3d assets properly, i guess
Diffstat (limited to 'frontend/site/projects/museum/stl/STLViewer.js')
| -rw-r--r-- | frontend/site/projects/museum/stl/STLViewer.js | 290 |
1 files changed, 145 insertions, 145 deletions
diff --git a/frontend/site/projects/museum/stl/STLViewer.js b/frontend/site/projects/museum/stl/STLViewer.js index 365b32d..a4bbad1 100644 --- a/frontend/site/projects/museum/stl/STLViewer.js +++ b/frontend/site/projects/museum/stl/STLViewer.js @@ -10,182 +10,182 @@ import OrbitControlsModule from 'three-orbit-controls' 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 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: '#B92C2C' }, - height: 400, - width: 400, - rotate: true, - orbitControls: true, - sceneClassName: '', - }; + static defaultProps = { + backgroundColor: '#EAEAEA', + modelMaterial: { color: '#888888' }, + height: 400, + width: 400, + rotate: true, + orbitControls: true, + sceneClassName: '', + }; - componentDidMount() { - this.renderModel(this.props); - } + componentDidMount() { + this.renderModel(this.props); + } - shouldComponentUpdate(nextProps, nextState) { - if (JSON.stringify(nextProps) === JSON.stringify(this.props)) { - return false - } - return true + shouldComponentUpdate(nextProps, nextState) { + if (JSON.stringify(nextProps) === JSON.stringify(this.props)) { + return false } + return true + } - componentDidUpdate(prevProps, nextState) { - this.renderModel(prevProps); - } + componentDidUpdate(prevProps, nextState) { + this.renderModel(prevProps); + } - componentWillUnmount() { - cancelAnimationFrame(this.animationFrame) - } + 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; + componentDidCatch(error, info) { + console.log(error, info) + } - if (!url) return + 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; - 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 = 300; - directionalLight.position.normalize(); - scene.add(directionalLight); + if (!url) return - const directionalLight2 = new THREE.DirectionalLight(0xffffdd, 0.6); - directionalLight2.position.x = -100; - directionalLight2.position.y = 100; - directionalLight2.position.z = -300; - directionalLight2.position.normalize(); - scene.add(directionalLight2); + 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 = 300; + directionalLight.position.normalize(); + scene.add(directionalLight); - const ambientLight = new THREE.AmbientLight(0x404040); // soft white light - scene.add(ambientLight); + const directionalLight2 = new THREE.DirectionalLight(0xffffdd, 0.6); + directionalLight2.position.x = -100; + directionalLight2.position.y = 100; + directionalLight2.position.z = -300; + directionalLight2.position.normalize(); + scene.add(directionalLight2); - const onLoad = geometry => { - geometry.computeFaceNormals(); - geometry.computeVertexNormals(); - geometry.center(); + const ambientLight = new THREE.AmbientLight(0x404040); // soft white light + scene.add(ambientLight); - mesh = new THREE.Mesh( - geometry, - new THREE.MeshPhongMaterial(modelMaterial) - ); + const onLoad = geometry => { + geometry.computeFaceNormals(); + geometry.computeVertexNormals(); + geometry.center(); - 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; + mesh = new THREE.Mesh( + geometry, + new THREE.MeshPhongMaterial(modelMaterial) + ); - scene.add(mesh); + 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; - mesh.rotateX(this.props.transform.rotate.x) - mesh.rotateY(this.props.transform.rotate.y) - mesh.rotateZ(this.props.transform.rotate.z) + scene.add(mesh); - camera = new THREE.PerspectiveCamera(30, width / height, 1, distance); - camera.position.set(0, 0, Math.max(xDims * 3, yDims * 3, zDims * 3)); + mesh.rotateX(this.props.transform.rotate.x) + mesh.rotateY(this.props.transform.rotate.y) + mesh.rotateZ(this.props.transform.rotate.z) - scene.add(camera); + camera = new THREE.PerspectiveCamera(30, width / height, 1, distance); + camera.position.set(0, 0, Math.max(xDims * 3, yDims * 3, zDims * 3)); - renderer = new THREE.WebGLRenderer({ - preserveDrawingBuffer: true, - antialias: true, - alpha: true, - }); - renderer.setSize(width, height); - renderer.setClearColor(backgroundColor, 0); - renderer.domElement.className = sceneClassName; + scene.add(camera); - if (orbitControls) { - controls = new OrbitControls(camera, ReactDOM.findDOMNode(component)); - controls.enableKeys = false; - controls.addEventListener('change', orbitRender); - } + renderer = new THREE.WebGLRenderer({ + preserveDrawingBuffer: true, + antialias: true, + alpha: true, + }); + renderer.setSize(width, height); + renderer.setClearColor(backgroundColor, 0); + renderer.domElement.className = sceneClassName; - ReactDOM.findDOMNode(this).replaceChild(renderer.domElement, - ReactDOM.findDOMNode(this).firstChild); + if (orbitControls) { + controls = new OrbitControls(camera, ReactDOM.findDOMNode(component)); + controls.enableKeys = false; + controls.addEventListener('change', orbitRender); + } - render(); - animate(); + ReactDOM.findDOMNode(this).replaceChild(renderer.domElement, + ReactDOM.findDOMNode(this).firstChild); - if (typeof onSceneRendered === "function") { - onSceneRendered(ReactDOM.findDOMNode(renderer.domElement)) - } - }; + render(); + animate(); - const onProgress = (xhr) => { - if (xhr.lengthComputable) { - let percentComplete = xhr.loaded / xhr.total * 100; - } - }; + if (typeof onSceneRendered === "function") { + onSceneRendered(ReactDOM.findDOMNode(renderer.domElement)) + } + }; - const loader = new STLLoader(); + 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); - } + if (file) { + loader.loadFile(file, onLoad, onProgress); + } else { + loader.load(url, onLoad, onProgress); + } - const render = () => { - renderer.render(scene, camera); - }; + const render = () => { + renderer.render(scene, camera); + }; - const orbitRender = () => { - render(); - }; + const orbitRender = () => { + render(); + }; - const animate = () => { - if (!mesh) return - this.animationFrame = requestAnimationFrame(animate) - mesh.rotateZ(Math.PI * 0.001) - 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> - ); - }; + 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;
\ No newline at end of file |
