playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
74 lines (73 loc) • 2.22 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { Vec3 } from "../math/vec3.js";
class Ray {
/**
* Creates a new Ray instance. The ray is infinite, starting at a given origin and pointing in
* a given direction.
*
* @param {Vec3} [origin] - The starting point of the ray. The constructor copies
* this parameter. Defaults to the origin (0, 0, 0).
* @param {Vec3} [direction] - The direction of the ray. The constructor copies
* this parameter. Defaults to a direction down the world negative Z axis (0, 0, -1).
* @example
* // Create a new ray starting at the position of this entity and pointing down
* // the entity's negative Z axis
* const ray = new pc.Ray(this.entity.getPosition(), this.entity.forward);
*/
constructor(origin, direction) {
/**
* The starting point of the ray.
*
* @readonly
* @type {Vec3}
*/
__publicField(this, "origin", new Vec3());
/**
* The direction of the ray.
*
* @readonly
* @type {Vec3}
*/
__publicField(this, "direction", Vec3.FORWARD.clone());
if (origin) {
this.origin.copy(origin);
}
if (direction) {
this.direction.copy(direction);
}
}
/**
* Sets origin and direction to the supplied vector values.
*
* @param {Vec3} origin - The starting point of the ray.
* @param {Vec3} direction - The direction of the ray.
* @returns {Ray} Self for chaining.
*/
set(origin, direction) {
this.origin.copy(origin);
this.direction.copy(direction);
return this;
}
/**
* Copies the contents of a source Ray.
*
* @param {Ray} src - The Ray to copy from.
* @returns {Ray} Self for chaining.
*/
copy(src) {
return this.set(src.origin, src.direction);
}
/**
* Returns a clone of the Ray.
*
* @returns {this} A duplicate Ray.
*/
clone() {
return new this.constructor(this.origin, this.direction);
}
}
export {
Ray
};