All files / lib colliders.js

0% Statements 0/18
0% Branches 0/9
0% Functions 0/3
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
import {
    Vector3,
    Raycaster
} from 'three';
import Scene from '../core/Scene';
import { Line } from '../entities';
 
export const DEFAULT_COLLIDER_OFFSET = { x: 0, y: 0, z: 0 };
export const DEFAULT_NEAR = 0;
export const DEFAULT_FAR = 10;
 
export const getPointsFromRayCollider = (ray, position) => {
    const origin = position.clone();
    const end = origin.add(ray.ray.direction.clone().multiplyScalar(ray.far));
 
    return [origin, end];
};
 
export const createColliderHelper = (ray, position) => {
    return new Line(getPointsFromRayCollider(ray, position));
};
 
export const parseColliderFromDescription = ({ type, vector }, options, mesh, isSprite = false) => {
    const { near = DEFAULT_NEAR, far = DEFAULT_FAR, offset = DEFAULT_COLLIDER_OFFSET, debug = false } = options;
    const parsedOffset = {
        ...DEFAULT_COLLIDER_OFFSET,
        ...offset
    };
    const position = mesh.position
        .clone()
        .add(new Vector3(parsedOffset.x, parsedOffset.y, parsedOffset.z));
    const ray = new Raycaster(position, vector, near, far);
    const helper = debug && createColliderHelper(ray, position);
    
    if (isSprite) {
        ray.setFromCamera(position, Scene.getCameraBody());
    }
 
    return {
        type,
        ray,
        helper,
        offset: parsedOffset
    };
};