transformNode.ts 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. import { DeepImmutable } from "../types";
  2. import { serialize, serializeAsVector3, serializeAsQuaternion, SerializationHelper } from "../Misc/decorators";
  3. import { Observable } from "../Misc/observable";
  4. import { Nullable } from "../types";
  5. import { Camera } from "../Cameras/camera";
  6. import { Scene } from "../scene";
  7. import { Quaternion, Matrix, Vector3, Tmp, Space } from "../Maths/math";
  8. import { Node } from "../node";
  9. import { Bone } from "../Bones/bone";
  10. /**
  11. * A TransformNode is an object that is not rendered but can be used as a center of transformation. This can decrease memory usage and increase rendering speed compared to using an empty mesh as a parent and is less complicated than using a pivot matrix.
  12. * @see https://doc.babylonjs.com/how_to/transformnode
  13. */
  14. export class TransformNode extends Node {
  15. // Statics
  16. /**
  17. * Object will not rotate to face the camera
  18. */
  19. public static BILLBOARDMODE_NONE = 0;
  20. /**
  21. * Object will rotate to face the camera but only on the x axis
  22. */
  23. public static BILLBOARDMODE_X = 1;
  24. /**
  25. * Object will rotate to face the camera but only on the y axis
  26. */
  27. public static BILLBOARDMODE_Y = 2;
  28. /**
  29. * Object will rotate to face the camera but only on the z axis
  30. */
  31. public static BILLBOARDMODE_Z = 4;
  32. /**
  33. * Object will rotate to face the camera
  34. */
  35. public static BILLBOARDMODE_ALL = 7;
  36. private _forward = new Vector3(0, 0, 1);
  37. private _forwardInverted = new Vector3(0, 0, -1);
  38. private _up = new Vector3(0, 1, 0);
  39. private _right = new Vector3(1, 0, 0);
  40. private _rightInverted = new Vector3(-1, 0, 0);
  41. // Properties
  42. @serializeAsVector3("position")
  43. private _position = Vector3.Zero();
  44. @serializeAsVector3("rotation")
  45. private _rotation = Vector3.Zero();
  46. @serializeAsQuaternion("rotationQuaternion")
  47. private _rotationQuaternion: Nullable<Quaternion>;
  48. @serializeAsVector3("scaling")
  49. protected _scaling = Vector3.One();
  50. protected _isDirty = false;
  51. private _transformToBoneReferal: Nullable<TransformNode>;
  52. /**
  53. * Set the billboard mode. Default is 0.
  54. *
  55. * | Value | Type | Description |
  56. * | --- | --- | --- |
  57. * | 0 | BILLBOARDMODE_NONE | |
  58. * | 1 | BILLBOARDMODE_X | |
  59. * | 2 | BILLBOARDMODE_Y | |
  60. * | 4 | BILLBOARDMODE_Z | |
  61. * | 7 | BILLBOARDMODE_ALL | |
  62. *
  63. */
  64. @serialize()
  65. public billboardMode = TransformNode.BILLBOARDMODE_NONE;
  66. /**
  67. * Gets or sets a boolean indicating that parent rotation should be preserved when using billboards.
  68. * This could be useful for glTF objects where parent rotation helps converting from right handed to left handed
  69. */
  70. public preserveParentRotationForBillboard = false;
  71. /**
  72. * Multiplication factor on scale x/y/z when computing the world matrix. Eg. for a 1x1x1 cube setting this to 2 will make it a 2x2x2 cube
  73. */
  74. @serialize()
  75. public scalingDeterminant = 1;
  76. /**
  77. * Sets the distance of the object to max, often used by skybox
  78. */
  79. @serialize()
  80. public infiniteDistance = false;
  81. /**
  82. * Gets or sets a boolean indicating that non uniform scaling (when at least one component is different from others) should be ignored.
  83. * By default the system will update normals to compensate
  84. */
  85. @serialize()
  86. public ignoreNonUniformScaling = false;
  87. // Cache
  88. /** @hidden */
  89. public _poseMatrix: Matrix;
  90. /** @hidden */
  91. public _localMatrix = Matrix.Zero();
  92. private _absolutePosition = Vector3.Zero();
  93. private _pivotMatrix = Matrix.Identity();
  94. private _pivotMatrixInverse: Matrix;
  95. protected _postMultiplyPivotMatrix = false;
  96. private _tempMatrix = Matrix.Identity();
  97. private _tempMatrix2 = Matrix.Identity();
  98. protected _isWorldMatrixFrozen = false;
  99. /** @hidden */
  100. public _indexInSceneTransformNodesArray = -1;
  101. /**
  102. * An event triggered after the world matrix is updated
  103. */
  104. public onAfterWorldMatrixUpdateObservable = new Observable<TransformNode>();
  105. constructor(name: string, scene: Nullable<Scene> = null, isPure = true) {
  106. super(name, scene);
  107. if (isPure) {
  108. this.getScene().addTransformNode(this);
  109. }
  110. }
  111. /**
  112. * Gets a string identifying the name of the class
  113. * @returns "TransformNode" string
  114. */
  115. public getClassName(): string {
  116. return "TransformNode";
  117. }
  118. /**
  119. * Gets or set the node position (default is (0.0, 0.0, 0.0))
  120. */
  121. public get position(): Vector3 {
  122. return this._position;
  123. }
  124. public set position(newPosition: Vector3) {
  125. this._position = newPosition;
  126. this._isDirty = true;
  127. }
  128. /**
  129. * Gets or sets the rotation property : a Vector3 defining the rotation value in radians around each local axis X, Y, Z (default is (0.0, 0.0, 0.0)).
  130. * If rotation quaternion is set, this Vector3 will be ignored and copy from the quaternion
  131. */
  132. public get rotation(): Vector3 {
  133. return this._rotation;
  134. }
  135. public set rotation(newRotation: Vector3) {
  136. this._rotation = newRotation;
  137. this._isDirty = true;
  138. }
  139. /**
  140. * Gets or sets the scaling property : a Vector3 defining the node scaling along each local axis X, Y, Z (default is (0.0, 0.0, 0.0)).
  141. */
  142. public get scaling(): Vector3 {
  143. return this._scaling;
  144. }
  145. public set scaling(newScaling: Vector3) {
  146. this._scaling = newScaling;
  147. this._isDirty = true;
  148. }
  149. /**
  150. * Gets or sets the rotation Quaternion property : this a Quaternion object defining the node rotation by using a unit quaternion (undefined by default, but can be null).
  151. * If set, only the rotationQuaternion is then used to compute the node rotation (ie. node.rotation will be ignored)
  152. */
  153. public get rotationQuaternion(): Nullable<Quaternion> {
  154. return this._rotationQuaternion;
  155. }
  156. public set rotationQuaternion(quaternion: Nullable<Quaternion>) {
  157. this._rotationQuaternion = quaternion;
  158. //reset the rotation vector.
  159. if (quaternion) {
  160. this.rotation.setAll(0.0);
  161. }
  162. }
  163. /**
  164. * The forward direction of that transform in world space.
  165. */
  166. public get forward(): Vector3 {
  167. return Vector3.Normalize(Vector3.TransformNormal(
  168. this.getScene().useRightHandedSystem ? this._forwardInverted : this._forward,
  169. this.getWorldMatrix()
  170. ));
  171. }
  172. /**
  173. * The up direction of that transform in world space.
  174. */
  175. public get up(): Vector3 {
  176. return Vector3.Normalize(Vector3.TransformNormal(
  177. this._up,
  178. this.getWorldMatrix()
  179. ));
  180. }
  181. /**
  182. * The right direction of that transform in world space.
  183. */
  184. public get right(): Vector3 {
  185. return Vector3.Normalize(Vector3.TransformNormal(
  186. this.getScene().useRightHandedSystem ? this._rightInverted : this._right,
  187. this.getWorldMatrix()
  188. ));
  189. }
  190. /**
  191. * Copies the parameter passed Matrix into the mesh Pose matrix.
  192. * @param matrix the matrix to copy the pose from
  193. * @returns this TransformNode.
  194. */
  195. public updatePoseMatrix(matrix: Matrix): TransformNode {
  196. this._poseMatrix.copyFrom(matrix);
  197. return this;
  198. }
  199. /**
  200. * Returns the mesh Pose matrix.
  201. * @returns the pose matrix
  202. */
  203. public getPoseMatrix(): Matrix {
  204. return this._poseMatrix;
  205. }
  206. /** @hidden */
  207. public _isSynchronized(): boolean {
  208. if (this._isDirty) {
  209. return false;
  210. }
  211. if (this.billboardMode !== this._cache.billboardMode || this.billboardMode !== TransformNode.BILLBOARDMODE_NONE) {
  212. return false;
  213. }
  214. if (this._cache.pivotMatrixUpdated) {
  215. return false;
  216. }
  217. if (this.infiniteDistance) {
  218. return false;
  219. }
  220. if (!this._cache.position.equals(this._position)) {
  221. return false;
  222. }
  223. if (this._rotationQuaternion) {
  224. if (!this._cache.rotationQuaternion.equals(this._rotationQuaternion)) {
  225. return false;
  226. }
  227. }
  228. if (!this._cache.rotation.equals(this._rotation)) {
  229. return false;
  230. }
  231. if (!this._cache.scaling.equals(this._scaling)) {
  232. return false;
  233. }
  234. return true;
  235. }
  236. /** @hidden */
  237. public _initCache() {
  238. super._initCache();
  239. this._cache.localMatrixUpdated = false;
  240. this._cache.position = Vector3.Zero();
  241. this._cache.scaling = Vector3.Zero();
  242. this._cache.rotation = Vector3.Zero();
  243. this._cache.rotationQuaternion = new Quaternion(0, 0, 0, 0);
  244. this._cache.billboardMode = -1;
  245. this._cache.infiniteDistance = false;
  246. }
  247. /**
  248. * Flag the transform node as dirty (Forcing it to update everything)
  249. * @param property if set to "rotation" the objects rotationQuaternion will be set to null
  250. * @returns this transform node
  251. */
  252. public markAsDirty(property: string): TransformNode {
  253. if (property === "rotation") {
  254. this.rotationQuaternion = null;
  255. }
  256. this._currentRenderId = Number.MAX_VALUE;
  257. this._isDirty = true;
  258. return this;
  259. }
  260. /**
  261. * Returns the current mesh absolute position.
  262. * Returns a Vector3.
  263. */
  264. public get absolutePosition(): Vector3 {
  265. return this._absolutePosition;
  266. }
  267. /**
  268. * Sets a new matrix to apply before all other transformation
  269. * @param matrix defines the transform matrix
  270. * @returns the current TransformNode
  271. */
  272. public setPreTransformMatrix(matrix: Matrix): TransformNode {
  273. return this.setPivotMatrix(matrix, false);
  274. }
  275. /**
  276. * Sets a new pivot matrix to the current node
  277. * @param matrix defines the new pivot matrix to use
  278. * @param postMultiplyPivotMatrix defines if the pivot matrix must be cancelled in the world matrix. When this parameter is set to true (default), the inverse of the pivot matrix is also applied at the end to cancel the transformation effect
  279. * @returns the current TransformNode
  280. */
  281. public setPivotMatrix(matrix: DeepImmutable<Matrix>, postMultiplyPivotMatrix = true): TransformNode {
  282. this._pivotMatrix.copyFrom(matrix);
  283. this._cache.pivotMatrixUpdated = true;
  284. this._postMultiplyPivotMatrix = postMultiplyPivotMatrix;
  285. if (this._postMultiplyPivotMatrix) {
  286. if (!this._pivotMatrixInverse) {
  287. this._pivotMatrixInverse = Matrix.Invert(this._pivotMatrix);
  288. } else {
  289. this._pivotMatrix.invertToRef(this._pivotMatrixInverse);
  290. }
  291. }
  292. return this;
  293. }
  294. /**
  295. * Returns the mesh pivot matrix.
  296. * Default : Identity.
  297. * @returns the matrix
  298. */
  299. public getPivotMatrix(): Matrix {
  300. return this._pivotMatrix;
  301. }
  302. /**
  303. * Prevents the World matrix to be computed any longer.
  304. * @returns the TransformNode.
  305. */
  306. public freezeWorldMatrix(): TransformNode {
  307. this._isWorldMatrixFrozen = false; // no guarantee world is not already frozen, switch off temporarily
  308. this.computeWorldMatrix(true);
  309. this._isWorldMatrixFrozen = true;
  310. return this;
  311. }
  312. /**
  313. * Allows back the World matrix computation.
  314. * @returns the TransformNode.
  315. */
  316. public unfreezeWorldMatrix() {
  317. this._isWorldMatrixFrozen = false;
  318. this.computeWorldMatrix(true);
  319. return this;
  320. }
  321. /**
  322. * True if the World matrix has been frozen.
  323. */
  324. public get isWorldMatrixFrozen(): boolean {
  325. return this._isWorldMatrixFrozen;
  326. }
  327. /**
  328. * Retuns the mesh absolute position in the World.
  329. * @returns a Vector3.
  330. */
  331. public getAbsolutePosition(): Vector3 {
  332. this.computeWorldMatrix();
  333. return this._absolutePosition;
  334. }
  335. /**
  336. * Sets the mesh absolute position in the World from a Vector3 or an Array(3).
  337. * @param absolutePosition the absolute position to set
  338. * @returns the TransformNode.
  339. */
  340. public setAbsolutePosition(absolutePosition: Vector3): TransformNode {
  341. if (!absolutePosition) {
  342. return this;
  343. }
  344. var absolutePositionX;
  345. var absolutePositionY;
  346. var absolutePositionZ;
  347. if (absolutePosition.x === undefined) {
  348. if (arguments.length < 3) {
  349. return this;
  350. }
  351. absolutePositionX = arguments[0];
  352. absolutePositionY = arguments[1];
  353. absolutePositionZ = arguments[2];
  354. }
  355. else {
  356. absolutePositionX = absolutePosition.x;
  357. absolutePositionY = absolutePosition.y;
  358. absolutePositionZ = absolutePosition.z;
  359. }
  360. if (this.parent) {
  361. const invertParentWorldMatrix = Tmp.Matrix[0];
  362. this.parent.getWorldMatrix().invertToRef(invertParentWorldMatrix);
  363. Vector3.TransformCoordinatesFromFloatsToRef(absolutePositionX, absolutePositionY, absolutePositionZ, invertParentWorldMatrix, this.position);
  364. } else {
  365. this.position.x = absolutePositionX;
  366. this.position.y = absolutePositionY;
  367. this.position.z = absolutePositionZ;
  368. }
  369. return this;
  370. }
  371. /**
  372. * Sets the mesh position in its local space.
  373. * @param vector3 the position to set in localspace
  374. * @returns the TransformNode.
  375. */
  376. public setPositionWithLocalVector(vector3: Vector3): TransformNode {
  377. this.computeWorldMatrix();
  378. this.position = Vector3.TransformNormal(vector3, this._localMatrix);
  379. return this;
  380. }
  381. /**
  382. * Returns the mesh position in the local space from the current World matrix values.
  383. * @returns a new Vector3.
  384. */
  385. public getPositionExpressedInLocalSpace(): Vector3 {
  386. this.computeWorldMatrix();
  387. const invLocalWorldMatrix = Tmp.Matrix[0];
  388. this._localMatrix.invertToRef(invLocalWorldMatrix);
  389. return Vector3.TransformNormal(this.position, invLocalWorldMatrix);
  390. }
  391. /**
  392. * Translates the mesh along the passed Vector3 in its local space.
  393. * @param vector3 the distance to translate in localspace
  394. * @returns the TransformNode.
  395. */
  396. public locallyTranslate(vector3: Vector3): TransformNode {
  397. this.computeWorldMatrix(true);
  398. this.position = Vector3.TransformCoordinates(vector3, this._localMatrix);
  399. return this;
  400. }
  401. private static _lookAtVectorCache = new Vector3(0, 0, 0);
  402. /**
  403. * Orients a mesh towards a target point. Mesh must be drawn facing user.
  404. * @param targetPoint the position (must be in same space as current mesh) to look at
  405. * @param yawCor optional yaw (y-axis) correction in radians
  406. * @param pitchCor optional pitch (x-axis) correction in radians
  407. * @param rollCor optional roll (z-axis) correction in radians
  408. * @param space the choosen space of the target
  409. * @returns the TransformNode.
  410. */
  411. public lookAt(targetPoint: Vector3, yawCor: number = 0, pitchCor: number = 0, rollCor: number = 0, space: Space = Space.LOCAL): TransformNode {
  412. var dv = TransformNode._lookAtVectorCache;
  413. var pos = space === Space.LOCAL ? this.position : this.getAbsolutePosition();
  414. targetPoint.subtractToRef(pos, dv);
  415. this.setDirection(dv, yawCor, pitchCor, rollCor);
  416. // Correct for parent's rotation offset
  417. if (space === Space.WORLD && this.parent) {
  418. if (this.rotationQuaternion) {
  419. // Get local rotation matrix of the looking object
  420. var rotationMatrix = Tmp.Matrix[0];
  421. this.rotationQuaternion.toRotationMatrix(rotationMatrix);
  422. // Offset rotation by parent's inverted rotation matrix to correct in world space
  423. var parentRotationMatrix = Tmp.Matrix[1];
  424. this.parent.getWorldMatrix().getRotationMatrixToRef(parentRotationMatrix);
  425. parentRotationMatrix.invert();
  426. rotationMatrix.multiplyToRef(parentRotationMatrix, rotationMatrix);
  427. this.rotationQuaternion.fromRotationMatrix(rotationMatrix);
  428. } else {
  429. // Get local rotation matrix of the looking object
  430. var quaternionRotation = Tmp.Quaternion[0];
  431. Quaternion.FromEulerVectorToRef(this.rotation, quaternionRotation);
  432. var rotationMatrix = Tmp.Matrix[0];
  433. quaternionRotation.toRotationMatrix(rotationMatrix);
  434. // Offset rotation by parent's inverted rotation matrix to correct in world space
  435. var parentRotationMatrix = Tmp.Matrix[1];
  436. this.parent.getWorldMatrix().getRotationMatrixToRef(parentRotationMatrix);
  437. parentRotationMatrix.invert();
  438. rotationMatrix.multiplyToRef(parentRotationMatrix, rotationMatrix);
  439. quaternionRotation.fromRotationMatrix(rotationMatrix);
  440. quaternionRotation.toEulerAnglesToRef(this.rotation);
  441. }
  442. }
  443. return this;
  444. }
  445. /**
  446. * Returns a new Vector3 that is the localAxis, expressed in the mesh local space, rotated like the mesh.
  447. * This Vector3 is expressed in the World space.
  448. * @param localAxis axis to rotate
  449. * @returns a new Vector3 that is the localAxis, expressed in the mesh local space, rotated like the mesh.
  450. */
  451. public getDirection(localAxis: Vector3): Vector3 {
  452. var result = Vector3.Zero();
  453. this.getDirectionToRef(localAxis, result);
  454. return result;
  455. }
  456. /**
  457. * Sets the Vector3 "result" as the rotated Vector3 "localAxis" in the same rotation than the mesh.
  458. * localAxis is expressed in the mesh local space.
  459. * result is computed in the Wordl space from the mesh World matrix.
  460. * @param localAxis axis to rotate
  461. * @param result the resulting transformnode
  462. * @returns this TransformNode.
  463. */
  464. public getDirectionToRef(localAxis: Vector3, result: Vector3): TransformNode {
  465. Vector3.TransformNormalToRef(localAxis, this.getWorldMatrix(), result);
  466. return this;
  467. }
  468. /**
  469. * Sets this transform node rotation to the given local axis.
  470. * @param localAxis the axis in local space
  471. * @param yawCor optional yaw (y-axis) correction in radians
  472. * @param pitchCor optional pitch (x-axis) correction in radians
  473. * @param rollCor optional roll (z-axis) correction in radians
  474. * @returns this TransformNode
  475. */
  476. public setDirection(localAxis: Vector3, yawCor: number = 0, pitchCor: number = 0, rollCor: number = 0): TransformNode {
  477. var yaw = -Math.atan2(localAxis.z, localAxis.x) + Math.PI / 2;
  478. var len = Math.sqrt(localAxis.x * localAxis.x + localAxis.z * localAxis.z);
  479. var pitch = -Math.atan2(localAxis.y, len);
  480. if (this.rotationQuaternion) {
  481. Quaternion.RotationYawPitchRollToRef(yaw + yawCor, pitch + pitchCor, rollCor, this.rotationQuaternion);
  482. }
  483. else {
  484. this.rotation.x = pitch + pitchCor;
  485. this.rotation.y = yaw + yawCor;
  486. this.rotation.z = rollCor;
  487. }
  488. return this;
  489. }
  490. /**
  491. * Sets a new pivot point to the current node
  492. * @param point defines the new pivot point to use
  493. * @param space defines if the point is in world or local space (local by default)
  494. * @returns the current TransformNode
  495. */
  496. public setPivotPoint(point: Vector3, space: Space = Space.LOCAL): TransformNode {
  497. if (this.getScene().getRenderId() == 0) {
  498. this.computeWorldMatrix(true);
  499. }
  500. var wm = this.getWorldMatrix();
  501. if (space == Space.WORLD) {
  502. var tmat = Tmp.Matrix[0];
  503. wm.invertToRef(tmat);
  504. point = Vector3.TransformCoordinates(point, tmat);
  505. }
  506. return this.setPivotMatrix(Matrix.Translation(-point.x, -point.y, -point.z), true);
  507. }
  508. /**
  509. * Returns a new Vector3 set with the mesh pivot point coordinates in the local space.
  510. * @returns the pivot point
  511. */
  512. public getPivotPoint(): Vector3 {
  513. var point = Vector3.Zero();
  514. this.getPivotPointToRef(point);
  515. return point;
  516. }
  517. /**
  518. * Sets the passed Vector3 "result" with the coordinates of the mesh pivot point in the local space.
  519. * @param result the vector3 to store the result
  520. * @returns this TransformNode.
  521. */
  522. public getPivotPointToRef(result: Vector3): TransformNode {
  523. result.x = -this._pivotMatrix.m[12];
  524. result.y = -this._pivotMatrix.m[13];
  525. result.z = -this._pivotMatrix.m[14];
  526. return this;
  527. }
  528. /**
  529. * Returns a new Vector3 set with the mesh pivot point World coordinates.
  530. * @returns a new Vector3 set with the mesh pivot point World coordinates.
  531. */
  532. public getAbsolutePivotPoint(): Vector3 {
  533. var point = Vector3.Zero();
  534. this.getAbsolutePivotPointToRef(point);
  535. return point;
  536. }
  537. /**
  538. * Sets the Vector3 "result" coordinates with the mesh pivot point World coordinates.
  539. * @param result vector3 to store the result
  540. * @returns this TransformNode.
  541. */
  542. public getAbsolutePivotPointToRef(result: Vector3): TransformNode {
  543. result.x = this._pivotMatrix.m[12];
  544. result.y = this._pivotMatrix.m[13];
  545. result.z = this._pivotMatrix.m[14];
  546. this.getPivotPointToRef(result);
  547. Vector3.TransformCoordinatesToRef(result, this.getWorldMatrix(), result);
  548. return this;
  549. }
  550. /**
  551. * Defines the passed node as the parent of the current node.
  552. * The node will remain exactly where it is and its position / rotation will be updated accordingly
  553. * @see https://doc.babylonjs.com/how_to/parenting
  554. * @param node the node ot set as the parent
  555. * @returns this TransformNode.
  556. */
  557. public setParent(node: Nullable<Node>): TransformNode {
  558. if (!node && !this.parent) {
  559. return this;
  560. }
  561. var quatRotation = Tmp.Quaternion[0];
  562. var position = Tmp.Vector3[0];
  563. var scale = Tmp.Vector3[1];
  564. if (!node) {
  565. if (this.parent && this.parent.computeWorldMatrix) {
  566. this.parent.computeWorldMatrix(true);
  567. }
  568. this.computeWorldMatrix(true);
  569. this.getWorldMatrix().decompose(scale, quatRotation, position);
  570. } else {
  571. var diffMatrix = Tmp.Matrix[0];
  572. var invParentMatrix = Tmp.Matrix[1];
  573. this.computeWorldMatrix(true);
  574. node.computeWorldMatrix(true);
  575. node.getWorldMatrix().invertToRef(invParentMatrix);
  576. this.getWorldMatrix().multiplyToRef(invParentMatrix, diffMatrix);
  577. diffMatrix.decompose(scale, quatRotation, position);
  578. }
  579. if (this.rotationQuaternion) {
  580. this.rotationQuaternion.copyFrom(quatRotation);
  581. } else {
  582. quatRotation.toEulerAnglesToRef(this.rotation);
  583. }
  584. this.scaling.copyFrom(scale);
  585. this.position.copyFrom(position);
  586. this.parent = node;
  587. return this;
  588. }
  589. private _nonUniformScaling = false;
  590. /**
  591. * True if the scaling property of this object is non uniform eg. (1,2,1)
  592. */
  593. public get nonUniformScaling(): boolean {
  594. return this._nonUniformScaling;
  595. }
  596. /** @hidden */
  597. public _updateNonUniformScalingState(value: boolean): boolean {
  598. if (this._nonUniformScaling === value) {
  599. return false;
  600. }
  601. this._nonUniformScaling = value;
  602. return true;
  603. }
  604. /**
  605. * Attach the current TransformNode to another TransformNode associated with a bone
  606. * @param bone Bone affecting the TransformNode
  607. * @param affectedTransformNode TransformNode associated with the bone
  608. * @returns this object
  609. */
  610. public attachToBone(bone: Bone, affectedTransformNode: TransformNode): TransformNode {
  611. this._transformToBoneReferal = affectedTransformNode;
  612. this.parent = bone;
  613. if (bone.getWorldMatrix().determinant() < 0) {
  614. this.scalingDeterminant *= -1;
  615. }
  616. return this;
  617. }
  618. /**
  619. * Detach the transform node if its associated with a bone
  620. * @returns this object
  621. */
  622. public detachFromBone(): TransformNode {
  623. if (!this.parent) {
  624. return this;
  625. }
  626. if (this.parent.getWorldMatrix().determinant() < 0) {
  627. this.scalingDeterminant *= -1;
  628. }
  629. this._transformToBoneReferal = null;
  630. this.parent = null;
  631. return this;
  632. }
  633. private static _rotationAxisCache = new Quaternion();
  634. /**
  635. * Rotates the mesh around the axis vector for the passed angle (amount) expressed in radians, in the given space.
  636. * space (default LOCAL) can be either Space.LOCAL, either Space.WORLD.
  637. * Note that the property `rotationQuaternion` is then automatically updated and the property `rotation` is set to (0,0,0) and no longer used.
  638. * The passed axis is also normalized.
  639. * @param axis the axis to rotate around
  640. * @param amount the amount to rotate in radians
  641. * @param space Space to rotate in (Default: local)
  642. * @returns the TransformNode.
  643. */
  644. public rotate(axis: Vector3, amount: number, space?: Space): TransformNode {
  645. axis.normalize();
  646. if (!this.rotationQuaternion) {
  647. this.rotationQuaternion = this.rotation.toQuaternion();
  648. this.rotation.setAll(0);
  649. }
  650. var rotationQuaternion: Quaternion;
  651. if (!space || (space as any) === Space.LOCAL) {
  652. rotationQuaternion = Quaternion.RotationAxisToRef(axis, amount, TransformNode._rotationAxisCache);
  653. this.rotationQuaternion.multiplyToRef(rotationQuaternion, this.rotationQuaternion);
  654. }
  655. else {
  656. if (this.parent) {
  657. const invertParentWorldMatrix = Tmp.Matrix[0];
  658. this.parent.getWorldMatrix().invertToRef(invertParentWorldMatrix);
  659. axis = Vector3.TransformNormal(axis, invertParentWorldMatrix);
  660. }
  661. rotationQuaternion = Quaternion.RotationAxisToRef(axis, amount, TransformNode._rotationAxisCache);
  662. rotationQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  663. }
  664. return this;
  665. }
  666. /**
  667. * Rotates the mesh around the axis vector for the passed angle (amount) expressed in radians, in world space.
  668. * Note that the property `rotationQuaternion` is then automatically updated and the property `rotation` is set to (0,0,0) and no longer used.
  669. * The passed axis is also normalized. .
  670. * Method is based on http://www.euclideanspace.com/maths/geometry/affine/aroundPoint/index.htm
  671. * @param point the point to rotate around
  672. * @param axis the axis to rotate around
  673. * @param amount the amount to rotate in radians
  674. * @returns the TransformNode
  675. */
  676. public rotateAround(point: Vector3, axis: Vector3, amount: number): TransformNode {
  677. axis.normalize();
  678. if (!this.rotationQuaternion) {
  679. this.rotationQuaternion = Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
  680. this.rotation.setAll(0);
  681. }
  682. const tmpVector = Tmp.Vector3[0];
  683. const finalScale = Tmp.Vector3[1];
  684. const finalTranslation = Tmp.Vector3[2];
  685. const finalRotation = Tmp.Quaternion[0];
  686. const translationMatrix = Tmp.Matrix[0]; // T
  687. const translationMatrixInv = Tmp.Matrix[1]; // T'
  688. const rotationMatrix = Tmp.Matrix[2]; // R
  689. const finalMatrix = Tmp.Matrix[3]; // T' x R x T
  690. point.subtractToRef(this.position, tmpVector);
  691. Matrix.TranslationToRef(tmpVector.x, tmpVector.y, tmpVector.z, translationMatrix); // T
  692. Matrix.TranslationToRef(-tmpVector.x, -tmpVector.y, -tmpVector.z, translationMatrixInv); // T'
  693. Matrix.RotationAxisToRef(axis, amount, rotationMatrix); // R
  694. translationMatrixInv.multiplyToRef(rotationMatrix, finalMatrix); // T' x R
  695. finalMatrix.multiplyToRef(translationMatrix, finalMatrix); // T' x R x T
  696. finalMatrix.decompose(finalScale, finalRotation, finalTranslation);
  697. this.position.addInPlace(finalTranslation);
  698. finalRotation.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  699. return this;
  700. }
  701. /**
  702. * Translates the mesh along the axis vector for the passed distance in the given space.
  703. * space (default LOCAL) can be either Space.LOCAL, either Space.WORLD.
  704. * @param axis the axis to translate in
  705. * @param distance the distance to translate
  706. * @param space Space to rotate in (Default: local)
  707. * @returns the TransformNode.
  708. */
  709. public translate(axis: Vector3, distance: number, space?: Space): TransformNode {
  710. var displacementVector = axis.scale(distance);
  711. if (!space || (space as any) === Space.LOCAL) {
  712. var tempV3 = this.getPositionExpressedInLocalSpace().add(displacementVector);
  713. this.setPositionWithLocalVector(tempV3);
  714. }
  715. else {
  716. this.setAbsolutePosition(this.getAbsolutePosition().add(displacementVector));
  717. }
  718. return this;
  719. }
  720. /**
  721. * Adds a rotation step to the mesh current rotation.
  722. * x, y, z are Euler angles expressed in radians.
  723. * This methods updates the current mesh rotation, either mesh.rotation, either mesh.rotationQuaternion if it's set.
  724. * This means this rotation is made in the mesh local space only.
  725. * It's useful to set a custom rotation order different from the BJS standard one YXZ.
  726. * Example : this rotates the mesh first around its local X axis, then around its local Z axis, finally around its local Y axis.
  727. * ```javascript
  728. * mesh.addRotation(x1, 0, 0).addRotation(0, 0, z2).addRotation(0, 0, y3);
  729. * ```
  730. * Note that `addRotation()` accumulates the passed rotation values to the current ones and computes the .rotation or .rotationQuaternion updated values.
  731. * Under the hood, only quaternions are used. So it's a little faster is you use .rotationQuaternion because it doesn't need to translate them back to Euler angles.
  732. * @param x Rotation to add
  733. * @param y Rotation to add
  734. * @param z Rotation to add
  735. * @returns the TransformNode.
  736. */
  737. public addRotation(x: number, y: number, z: number): TransformNode {
  738. var rotationQuaternion;
  739. if (this.rotationQuaternion) {
  740. rotationQuaternion = this.rotationQuaternion;
  741. }
  742. else {
  743. rotationQuaternion = Tmp.Quaternion[1];
  744. Quaternion.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, rotationQuaternion);
  745. }
  746. var accumulation = Tmp.Quaternion[0];
  747. Quaternion.RotationYawPitchRollToRef(y, x, z, accumulation);
  748. rotationQuaternion.multiplyInPlace(accumulation);
  749. if (!this.rotationQuaternion) {
  750. rotationQuaternion.toEulerAnglesToRef(this.rotation);
  751. }
  752. return this;
  753. }
  754. /**
  755. * Computes the world matrix of the node
  756. * @param force defines if the cache version should be invalidated forcing the world matrix to be created from scratch
  757. * @returns the world matrix
  758. */
  759. public computeWorldMatrix(force?: boolean): Matrix {
  760. if (this._isWorldMatrixFrozen) {
  761. return this._worldMatrix;
  762. }
  763. if (!force && this.isSynchronized()) {
  764. this._currentRenderId = this.getScene().getRenderId();
  765. return this._worldMatrix;
  766. }
  767. this._updateCache();
  768. this._cache.position.copyFrom(this.position);
  769. this._cache.scaling.copyFrom(this.scaling);
  770. this._cache.pivotMatrixUpdated = false;
  771. this._cache.billboardMode = this.billboardMode;
  772. this._cache.infiniteDistance = this.infiniteDistance;
  773. this._currentRenderId = this.getScene().getRenderId();
  774. this._childUpdateId++;
  775. this._isDirty = false;
  776. // Scaling
  777. Matrix.ScalingToRef(this.scaling.x * this.scalingDeterminant, this.scaling.y * this.scalingDeterminant, this.scaling.z * this.scalingDeterminant, Tmp.Matrix[1]);
  778. // Rotation
  779. //rotate, if quaternion is set and rotation was used
  780. if (this.rotationQuaternion) {
  781. var len = this.rotation.length();
  782. if (len) {
  783. this.rotationQuaternion.multiplyInPlace(Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z));
  784. this.rotation.copyFromFloats(0, 0, 0);
  785. }
  786. }
  787. if (this.rotationQuaternion) {
  788. this.rotationQuaternion.toRotationMatrix(Tmp.Matrix[0]);
  789. this._cache.rotationQuaternion.copyFrom(this.rotationQuaternion);
  790. } else {
  791. Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, Tmp.Matrix[0]);
  792. this._cache.rotation.copyFrom(this.rotation);
  793. }
  794. // Translation
  795. let camera = (<Camera>this.getScene().activeCamera);
  796. if (this.infiniteDistance && !this.parent && camera) {
  797. var cameraWorldMatrix = camera.getWorldMatrix();
  798. var cameraGlobalPosition = new Vector3(cameraWorldMatrix.m[12], cameraWorldMatrix.m[13], cameraWorldMatrix.m[14]);
  799. Matrix.TranslationToRef(this.position.x + cameraGlobalPosition.x, this.position.y + cameraGlobalPosition.y,
  800. this.position.z + cameraGlobalPosition.z, this._tempMatrix2);
  801. } else {
  802. Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._tempMatrix2);
  803. }
  804. // Composing transformations
  805. this._pivotMatrix.multiplyToRef(Tmp.Matrix[1], Tmp.Matrix[4]);
  806. Tmp.Matrix[4].multiplyToRef(Tmp.Matrix[0], this._tempMatrix);
  807. // Post multiply inverse of pivotMatrix
  808. if (this._postMultiplyPivotMatrix) {
  809. this._tempMatrix.multiplyToRef(this._pivotMatrixInverse, this._tempMatrix);
  810. }
  811. // Local world
  812. this._tempMatrix.multiplyToRef(this._tempMatrix2, this._localMatrix);
  813. // Parent
  814. if (this.parent && this.parent.getWorldMatrix) {
  815. // We do not want parent rotation
  816. if (this.billboardMode !== TransformNode.BILLBOARDMODE_NONE && !this.preserveParentRotationForBillboard) {
  817. if (this._transformToBoneReferal) {
  818. this.parent.getWorldMatrix().multiplyToRef(this._transformToBoneReferal.getWorldMatrix(), Tmp.Matrix[7]);
  819. } else {
  820. Tmp.Matrix[7].copyFrom(this.parent.getWorldMatrix());
  821. }
  822. // Extract scaling and translation from parent
  823. let translation = Tmp.Vector3[5];
  824. let scale = Tmp.Vector3[6];
  825. Tmp.Matrix[7].decompose(scale, undefined, translation);
  826. Matrix.ScalingToRef(scale.x, scale.y, scale.z, Tmp.Matrix[7]);
  827. Tmp.Matrix[7].setTranslation(translation);
  828. this._localMatrix.multiplyToRef(Tmp.Matrix[7], this._worldMatrix);
  829. } else {
  830. if (this._transformToBoneReferal) {
  831. this._localMatrix.multiplyToRef(this.parent.getWorldMatrix(), Tmp.Matrix[6]);
  832. Tmp.Matrix[6].multiplyToRef(this._transformToBoneReferal.getWorldMatrix(), this._worldMatrix);
  833. } else {
  834. this._localMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);
  835. }
  836. }
  837. this._markSyncedWithParent();
  838. } else {
  839. this._worldMatrix.copyFrom(this._localMatrix);
  840. }
  841. // Billboarding (testing PG:http://www.babylonjs-playground.com/#UJEIL#13)
  842. if (this.billboardMode !== TransformNode.BILLBOARDMODE_NONE && camera) {
  843. let storedTranslation = Tmp.Vector3[0];
  844. this._worldMatrix.getTranslationToRef(storedTranslation); // Save translation
  845. // Cancel camera rotation
  846. Tmp.Matrix[1].copyFrom(camera.getViewMatrix());
  847. Tmp.Matrix[1].setTranslationFromFloats(0, 0, 0);
  848. Tmp.Matrix[1].invertToRef(Tmp.Matrix[0]);
  849. if ((this.billboardMode & TransformNode.BILLBOARDMODE_ALL) !== TransformNode.BILLBOARDMODE_ALL) {
  850. Tmp.Matrix[0].decompose(undefined, Tmp.Quaternion[0], undefined);
  851. let eulerAngles = Tmp.Vector3[1];
  852. Tmp.Quaternion[0].toEulerAnglesToRef(eulerAngles);
  853. if ((this.billboardMode & TransformNode.BILLBOARDMODE_X) !== TransformNode.BILLBOARDMODE_X) {
  854. eulerAngles.x = 0;
  855. }
  856. if ((this.billboardMode & TransformNode.BILLBOARDMODE_Y) !== TransformNode.BILLBOARDMODE_Y) {
  857. eulerAngles.y = 0;
  858. }
  859. if ((this.billboardMode & TransformNode.BILLBOARDMODE_Z) !== TransformNode.BILLBOARDMODE_Z) {
  860. eulerAngles.z = 0;
  861. }
  862. Matrix.RotationYawPitchRollToRef(eulerAngles.y, eulerAngles.x, eulerAngles.z, Tmp.Matrix[0]);
  863. }
  864. this._worldMatrix.setTranslationFromFloats(0, 0, 0);
  865. this._worldMatrix.multiplyToRef(Tmp.Matrix[0], this._worldMatrix);
  866. // Restore translation
  867. this._worldMatrix.setTranslation(Tmp.Vector3[0]);
  868. }
  869. // Normal matrix
  870. if (!this.ignoreNonUniformScaling) {
  871. if (this.scaling.isNonUniform) {
  872. this._updateNonUniformScalingState(true);
  873. } else if (this.parent && (<TransformNode>this.parent)._nonUniformScaling) {
  874. this._updateNonUniformScalingState((<TransformNode>this.parent)._nonUniformScaling);
  875. } else {
  876. this._updateNonUniformScalingState(false);
  877. }
  878. } else {
  879. this._updateNonUniformScalingState(false);
  880. }
  881. this._afterComputeWorldMatrix();
  882. // Absolute position
  883. this._absolutePosition.copyFromFloats(this._worldMatrix.m[12], this._worldMatrix.m[13], this._worldMatrix.m[14]);
  884. // Callbacks
  885. this.onAfterWorldMatrixUpdateObservable.notifyObservers(this);
  886. if (!this._poseMatrix) {
  887. this._poseMatrix = Matrix.Invert(this._worldMatrix);
  888. }
  889. // Cache the determinant
  890. this._worldMatrixDeterminant = this._worldMatrix.determinant();
  891. return this._worldMatrix;
  892. }
  893. protected _afterComputeWorldMatrix(): void {
  894. }
  895. /**
  896. * If you'd like to be called back after the mesh position, rotation or scaling has been updated.
  897. * @param func callback function to add
  898. *
  899. * @returns the TransformNode.
  900. */
  901. public registerAfterWorldMatrixUpdate(func: (mesh: TransformNode) => void): TransformNode {
  902. this.onAfterWorldMatrixUpdateObservable.add(func);
  903. return this;
  904. }
  905. /**
  906. * Removes a registered callback function.
  907. * @param func callback function to remove
  908. * @returns the TransformNode.
  909. */
  910. public unregisterAfterWorldMatrixUpdate(func: (mesh: TransformNode) => void): TransformNode {
  911. this.onAfterWorldMatrixUpdateObservable.removeCallback(func);
  912. return this;
  913. }
  914. /**
  915. * Gets the position of the current mesh in camera space
  916. * @param camera defines the camera to use
  917. * @returns a position
  918. */
  919. public getPositionInCameraSpace(camera: Nullable<Camera> = null): Vector3 {
  920. if (!camera) {
  921. camera = (<Camera>this.getScene().activeCamera);
  922. }
  923. return Vector3.TransformCoordinates(this.absolutePosition, camera.getViewMatrix());
  924. }
  925. /**
  926. * Returns the distance from the mesh to the active camera
  927. * @param camera defines the camera to use
  928. * @returns the distance
  929. */
  930. public getDistanceToCamera(camera: Nullable<Camera> = null): number {
  931. if (!camera) {
  932. camera = (<Camera>this.getScene().activeCamera);
  933. }
  934. return this.absolutePosition.subtract(camera.globalPosition).length();
  935. }
  936. /**
  937. * Clone the current transform node
  938. * @param name Name of the new clone
  939. * @param newParent New parent for the clone
  940. * @param doNotCloneChildren Do not clone children hierarchy
  941. * @returns the new transform node
  942. */
  943. public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): Nullable<TransformNode> {
  944. var result = SerializationHelper.Clone(() => new TransformNode(name, this.getScene()), this);
  945. result.name = name;
  946. result.id = name;
  947. if (newParent) {
  948. result.parent = newParent;
  949. }
  950. if (!doNotCloneChildren) {
  951. // Children
  952. let directDescendants = this.getDescendants(true);
  953. for (let index = 0; index < directDescendants.length; index++) {
  954. var child = directDescendants[index];
  955. if ((<any>child).clone) {
  956. (<any>child).clone(name + "." + child.name, result);
  957. }
  958. }
  959. }
  960. return result;
  961. }
  962. /**
  963. * Serializes the objects information.
  964. * @param currentSerializationObject defines the object to serialize in
  965. * @returns the serialized object
  966. */
  967. public serialize(currentSerializationObject?: any): any {
  968. let serializationObject = SerializationHelper.Serialize(this, currentSerializationObject);
  969. serializationObject.type = this.getClassName();
  970. // Parent
  971. if (this.parent) {
  972. serializationObject.parentId = this.parent.id;
  973. }
  974. serializationObject.localMatrix = this.getPivotMatrix().asArray();
  975. serializationObject.isEnabled = this.isEnabled();
  976. // Parent
  977. if (this.parent) {
  978. serializationObject.parentId = this.parent.id;
  979. }
  980. return serializationObject;
  981. }
  982. // Statics
  983. /**
  984. * Returns a new TransformNode object parsed from the source provided.
  985. * @param parsedTransformNode is the source.
  986. * @param scene the scne the object belongs to
  987. * @param rootUrl is a string, it's the root URL to prefix the `delayLoadingFile` property with
  988. * @returns a new TransformNode object parsed from the source provided.
  989. */
  990. public static Parse(parsedTransformNode: any, scene: Scene, rootUrl: string): TransformNode {
  991. var transformNode = SerializationHelper.Parse(() => new TransformNode(parsedTransformNode.name, scene), parsedTransformNode, scene, rootUrl);
  992. if (parsedTransformNode.localMatrix) {
  993. transformNode.setPreTransformMatrix(Matrix.FromArray(parsedTransformNode.localMatrix));
  994. } else if (parsedTransformNode.pivotMatrix) {
  995. transformNode.setPivotMatrix(Matrix.FromArray(parsedTransformNode.pivotMatrix));
  996. }
  997. transformNode.setEnabled(parsedTransformNode.isEnabled);
  998. // Parent
  999. if (parsedTransformNode.parentId) {
  1000. transformNode._waitingParentId = parsedTransformNode.parentId;
  1001. }
  1002. return transformNode;
  1003. }
  1004. /**
  1005. * Get all child-transformNodes of this node
  1006. * @param directDescendantsOnly defines if true only direct descendants of 'this' will be considered, if false direct and also indirect (children of children, an so on in a recursive manner) descendants of 'this' will be considered
  1007. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  1008. * @returns an array of TransformNode
  1009. */
  1010. public getChildTransformNodes(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): TransformNode[] {
  1011. var results: Array<TransformNode> = [];
  1012. this._getDescendants(results, directDescendantsOnly, (node: Node) => {
  1013. return ((!predicate || predicate(node)) && (node instanceof TransformNode));
  1014. });
  1015. return results;
  1016. }
  1017. /**
  1018. * Releases resources associated with this transform node.
  1019. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  1020. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  1021. */
  1022. public dispose(doNotRecurse?: boolean, disposeMaterialAndTextures = false): void {
  1023. // Animations
  1024. this.getScene().stopAnimation(this);
  1025. // Remove from scene
  1026. this.getScene().removeTransformNode(this);
  1027. this.onAfterWorldMatrixUpdateObservable.clear();
  1028. if (doNotRecurse) {
  1029. const transformNodes = this.getChildTransformNodes(true);
  1030. for (const transformNode of transformNodes) {
  1031. transformNode.parent = null;
  1032. transformNode.computeWorldMatrix(true);
  1033. }
  1034. }
  1035. super.dispose(doNotRecurse, disposeMaterialAndTextures);
  1036. }
  1037. }