@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 4.68 kB
JavaScript
import{LAYERID_WORLD as e}from"../../../scene/constants.js";import{Asset as s}from"../../asset/asset.js";import{AssetReference as t}from"../../asset/asset-reference.js";import{Component as n}from"../component.js";class a extends n{constructor(s,n){super(s,n),this._layers=[e],this._instance=null,this._customAabb=null,this._assetReference=void 0,this._materialOptions=null,this._evtLayersChanged=null,this._evtLayerAdded=null,this._evtLayerRemoved=null,this._assetReference=new t("asset",this,s.app.assets,{add:this._onGSplatAssetAdded,load:this._onGSplatAssetLoad,remove:this._onGSplatAssetRemove,unload:this._onGSplatAssetUnload},this),n.on("remove",this.onRemoveChild,this),n.on("removehierarchy",this.onRemoveChild,this),n.on("insert",this.onInsertChild,this),n.on("inserthierarchy",this.onInsertChild,this)}set customAabb(e){var s;this._customAabb=e,null==(s=this._instance)||null==(s=s.meshInstance)||s.setCustomAabb(this._customAabb)}get customAabb(){return this._customAabb}set instance(e){var s;if(this.destroyInstance(),this._instance=e,null!=(s=this._instance)&&s.meshInstance){const e=this._instance.meshInstance;e.node||(e.node=this.entity),e.setCustomAabb(this._customAabb),this._materialOptions&&this._instance.createMaterial(this._materialOptions),this.enabled&&this.entity.enabled&&this.addToLayers()}}get instance(){return this._instance}set materialOptions(e){this._materialOptions=Object.assign({},e),this._instance&&this._instance.createMaterial(this._materialOptions)}get materialOptions(){return this._materialOptions}get material(){var e;return null==(e=this._instance)?void 0:e.material}set layers(e){this.removeFromLayers(),this._layers.length=0;for(let s=0;s<e.length;s++)this._layers[s]=e[s];this.enabled&&this.entity.enabled&&this.addToLayers()}get layers(){return this._layers}set asset(e){const t=e instanceof s?e.id:e;this._assetReference.id!==t&&(this._assetReference.asset&&this._assetReference.asset.resource&&this._onGSplatAssetRemove(),this._assetReference.id=t,this._assetReference.asset&&this._onGSplatAssetAdded())}get asset(){return this._assetReference.id}assignAsset(e){const t=e instanceof s?e.id:e;this._assetReference.id=t}destroyInstance(){var e;this._instance&&(this.removeFromLayers(),null==(e=this._instance)||e.destroy(),this._instance=null)}addToLayers(){var e;const s=null==(e=this.instance)?void 0:e.meshInstance;if(s){const e=this.system.app.scene.layers;for(let n=0;n<this._layers.length;n++){var t;null==(t=e.getLayerById(this._layers[n]))||t.addMeshInstances([s])}}}removeFromLayers(){var e;const s=null==(e=this.instance)?void 0:e.meshInstance;if(s){const e=this.system.app.scene.layers;for(let n=0;n<this._layers.length;n++){var t;null==(t=e.getLayerById(this._layers[n]))||t.removeMeshInstances([s])}}}onRemoveChild(){this.removeFromLayers()}onInsertChild(){this._instance&&this.enabled&&this.entity.enabled&&this.addToLayers()}onRemove(){this.destroyInstance(),this.asset=null,this._assetReference.id=null,this.entity.off("remove",this.onRemoveChild,this),this.entity.off("insert",this.onInsertChild,this)}onLayersChanged(e,s){this.addToLayers(),e.off("add",this.onLayerAdded,this),e.off("remove",this.onLayerRemoved,this),s.on("add",this.onLayerAdded,this),s.on("remove",this.onLayerRemoved,this)}onLayerAdded(e){this.layers.indexOf(e.id)<0||this._instance&&e.addMeshInstances(this._instance.meshInstance)}onLayerRemoved(e){this.layers.indexOf(e.id)<0||this._instance&&e.removeMeshInstances(this._instance.meshInstance)}onEnable(){const e=this.system.app.scene,s=e.layers;this._evtLayersChanged=e.on("set:layers",this.onLayersChanged,this),s&&(this._evtLayerAdded=s.on("add",this.onLayerAdded,this),this._evtLayerRemoved=s.on("remove",this.onLayerRemoved,this)),this._instance?this.addToLayers():this.asset&&this._onGSplatAssetAdded()}onDisable(){var e;const s=this.system.app.scene.layers;var t,n;(null==(e=this._evtLayersChanged)||e.off(),this._evtLayersChanged=null,s)&&(null==(t=this._evtLayerAdded)||t.off(),this._evtLayerAdded=null,null==(n=this._evtLayerRemoved)||n.off(),this._evtLayerRemoved=null);this.removeFromLayers()}hide(){this._instance&&(this._instance.meshInstance.visible=!1)}show(){this._instance&&(this._instance.meshInstance.visible=!0)}_onGSplatAssetAdded(){this._assetReference.asset&&(this._assetReference.asset.resource?this._onGSplatAssetLoad():this.enabled&&this.entity.enabled&&this.system.app.assets.load(this._assetReference.asset))}_onGSplatAssetLoad(){this.destroyInstance();const e=this._assetReference.asset;e&&(this.instance=e.resource.createInstance())}_onGSplatAssetUnload(){this.destroyInstance()}_onGSplatAssetRemove(){this._onGSplatAssetUnload()}}export{a as GSplatComponent};