Files
cocos4/cocos/3d/geom-utils/ray.js
T
Yun Hsiao Wu 243d645760 ES6 scene-graph (#3234)
* ES6 base-node

* ES6 node

* ES6 node activator

* ES6 component scheduler

* ES6 find

* ES6 scene

* ES6 private node

* fix linter globals
2018-09-13 01:29:59 +08:00

90 lines
2.0 KiB
JavaScript

import enums from './enums';
import { vec3 } from '../../core/vmath';
export default class ray {
constructor(ox = 0, oy = 0, oz = 0, dx = 0, dy = 0, dz = -1) {
this._type = enums.SHAPE_RAY;
this.o = vec3.create(ox, oy, oz);
this.d = vec3.create(dx, dy, dz);
}
/**
* create a new ray
*
* @param {number} ox origin X component
* @param {number} oy origin Y component
* @param {number} oz origin Z component
* @param {number} dx dir X component
* @param {number} dy dir Y component
* @param {number} dz dir Z component
* @return {ray}
*/
static create(ox, oy, oz, dx, dy, dz) {
return new ray(ox, oy, oz, dx, dy, dz);
}
/**
* Creates a new ray initialized with values from an existing ray
*
* @param {ray} a ray to clone
* @return {ray} a new ray
*/
static clone(a) {
return new ray(
a.o.x, a.o.y, a.o.z,
a.d.x, a.d.y, a.d.z
);
}
/**
* Copy the values from one ray to another
*
* @param {ray} out the receiving ray
* @param {ray} a the source ray
* @return {ray} out
*/
static copy(out, a) {
vec3.copy(out.o, a.o);
vec3.copy(out.d, a.d);
return out;
}
/**
* create a ray from two points
*
* @param {ray} out the receiving ray
* @param {vec3} origin ray origin
* @param {vec3} target target position
* @return {ray} out
*/
static fromPoints(out, origin, target) {
vec3.copy(out.o, origin);
vec3.normalize(out.d, vec3.sub(out.d, target, origin));
return out;
}
/**
* Set the components of a ray to the given values
*
* @param {ray} out the receiving ray
* @param {number} ox origin X component
* @param {number} oy origin Y component
* @param {number} oz origin Z component
* @param {number} dx dir X component
* @param {number} dy dir Y component
* @param {number} dz dir Z component
* @return {ray} out
*/
static set(out, ox, oy, oz, dx, dy, dz) {
out.o.x = ox;
out.o.y = oy;
out.o.z = oz;
out.d.x = dx;
out.d.y = dy;
out.d.z = dz;
return out;
}
}