123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938 |
- module BABYLON {
- export class AbstractMesh extends Node implements IDisposable {
- // Statics
- private static _BILLBOARDMODE_NONE = 0;
- private static _BILLBOARDMODE_X = 1;
- private static _BILLBOARDMODE_Y = 2;
- private static _BILLBOARDMODE_Z = 4;
- private static _BILLBOARDMODE_ALL = 7;
- public static get BILLBOARDMODE_NONE(): number {
- return AbstractMesh._BILLBOARDMODE_NONE;
- }
- public static get BILLBOARDMODE_X(): number {
- return AbstractMesh._BILLBOARDMODE_X;
- }
- public static get BILLBOARDMODE_Y(): number {
- return AbstractMesh._BILLBOARDMODE_Y;
- }
- public static get BILLBOARDMODE_Z(): number {
- return AbstractMesh._BILLBOARDMODE_Z;
- }
- public static get BILLBOARDMODE_ALL(): number {
- return AbstractMesh._BILLBOARDMODE_ALL;
- }
- // Properties
- public definedFacingForward = true; // orientation for POV movement & rotation
- public position = new Vector3(0, 0, 0);
- public rotation = new Vector3(0, 0, 0);
- public rotationQuaternion: Quaternion;
- public scaling = new Vector3(1, 1, 1);
- public billboardMode = AbstractMesh.BILLBOARDMODE_NONE;
- public visibility = 1.0;
- public alphaIndex = Number.MAX_VALUE;
- public infiniteDistance = false;
- public isVisible = true;
- public isPickable = true;
- public showBoundingBox = false;
- public showSubMeshesBoundingBox = false;
- public onDispose = null;
- public checkCollisions = false;
- public isBlocker = false;
- public skeleton: Skeleton;
- public renderingGroupId = 0;
- public material: Material;
- public receiveShadows = false;
- public actionManager: ActionManager;
- public renderOutline = false;
- public outlineColor = Color3.Red();
- public outlineWidth = 0.02;
- public renderOverlay = false;
- public overlayColor = Color3.Red();
- public overlayAlpha = 0.5;
- public hasVertexAlpha = false;
- public useVertexColors = true;
- public applyFog = true;
- public useOctreeForRenderingSelection = true;
- public useOctreeForPicking = true;
- public useOctreeForCollisions = true;
- public layerMask: number = 0x0FFFFFFF;
- // Physics
- public _physicImpostor = PhysicsEngine.NoImpostor;
- public _physicsMass: number;
- public _physicsFriction: number;
- public _physicRestitution: number;
- // Collisions
- public ellipsoid = new Vector3(0.5, 1, 0.5);
- public ellipsoidOffset = new Vector3(0, 0, 0);
- private _collider = new Collider();
- private _oldPositionForCollisions = new Vector3(0, 0, 0);
- private _diffPositionForCollisions = new Vector3(0, 0, 0);
- private _newPositionForCollisions = new Vector3(0, 0, 0);
- // Cache
- private _localScaling = Matrix.Zero();
- private _localRotation = Matrix.Zero();
- private _localTranslation = Matrix.Zero();
- private _localBillboard = Matrix.Zero();
- private _localPivotScaling = Matrix.Zero();
- private _localPivotScalingRotation = Matrix.Zero();
- private _localWorld = Matrix.Zero();
- public _worldMatrix = Matrix.Zero();
- private _rotateYByPI = Matrix.RotationY(Math.PI);
- private _absolutePosition = Vector3.Zero();
- private _collisionsTransformMatrix = Matrix.Zero();
- private _collisionsScalingMatrix = Matrix.Zero();
- public _positions: Vector3[];
- private _isDirty = false;
- public _masterMesh: AbstractMesh;
- public _boundingInfo: BoundingInfo;
- private _pivotMatrix = Matrix.Identity();
- public _isDisposed = false;
- public _renderId = 0;
- public subMeshes: SubMesh[];
- public _submeshesOctree: Octree<SubMesh>;
- public _intersectionsInProgress = new Array<AbstractMesh>();
- private _onAfterWorldMatrixUpdate = new Array<(mesh: AbstractMesh) => void>();
- // Loading properties
- public _waitingActions: any;
- constructor(name: string, scene: Scene) {
- super(name, scene);
- scene.addMesh(this);
- }
- // Methods
- public get isBlocked(): boolean {
- return false;
- }
- public getLOD(camera: Camera): AbstractMesh {
- return this;
- }
- public getTotalVertices(): number {
- return 0;
- }
- public getIndices(): number[] {
- return null;
- }
- public getVerticesData(kind: string): number[] {
- return null;
- }
- public isVerticesDataPresent(kind: string): boolean {
- return false;
- }
- public getBoundingInfo(): BoundingInfo {
- if (this._masterMesh) {
- return this._masterMesh.getBoundingInfo();
- }
- if (!this._boundingInfo) {
- this._updateBoundingInfo();
- }
- return this._boundingInfo;
- }
- public get useBones(): boolean {
- return this.skeleton && this.getScene().skeletonsEnabled && this.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && this.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind);
- }
-
- public _preActivate(): void {
- }
- public _activate(renderId: number): void {
- this._renderId = renderId;
- }
- public getWorldMatrix(): Matrix {
- if (this._masterMesh) {
- return this._masterMesh.getWorldMatrix();
- }
- if (this._currentRenderId !== this.getScene().getRenderId()) {
- this.computeWorldMatrix();
- }
- return this._worldMatrix;
- }
- public get worldMatrixFromCache(): Matrix {
- return this._worldMatrix;
- }
- public get absolutePosition(): Vector3 {
- return this._absolutePosition;
- }
- public rotate(axis: Vector3, amount: number, space: Space): void {
- if (!this.rotationQuaternion) {
- this.rotationQuaternion = Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
- this.rotation = Vector3.Zero();
- }
- if (!space || space === Space.LOCAL) {
- var rotationQuaternion = Quaternion.RotationAxis(axis, amount);
- this.rotationQuaternion = this.rotationQuaternion.multiply(rotationQuaternion);
- }
- else {
- if (this.parent) {
- var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
- invertParentWorldMatrix.invert();
- axis = Vector3.TransformNormal(axis, invertParentWorldMatrix);
- }
- rotationQuaternion = Quaternion.RotationAxis(axis, amount);
- this.rotationQuaternion = rotationQuaternion.multiply(this.rotationQuaternion);
- }
- }
- public translate(axis: Vector3, distance: number, space: Space): void {
- var displacementVector = axis.scale(distance);
- if (!space || space === Space.LOCAL) {
- var tempV3 = this.getPositionExpressedInLocalSpace().add(displacementVector);
- this.setPositionWithLocalVector(tempV3);
- }
- else {
- this.setAbsolutePosition(this.getAbsolutePosition().add(displacementVector));
- }
- }
- public getAbsolutePosition(): Vector3 {
- this.computeWorldMatrix();
- return this._absolutePosition;
- }
- public setAbsolutePosition(absolutePosition: Vector3): void {
- if (!absolutePosition) {
- return;
- }
- var absolutePositionX;
- var absolutePositionY;
- var absolutePositionZ;
- if (absolutePosition.x === undefined) {
- if (arguments.length < 3) {
- return;
- }
- absolutePositionX = arguments[0];
- absolutePositionY = arguments[1];
- absolutePositionZ = arguments[2];
- }
- else {
- absolutePositionX = absolutePosition.x;
- absolutePositionY = absolutePosition.y;
- absolutePositionZ = absolutePosition.z;
- }
- if (this.parent) {
- var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
- invertParentWorldMatrix.invert();
- var worldPosition = new Vector3(absolutePositionX, absolutePositionY, absolutePositionZ);
- this.position = Vector3.TransformCoordinates(worldPosition, invertParentWorldMatrix);
- } else {
- this.position.x = absolutePositionX;
- this.position.y = absolutePositionY;
- this.position.z = absolutePositionZ;
- }
- }
- // ================================== Point of View Movement =================================
- /**
- * Perform relative position change from the point of view of behind the front of the mesh.
- * This is performed taking into account the meshes current rotation, so you do not have to care.
- * Supports definition of mesh facing forward or backward.
- * @param {number} amountRight
- * @param {number} amountUp
- * @param {number} amountForward
- */
- public movePOV(amountRight : number, amountUp : number, amountForward : number) : void {
- this.position.addInPlace(this.calcMovePOV(amountRight, amountUp, amountForward));
- }
-
- /**
- * Calculate relative position change from the point of view of behind the front of the mesh.
- * This is performed taking into account the meshes current rotation, so you do not have to care.
- * Supports definition of mesh facing forward or backward.
- * @param {number} amountRight
- * @param {number} amountUp
- * @param {number} amountForward
- */
- public calcMovePOV(amountRight : number, amountUp : number, amountForward : number) : Vector3 {
- var rotMatrix = new Matrix();
- var rotQuaternion = (this.rotationQuaternion) ? this.rotationQuaternion : Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
- rotQuaternion.toRotationMatrix(rotMatrix);
-
- var translationDelta = Vector3.Zero();
- var defForwardMult = this.definedFacingForward ? -1 : 1;
- Vector3.TransformCoordinatesFromFloatsToRef(amountRight * defForwardMult, amountUp, amountForward * defForwardMult, rotMatrix, translationDelta);
- return translationDelta;
- }
- // ================================== Point of View Rotation =================================
- /**
- * Perform relative rotation change from the point of view of behind the front of the mesh.
- * Supports definition of mesh facing forward or backward.
- * @param {number} flipBack
- * @param {number} twirlClockwise
- * @param {number} tiltRight
- */
- public rotatePOV(flipBack : number, twirlClockwise : number, tiltRight : number) : void {
- this.rotation.addInPlace(this.calcRotatePOV(flipBack, twirlClockwise, tiltRight));
- }
-
- /**
- * Calculate relative rotation change from the point of view of behind the front of the mesh.
- * Supports definition of mesh facing forward or backward.
- * @param {number} flipBack
- * @param {number} twirlClockwise
- * @param {number} tiltRight
- */
- public calcRotatePOV(flipBack : number, twirlClockwise : number, tiltRight : number) : Vector3 {
- var defForwardMult = this.definedFacingForward ? 1 : -1;
- return new Vector3(flipBack * defForwardMult, twirlClockwise, tiltRight * defForwardMult);
- }
-
- public setPivotMatrix(matrix: Matrix): void {
- this._pivotMatrix = matrix;
- this._cache.pivotMatrixUpdated = true;
- }
- public getPivotMatrix(): Matrix {
- return this._pivotMatrix;
- }
- public _isSynchronized(): boolean {
- if (this._isDirty) {
- return false;
- }
- if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
- return false;
- if (this._cache.pivotMatrixUpdated) {
- return false;
- }
- if (this.infiniteDistance) {
- return false;
- }
- if (!this._cache.position.equals(this.position))
- return false;
- if (this.rotationQuaternion) {
- if (!this._cache.rotationQuaternion.equals(this.rotationQuaternion))
- return false;
- } else {
- if (!this._cache.rotation.equals(this.rotation))
- return false;
- }
- if (!this._cache.scaling.equals(this.scaling))
- return false;
- return true;
- }
- public _initCache() {
- super._initCache();
- this._cache.localMatrixUpdated = false;
- this._cache.position = Vector3.Zero();
- this._cache.scaling = Vector3.Zero();
- this._cache.rotation = Vector3.Zero();
- this._cache.rotationQuaternion = new Quaternion(0, 0, 0, 0);
- }
- public markAsDirty(property: string): void {
- if (property === "rotation") {
- this.rotationQuaternion = null;
- }
- this._currentRenderId = Number.MAX_VALUE;
- this._isDirty = true;
- }
- public _updateBoundingInfo(): void {
- this._boundingInfo = this._boundingInfo || new BoundingInfo(this.absolutePosition, this.absolutePosition);
- this._boundingInfo._update(this.worldMatrixFromCache);
- this._updateSubMeshesBoundingInfo(this.worldMatrixFromCache);
- }
- public _updateSubMeshesBoundingInfo(matrix: Matrix): void {
- if (!this.subMeshes) {
- return;
- }
- for (var subIndex = 0; subIndex < this.subMeshes.length; subIndex++) {
- var subMesh = this.subMeshes[subIndex];
- subMesh.updateBoundingInfo(matrix);
- }
- }
- public computeWorldMatrix(force?: boolean): Matrix {
- if (!force && (this._currentRenderId === this.getScene().getRenderId() || this.isSynchronized(true))) {
- return this._worldMatrix;
- }
- this._cache.position.copyFrom(this.position);
- this._cache.scaling.copyFrom(this.scaling);
- this._cache.pivotMatrixUpdated = false;
- this._currentRenderId = this.getScene().getRenderId();
- this._isDirty = false;
- // Scaling
- Matrix.ScalingToRef(this.scaling.x, this.scaling.y, this.scaling.z, this._localScaling);
- // Rotation
- if (this.rotationQuaternion) {
- this.rotationQuaternion.toRotationMatrix(this._localRotation);
- this._cache.rotationQuaternion.copyFrom(this.rotationQuaternion);
- } else {
- Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._localRotation);
- this._cache.rotation.copyFrom(this.rotation);
- }
- // Translation
- if (this.infiniteDistance && !this.parent) {
- var camera = this.getScene().activeCamera;
- var cameraWorldMatrix = camera.getWorldMatrix();
- var cameraGlobalPosition = new Vector3(cameraWorldMatrix.m[12], cameraWorldMatrix.m[13], cameraWorldMatrix.m[14]);
- Matrix.TranslationToRef(this.position.x + cameraGlobalPosition.x, this.position.y + cameraGlobalPosition.y,
- this.position.z + cameraGlobalPosition.z, this._localTranslation);
- } else {
- Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._localTranslation);
- }
- // Composing transformations
- this._pivotMatrix.multiplyToRef(this._localScaling, this._localPivotScaling);
- this._localPivotScaling.multiplyToRef(this._localRotation, this._localPivotScalingRotation);
- // Billboarding
- if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE && this.getScene().activeCamera) {
- var localPosition = this.position.clone();
- var zero = this.getScene().activeCamera.position.clone();
- if (this.parent && (<any>this.parent).position) {
- localPosition.addInPlace((<any>this.parent).position);
- Matrix.TranslationToRef(localPosition.x, localPosition.y, localPosition.z, this._localTranslation);
- }
- if ((this.billboardMode & AbstractMesh.BILLBOARDMODE_ALL) === AbstractMesh.BILLBOARDMODE_ALL) {
- zero = this.getScene().activeCamera.position;
- } else {
- if (this.billboardMode & AbstractMesh.BILLBOARDMODE_X)
- zero.x = localPosition.x + Engine.Epsilon;
- if (this.billboardMode & AbstractMesh.BILLBOARDMODE_Y)
- zero.y = localPosition.y + 0.001;
- if (this.billboardMode & AbstractMesh.BILLBOARDMODE_Z)
- zero.z = localPosition.z + 0.001;
- }
- Matrix.LookAtLHToRef(localPosition, zero, Vector3.Up(), this._localBillboard);
- this._localBillboard.m[12] = this._localBillboard.m[13] = this._localBillboard.m[14] = 0;
- this._localBillboard.invert();
- this._localPivotScalingRotation.multiplyToRef(this._localBillboard, this._localWorld);
- this._rotateYByPI.multiplyToRef(this._localWorld, this._localPivotScalingRotation);
- }
- // Local world
- this._localPivotScalingRotation.multiplyToRef(this._localTranslation, this._localWorld);
- // Parent
- if (this.parent && this.parent.getWorldMatrix && this.billboardMode === AbstractMesh.BILLBOARDMODE_NONE) {
- this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);
- } else {
- this._worldMatrix.copyFrom(this._localWorld);
- }
- // Bounding info
- this._updateBoundingInfo();
- // Absolute position
- this._absolutePosition.copyFromFloats(this._worldMatrix.m[12], this._worldMatrix.m[13], this._worldMatrix.m[14]);
- // Callbacks
- for (var callbackIndex = 0; callbackIndex < this._onAfterWorldMatrixUpdate.length; callbackIndex++) {
- this._onAfterWorldMatrixUpdate[callbackIndex](this);
- }
- return this._worldMatrix;
- }
- /**
- * If you'd like to be callbacked after the mesh position, rotation or scaling has been updated
- * @param func: callback function to add
- */
- public registerAfterWorldMatrixUpdate(func: (mesh: AbstractMesh) => void): void {
- this._onAfterWorldMatrixUpdate.push(func);
- }
- public unregisterAfterWorldMatrixUpdate(func: (mesh: AbstractMesh) => void): void {
- var index = this._onAfterWorldMatrixUpdate.indexOf(func);
- if (index > -1) {
- this._onAfterWorldMatrixUpdate.splice(index, 1);
- }
- }
- public setPositionWithLocalVector(vector3: Vector3): void {
- this.computeWorldMatrix();
- this.position = Vector3.TransformNormal(vector3, this._localWorld);
- }
- public getPositionExpressedInLocalSpace(): Vector3 {
- this.computeWorldMatrix();
- var invLocalWorldMatrix = this._localWorld.clone();
- invLocalWorldMatrix.invert();
- return Vector3.TransformNormal(this.position, invLocalWorldMatrix);
- }
- public locallyTranslate(vector3: Vector3): void {
- this.computeWorldMatrix();
- this.position = Vector3.TransformCoordinates(vector3, this._localWorld);
- }
- public lookAt(targetPoint: Vector3, yawCor: number, pitchCor: number, rollCor: number): void {
- /// <summary>Orients a mesh towards a target point. Mesh must be drawn facing user.</summary>
- /// <param name="targetPoint" type="Vector3">The position (must be in same space as current mesh) to look at</param>
- /// <param name="yawCor" type="Number">optional yaw (y-axis) correction in radians</param>
- /// <param name="pitchCor" type="Number">optional pitch (x-axis) correction in radians</param>
- /// <param name="rollCor" type="Number">optional roll (z-axis) correction in radians</param>
- /// <returns>Mesh oriented towards targetMesh</returns>
- yawCor = yawCor || 0; // default to zero if undefined
- pitchCor = pitchCor || 0;
- rollCor = rollCor || 0;
- var dv = targetPoint.subtract(this.position);
- var yaw = -Math.atan2(dv.z, dv.x) - Math.PI / 2;
- var len = Math.sqrt(dv.x * dv.x + dv.z * dv.z);
- var pitch = Math.atan2(dv.y, len);
- this.rotationQuaternion = Quaternion.RotationYawPitchRoll(yaw + yawCor, pitch + pitchCor, rollCor);
- }
- public isInFrustum(frustumPlanes: Plane[]): boolean {
- if (!this._boundingInfo.isInFrustum(frustumPlanes)) {
- return false;
- }
- return true;
- }
- public isCompletelyInFrustum(camera?: Camera): boolean {
- if (!camera) {
- camera = this.getScene().activeCamera;
- }
- var transformMatrix = camera.getViewMatrix().multiply(camera.getProjectionMatrix());
- if (!this._boundingInfo.isCompletelyInFrustum(Frustum.GetPlanes(transformMatrix))) {
- return false;
- }
- return true;
- }
- public intersectsMesh(mesh: AbstractMesh, precise?: boolean): boolean {
- if (!this._boundingInfo || !mesh._boundingInfo) {
- return false;
- }
- return this._boundingInfo.intersects(mesh._boundingInfo, precise);
- }
- public intersectsPoint(point: Vector3): boolean {
- if (!this._boundingInfo) {
- return false;
- }
- return this._boundingInfo.intersectsPoint(point);
- }
- // Physics
- public setPhysicsState(impostor?: any, options?: PhysicsBodyCreationOptions): any {
- var physicsEngine = this.getScene().getPhysicsEngine();
- if (!physicsEngine) {
- return;
- }
- impostor = impostor || PhysicsEngine.NoImpostor;
- if (impostor.impostor) {
- // Old API
- options = impostor;
- impostor = impostor.impostor;
- }
- if (impostor === PhysicsEngine.NoImpostor) {
- physicsEngine._unregisterMesh(this);
- return;
- }
- if (!options) {
- options = { mass: 0, friction: 0.2, restitution: 0.2 };
- } else {
- if (!options.mass && options.mass !== 0) options.mass = 0;
- if (!options.friction && options.friction !== 0) options.friction = 0.2;
- if (!options.restitution && options.restitution !== 0) options.restitution = 0.2;
- }
- this._physicImpostor = impostor;
- this._physicsMass = options.mass;
- this._physicsFriction = options.friction;
- this._physicRestitution = options.restitution;
- return physicsEngine._registerMesh(this, impostor, options);
- }
- public getPhysicsImpostor(): number {
- if (!this._physicImpostor) {
- return PhysicsEngine.NoImpostor;
- }
- return this._physicImpostor;
- }
- public getPhysicsMass(): number {
- if (!this._physicsMass) {
- return 0;
- }
- return this._physicsMass;
- }
- public getPhysicsFriction(): number {
- if (!this._physicsFriction) {
- return 0;
- }
- return this._physicsFriction;
- }
- public getPhysicsRestitution(): number {
- if (!this._physicRestitution) {
- return 0;
- }
- return this._physicRestitution;
- }
- public getPositionInCameraSpace(camera?: Camera): Vector3 {
- if (!camera) {
- camera = this.getScene().activeCamera;
- }
- return Vector3.TransformCoordinates(this.absolutePosition, camera.getViewMatrix());
- }
- public getDistanceToCamera(camera?: Camera): number {
- if (!camera) {
- camera = this.getScene().activeCamera;
- }
- return this.absolutePosition.subtract(camera.position).length();
- }
- public applyImpulse(force: Vector3, contactPoint: Vector3): void {
- if (!this._physicImpostor) {
- return;
- }
- this.getScene().getPhysicsEngine()._applyImpulse(this, force, contactPoint);
- }
- public setPhysicsLinkWith(otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3, options?: any): void {
- if (!this._physicImpostor) {
- return;
- }
- this.getScene().getPhysicsEngine()._createLink(this, otherMesh, pivot1, pivot2, options);
- }
- public updatePhysicsBodyPosition(): void {
- if (!this._physicImpostor) {
- return;
- }
- this.getScene().getPhysicsEngine()._updateBodyPosition(this);
- }
- // Collisions
- public moveWithCollisions(velocity: Vector3): void {
- var globalPosition = this.getAbsolutePosition();
- globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPositionForCollisions);
- this._oldPositionForCollisions.addInPlace(this.ellipsoidOffset);
- this._collider.radius = this.ellipsoid;
- this.getScene()._getNewPosition(this._oldPositionForCollisions, velocity, this._collider, 3, this._newPositionForCollisions, this);
- this._newPositionForCollisions.subtractToRef(this._oldPositionForCollisions, this._diffPositionForCollisions);
- if (this._diffPositionForCollisions.length() > Engine.CollisionsEpsilon) {
- this.position.addInPlace(this._diffPositionForCollisions);
- }
- }
- // Submeshes octree
- /**
- * This function will create an octree to help select the right submeshes for rendering, picking and collisions
- * Please note that you must have a decent number of submeshes to get performance improvements when using octree
- */
- public createOrUpdateSubmeshesOctree(maxCapacity = 64, maxDepth = 2): Octree<SubMesh> {
- if (!this._submeshesOctree) {
- this._submeshesOctree = new Octree<SubMesh>(Octree.CreationFuncForSubMeshes, maxCapacity, maxDepth);
- }
- this.computeWorldMatrix(true);
- // Update octree
- var bbox = this.getBoundingInfo().boundingBox;
- this._submeshesOctree.update(bbox.minimumWorld, bbox.maximumWorld, this.subMeshes);
- return this._submeshesOctree;
- }
- // Collisions
- public _collideForSubMesh(subMesh: SubMesh, transformMatrix: Matrix, collider: Collider): void {
- this._generatePointsArray();
- // Transformation
- if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
- subMesh._lastColliderTransformMatrix = transformMatrix.clone();
- subMesh._lastColliderWorldVertices = [];
- subMesh._trianglePlanes = [];
- var start = subMesh.verticesStart;
- var end = (subMesh.verticesStart + subMesh.verticesCount);
- for (var i = start; i < end; i++) {
- subMesh._lastColliderWorldVertices.push(Vector3.TransformCoordinates(this._positions[i], transformMatrix));
- }
- }
- // Collide
- collider._collide(subMesh, subMesh._lastColliderWorldVertices, this.getIndices(), subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart);
- }
- public _processCollisionsForSubMeshes(collider: Collider, transformMatrix: Matrix): void {
- var subMeshes: SubMesh[];
- var len: number;
- // Octrees
- if (this._submeshesOctree && this.useOctreeForCollisions) {
- var radius = collider.velocityWorldLength + Math.max(collider.radius.x, collider.radius.y, collider.radius.z);
- var intersections = this._submeshesOctree.intersects(collider.basePointWorld, radius);
- len = intersections.length;
- subMeshes = intersections.data;
- } else {
- subMeshes = this.subMeshes;
- len = subMeshes.length;
- }
- for (var index = 0; index < len; index++) {
- var subMesh = subMeshes[index];
- // Bounding test
- if (len > 1 && !subMesh._checkCollision(collider))
- continue;
- this._collideForSubMesh(subMesh, transformMatrix, collider);
- }
- }
- public _checkCollision(collider: Collider): void {
- // Bounding box test
- if (!this._boundingInfo._checkCollision(collider))
- return;
- // Transformation matrix
- Matrix.ScalingToRef(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z, this._collisionsScalingMatrix);
- this.worldMatrixFromCache.multiplyToRef(this._collisionsScalingMatrix, this._collisionsTransformMatrix);
- this._processCollisionsForSubMeshes(collider, this._collisionsTransformMatrix);
- }
- // Picking
- public _generatePointsArray(): boolean {
- return false;
- }
- public intersects(ray: Ray, fastCheck?: boolean): PickingInfo {
- var pickingInfo = new PickingInfo();
- if (!this.subMeshes || !this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere) || !ray.intersectsBox(this._boundingInfo.boundingBox)) {
- return pickingInfo;
- }
- if (!this._generatePointsArray()) {
- return pickingInfo;
- }
- var intersectInfo: IntersectionInfo = null;
- // Octrees
- var subMeshes: SubMesh[];
- var len: number;
- if (this._submeshesOctree && this.useOctreeForPicking) {
- var worldRay = Ray.Transform(ray, this.getWorldMatrix());
- var intersections = this._submeshesOctree.intersectsRay(worldRay);
- len = intersections.length;
- subMeshes = intersections.data;
- } else {
- subMeshes = this.subMeshes;
- len = subMeshes.length;
- }
- for (var index = 0; index < len; index++) {
- var subMesh = subMeshes[index];
- // Bounding test
- if (len > 1 && !subMesh.canIntersects(ray))
- continue;
- var currentIntersectInfo = subMesh.intersects(ray, this._positions, this.getIndices(), fastCheck);
- if (currentIntersectInfo) {
- if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
- intersectInfo = currentIntersectInfo;
- intersectInfo.subMeshId = index;
- if (fastCheck) {
- break;
- }
- }
- }
- }
- if (intersectInfo) {
- // Get picked point
- var world = this.getWorldMatrix();
- var worldOrigin = Vector3.TransformCoordinates(ray.origin, world);
- var direction = ray.direction.clone();
- direction = direction.scale(intersectInfo.distance);
- var worldDirection = Vector3.TransformNormal(direction, world);
- var pickedPoint = worldOrigin.add(worldDirection);
- // Return result
- pickingInfo.hit = true;
- pickingInfo.distance = Vector3.Distance(worldOrigin, pickedPoint);
- pickingInfo.pickedPoint = pickedPoint;
- pickingInfo.pickedMesh = this;
- pickingInfo.bu = intersectInfo.bu;
- pickingInfo.bv = intersectInfo.bv;
- pickingInfo.faceId = intersectInfo.faceId;
- pickingInfo.subMeshId = intersectInfo.subMeshId;
- return pickingInfo;
- }
- return pickingInfo;
- }
- public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): AbstractMesh {
- return null;
- }
- public releaseSubMeshes(): void {
- if (this.subMeshes) {
- while (this.subMeshes.length) {
- this.subMeshes[0].dispose();
- }
- } else {
- this.subMeshes = new Array<SubMesh>();
- }
- }
- public dispose(doNotRecurse?: boolean): void {
- var index: number;
- // Physics
- if (this.getPhysicsImpostor() !== PhysicsEngine.NoImpostor) {
- this.setPhysicsState(PhysicsEngine.NoImpostor);
- }
- // Intersections in progress
- for (index = 0; index < this._intersectionsInProgress.length; index++) {
- var other = this._intersectionsInProgress[index];
- var pos = other._intersectionsInProgress.indexOf(this);
- other._intersectionsInProgress.splice(pos, 1);
- }
- this._intersectionsInProgress = [];
- // SubMeshes
- this.releaseSubMeshes();
- // Remove from scene
- this.getScene().removeMesh(this);
- if (!doNotRecurse) {
- // Particles
- for (index = 0; index < this.getScene().particleSystems.length; index++) {
- if (this.getScene().particleSystems[index].emitter === this) {
- this.getScene().particleSystems[index].dispose();
- index--;
- }
- }
- // Children
- var objects = this.getScene().meshes.slice(0);
- for (index = 0; index < objects.length; index++) {
- if (objects[index].parent === this) {
- objects[index].dispose();
- }
- }
- } else {
- for (index = 0; index < this.getScene().meshes.length; index++) {
- var obj = this.getScene().meshes[index];
- if (obj.parent === this) {
- obj.parent = null;
- obj.computeWorldMatrix(true);
- }
- }
- }
- this._onAfterWorldMatrixUpdate = [];
- this._isDisposed = true;
- // Callback
- if (this.onDispose) {
- this.onDispose();
- }
- }
- }
- }
|