Skip to content

Instantly share code, notes, and snippets.

@EvanBacon
Last active October 18, 2017 18:31
Show Gist options
  • Save EvanBacon/5e18a49260cbdc4bde2287a823866d99 to your computer and use it in GitHub Desktop.
Save EvanBacon/5e18a49260cbdc4bde2287a823866d99 to your computer and use it in GitHub Desktop.
Three.js helper function for aligning a mesh.
const alignMesh = (mesh, axis = { x: 0.5, y: 0.5, z: 0.5 }) => {
axis = axis || {};
const box = new THREE.Box3().setFromObject(mesh);
const size = box.getSize();
const { max } = box;
const min = { x: -box.min.x, y: -box.min.y, z: -box.min.z };
Object.keys(axis).map(key => {
const scale = axis[key];
mesh.position[key] = (min[key] - size[key]) + (size[key] * scale);
});
}
export default alignMesh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment