@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 3.17 kB
JavaScript
import{AnimBinder as t}from"./anim-binder.js";import{AnimTarget as e}from"../evaluator/anim-target.js";import{Entity as n}from"../../entity.js";class i{constructor(t){if(this._isPathInMask=(t,e)=>{const n=this._mask[t];return!!n&&!!(n.children||e&&!1!==n.value)},this.graph=t,!t)return;this._mask=null;const e={};!function t(n){e[n.name]=n;for(let e=0;e<n.children.length;++e)t(n.children[e])}(t),this.nodes=e,this.targetCache={};const r=function(t){let e,i=t;for(;i&&!(i instanceof n);)i=i.parent;return i&&(i.render?e=i.render.meshInstances:i.model&&(e=i.model.meshInstances)),e};this.nodeCounts={},this.activeNodes=[],this.handlers={localPosition:function(t){const e=t.localPosition;return i.createAnimTarget((function(t){e.set(...t)}),"vector",3,t,"localPosition")},localRotation:function(t){const e=t.localRotation;return i.createAnimTarget((function(t){e.set(...t)}),"quaternion",4,t,"localRotation")},localScale:function(t){const e=t.localScale;return i.createAnimTarget((function(t){e.set(...t)}),"vector",3,t,"localScale")},weight:function(t,e){e=0===e.indexOf("name.")?e.replace("name.",""):Number(e);const n=r(t);let a;if(n)for(let i=0;i<n.length;++i)if(n[i].node.name===t.name&&n[i].morphInstance){const t=n[i].morphInstance,r=n=>{t.setWeight(e,n[0])};a||(a=[]),a.push(r)}if(a){const n=t=>{for(let e=0;e<a.length;++e)a[e](t)};return i.createAnimTarget(n,"number",1,t,`weight.${e}`)}return null},materialTexture:(t,e)=>{const n=r(t);if(n){let r;for(let e=0;e<n.length;++e)if(n[e].node.name===t.name){r=n[e];break}if(r){const n=t=>{const n=this.animComponent.system.app.assets.get(t[0]);n&&n.resource&&"texture"===n.type&&(r.material[e]=n.resource,r.material.update())};return i.createAnimTarget(n,"vector",1,t,"materialTexture","material")}}return null}}}_isPathActive(t){if(!this._mask)return!0;const e=[t.entityPath[0],this.graph.name];for(let n=0;n<e.length;++n){let i=e[n];if(this._isPathInMask(i,1===t.entityPath.length))return!0;for(let e=1;e<t.entityPath.length;e++)if(i+=`/${t.entityPath[e]}`,this._isPathInMask(i,e===t.entityPath.length-1))return!0}return!1}findNode(t){if(!this._isPathActive(t))return null;let e;return this.graph&&(e=this.graph.findByPath(t.entityPath),e||(e=this.graph.findByPath(t.entityPath.slice(1)))),e||(e=this.nodes[t.entityPath[t.entityPath.length-1]||""]),e}static createAnimTarget(n,i,r,a,s,o){const h=t.encode(a.path,o||"entity",s);return new e(n,i,r,h)}resolve(e){const n=t.encode(e.entityPath,e.component,e.propertyPath);let i=this.targetCache[n];if(i)return i;const r=this.findNode(e);if(!r)return null;const a=this.handlers[e.propertyPath];return a?(i=a(r),i?(this.targetCache[n]=i,this.nodeCounts[r.path]?this.nodeCounts[r.path]++:(this.activeNodes.push(r),this.nodeCounts[r.path]=1),i):null):null}unresolve(t){if("graph"!==t.component)return;const e=this.nodes[t.entityPath[t.entityPath.length-1]||""];if(this.nodeCounts[e.path]--,0===this.nodeCounts[e.path]){const t=this.activeNodes,n=t.indexOf(e.node),i=t.length;n<i-1&&(t[n]=t[i-1]),t.pop()}}update(t){const e=this.activeNodes;for(let t=0;t<e.length;++t)e[t]._dirtifyLocal()}assignMask(t){return t!==this._mask&&(this._mask=t,!0)}}export{i as DefaultAnimBinder};