lingo3d
Version:
Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor
239 lines • 8.49 kB
JavaScript
import { Object3D, PropertyBinding, Vector3 } from "three";
import getWorldDirection from "../../memo/getWorldDirection";
import getWorldPosition from "../../memo/getWorldPosition";
import { quaternion, ray, vector3 } from "../utils/reusables";
import { point2Vec, vec2Point } from "../utils/vec2Point";
import { CM2M, M2CM } from "../../globals";
import { setManager } from "./utils/getManager";
import Appendable from "./Appendable";
import { deg2Rad, quadrant, rad2Deg } from "@lincode/math";
import getCenter from "../../memo/getCenter";
import worldToCanvas from "../../memo/worldToCanvas";
import { fpsRatioPtr } from "../../pointers/fpsRatioPtr";
import { physxPtr } from "../../pointers/physxPtr";
import { assignPxTransform } from "../../engine/physx/pxMath";
import { actorPtrManagerMap } from "../../collections/pxCollections";
import scene from "../../engine/scene";
import Point3d from "../../math/Point3d";
import { onMoveSystem } from "../../systems/onMoveSystem";
import { lerpToSystem } from "../../systems/lerpToSystem";
import { lookToSystem } from "../../systems/lookToSystem";
import { moveToSystem } from "../../systems/moveToSystem";
import { configMeshAppendableSystem } from "../../systems/configSystems/configMeshAppendableSystem";
import { configPhysicsSystem } from "../../systems/configLoadedSystems/configPhysicsSystem";
import { placeAtSystem } from "../../systems/configLoadedSystems/placeAtSystem";
const up = new Vector3(0, 1, 0);
export default class MeshAppendable extends Appendable {
outerObject3d;
object3d;
position;
quaternion;
userData;
constructor(outerObject3d = new Object3D()) {
super();
this.outerObject3d = outerObject3d;
setManager(outerObject3d, this);
configMeshAppendableSystem.add(this);
this.object3d = outerObject3d;
this.position = outerObject3d.position;
this.quaternion = outerObject3d.quaternion;
this.userData = outerObject3d.userData;
}
append(child) {
this.$appendNode(child);
"object3d" in child && this.object3d.add(child.outerObject3d);
}
attach(child) {
this.$appendNode(child);
"object3d" in child && this.object3d.attach(child.outerObject3d);
}
disposeNode() {
super.disposeNode();
this.outerObject3d.parent?.remove(this.outerObject3d);
this.querySphere && physxPtr[0].destroy(this.querySphere);
}
get name() {
return super.name;
}
set name(val) {
super.name = this.outerObject3d.name = PropertyBinding.sanitizeNodeName(val ?? "");
}
get x() {
return this.position.x * M2CM;
}
set x(val) {
this.position.x = val * CM2M;
configPhysicsSystem.add(this);
}
get y() {
return this.position.y * M2CM;
}
set y(val) {
this.position.y = val * CM2M;
configPhysicsSystem.add(this);
}
get z() {
return this.position.z * M2CM;
}
set z(val) {
this.position.z = val * CM2M;
configPhysicsSystem.add(this);
}
getWorldPosition() {
return vec2Point(getWorldPosition(this.object3d));
}
getCenter() {
return vec2Point(getCenter(this.object3d));
}
getWorldDirection() {
return getWorldDirection(this.object3d);
}
getProjectedPosition() {
return worldToCanvas(this.object3d);
}
_onMove;
get onMove() {
return this._onMove;
}
set onMove(cb) {
this._onMove = cb;
cb ? onMoveSystem.add(this) : onMoveSystem.delete(this);
}
translateX(val) {
this.outerObject3d.translateX(val * CM2M * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
translateY(val) {
this.outerObject3d.translateY(val * CM2M * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
translateZ(val) {
this.outerObject3d.translateZ(val * CM2M * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
rotateX(val) {
this.outerObject3d.rotateX(val * deg2Rad * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
rotateY(val) {
this.outerObject3d.rotateY(val * deg2Rad * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
rotateZ(val) {
this.outerObject3d.rotateZ(val * deg2Rad * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
setRotationFromDirection(direction) {
const ogParent = this.outerObject3d.parent;
ogParent !== scene && scene.attach(this.outerObject3d);
this.outerObject3d.setRotationFromQuaternion(quaternion.setFromUnitVectors(up, direction));
ogParent !== scene && ogParent.attach(this.outerObject3d);
}
placeAt(target) {
placeAtSystem.add(this, { target });
}
moveForward(distance) {
vector3.setFromMatrixColumn(this.outerObject3d.matrix, 0);
vector3.crossVectors(this.outerObject3d.up, vector3);
this.position.addScaledVector(vector3, distance * CM2M * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
moveRight(distance) {
vector3.setFromMatrixColumn(this.outerObject3d.matrix, 0);
this.position.addScaledVector(vector3, distance * CM2M * fpsRatioPtr[0]);
configPhysicsSystem.add(this);
}
onMoveToEnd;
lerpTo(x, y, z, alpha = 0.05) {
const from = new Vector3(this.x, this.y, this.z);
const to = new Vector3(x, y, z);
moveToSystem.delete(this);
lerpToSystem.add(this, { from, to, alpha });
}
moveTo(x, y, z, speed = 5) {
if (x === this.x)
x += 0.01;
if (z === this.z)
z += 0.01;
const { x: rx, y: ry, z: rz } = new Vector3(x - this.x, y === undefined ? 0 : y - this.y, z - this.z).normalize();
const sx = speed * rx;
const sy = speed * ry;
const sz = speed * rz;
const quad = quadrant(x, z, this.x, this.z);
lerpToSystem.delete(this);
moveToSystem.add(this, { sx, sy, sz, x, y, z, quad });
}
getRay() {
return ray.set(getWorldPosition(this.object3d), getWorldDirection(this.object3d));
}
pointAt(distance) {
return vec2Point(this.getRay().at(distance * CM2M, vector3));
}
get rotationX() {
return this.outerObject3d.rotation.x * rad2Deg;
}
set rotationX(val) {
this.outerObject3d.rotation.x = val * deg2Rad;
configPhysicsSystem.add(this);
}
get rotationY() {
return this.outerObject3d.rotation.y * rad2Deg;
}
set rotationY(val) {
this.outerObject3d.rotation.y = val * deg2Rad;
configPhysicsSystem.add(this);
}
get rotationZ() {
return this.outerObject3d.rotation.z * rad2Deg;
}
set rotationZ(val) {
this.outerObject3d.rotation.z = val * deg2Rad;
configPhysicsSystem.add(this);
}
get rotation() {
return this.rotationZ;
}
set rotation(val) {
this.rotationZ = val;
}
lookAt(a0, a1, a2) {
if (typeof a0 === "number") {
this.lookAt(new Point3d(a0, a1 === undefined ? this.y : a1, a2));
return;
}
if ("outerObject3d" in a0)
this.outerObject3d.lookAt(getWorldPosition(a0.object3d));
else
this.outerObject3d.lookAt(point2Vec(a0));
}
onLookToEnd;
lookTo(a0, a1, a2, a3) {
if (typeof a0 === "number") {
this.lookTo(new Point3d(a0, a1 === undefined ? this.y : a1, a2), a3);
return;
}
const { quaternion } = this.outerObject3d;
const quaternionOld = quaternion.clone();
this.lookAt(a0);
const quaternionNew = quaternion.clone();
quaternion.copy(quaternionOld);
lookToSystem.add(this, { quaternion, quaternionNew, a1 });
}
queryRadius;
querySphere;
queryNearby(radius) {
const { PxSphereGeometry, pxOverlap, destroy } = physxPtr[0];
if (!PxSphereGeometry)
return [];
if (radius !== this.queryRadius) {
this.queryRadius = radius;
this.querySphere && destroy(this.querySphere);
this.querySphere = new PxSphereGeometry(radius * CM2M);
}
const result = [];
for (const item of pxOverlap(this.querySphere, assignPxTransform(this)))
result.push(actorPtrManagerMap.get(item.actor.ptr));
return result;
}
}
//# sourceMappingURL=MeshAppendable.js.map