This file contains 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
export type Work = (...args: any[]) => any; | |
// Pass a function that provides a callback as single argument and returns another function F that calls that | |
// callback at some point. The result of makeContinuable will be a function that returns a promise that will | |
// resolve to the return value of F, once the callback is called. | |
export function makeContinuable<F extends Work>(wrapperFn: (cb: () => void) => F) { | |
return async (...args: Parameters<F>) => { | |
const outerPromise = new Promise<ReturnType<F>>(async (resolve1) => { | |
let res = null as ReturnType<F>; | |
// create another promise in whose executioner we'll call the passed function |
This file contains 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
// Adapted from https://github.com/donmccurdy/three.js/blob/feat/extract-root-motion/examples/jsm/utils/SkeletonUtils.js#L606 | |
import {AnimationClip, Bone, BufferAttribute, Object3D, Quaternion, Vector3, Vector4} from "three"; | |
function getRootBone(rootNode: Object3D) { | |
const rootBones: Bone[] = []; | |
rootNode.traverse(function (object) { | |
// @ts-ignore |
This file contains 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
class WeakCollection<T extends object> { | |
#registry = new FinalizationRegistry((heldValue: WeakRef<T>) => { | |
this.delete(heldValue); | |
}); | |
#refs: WeakRef<T>[] = []; | |
#values: T[] = []; | |
#clean() { | |
let index = 0; |
This file contains 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
/** | |
* A class to store key value pairs while keeping | |
* both available in contiguous arrays. Basically | |
* always prefer a Map over this, unless you regularly | |
* need to create arrays from the values or keys | |
* (i.e. inside an update loop). Is strict about | |
* duplicate or non-existing keys. | |
* | |
* - Does not keep insertion order! | |
* - Throws error when using .set() with existing keys (use .mutate() instead) |
This file contains 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
const getPointerPosition = (() => { | |
const testEvent = new PointerEvent("pointermove"); | |
// check for the existence of getCoalescedEvents and getPredictedEvents (Safari currently has both in tech preview) | |
const _pos = {x: 0, y: 0}; | |
if (testEvent.getCoalescedEvents && testEvent.getCoalescedEvents) { | |
return (evt: PointerEvent, pos: {x: number, y: number} = _pos) => { | |
evt.stopPropagation(); | |
// using the first prediction (the latest seems too chaotic when the cursor comes to a halt) | |
const latestPrediction = evt.getPredictedEvents().shift(); |