threepipe
Version:
A 3D viewer framework built on top of three.js in TypeScript with a focus on quality rendering, modularity and extensibility.
53 lines • 1.83 kB
JavaScript
import { MathUtils, Quaternion } from 'three';
import { mergeVertices } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
/**
* Convert geometry to BufferGeometry with indexed attributes.
*/
export function toIndexedGeometry(geometry, tolerance = -1) {
return mergeVertices(geometry, tolerance);
}
export function generateUUID() {
return MathUtils.generateUUID();
}
/**
* Check if a single or multiple object/geometry/material/texture is in the scene.
* This is used internally to check if a material is used by any object in the scene, and if not, it can be disposed.
* @param sceneObj
*/
export function isInScene(...sceneObj) {
if (sceneObj.length > 1)
return sceneObj.some((a) => isInScene(a));
const o = sceneObj[0];
if (o.isTexture)
return Array.from(o._appliedMaterials || []).some((m) => isInScene(m)) ?? false;
const objects = o.isObject3D ? [o] :
o.appliedMeshes;
for (const obj of objects) {
let inScene = false;
obj.traverseAncestors((ob) => ob.isScene && (inScene = true));
if (inScene)
return true;
}
return false;
}
/**
* Convert a world-space quaternion to local-space quaternion.
* https://github.com/mrdoob/three.js/pull/20243
* @param object
* @param quaternion
* @param _q
*/
export function worldToLocalQuaternion(object, quaternion, _q = new Quaternion()) {
return quaternion.premultiply(object.getWorldQuaternion(_q).invert());
}
/**
* Convert a local-space quaternion to world-space quaternion.
* https://github.com/mrdoob/three.js/pull/20243
* @param object
* @param quaternion
* @param _q
*/
export function localToWorldQuaternion(object, quaternion, _q = new Quaternion()) {
return quaternion.premultiply(object.getWorldQuaternion(_q));
}
//# sourceMappingURL=misc.js.map