@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
147 lines (146 loc) • 3.65 kB
JavaScript
;
import {
Vector3,
Raycaster,
Object3D,
PerspectiveCamera,
OrthographicCamera,
Vector2
} from "three";
import { ObjectNamedFunction3, NamedFunction2, NamedFunction3 } from "./_Base";
import { getDefaultCamera } from "./_Camera";
const raycaster = new Raycaster();
const DEFAULT_POS = new Vector3();
const DEFAULT_INTERSECTION = {
distance: -1,
point: DEFAULT_POS,
object: new Object3D()
};
const tmpV2 = new Vector2();
export class raySet extends NamedFunction3 {
static type() {
return "raySet";
}
func(origin, direction, target) {
target.origin.copy(origin);
target.direction.copy(direction);
return target;
}
}
export class rayFromCamera extends ObjectNamedFunction3 {
static type() {
return "rayFromCamera";
}
func(object3D, x, y, target) {
if (object3D == null) {
this._getDefaultCamera = this._getDefaultCamera || new getDefaultCamera(this.node, this.shadersCollectionController);
object3D = this._getDefaultCamera.func();
}
if (!(object3D instanceof PerspectiveCamera || object3D instanceof OrthographicCamera)) {
return target;
}
tmpV2.set(x, y);
raycaster.setFromCamera(tmpV2, object3D);
target.copy(raycaster.ray);
return target;
}
}
export class getRayOrigin extends NamedFunction2 {
static type() {
return "getRayOrigin";
}
func(ray, target) {
return target.copy(ray.origin);
}
}
export class getRayDirection extends NamedFunction2 {
static type() {
return "getRayDirection";
}
func(ray, target) {
return target.copy(ray.direction);
}
}
export class rayIntersectBox3 extends NamedFunction3 {
static type() {
return "rayIntersectBox3";
}
func(ray, box, target) {
ray.intersectBox(box, target);
return target;
}
}
export class rayIntersectsBox3 extends NamedFunction2 {
static type() {
return "rayIntersectsBox3";
}
func(ray, box) {
return ray.intersectsBox(box);
}
}
function _defaultIntersection(object3D) {
DEFAULT_INTERSECTION.object = object3D;
return DEFAULT_INTERSECTION;
}
export class rayIntersectObject3D extends NamedFunction3 {
static type() {
return "rayIntersectObject3D";
}
func(ray, object3D, recursive) {
raycaster.ray.copy(ray);
const intersections = raycaster.intersectObject(object3D, recursive);
return intersections[0] || _defaultIntersection(object3D);
}
}
export class rayIntersectsObject3D extends NamedFunction3 {
static type() {
return "rayIntersectsObject3D";
}
func(ray, object3D, recursive) {
raycaster.ray.copy(ray);
const intersections = raycaster.intersectObject(object3D, recursive);
return intersections.length > 0;
}
}
export class rayIntersectPlane extends NamedFunction3 {
static type() {
return "rayIntersectPlane";
}
func(ray, plane, target) {
ray.intersectPlane(plane, target);
return target;
}
}
export class rayIntersectsPlane extends NamedFunction2 {
static type() {
return "rayIntersectsPlane";
}
func(ray, plane) {
return ray.intersectsPlane(plane);
}
}
export class rayDistanceToPlane extends NamedFunction2 {
static type() {
return "rayDistanceToPlane";
}
func(ray, plane) {
return ray.distanceToPlane(plane);
}
}
export class rayIntersectSphere extends NamedFunction3 {
static type() {
return "rayIntersectSphere";
}
func(ray, sphere, target) {
ray.intersectSphere(sphere, target);
return target;
}
}
export class rayIntersectsSphere extends NamedFunction2 {
static type() {
return "rayIntersectsSphere";
}
func(ray, sphere) {
return ray.intersectsSphere(sphere);
}
}