Last active
December 7, 2020 03:13
-
-
Save AndrewRayCode/c9c41b549d0b1e97da8890a79e3ab8d0 to your computer and use it in GitHub Desktop.
Face centroids were removed from Three.js some time ago. These function modify faces **in place** to add a `.centroid` property, which a `Vector3` representing the center of that face. I've added an ES6 version as well for convenience if you're using ES6.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function computeFaceCentroids( geometry ) { | |
var f, fl, face; | |
for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) { | |
face = geometry.faces[ f ]; | |
face.centroid = new THREE.Vector3( 0, 0, 0 ); | |
if ( face instanceof THREE.Face3 ) { | |
face.centroid.add( geometry.vertices[ face.a ] ); | |
face.centroid.add( geometry.vertices[ face.b ] ); | |
face.centroid.add( geometry.vertices[ face.c ] ); | |
face.centroid.divideScalar( 3 ); | |
} else if ( face instanceof THREE.Face4 ) { | |
face.centroid.add( geometry.vertices[ face.a ] ); | |
face.centroid.add( geometry.vertices[ face.b ] ); | |
face.centroid.add( geometry.vertices[ face.c ] ); | |
face.centroid.add( geometry.vertices[ face.d ] ); | |
face.centroid.divideScalar( 4 ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Vector3, Face3, Face4 } from 'three'; | |
function computeFaceCentroids( geometry ) { | |
let f; | |
let fl; | |
let face; | |
for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) { | |
face = geometry.faces[ f ]; | |
face.centroid = new Vector3( 0, 0, 0 ); | |
if ( face instanceof Face3 ) { | |
face.centroid.add( geometry.vertices[ face.a ] ); | |
face.centroid.add( geometry.vertices[ face.b ] ); | |
face.centroid.add( geometry.vertices[ face.c ] ); | |
face.centroid.divideScalar( 3 ); | |
} else if ( face instanceof Face4 ) { | |
face.centroid.add( geometry.vertices[ face.a ] ); | |
face.centroid.add( geometry.vertices[ face.b ] ); | |
face.centroid.add( geometry.vertices[ face.c ] ); | |
face.centroid.add( geometry.vertices[ face.d ] ); | |
face.centroid.divideScalar( 4 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Just find your function super usefull, but it's missing the matrix to work perfectly !
I just add it as an helper function inside my project.
`export function getFacesCenterByCardinalDirection(object,cardinalDirection,matrix) {
}`