/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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{j as n}from"../../../../chunks/vec32.js";import{create as m}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{perObjectElevationAligner as l}from"./ElevationAligners.js";import{evaluateElevationInfoAtPoint as f}from"./elevationAlignmentUtils.js";import{Graphics3DObject3DGraphicLayer as d}from"./Graphics3DObject3DGraphicLayer.js";import{meshAutoFastUpdateConstants as c}from"./meshAutoFastUpdateConstants.js";class h extends d{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(g,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.map(e=>t(e.material));e.removeAllGeometries();for(let s=0;s<this._originalGeometries.length;s++){const t=this._originalGeometries[s],r=a[s];r.setParameters({modelTransformation:null}),r===t.material?e.addGeometry(t):e.addGeometry(t.instantiate({material:r}))}this._originalGeometries.length=0}updateFastLocalOrigin(e,a,s){if(!this._fastTransformUpdatesEnabled)return;const{stageObject:r}=this;if(0===r.geometries.length)return;const o=r.geometries[0].localOrigin,n=t(g,e),m=s.getOrigin(n);if(m===o)return;const l=a?.localMatrix??i;r.shaderTransformation=null,r.transformation=e,r.geometries.forEach(t=>{t.transformation=l,t.localOrigin=m})}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,f=t,d=n,c=e(p,m,l),h=e(u,f,d),g=e(b,h,a(b,l));o.shaderTransformation=g,this._setFastMaterialTransformation({matA:c,matB:h}),this._updateEdgeTransform(r)}alignWithElevation(t,a,r){if(!this._fastTransformUpdatesEnabled)return void super.alignWithElevation(t,a,r);const i=(e,s)=>f(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,d=e(p,n,m),c=o.effectiveTransformation,h=s(T,c);this.alignedSampledElevation=l(this,this.elevationContext,t.spatialReference,i,a,h),o.shaderTransformation=h;const g=o.geometries[0].transformation,b=e(u,h,g);this._setFastMaterialTransformation({matA:d,matB:b}),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(g,o.vec3,-1)),l=e(_,m,t),f=e(U,m,s),d=a(_,l),c=e(U,f,d);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 g=m(),p=o(),u=o(),T=o(),b=o(),_=o(),U=o(),E=o();export{h as Graphics3DMeshObject3DGraphicLayer};
