Skip to content

Instantly share code, notes, and snippets.

@SuperstrongBE
Created November 29, 2020 19:12
Show Gist options
  • Save SuperstrongBE/0ab34d9ef6d8c93c4987a9c7e2275d60 to your computer and use it in GitHub Desktop.
Save SuperstrongBE/0ab34d9ef6d8c93c4987a9c7e2275d60 to your computer and use it in GitHub Desktop.
A threejs helper for faces, planar, center for faces selection, ...
import {Vector3} from 'three';
/**
*
* @param refFace {Face3D}
* @param faces {Array}
* @returns {Array}
*/
export function findPlanarFaces(refFace, faces) {
return faces.filter((face)=> {
return refFace.normal.equals(face.normal);
})
}
export function getFacesByNormalDirection(faces, normalComponent) {
if (normalComponent == NormalComponent.positive.X) {
return faces.filter((face)=> {
return face.normal.x >0
})
}
if (normalComponent == NormalComponent.negative.X) {
return faces.filter((face)=> {
return face.normal.x <0
})
}
if (normalComponent == NormalComponent.positive.Y) {
return faces.filter((face)=> {
return face.normal.y >0
})
}
if (normalComponent == NormalComponent.negative.Y) {
return faces.filter((face)=> {
return face.normal.y <0
})
}
if (normalComponent == NormalComponent.positive.Z) {
return faces.filter((face)=> {
return face.normal.z >0
})
}
if (normalComponent == NormalComponent.negative.Z) {
return faces.filter((face)=> {
return face.normal.z <0
})
}
}
export function getFacesByCardinalDirection(faces,cardinalDirection) {
if(cardinalDirection == CARDINAL_DIRECTION.NORTH) return getFacesByNormalDirection(faces,NormalComponent.negative.Z);
if(cardinalDirection == CARDINAL_DIRECTION.SOUTH) return getFacesByNormalDirection(faces,NormalComponent.positive.Z);
if(cardinalDirection == CARDINAL_DIRECTION.EAST) return getFacesByNormalDirection(faces,NormalComponent.positive.X);
if(cardinalDirection == CARDINAL_DIRECTION.WEST) return getFacesByNormalDirection(faces,NormalComponent.negative.X);
}
export function getFacesCenterByCardinalDirection(object,cardinalDirection,matrix) {
object.updateWorldMatrix();
const center = new Vector3();
const faces = getFacesByCardinalDirection(object.geometry.faces,cardinalDirection);
faces.map((face,index)=>{
let centroid = new Vector3();
centroid.copy(object.geometry.vertices[face.a]);
centroid.add(object.geometry.vertices[face.b]);
centroid.add(object.geometry.vertices[face.c]);
centroid.divideScalar(3);
centroid.applyMatrix4(object.matrixWorld);
center.add(centroid);
});
return center.divideScalar(faces.length);
}
export const NormalComponent = {
positive: {
X: 0,
Y: 1,
Z: 2
},negative: {
X: 3,
Y: 4,
Z: 5
}
}
export const CARDINAL_DIRECTION = {
NORTH:0,
WEST:1,
SOUTH:2,
EAST:3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment