@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 5.36 kB
JavaScript
import{lerp as t,clamp as e}from"../../../core/mathUtils.js";import{isSome as i}from"../../../core/maybe.js";import{d as n,i as o,r as a,n as r,o as l}from"../../../chunks/mat4.js";import{c}from"../../../chunks/mat4f64.js";import{s,h as u,n as m,g as d,m as g,a as h}from"../../../chunks/vec3.js";import{f as b,c as f,a as y}from"../../../chunks/vec3f64.js";import{ViewingMode as p}from"../../ViewingMode.js";import{S as A}from"../../../chunks/SunCalc.js";import{angle as w}from"./mathUtils.js";const N={local:{altitude:1500,ambientAtNight:.1,ambientAtNoon:.45,ambientAtTwilight:.2,directAtNoon:.65,directAtTwilight:.7},global:{altitude:8e5,ambient:.015,direct:.75},planarDirection:{localAltitude:1e4,globalAltitude:1e6,globalAngles:{azimuth:1.3*Math.PI,altitude:.6*Math.PI}}};function T(n,o,a,r,l,c){s(c.ambient.color,1,1,1),c.ambient.intensity=N.global.ambient,s(c.direct.color,1,1,1),c.direct.intensity=N.global.direct;const m=o[2],d=e((Math.abs(m)-N.local.altitude)/(N.global.altitude-N.local.altitude),0,1);let g;if(c.globalFactor=d,i(n)&&(g=A.getTimes(n,o[1],o[0])),d<1){let e;if(i(n))e=G(n,g,r);else{const t=E(r);e={direct:{intensity:N.local.directAtNoon*t.direct,color:b(1,1,1)},ambient:{intensity:N.local.ambientAtNoon*t.ambient,color:b(1,1,1)},timeOfDay:"early afternoon"}}u(c.ambient.color,e.ambient.color,c.ambient.color,d),c.ambient.intensity=t(e.ambient.intensity,c.ambient.intensity,d),u(c.direct.color,e.direct.color,c.direct.color,d),c.direct.intensity=t(e.direct.intensity,c.direct.intensity,d),c.specularStrength="rainy"===r||"snowy"===r||"foggy"===r?0:1,c.environmentStrength="rainy"===r?.7:"snowy"===r||"foggy"===r?.75:1}c.noonFactor=i(n)?D(n,g):1,i(n)?v(n,o,a,c.direct.directionToLightSource):M(l,a,c.direct.directionToLightSource)}function M(t,e,i){e===p.Global?m(L,t.eye):s(L,0,0,1),d(R,t.viewForward,-1);const o=w(R,L),a=Math.max(o-2*_,0),r=.85*a/(a+1),l=Math.max(_,o-_-r);n(F,-l,t.viewRight),g(i,R,F),h(i,i,d(U,t.viewRight,C)),m(i,i)}function v(t,i,n,c){const u=z,m=o(j);if(n===p.Global)A.getPosition(t,0,0,u),s(c,0,0,-1),a(m,m,-u.azimuth),r(m,m,-u.altitude),g(c,c,m);else{const n=N.planarDirection,o=n.globalAngles,r=i[2];let d=(Math.abs(r)-n.localAltitude)/(n.globalAltitude-n.localAltitude);d=e(d,0,1),d<1?(A.getPosition(t,i[1],i[0],u),u.azimuth=(1-d)*u.azimuth+d*o.azimuth,u.altitude=(1-d)*u.altitude+d*o.altitude):(u.azimuth=o.azimuth,u.altitude=o.altitude),s(c,0,-1,0),l(m,m,-u.azimuth),a(m,m,-u.altitude),g(c,c,m)}}function x(t,e){if(e===p.Global)return!0;const i=N.planarDirection;return Math.abs(t)<i.localAltitude}function S(t,e,i,n,o){const a=t.getTime(),r=e.getTime()-a,l=Math.floor(r/i)+1,c=new Array(l);for(let s=0;s<l;++s)k.setTime(a+i*s),c[s]=f(),v(k,n,o,c[s]);return c}const O=b(.5773502691896258,-.5773502691896258,.5773502691896258);class P{constructor(){this.ambient={color:b(1,1,1),intensity:.55},this.direct={color:b(1,1,1),intensity:.55,directionToLightSource:y(O)},this.noonFactor=.5,this.globalFactor=0,this.specularStrength=1,this.environmentStrength=1}}const j=c(),z={azimuth:0,altitude:0};function D(t,i){const n=t.valueOf();let o,a;i.polarException===A.PolarException.MIDNIGHT_SUN?(o=n-60*(t.getHours()+48)*60*1e3-60*t.getMinutes()*1e3,a=o+432e6):i.polarException===A.PolarException.POLAR_NIGHT?(o=n-2,a=n-1):(o=i.sunrise.valueOf(),a=i.sunset.valueOf());const r=o+(a-o)/2;return 1-e(Math.abs(n-r)/432e5,0,1)}function E(t){switch(t){case"disabled":case"sunny":case"cloudy":return{direct:1,ambient:1};case"rainy":return{direct:.4,ambient:1.2};case"snowy":return{direct:.5,ambient:1.3};case"foggy":return{direct:.2,ambient:1.6}}}function I(t,e){const i=(t[0]+t[1]+t[2])/3;for(let n=0;n<3;n++)t[n]=t[n]+(i-t[n])*e;return t}function G(t,e,i){const n=t.valueOf();let o,a;e.polarException===A.PolarException.MIDNIGHT_SUN?(o=n-60*(t.getHours()+48)*60*1e3-60*t.getMinutes()*1e3,a=o+432e6):e.polarException===A.PolarException.POLAR_NIGHT?(o=n-2,a=n-1):(o=e.sunrise.valueOf(),a=e.sunset.valueOf());const r=a-o,l=o+r/2,c=r/4,s=l-c,u=l+c,m=.06*r,d=o-m/2,g=o+m/2,h=a-m/2,f=a+m/2,y=N.local,p=[.01,y.ambientAtNight],w=[.8,.8,1],T=[.01,.01,.01],M=[y.directAtTwilight,y.ambientAtTwilight],v=[1,.6,.5],x=[.8,.8,1],S=[.9*y.directAtNoon,y.ambientAtNoon],O=[1,.98,.98],P=[.98,.98,1],j=[y.directAtNoon,y.ambientAtNoon],z=[1,1,1],D=[1,1,1],G=S,k=O,F=P,L=M,R=v,U=x;let _,C,V=[0,0],q=[0,0,0],B=[0,0,0];n<d||n>f?(V=p,q=T,B=w,C="night"):n<g?(_=g-d,V=H(n-d,_,p,M),q=H(n-d,_,T,v),B=H(n-d,_,w,x),C="sun rising"):n<s?(_=s-g,V=H(n-g,_,M,S),q=H(n-g,_,v,O),B=H(n-g,_,x,P),C="early morning"):n<l?(_=l-s,V=H(n-s,_,S,j),q=H(n-s,_,O,z),B=H(n-s,_,P,D),C="late morning"):n<u?(_=u-l,V=H(n-l,_,j,G),q=H(n-l,_,z,k),B=H(n-l,_,D,F),C="early afternoon"):n<h?(_=h-u,V=H(n-u,_,G,L),q=H(n-u,_,k,R),B=H(n-u,_,F,U),C="late afternoon"):n<f&&(_=f-h,V=H(n-h,_,L,p),q=H(n-h,_,R,T),B=H(n-h,_,U,w),C="sun setting");let J=0;switch(i){case"rainy":case"foggy":case"snowy":J=.5}J>0&&(q=I(q,J),B=I(B,J));const K=b(q[0],q[1],q[2]),Q=b(B[0],B[1],B[2]),W=E(i);return{direct:{intensity:V[0]*W.direct,color:K},ambient:{intensity:V[1]*W.ambient,color:Q},timeOfDay:C}}function H(t,e,i,n){const o=[];for(let a=0;a<i.length;a++)o[a]=(n[a]-i[a])*t/e+i[a];return o}const k=new Date(0),F=c(),L=f(),R=f(),U=f(),_=.25,C=.2;export{P as ColorAndIntensity,T as computeColorAndIntensity,S as computeDirectionsOverTime,x as computeShadowsEnabled,M as computeVirtualLightDirection};