/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{getTranslation as t,multiply as e,invert as a,copy as s,fromTranslation as r}from"../../../../core/libs/gl-matrix-2/math/mat4.js";import{IDENTITY as i,create as o}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{scale as n}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as m}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{perObjectElevationAligner as l}from"./ElevationAligners.js";import{evaluateElevationInfoAtPoint as d}from"./elevationAlignmentUtils.js";import{Graphics3DObject3DGraphicLayer as f}from"./Graphics3DObject3DGraphicLayer.js";import{meshAutoFastUpdateConstants as c}from"./meshAutoFastUpdateConstants.js";class g extends f{constructor(){super(...arguments),this.fastTransformUpdatesAllowed=!1,this._originalGeometries=[],this._fastTransformUpdatesEnabled=!1,this._autoDisableFastUpdatesTimeoutId=0}get fastTransformUpdatesEnabled(){return this._fastTransformUpdatesEnabled}destroy(){super.destroy(),this._cancelAutoDisableFastUpdates()}enableFastTransformUpdates(e,a){if(!this.fastTransformUpdatesAllowed||this._fastTransformUpdatesEnabled)return;this._cancelAutoDisableFastUpdates(),this._fastTransformUpdatesEnabled=!0;const{stageObject:s}=this,r=s.geometries.slice();s.removeAllGeometries();const i=t(h,s.transformation),o=a.getOrigin(i);for(const t of r){const a=e(t.material),r=t.instantiate({material:a});r.localOrigin=o,s.addGeometry(r)}this._originalGeometries=r}autoDisableFastTransformUpdates(t){this._cancelAutoDisableFastUpdates(),this._autoDisableFastUpdatesTimeoutId=setTimeout(()=>{this._autoDisableFastUpdatesTimeoutId=0,t()},c.disableDelayMs)}updateAutoDisableFastTransformUpdates(t){this._autoDisableFastUpdatesTimeoutId&&this.autoDisableFastTransformUpdates(t)}_cancelAutoDisableFastUpdates(){clearTimeout(this._autoDisableFastUpdatesTimeoutId),this._autoDisableFastUpdatesTimeoutId=0}disableFastTransformUpdates(t){if(!this._fastTransformUpdatesEnabled)return;this._cancelAutoDisableFastUpdates(),this._fastTransformUpdatesEnabled=!1;const{stageObject:e}=this,a=e.geometries.length>0?e.geometries[0].localOrigin:void 0,s=e.geometries.map(e=>t(e.material));e.removeAllGeometries();for(let r=0;r<this._originalGeometries.length;r++){const t=this._originalGeometries[r],i=s[r];i.setParameters({modelTransformation:null}),null!=t.localOrigin&&(t.localOrigin=a),i===t.material?e.addGeometry(t):e.addGeometry(t.instantiate({material:i}))}this._originalGeometries.length=0}updateFastLocalOrigin(e,a,s,r=!0){if(!this._fastTransformUpdatesEnabled)return;const{stageObject:o}=this;if(0===o.geometries.length)return;const n=o.geometries[0].localOrigin,m=t(h,e);if(n&&!r&&!s.needsOriginUpdate(n,m,2*o.boundingVolumeWorldSpace.bounds.radius))return;const l=s.getOrigin(m);if(l===n)return;const d=a?.localMatrix??i;o.shaderTransformation=null,o.transformation=e,o.geometries.forEach(t=>{t.transformation=d,t.localOrigin=l})}updateTransform(t,s,r){const{stageObject:o}=this,n=s?.localMatrix??i;if(!this._fastTransformUpdatesEnabled)return o.shaderTransformation=null,o.transformation=t,o.geometries.forEach(t=>t.transformation=n),void this._updateEdgeTransform(r);const m=o.transformation,l=o.geometries[0].transformation,d=t,f=n,c=e(p,m,l),g=e(u,d,f),h=e(T,g,a(T,l));o.shaderTransformation=h,this._setFastMaterialTransformation({matA:c,matB:g}),this._updateEdgeTransform(r)}alignWithElevation(t,a,r){if(!this._fastTransformUpdatesEnabled)return void super.alignWithElevation(t,a,r);const i=(e,s)=>d(e,t,this.elevationContext,a,s),{stageObject:o}=this;if(!o.geometries[0].material.parameters.modelTransformation)return;const n=o.transformation,m=o.geometries[0].transformation,f=e(p,n,m),c=o.effectiveTransformation,g=s(b,c);this.alignedSampledElevation=l(this,this.elevationContext,t.spatialReference,i,a,g),o.shaderTransformation=g;const h=o.geometries[0].transformation,T=e(u,g,h);this._setFastMaterialTransformation({matA:f,matB:T}),this._updateEdgeTransform(r)}_setFastMaterialTransformation({matA:t,matB:s}){const{stageObject:i}=this;if(0===i.geometries.length)return;const o=i.geometries[0].localOrigin,m=r(E,n(h,o.vec3,-1)),l=e(_,m,t),d=e(U,m,s),f=a(_,l),c=e(U,d,f);for(const e of i.geometries)e.material.setParameters({modelTransformation:c})}_updateEdgeTransform(t){const{stageObject:e,_stageLayer:a}=this;a.stage.renderer.withEdgeView(a=>{a.fastUpdateObject3DEdgesTransform(e)||this.resetEdgeObject(t)})}}const h=m(),p=o(),u=o(),b=o(),T=o(),_=o(),U=o(),E=o();export{g as Graphics3DMeshObject3DGraphicLayer};
