@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 10.6 kB
JavaScript
import{EventHandler as t}from"../core/event-handler.js";import{Tags as i}from"../core/tags.js";import{Mat3 as e}from"../core/math/mat3.js";import{Mat4 as r}from"../core/math/mat4.js";import{Quat as o}from"../core/math/quat.js";import{Vec3 as s}from"../core/math/vec3.js";const n=new r,l=new s,a=new o,h=new o,c=new s,d=new s,_=new r,f=new o,p=new s,y=new r,m=new o,u=new o,g=new r,w=new s,T=new s;function L(t,i){return t instanceof Function?t:e=>{let r=e[t];return r instanceof Function&&(r=r()),r===i}}function R(t,i){if(i(t))return t;const e=t._children,r=e.length;for(let t=0;t<r;++t){const r=R(e[t],i);if(r)return r}return null}class S extends t{constructor(t="Untitled"){super(),this.name=void 0,this.tags=new i(this),this._labels={},this.localPosition=new s,this.localRotation=new o,this.localScale=new s(1,1,1),this.localEulerAngles=new s,this.position=new s,this.rotation=new o,this.eulerAngles=new s,this._scale=null,this.localTransform=new r,this._dirtyLocal=!1,this._aabbVer=0,this._frozen=!1,this.worldTransform=new r,this._dirtyWorld=!1,this._worldScaleSign=0,this._normalMatrix=new e,this._dirtyNormal=!0,this._right=null,this._up=null,this._forward=null,this._parent=null,this._children=[],this._graphDepth=0,this._enabled=!0,this._enabledInHierarchy=!1,this.scaleCompensation=!1,this.name=t}get right(){return this._right||(this._right=new s),this.getWorldTransform().getX(this._right).normalize()}get up(){return this._up||(this._up=new s),this.getWorldTransform().getY(this._up).normalize()}get forward(){return this._forward||(this._forward=new s),this.getWorldTransform().getZ(this._forward).normalize().mulScalar(-1)}get normalMatrix(){const t=this._normalMatrix;return this._dirtyNormal&&(t.invertMat4(this.getWorldTransform()).transpose(),this._dirtyNormal=!1),t}set enabled(t){var i;this._enabled!==t&&(this._enabled=t,(t&&null!=(i=this._parent)&&i.enabled||!t)&&this._notifyHierarchyStateChanged(this,t))}get enabled(){return this._enabled&&this._enabledInHierarchy}get parent(){return this._parent}get path(){let t=this._parent;if(!t)return"";let i=this.name;for(;t&&t._parent;)i=`${t.name}/${i}`,t=t._parent;return i}get root(){let t=this;for(;t._parent;)t=t._parent;return t}get children(){return this._children}get graphDepth(){return this._graphDepth}_notifyHierarchyStateChanged(t,i){t._onHierarchyStateChanged(i);const e=t._children;for(let t=0,r=e.length;t<r;t++)e[t]._enabled&&this._notifyHierarchyStateChanged(e[t],i)}_onHierarchyStateChanged(t){this._enabledInHierarchy=t,t&&!this._frozen&&this._unfreezeParentToRoot()}_cloneInternal(t){t.name=this.name;const i=this.tags._list;t.tags.clear();for(let e=0;e<i.length;e++)t.tags.add(i[e]);t._labels=Object.assign({},this._labels),t.localPosition.copy(this.localPosition),t.localRotation.copy(this.localRotation),t.localScale.copy(this.localScale),t.localEulerAngles.copy(this.localEulerAngles),t.position.copy(this.position),t.rotation.copy(this.rotation),t.eulerAngles.copy(this.eulerAngles),t.localTransform.copy(this.localTransform),t._dirtyLocal=this._dirtyLocal,t.worldTransform.copy(this.worldTransform),t._dirtyWorld=this._dirtyWorld,t._dirtyNormal=this._dirtyNormal,t._aabbVer=this._aabbVer+1,t._enabled=this._enabled,t.scaleCompensation=this.scaleCompensation,t._enabledInHierarchy=!1}clone(){const t=new this.constructor;return this._cloneInternal(t),t}copy(t){return t._cloneInternal(this),this}destroy(){this.remove();const t=this._children;for(;t.length;){const i=t.pop();i._parent=null,i.destroy()}this.fire("destroy",this),this.off()}find(t,i){const e=[],r=L(t,i);return this.forEach((t=>{r(t)&&e.push(t)})),e}findOne(t,i){return R(this,L(t,i))}findByTag(){const t=arguments,i=[],e=(r,o)=>{o&&r.tags.has(...t)&&i.push(r);for(let t=0;t<r._children.length;t++)e(r._children[t],!0)};return e(this,!1),i}findByName(t){return this.findOne("name",t)}findByPath(t){const i=Array.isArray(t)?t:t.split("/");let e=this;for(let t=0,r=i.length;t<r;++t)if(e=e.children.find((e=>e.name===i[t])),!e)return null;return e}forEach(t,i){t.call(i,this);const e=this._children,r=e.length;for(let o=0;o<r;++o)e[o].forEach(t,i)}isDescendantOf(t){let i=this._parent;for(;i;){if(i===t)return!0;i=i._parent}return!1}isAncestorOf(t){return t.isDescendantOf(this)}getEulerAngles(){return this.getWorldTransform().getEulerAngles(this.eulerAngles),this.eulerAngles}getLocalEulerAngles(){return this.localRotation.getEulerAngles(this.localEulerAngles),this.localEulerAngles}getLocalPosition(){return this.localPosition}getLocalRotation(){return this.localRotation}getLocalScale(){return this.localScale}getLocalTransform(){return this._dirtyLocal&&(this.localTransform.setTRS(this.localPosition,this.localRotation,this.localScale),this._dirtyLocal=!1),this.localTransform}getPosition(){return this.getWorldTransform().getTranslation(this.position),this.position}getRotation(){return this.rotation.setFromMat4(this.getWorldTransform()),this.rotation}getScale(){return this._scale||(this._scale=new s),this.getWorldTransform().getScale(this._scale)}getWorldTransform(){return this._dirtyLocal||this._dirtyWorld?(this._parent&&this._parent.getWorldTransform(),this._sync(),this.worldTransform):this.worldTransform}get worldScaleSign(){return 0===this._worldScaleSign&&(this._worldScaleSign=this.getWorldTransform().scaleSign),this._worldScaleSign}remove(){var t;null==(t=this._parent)||t.removeChild(this)}reparent(t,i){this.remove(),t&&(i>=0?t.insertChild(this,i):t.addChild(this))}setLocalEulerAngles(t,i,e){this.localRotation.setFromEulerAngles(t,i,e),this._dirtyLocal||this._dirtifyLocal()}setLocalPosition(t,i,e){t instanceof s?this.localPosition.copy(t):this.localPosition.set(t,i,e),this._dirtyLocal||this._dirtifyLocal()}setLocalRotation(t,i,e,r){t instanceof o?this.localRotation.copy(t):this.localRotation.set(t,i,e,r),this._dirtyLocal||this._dirtifyLocal()}setLocalScale(t,i,e){t instanceof s?this.localScale.copy(t):this.localScale.set(t,i,e),this._dirtyLocal||this._dirtifyLocal()}_dirtifyLocal(){this._dirtyLocal||(this._dirtyLocal=!0,this._dirtyWorld||this._dirtifyWorld())}_unfreezeParentToRoot(){let t=this._parent;for(;t;)t._frozen=!1,t=t._parent}_dirtifyWorld(){this._dirtyWorld||this._unfreezeParentToRoot(),this._dirtifyWorldInternal()}_dirtifyWorldInternal(){if(!this._dirtyWorld){this._frozen=!1,this._dirtyWorld=!0;for(let t=0;t<this._children.length;t++)this._children[t]._dirtyWorld||this._children[t]._dirtifyWorldInternal()}this._dirtyNormal=!0,this._worldScaleSign=0,this._aabbVer++}setPosition(t,i,e){t instanceof s?p.copy(t):p.set(t,i,e),null===this._parent?this.localPosition.copy(p):(y.copy(this._parent.getWorldTransform()).invert(),y.transformPoint(p,this.localPosition)),this._dirtyLocal||this._dirtifyLocal()}setRotation(t,i,e,r){if(t instanceof o?m.copy(t):m.set(t,i,e,r),null===this._parent)this.localRotation.copy(m);else{const t=this._parent.getRotation();u.copy(t).invert(),this.localRotation.copy(u).mul(m)}this._dirtyLocal||this._dirtifyLocal()}setPositionAndRotation(t,i){if(null===this._parent)this.localPosition.copy(t),this.localRotation.copy(i);else{const e=this._parent.getWorldTransform();y.copy(e).invert(),y.transformPoint(t,this.localPosition),this.localRotation.setFromMat4(y).mul(i)}this._dirtyLocal||this._dirtifyLocal()}setEulerAngles(t,i,e){if(this.localRotation.setFromEulerAngles(t,i,e),null!==this._parent){const t=this._parent.getRotation();u.copy(t).invert(),this.localRotation.mul2(u,this.localRotation)}this._dirtyLocal||this._dirtifyLocal()}addChild(t){this._prepareInsertChild(t),this._children.push(t),this._onInsertChild(t)}addChildAndSaveTransform(t){const i=t.getPosition(),e=t.getRotation();this._prepareInsertChild(t),t.setPosition(_.copy(this.worldTransform).invert().transformPoint(i)),t.setRotation(f.copy(this.getRotation()).invert().mul(e)),this._children.push(t),this._onInsertChild(t)}insertChild(t,i){this._prepareInsertChild(t),this._children.splice(i,0,t),this._onInsertChild(t)}_prepareInsertChild(t){t.remove()}_fireOnHierarchy(t,i,e){this.fire(t,e);for(let t=0;t<this._children.length;t++)this._children[t]._fireOnHierarchy(i,i,e)}_onInsertChild(t){t._parent=this;const i=t._enabled&&this.enabled;t._enabledInHierarchy!==i&&(t._enabledInHierarchy=i,t._notifyHierarchyStateChanged(t,i)),t._updateGraphDepth(),t._dirtifyWorld(),this._frozen&&t._unfreezeParentToRoot(),t._fireOnHierarchy("insert","inserthierarchy",this),this.fire&&this.fire("childinsert",t)}_updateGraphDepth(){this._graphDepth=this._parent?this._parent._graphDepth+1:0;for(let t=0,i=this._children.length;t<i;t++)this._children[t]._updateGraphDepth()}removeChild(t){const i=this._children.indexOf(t);-1!==i&&(this._children.splice(i,1),t._parent=null,t._fireOnHierarchy("remove","removehierarchy",this),this.fire("childremove",t))}_sync(){if(this._dirtyLocal&&(this.localTransform.setTRS(this.localPosition,this.localRotation,this.localScale),this._dirtyLocal=!1),this._dirtyWorld){if(null===this._parent)this.worldTransform.copy(this.localTransform);else if(this.scaleCompensation){let t;const i=this._parent;let e=this.localScale,r=i;if(r){for(;r&&r.scaleCompensation;)r=r._parent;r&&(r=r._parent,r&&(t=r.worldTransform.getScale(),c.mul2(t,this.localScale),e=c))}h.setFromMat4(i.worldTransform),a.mul2(h,this.localRotation);let o=i.worldTransform;i.scaleCompensation&&(d.mul2(t,i.getLocalScale()),n.setTRS(i.worldTransform.getTranslation(l),h,d),o=n),o.transformPoint(this.localPosition,l),this.worldTransform.setTRS(l,a,e)}else this.worldTransform.mulAffine2(this._parent.worldTransform,this.localTransform);this._dirtyWorld=!1}}syncHierarchy(){if(!this._enabled)return;if(this._frozen)return;this._frozen=!0,(this._dirtyLocal||this._dirtyWorld)&&this._sync();const t=this._children;for(let i=0,e=t.length;i<e;i++)t[i].syncHierarchy()}lookAt(t,i,e,r=0,o=1,n=0){if(t instanceof s)w.copy(t),i instanceof s?T.copy(i):T.copy(s.UP);else{if(void 0===e)return;w.set(t,i,e),T.set(r,o,n)}g.setLookAt(this.getPosition(),w,T),m.setFromMat4(g),this.setRotation(m)}translate(t,i,e){t instanceof s?p.copy(t):p.set(t,i,e),p.add(this.getPosition()),this.setPosition(p)}translateLocal(t,i,e){t instanceof s?p.copy(t):p.set(t,i,e),this.localRotation.transformVector(p,p),this.localPosition.add(p),this._dirtyLocal||this._dirtifyLocal()}rotate(t,i,e){if(m.setFromEulerAngles(t,i,e),null===this._parent)this.localRotation.mul2(m,this.localRotation);else{const t=this.getRotation(),i=this._parent.getRotation();u.copy(i).invert(),m.mul2(u,m),this.localRotation.mul2(m,t)}this._dirtyLocal||this._dirtifyLocal()}rotateLocal(t,i,e){m.setFromEulerAngles(t,i,e),this.localRotation.mul(m),this._dirtyLocal||this._dirtifyLocal()}}export{S as GraphNode};