add customizable material and fix flatShading
This commit is contained in:
parent
1d277bc81f
commit
fa0781595d
1 changed files with 47 additions and 6 deletions
|
@ -78,6 +78,22 @@ class ThreeViewer extends React.PureComponent<Props, State> {
|
||||||
group.position.setY(group.position.y + meshY * scaleFactor);
|
group.position.setY(group.position.y + meshY * scaleFactor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
See: https://github.com/mrdoob/three.js/blob/dev/docs/scenes/js/material.js#L195
|
||||||
|
*/
|
||||||
|
|
||||||
|
static updateMaterial(material, geometry) {
|
||||||
|
material.vertexColors = +material.vertexColors; // Ensure number
|
||||||
|
material.side = +material.side; // Ensure number
|
||||||
|
material.needsUpdate = true;
|
||||||
|
// If Geometry needs update
|
||||||
|
if (geometry) {
|
||||||
|
geometry.verticesNeedUpdate = true;
|
||||||
|
geometry.normalsNeedUpdate = true;
|
||||||
|
geometry.colorsNeedUpdate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
super(props);
|
super(props);
|
||||||
const { theme } = this.props;
|
const { theme } = this.props;
|
||||||
|
@ -192,8 +208,10 @@ class ThreeViewer extends React.PureComponent<Props, State> {
|
||||||
config.reset = () => {
|
config.reset = () => {
|
||||||
// Reset material color
|
// Reset material color
|
||||||
config.color = this.materialColor;
|
config.color = this.materialColor;
|
||||||
|
// Reset material
|
||||||
this.material.color.set(config.color);
|
this.material.color.set(config.color);
|
||||||
// Reset wireframe
|
this.material.flatShading = false;
|
||||||
|
this.material.shininess = 30;
|
||||||
this.material.wireframe = false;
|
this.material.wireframe = false;
|
||||||
// Reset autoRotate
|
// Reset autoRotate
|
||||||
this.controls.autoRotate = false;
|
this.controls.autoRotate = false;
|
||||||
|
@ -201,8 +219,10 @@ class ThreeViewer extends React.PureComponent<Props, State> {
|
||||||
this.restoreCamera();
|
this.restoreCamera();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const materialFolder = this.gui.addFolder('Material');
|
||||||
|
|
||||||
// Color picker
|
// Color picker
|
||||||
const colorPicker = this.gui
|
const colorPicker = materialFolder
|
||||||
.addColor(config, 'color')
|
.addColor(config, 'color')
|
||||||
.name('Color')
|
.name('Color')
|
||||||
.listen();
|
.listen();
|
||||||
|
@ -211,15 +231,33 @@ class ThreeViewer extends React.PureComponent<Props, State> {
|
||||||
this.material.color.set(color);
|
this.material.color.set(color);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.gui
|
materialFolder
|
||||||
|
.add(this.material, 'shininess', 0, 100)
|
||||||
|
.name('Shininess')
|
||||||
|
.listen();
|
||||||
|
|
||||||
|
materialFolder
|
||||||
|
.add(this.material, 'flatShading')
|
||||||
|
.name('FlatShading')
|
||||||
|
.onChange(() => {
|
||||||
|
ThreeViewer.updateMaterial(this.material);
|
||||||
|
})
|
||||||
|
.listen();
|
||||||
|
|
||||||
|
materialFolder
|
||||||
.add(this.material, 'wireframe')
|
.add(this.material, 'wireframe')
|
||||||
.name('Wireframe')
|
.name('Wireframe')
|
||||||
.listen();
|
.listen();
|
||||||
this.gui
|
|
||||||
|
const sceneFolder = this.gui.addFolder('Scene');
|
||||||
|
|
||||||
|
sceneFolder
|
||||||
.add(this.controls, 'autoRotate')
|
.add(this.controls, 'autoRotate')
|
||||||
.name('Auto-Rotate')
|
.name('Auto-Rotate')
|
||||||
.listen();
|
.listen();
|
||||||
this.gui.add(config, 'reset').name('Reset');
|
|
||||||
|
sceneFolder.add(config, 'reset').name('Reset');
|
||||||
|
|
||||||
this.guiContainer.current.appendChild(this.gui.domElement);
|
this.guiContainer.current.appendChild(this.gui.domElement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,13 +265,13 @@ class ThreeViewer extends React.PureComponent<Props, State> {
|
||||||
createGeometry(data) {
|
createGeometry(data) {
|
||||||
this.bufferGeometry = data;
|
this.bufferGeometry = data;
|
||||||
this.bufferGeometry.computeBoundingBox();
|
this.bufferGeometry.computeBoundingBox();
|
||||||
this.bufferGeometry.computeVertexNormals();
|
|
||||||
this.bufferGeometry.center();
|
this.bufferGeometry.center();
|
||||||
this.bufferGeometry.rotateX(-Math.PI / 2);
|
this.bufferGeometry.rotateX(-Math.PI / 2);
|
||||||
this.bufferGeometry.lookAt(new THREE.Vector3(0, 0, 1));
|
this.bufferGeometry.lookAt(new THREE.Vector3(0, 0, 1));
|
||||||
// Get geometry from bufferGeometry
|
// Get geometry from bufferGeometry
|
||||||
this.geometry = new THREE.Geometry().fromBufferGeometry(this.bufferGeometry);
|
this.geometry = new THREE.Geometry().fromBufferGeometry(this.bufferGeometry);
|
||||||
this.geometry.mergeVertices();
|
this.geometry.mergeVertices();
|
||||||
|
this.geometry.computeVertexNormals();
|
||||||
}
|
}
|
||||||
|
|
||||||
startLoader() {
|
startLoader() {
|
||||||
|
@ -300,6 +338,8 @@ class ThreeViewer extends React.PureComponent<Props, State> {
|
||||||
// Get geometry from child
|
// Get geometry from child
|
||||||
const geometry = new THREE.Geometry();
|
const geometry = new THREE.Geometry();
|
||||||
geometry.fromBufferGeometry(child.geometry);
|
geometry.fromBufferGeometry(child.geometry);
|
||||||
|
geometry.mergeVertices();
|
||||||
|
geometry.computeVertexNormals();
|
||||||
// Create and regroup inner objects
|
// Create and regroup inner objects
|
||||||
const innerObj = new THREE.Mesh(geometry, this.material);
|
const innerObj = new THREE.Mesh(geometry, this.material);
|
||||||
this.mesh.add(innerObj);
|
this.mesh.add(innerObj);
|
||||||
|
@ -357,6 +397,7 @@ class ThreeViewer extends React.PureComponent<Props, State> {
|
||||||
// Create model material
|
// Create model material
|
||||||
this.material = new THREE.MeshPhongMaterial({
|
this.material = new THREE.MeshPhongMaterial({
|
||||||
depthWrite: true,
|
depthWrite: true,
|
||||||
|
flatShading: false,
|
||||||
vertexColors: THREE.FaceColors,
|
vertexColors: THREE.FaceColors,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue