import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
import { THREE } from "threebox-plugin";

const textureLoader = new THREE.TextureLoader();

export const loadObj = (src) =>
    new Promise((resolve) => {
        const loader = new OBJLoader();
        loader.load(src, (obj) => {
            resolve(obj);
        });
    });

export const loadTexture = (src) =>
    new Promise((resolve) => {
        textureLoader.load(src, (t) => {
            resolve(t);
        });
    });

export function getVCenter(v1, v2) {
    let v = v1.clone().add(v2.clone());
    return v.divideScalar(2);
}

export function getLenVec(v1, v2, len) {
    const _v1 = v1.clone();
    let dist = _v1.distanceTo(v2);
    return _v1.lerp(v2, len / dist);
}

export function midpoint(point1, point2) {
    const _point1 = [point1[0] + 180, point1[1] + 90]
    const _point2 = [point2[0] + 180, point2[1] + 90]

    const mid = [
        (_point1[0] + _point2[0]) / 2 - 180,
        (_point1[1] + _point2[1]) / 2 - 90,
    ]
    return mid
}
