/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import has from"../../../../../core/has.js";import{translate as e}from"../../../../../core/libs/gl-matrix-2/math/mat4.js";import{create as s}from"../../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{i,p as a,e as l,h as c}from"../../../../../chunks/vec32.js";import{ZEROS as n,create as o}from"../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{Float3DrawUniform as r}from"../shaderModules/Float3DrawUniform.js";import{Float3PassUniform as t}from"../shaderModules/Float3PassUniform.js";import{glsl as f}from"../shaderModules/glsl.js";import{NoParameters as d}from"../../../../webgl/NoParameters.js";class u extends d{constructor(e){super(),this.slicePlaneLocalOrigin=e}}function p(e,s){w(e,s,new t("slicePlaneOrigin",(e,i)=>g(s,e,i)),new t("slicePlaneBasis1",(e,i)=>F(s,e,i,i.slicePlane?.basis1)),new t("slicePlaneBasis2",(e,i)=>F(s,e,i,i.slicePlane?.basis2)))}function P(e,s){w(e,s,new r("slicePlaneOrigin",(e,i)=>g(s,e,i)),new r("slicePlaneBasis1",(e,i)=>F(s,e,i,i.slicePlane?.basis1)),new r("slicePlaneBasis2",(e,i)=>F(s,e,i,i.slicePlane?.basis2)))}function m(e,s){v(e,s,new r("slicePlaneOrigin",(e,i)=>g(s,e,i)),new r("slicePlaneBasis1",(e,i)=>F(s,e,i,i.slicePlane?.basis1)),new r("slicePlaneBasis2",(e,i)=>F(s,e,i,i.slicePlane?.basis2)))}const b=f`struct SliceFactors {
float front;
float side0;
float side1;
float side2;
float side3;
};
SliceFactors calculateSliceFactors(vec3 pos) {
vec3 rel = pos - slicePlaneOrigin;
vec3 slicePlaneNormal = -cross(slicePlaneBasis1, slicePlaneBasis2);
float slicePlaneW = -dot(slicePlaneNormal, slicePlaneOrigin);
float basis1Len2 = dot(slicePlaneBasis1, slicePlaneBasis1);
float basis2Len2 = dot(slicePlaneBasis2, slicePlaneBasis2);
float basis1Dot = dot(slicePlaneBasis1, rel);
float basis2Dot = dot(slicePlaneBasis2, rel);
return SliceFactors(
dot(slicePlaneNormal, pos) + slicePlaneW,
-basis1Dot - basis1Len2,
basis1Dot - basis1Len2,
-basis2Dot - basis2Len2,
basis2Dot - basis2Len2
);
}
bool sliceByFactors(SliceFactors factors) {
return factors.front < 0.0
&& factors.side0 < 0.0
&& factors.side1 < 0.0
&& factors.side2 < 0.0
&& factors.side3 < 0.0;
}
bool sliceEnabled() {
return dot(slicePlaneBasis1, slicePlaneBasis1) != 0.0;
}
bool rejectBySlice(vec3 pos) {
return sliceEnabled() && sliceByFactors(calculateSliceFactors(pos));
}`;function v(e,s,...i){s.hasSlicePlane?(e.uniforms.add(...i),e.code.add(b)):e.code.add("bool rejectBySlice(vec3 pos) { return false; }")}function w(e,s,...i){e.constants.add("groundSliceOpacity","float",has("enable-feature:oit-ground")?.25:.2),v(e,s,...i),s.hasSlicePlane?e.code.add("\n    void discardBySlice(vec3 pos) {\n      if (rejectBySlice(pos)) {\n        discard;\n      }\n    }\n\n    vec4 applySliceOutline(vec4 color, vec3 pos) {\n      SliceFactors factors = calculateSliceFactors(pos);\n\n      factors.front /= 2.0 * fwidth(factors.front);\n      factors.side0 /= 2.0 * fwidth(factors.side0);\n      factors.side1 /= 2.0 * fwidth(factors.side1);\n      factors.side2 /= 2.0 * fwidth(factors.side2);\n      factors.side3 /= 2.0 * fwidth(factors.side3);\n\n      // return after calling fwidth, to avoid aliasing caused by discontinuities in the input to fwidth\n      if (sliceByFactors(factors)) {\n        return color;\n      }\n\n      float outlineFactor = (1.0 - step(0.5, factors.front))\n        * (1.0 - step(0.5, factors.side0))\n        * (1.0 - step(0.5, factors.side1))\n        * (1.0 - step(0.5, factors.side2))\n        * (1.0 - step(0.5, factors.side3));\n\n      return mix(color, vec4(vec3(0.0), color.a), outlineFactor * 0.3);\n    }\n\n    vec4 applySlice(vec4 color, vec3 pos) {\n      return sliceEnabled() ? applySliceOutline(color, pos) : color;\n    }\n  "):e.code.add(f`void discardBySlice(vec3 pos) { }
vec4 applySlice(vec4 color, vec3 pos) { return color; }`)}function B(e,s,i){return e.instancedDoublePrecision?c(y,i.camera.viewInverseTransposeMatrix[3],i.camera.viewInverseTransposeMatrix[7],i.camera.viewInverseTransposeMatrix[11]):s.slicePlaneLocalOrigin}function S(e,s){return null!=e?l(j,s.origin,e):s.origin}function h(s,i,a){return s.hasSliceTranslatedView?null!=i?e(O,a.camera.viewMatrix,i):a.camera.viewMatrix:null}function g(e,s,i){if(null==i.slicePlane)return n;const l=B(e,s,i),c=S(l,i.slicePlane),o=h(e,l,i);return null!=o?a(j,c,o):c}function F(e,s,c,o){if(null==o||null==c.slicePlane)return n;const r=B(e,s,c),t=S(r,c.slicePlane),f=h(e,r,c);return null!=f?(i(x,o,t),a(j,t,f),a(x,x,f),l(x,x,j)):o}const y=o(),j=o(),x=o(),O=s();export{m as RejectBySlice,P as SliceDraw,p as SlicePass,u as SlicePlaneParameters};
