|
@@ -4,41 +4,82 @@
|
|
|
* Node is the basic class for all scene objects (Mesh, Light Camera).
|
|
|
*/
|
|
|
export class Node {
|
|
|
+ /**
|
|
|
+ * Gets or sets the name of the node
|
|
|
+ */
|
|
|
@serialize()
|
|
|
public name: string;
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets or sets the id of the node
|
|
|
+ */
|
|
|
@serialize()
|
|
|
public id: string;
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets or sets the unique id of the node
|
|
|
+ */
|
|
|
@serialize()
|
|
|
public uniqueId: number;
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets or sets a string used to store user defined state for the node
|
|
|
+ */
|
|
|
@serialize()
|
|
|
public state = "";
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets or sets an object used to store user defined information for the node
|
|
|
+ */
|
|
|
@serialize()
|
|
|
public metadata: any = null;
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets or sets a boolean used to define if the node must be serialized
|
|
|
+ */
|
|
|
public doNotSerialize = false;
|
|
|
+
|
|
|
+ /** @ignore */
|
|
|
+ public _isDisposed = false;
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets a list of {BABYLON.Animation} associated with the node
|
|
|
+ */
|
|
|
public animations = new Array<Animation>();
|
|
|
private _ranges: { [name: string]: Nullable<AnimationRange> } = {};
|
|
|
|
|
|
+ /**
|
|
|
+ * Callback raised when the node is ready to be used
|
|
|
+ */
|
|
|
public onReady: (node: Node) => void;
|
|
|
|
|
|
private _isEnabled = true;
|
|
|
private _isReady = true;
|
|
|
+ /** @ignore */
|
|
|
public _currentRenderId = -1;
|
|
|
private _parentRenderId = -1;
|
|
|
|
|
|
+ /** @ignore */
|
|
|
public _waitingParentId: Nullable<string>;
|
|
|
|
|
|
private _scene: Scene;
|
|
|
+ /** @ignore */
|
|
|
public _cache: any;
|
|
|
|
|
|
private _parentNode: Nullable<Node>;
|
|
|
private _children: Node[];
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets a boolean indicating if the node has been disposed
|
|
|
+ * @returns true if the node was disposed
|
|
|
+ */
|
|
|
+ public isDisposed(): boolean {
|
|
|
+ return this._isDisposed;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Gets or sets the parent of the node
|
|
|
+ */
|
|
|
public set parent(parent: Nullable<Node>) {
|
|
|
if (this._parentNode === parent) {
|
|
|
return;
|
|
@@ -68,17 +109,24 @@
|
|
|
return this._parentNode;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets a string idenfifying the name of the class
|
|
|
+ * @returns "Node" string
|
|
|
+ */
|
|
|
public getClassName(): string {
|
|
|
return "Node";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * An event triggered when the mesh is disposed.
|
|
|
+ * An event triggered when the mesh is disposed
|
|
|
* @type {BABYLON.Observable}
|
|
|
*/
|
|
|
public onDisposeObservable = new Observable<Node>();
|
|
|
|
|
|
private _onDisposeObserver: Nullable<Observer<Node>>;
|
|
|
+ /**
|
|
|
+ * Sets a callback that will be raised when the node will be disposed
|
|
|
+ */
|
|
|
public set onDispose(callback: () => void) {
|
|
|
if (this._onDisposeObserver) {
|
|
|
this.onDisposeObservable.remove(this._onDisposeObserver);
|
|
@@ -87,7 +135,7 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @constructor
|
|
|
+ * Creates a new Node
|
|
|
* @param {string} name - the name and id to be given to this node
|
|
|
* @param {BABYLON.Scene} the scene this node will be added to
|
|
|
*/
|
|
@@ -99,10 +147,18 @@
|
|
|
this._initCache();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the scene of the node
|
|
|
+ * @returns a {BABYLON.Scene}
|
|
|
+ */
|
|
|
public getScene(): Scene {
|
|
|
return this._scene;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the engine of the node
|
|
|
+ * @returns a {BABYLON.Engine}
|
|
|
+ */
|
|
|
public getEngine(): Engine {
|
|
|
return this._scene.getEngine();
|
|
|
}
|
|
@@ -110,6 +166,12 @@
|
|
|
// Behaviors
|
|
|
private _behaviors = new Array<Behavior<Node>>();
|
|
|
|
|
|
+ /**
|
|
|
+ * Attach a behavior to the node
|
|
|
+ * @see http://doc.babylonjs.com/features/behaviour
|
|
|
+ * @param behavior defines the behavior to attach
|
|
|
+ * @returns the current Node
|
|
|
+ */
|
|
|
public addBehavior(behavior: Behavior<Node>): Node {
|
|
|
var index = this._behaviors.indexOf(behavior);
|
|
|
|
|
@@ -135,6 +197,12 @@
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Remove an attached behavior
|
|
|
+ * @see http://doc.babylonjs.com/features/behaviour
|
|
|
+ * @param behavior defines the behavior to attach
|
|
|
+ * @returns the current Node
|
|
|
+ */
|
|
|
public removeBehavior(behavior: Behavior<Node>): Node {
|
|
|
var index = this._behaviors.indexOf(behavior);
|
|
|
|
|
@@ -148,10 +216,20 @@
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the list of attached behaviors
|
|
|
+ * @see http://doc.babylonjs.com/features/behaviour
|
|
|
+ */
|
|
|
public get behaviors(): Behavior<Node>[] {
|
|
|
return this._behaviors;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets an attached behavior by name
|
|
|
+ * @param name defines the name of the behavior to look for
|
|
|
+ * @see http://doc.babylonjs.com/features/behaviour
|
|
|
+ * @returns null if behavior was not found else the requested behavior
|
|
|
+ */
|
|
|
public getBehaviorByName(name: string): Nullable<Behavior<Node>> {
|
|
|
for (var behavior of this._behaviors) {
|
|
|
if (behavior.name === name) {
|
|
@@ -162,18 +240,23 @@
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- // override it in derived class
|
|
|
+ /**
|
|
|
+ * Returns the world matrix of the node
|
|
|
+ * @returns a matrix containing the node's world matrix
|
|
|
+ */
|
|
|
public getWorldMatrix(): Matrix {
|
|
|
return Matrix.Identity();
|
|
|
}
|
|
|
|
|
|
// override it in derived class if you add new variables to the cache
|
|
|
// and call the parent class method
|
|
|
+ /** @ignore */
|
|
|
public _initCache() {
|
|
|
this._cache = {};
|
|
|
this._cache.parent = undefined;
|
|
|
}
|
|
|
|
|
|
+ /** @ignore */
|
|
|
public updateCache(force?: boolean): void {
|
|
|
if (!force && this.isSynchronized())
|
|
|
return;
|
|
@@ -185,20 +268,24 @@
|
|
|
|
|
|
// override it in derived class if you add new variables to the cache
|
|
|
// and call the parent class method if !ignoreParentClass
|
|
|
+ /** @ignore */
|
|
|
public _updateCache(ignoreParentClass?: boolean): void {
|
|
|
}
|
|
|
|
|
|
// override it in derived class if you add new variables to the cache
|
|
|
+ /** @ignore */
|
|
|
public _isSynchronized(): boolean {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /** @ignore */
|
|
|
public _markSyncedWithParent() {
|
|
|
if (this.parent) {
|
|
|
this._parentRenderId = this.parent._currentRenderId;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /** @ignore */
|
|
|
public isSynchronizedWithParent(): boolean {
|
|
|
if (!this.parent) {
|
|
|
return true;
|
|
@@ -211,6 +298,7 @@
|
|
|
return this.parent.isSynchronized();
|
|
|
}
|
|
|
|
|
|
+ /** @ignore */
|
|
|
public isSynchronized(updateCache?: boolean): boolean {
|
|
|
var check = this.hasNewParent();
|
|
|
|
|
@@ -224,6 +312,7 @@
|
|
|
return !check;
|
|
|
}
|
|
|
|
|
|
+ /** @ignore */
|
|
|
public hasNewParent(update?: boolean): boolean {
|
|
|
if (this._cache.parent === this.parent)
|
|
|
return false;
|
|
@@ -236,17 +325,17 @@
|
|
|
|
|
|
/**
|
|
|
* Is this node ready to be used/rendered
|
|
|
- * @return {boolean} is it ready
|
|
|
+ * @return true if the node is ready
|
|
|
*/
|
|
|
public isReady(): boolean {
|
|
|
return this._isReady;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Is this node enabled.
|
|
|
- * If the node has a parent, all ancestors will be checked and false will be returned if any are false (not enabled), otherwise will return true.
|
|
|
- * @param {boolean} [checkAncestors=true] - Indicates if this method should check the ancestors. The default is to check the ancestors. If set to false, the method will return the value of this node without checking ancestors.
|
|
|
- * @return {boolean} whether this node (and its parent) is enabled.
|
|
|
+ * Is this node enabled?
|
|
|
+ * If the node has a parent, all ancestors will be checked and false will be returned if any are false (not enabled), otherwise will return true
|
|
|
+ * @param checkAncestors indicates if this method should check the ancestors. The default is to check the ancestors. If set to false, the method will return the value of this node without checking ancestors
|
|
|
+ * @return whether this node (and its parent) is enabled
|
|
|
* @see setEnabled
|
|
|
*/
|
|
|
public isEnabled(checkAncestors: boolean = true): boolean {
|
|
@@ -266,8 +355,8 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Set the enabled state of this node.
|
|
|
- * @param {boolean} value - the new enabled state
|
|
|
+ * Set the enabled state of this node
|
|
|
+ * @param value defines the new enabled state
|
|
|
* @see isEnabled
|
|
|
*/
|
|
|
public setEnabled(value: boolean): void {
|
|
@@ -275,10 +364,11 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Is this node a descendant of the given node.
|
|
|
- * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
|
|
|
- * @param {BABYLON.Node} ancestor - The parent node to inspect
|
|
|
+ * Is this node a descendant of the given node?
|
|
|
+ * The function will iterate up the hierarchy until the ancestor was found or no more parents defined
|
|
|
+ * @param ancestor defines the parent node to inspect
|
|
|
* @see parent
|
|
|
+ * @returns a boolean indicating if this node is a descendant of the given node
|
|
|
*/
|
|
|
public isDescendantOf(ancestor: Node): boolean {
|
|
|
if (this.parent) {
|
|
@@ -291,12 +381,7 @@
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Evaluate the list of children and determine if they should be considered as descendants considering the given criterias
|
|
|
- * @param {BABYLON.Node[]} results the result array containing the nodes matching the given criterias
|
|
|
- * @param {boolean} directDescendantsOnly 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.
|
|
|
- * @param predicate: an optional predicate that will be called on every evaluated children, the predicate must return true for a given child to be part of the result, otherwise it will be ignored.
|
|
|
- */
|
|
|
+ /** @ignore */
|
|
|
public _getDescendants(results: Node[], directDescendantsOnly: boolean = false, predicate?: (node: Node) => boolean): void {
|
|
|
if (!this._children) {
|
|
|
return;
|
|
@@ -316,10 +401,10 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Will return all nodes that have this node as ascendant.
|
|
|
- * @param {boolean} directDescendantsOnly 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.
|
|
|
- * @param predicate: an optional predicate that will be called on every evaluated children, the predicate must return true for a given child to be part of the result, otherwise it will be ignored.
|
|
|
- * @return {BABYLON.Node[]} all children nodes of all types.
|
|
|
+ * Will return all nodes that have this node as ascendant
|
|
|
+ * @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
|
|
|
+ * @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
|
|
|
+ * @return all children nodes of all types
|
|
|
*/
|
|
|
public getDescendants(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): Node[] {
|
|
|
var results = new Array<Node>();
|
|
@@ -330,7 +415,10 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Get all child-meshes of this node.
|
|
|
+ * Get all child-meshes of this node
|
|
|
+ * @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
|
|
|
+ * @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
|
|
|
+ * @returns an array of {BABYLON.AbstractMesh}
|
|
|
*/
|
|
|
public getChildMeshes(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): AbstractMesh[] {
|
|
|
var results: Array<AbstractMesh> = [];
|
|
@@ -341,7 +429,10 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Get all child-transformNodes of this node.
|
|
|
+ * Get all child-transformNodes of this node
|
|
|
+ * @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
|
|
|
+ * @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
|
|
|
+ * @returns an array of {BABYLON.TransformNode}
|
|
|
*/
|
|
|
public getChildTransformNodes(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): TransformNode[] {
|
|
|
var results: Array<TransformNode> = [];
|
|
@@ -352,12 +443,15 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Get all direct children of this node.
|
|
|
- */
|
|
|
+ * Get all direct children of this node
|
|
|
+ * @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
|
|
|
+ * @returns an array of {BABYLON.Node}
|
|
|
+ */
|
|
|
public getChildren(predicate?: (node: Node) => boolean): Node[] {
|
|
|
return this.getDescendants(true, predicate);
|
|
|
}
|
|
|
|
|
|
+ /** @ignore */
|
|
|
public _setReady(state: boolean): void {
|
|
|
if (state === this._isReady) {
|
|
|
return;
|
|
@@ -374,6 +468,11 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get an animation by name
|
|
|
+ * @param name defines the name of the animation to look for
|
|
|
+ * @returns null if not found else the requested animation
|
|
|
+ */
|
|
|
public getAnimationByName(name: string): Nullable<Animation> {
|
|
|
for (var i = 0; i < this.animations.length; i++) {
|
|
|
var animation = this.animations[i];
|
|
@@ -386,6 +485,12 @@
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates an animation range for this node
|
|
|
+ * @param name defines the name of the range
|
|
|
+ * @param from defines the starting key
|
|
|
+ * @param to defines the end key
|
|
|
+ */
|
|
|
public createAnimationRange(name: string, from: number, to: number): void {
|
|
|
// check name not already in use
|
|
|
if (!this._ranges[name]) {
|
|
@@ -398,6 +503,11 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Delete a specific animation range
|
|
|
+ * @param name defines the name of the range to delete
|
|
|
+ * @param deleteFrames defines if animation frames from the range must be deleted as well
|
|
|
+ */
|
|
|
public deleteAnimationRange(name: string, deleteFrames = true): void {
|
|
|
for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
|
|
|
if (this.animations[i]) {
|
|
@@ -407,6 +517,11 @@
|
|
|
this._ranges[name] = null; // said much faster than 'delete this._range[name]'
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get an animation range by name
|
|
|
+ * @param name defines the name of the animation range to look for
|
|
|
+ * @returns null if not found else the requested animation range
|
|
|
+ */
|
|
|
public getAnimationRange(name: string): Nullable<AnimationRange> {
|
|
|
return this._ranges[name];
|
|
|
}
|
|
@@ -417,7 +532,7 @@
|
|
|
* @param loop defines if the animation should loop (false by default)
|
|
|
* @param speedRatio defines the speed factor in which to run the animation (1 by default)
|
|
|
* @param onAnimationEnd defines a function to be executed when the animation ended (undefined by default)
|
|
|
- * @returns the {BABYLON.Animatable} object created for this animation. If range does not exist, it will return null
|
|
|
+ * @returns the object created for this animation. If range does not exist, it will return null
|
|
|
*/
|
|
|
public beginAnimation(name: string, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): Nullable<Animatable> {
|
|
|
var range = this.getAnimationRange(name);
|
|
@@ -429,6 +544,10 @@
|
|
|
return this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Serialize animation ranges into a JSON compatible object
|
|
|
+ * @returns serialization object
|
|
|
+ */
|
|
|
public serializeAnimationRanges(): any {
|
|
|
var serializationRanges = [];
|
|
|
for (var name in this._ranges) {
|
|
@@ -445,11 +564,18 @@
|
|
|
return serializationRanges;
|
|
|
}
|
|
|
|
|
|
- // override it in derived class
|
|
|
+ /**
|
|
|
+ * Computes the world matrix of the node
|
|
|
+ * @param force defines if the cache version should be invalidated forcing the world matrix to be created from scratch
|
|
|
+ * @returns the world matrix
|
|
|
+ */
|
|
|
public computeWorldMatrix(force?: boolean): Matrix {
|
|
|
return Matrix.Identity();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Releases all associated resources
|
|
|
+ */
|
|
|
public dispose(): void {
|
|
|
this.parent = null;
|
|
|
|
|
@@ -463,8 +589,15 @@
|
|
|
}
|
|
|
|
|
|
this._behaviors = [];
|
|
|
+ this._isDisposed = true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Parse animation range data from a serialization object and store them into a given node
|
|
|
+ * @param node defines where to store the animation ranges
|
|
|
+ * @param parsedNode defines the serialization object to read data from
|
|
|
+ * @param scene defines the hosting scene
|
|
|
+ */
|
|
|
public static ParseAnimationRanges(node: Node, parsedNode: any, scene: Scene): void {
|
|
|
if (parsedNode.ranges) {
|
|
|
for (var index = 0; index < parsedNode.ranges.length; index++) {
|