@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 9.02 kB
JavaScript
import{adjustStaticAGOUrl as e}from"../../../../core/devEnvironmentUtils.js";import{get as t,isSome as r,isNone as o}from"../../../../core/maybe.js";import{b as s}from"../../../../chunks/mat3.js";import{c as i}from"../../../../chunks/mat3f64.js";import{a as n}from"../../../../chunks/mat4.js";import{c as a}from"../../../../chunks/mat4f64.js";import{m as u,b as l,C as c,l as m,n as f,h as d}from"../../../../chunks/vec3.js";import{c as p}from"../../../../chunks/vec3f64.js";import{empty as g,expandWithVec3 as b}from"../../../../geometry/support/aaBoundingBox.js";import{BufferViewVec3f as x,BufferViewVec4f as h,BufferViewVec2f as T,BufferViewVec4u8 as R,BufferViewVec4u16 as w,BufferViewVec3u8 as j,BufferViewVec3u16 as M}from"../../../../geometry/support/buffer/BufferView.js";import{t as v,a as y,s as B}from"../../../../chunks/vec32.js";import{t as L,s as C}from"../../../../chunks/vec42.js";import{createBuffer as I}from"../../../../geometry/support/buffer/utils.js";import{DefaultLoadingContext as A}from"../../glTF/DefaultLoadingContext.js";import{loadGLTF as O}from"../../glTF/loader.js";import{triangleFanToTriangles as S,triangleStripToTriangles as E,trianglesToTriangles as N}from"../../glTF/internal/indexUtils.js";import{isEncodedMeshTexture as k}from"../../glTF/internal/resourceUtils.js";import{getTransformMatrix as F}from"../../glTF/internal/TextureTransformUtils.js";import{ProcessedObjectResource as P}from"./ProcessedObjectResource.js";import{load as V,processLoadResult as _}from"./wosrLoader.js";import{NormalAttributeType as G}from"../../webgl-engine/core/shaderLibrary/attributes/NormalAttribute.glsl.js";import{Attribute as D}from"../../webgl-engine/lib/Attribute.js";import{AlphaDiscardMode as U,DepthTestFunction as H,CullFaceOptions as q}from"../../webgl-engine/lib/basicInterfaces.js";import{Geometry as W}from"../../webgl-engine/lib/Geometry.js";import{Texture as $}from"../../webgl-engine/lib/Texture.js";import{VertexAttribute as z}from"../../webgl-engine/lib/VertexAttribute.js";import{DefaultMaterial as K}from"../../webgl-engine/materials/DefaultMaterial.js";import{COLOR_GAMMA as Q}from"../../webgl-engine/materials/DefaultMaterial_COLOR_GAMMA.js";import{PrimitiveType as J}from"../../../webgl/enums.js";import{n as X}from"../../../../chunks/vec22.js";import{c as Y,f as Z}from"../../../../chunks/vec43.js";import{c as ee}from"../../../../chunks/vec33.js";async function te(o,s){const i=re(e(o));if("wosr"===i.fileType){const e=await(s.cache?s.cache.loadWOSR(i.url,s):V(i.url,s)),{engineResources:t,referenceBoundingBox:r}=_(e,s);return{lods:t,referenceBoundingBox:r,isEsriSymbolResource:!1,isWosr:!0}}const n=await(s.cache?s.cache.loadGLTF(i.url,s,!!s.usePBR):O(new A(s.streamDataRequester),i.url,s,s.usePBR)),a=t(n.model.meta,"ESRI_proxyEllipsoid"),u=n.meta.isEsriSymbolResource&&r(a)&&n.meta.uri.includes("/RealisticTrees/");u&&!n.customMeta.esriTreeRendering&&(n.customMeta.esriTreeRendering=!0,le(n,a));const l=!!s.usePBR,c=n.meta.isEsriSymbolResource?{usePBR:l,isSchematic:!1,treeRendering:u,mrrFactors:[0,1,.2]}:{usePBR:l,isSchematic:!1,treeRendering:!1,mrrFactors:[0,1,.5]},m={...s.materialParamsMixin,treeRendering:u},{engineResources:f,referenceBoundingBox:d}=oe(n,c,m,s.skipHighLods&&null==i.specifiedLodIndex?{skipHighLods:!0}:{skipHighLods:!1,singleLodIndex:i.specifiedLodIndex});return{lods:f,referenceBoundingBox:d,isEsriSymbolResource:n.meta.isEsriSymbolResource,isWosr:!1}}function re(e){const t=e.match(/(.*\.(gltf|glb))(\?lod=([0-9]+))?$/);if(t)return{fileType:"gltf",url:t[1],specifiedLodIndex:null!=t[4]?Number(t[4]):null};return e.match(/(.*\.(json|json\.gz))$/)?{fileType:"wosr",url:e,specifiedLodIndex:null}:{fileType:"unknown",url:e,specifiedLodIndex:null}}function oe(e,t,o,s){const i=e.model,n=new Array,a=new Map,u=new Map,l=i.lods.length,c=g();return i.lods.forEach(((e,m)=>{const f=!0===s.skipHighLods&&(l>1&&0===m||l>3&&1===m)||!1===s.skipHighLods&&null!=s.singleLodIndex&&m!==s.singleLodIndex;if(f&&0!==m)return;const d=new P(e.name,e.lodThreshold,[0,0,0]);e.parts.forEach((e=>{const s=f?new K({}):se(i,e,d,t,o,a,u),{geometry:n,vertexCount:l}=ie(e,r(s)?s:new K({})),p=n.boundingInfo;r(p)&&0===m&&(b(c,p.bbMin),b(c,p.bbMax)),r(s)&&(d.stageResources.geometries.push(n),d.numberOfVertices+=l)})),f||n.push(d)})),{engineResources:n,referenceBoundingBox:c}}function se(e,t,s,i,n,a,u){const l=t.material+(t.attributes.normal?"_normal":"")+(t.attributes.color?"_color":"")+(t.attributes.texCoord0?"_texCoord0":"")+(t.attributes.tangent?"_tangent":""),c=e.materials.get(t.material),m=r(t.attributes.texCoord0),f=r(t.attributes.normal);if(o(c))return null;const d=ae(c.alphaMode);if(!a.has(l)){if(m){const t=(t,o=!1)=>{if(r(t)&&!u.has(t)){const s=e.textures.get(t);if(r(s)){const e=s.data;u.set(t,new $(k(e)?e.data:e,{...s.parameters,preMultiplyAlpha:!k(e)&&o,encoding:k(e)&&r(e.encoding)?e.encoding:void 0}))}}};t(c.textureColor,d!==U.Opaque),t(c.textureNormal),t(c.textureOcclusion),t(c.textureEmissive),t(c.textureMetallicRoughness)}const o=c.color[0]**(1/Q),s=c.color[1]**(1/Q),p=c.color[2]**(1/Q),g=c.emissiveFactor[0]**(1/Q),b=c.emissiveFactor[1]**(1/Q),x=c.emissiveFactor[2]**(1/Q),h=r(c.textureColor)&&m?u.get(c.textureColor):null;a.set(l,new K({...i,transparent:d===U.Blend,customDepthTest:H.Lequal,textureAlphaMode:d,textureAlphaCutoff:c.alphaCutoff,diffuse:[o,s,p],ambient:[o,s,p],opacity:c.opacity,doubleSided:c.doubleSided,doubleSidedType:"winding-order",cullFace:c.doubleSided?q.None:q.Back,hasVertexColors:!!t.attributes.color,hasVertexTangents:!!t.attributes.tangent,normalType:f?G.Attribute:G.ScreenDerivative,castShadows:!0,receiveSSAO:!0,textureId:r(h)?h.id:void 0,colorMixMode:c.colorMixMode,normalTextureId:r(c.textureNormal)&&m?u.get(c.textureNormal).id:void 0,textureAlphaPremultiplied:r(h)&&!!h.params.preMultiplyAlpha,occlusionTextureId:r(c.textureOcclusion)&&m?u.get(c.textureOcclusion).id:void 0,emissiveTextureId:r(c.textureEmissive)&&m?u.get(c.textureEmissive).id:void 0,metallicRoughnessTextureId:r(c.textureMetallicRoughness)&&m?u.get(c.textureMetallicRoughness).id:void 0,emissiveFactor:[g,b,x],mrrFactors:[c.metallicFactor,c.roughnessFactor,i.mrrFactors[2]],isSchematic:!1,colorTextureTransformMatrix:F(c.colorTextureTransform),normalTextureTransformMatrix:F(c.normalTextureTransform),occlusionTextureTransformMatrix:F(c.occlusionTextureTransform),emissiveTextureTransformMatrix:F(c.emissiveTextureTransform),metallicRoughnessTextureTransformMatrix:F(c.metallicRoughnessTextureTransform),...n}))}const p=a.get(l);if(s.stageResources.materials.push(p),m){const e=e=>{r(e)&&s.stageResources.textures.push(u.get(e))};e(c.textureColor),e(c.textureNormal),e(c.textureOcclusion),e(c.textureEmissive),e(c.textureMetallicRoughness)}return p}function ie(e,t){const o=e.attributes.position.count,i=ue(e.indices||o,e.primitiveType),n=I(x,o);v(n,e.attributes.position,e.transform);const a=[[z.POSITION,new D(n.typedBuffer,n.elementCount,!0)]],u=[[z.POSITION,i]];if(r(e.attributes.normal)){const t=I(x,o);s(ne,e.transform),y(t,e.attributes.normal,ne),a.push([z.NORMAL,new D(t.typedBuffer,t.elementCount,!0)]),u.push([z.NORMAL,i])}if(r(e.attributes.tangent)){const t=I(h,o);s(ne,e.transform),L(t,e.attributes.tangent,ne),a.push([z.TANGENT,new D(t.typedBuffer,t.elementCount,!0)]),u.push([z.TANGENT,i])}if(r(e.attributes.texCoord0)){const t=I(T,o);X(t,e.attributes.texCoord0),a.push([z.UV0,new D(t.typedBuffer,t.elementCount,!0)]),u.push([z.UV0,i])}if(r(e.attributes.color)){const t=I(R,o);if(4===e.attributes.color.elementCount)e.attributes.color instanceof h?C(t,e.attributes.color,255):e.attributes.color instanceof R?Y(t,e.attributes.color):e.attributes.color instanceof w&&C(t,e.attributes.color,1/256);else{Z(t,255,255,255,255);const r=new j(t.buffer,0,4);e.attributes.color instanceof x?B(r,e.attributes.color,255):e.attributes.color instanceof j?ee(r,e.attributes.color):e.attributes.color instanceof M&&B(r,e.attributes.color,1/256)}a.push([z.COLOR,new D(t.typedBuffer,t.elementCount,!0)]),u.push([z.COLOR,i])}return{geometry:new W(t,a,u),vertexCount:o}}const ne=i();function ae(e){switch(e){case"BLEND":return U.Blend;case"MASK":return U.Mask;case"OPAQUE":case null:case void 0:return U.Opaque}}function ue(e,t){switch(t){case J.TRIANGLES:return N(e);case J.TRIANGLE_STRIP:return E(e);case J.TRIANGLE_FAN:return S(e)}}function le(e,t){for(let r=0;r<e.model.lods.length;++r){const s=e.model.lods[r];for(const i of s.parts){const s=i.attributes.normal;if(o(s))return;const g=i.attributes.position,b=g.count,h=p(),T=p(),w=p(),j=I(R,b),M=I(x,b),v=n(a(),i.transform);for(let o=0;o<b;o++){g.getVec(o,T),s.getVec(o,h),u(T,T,i.transform),l(w,T,t.center),c(w,w,t.radius);const n=w[2],a=m(w),p=Math.min(.45+.55*a*a,1);c(w,w,t.radius),null!==v&&u(w,w,v),f(w,w),r+1!==e.model.lods.length&&e.model.lods.length>1&&d(w,w,h,n>-1?.2:Math.min(-4*n-3.8,1)),M.setVec(o,w),j.set(o,0,255*p),j.set(o,1,255*p),j.set(o,2,255*p),j.set(o,3,255)}i.attributes.normal=M,i.attributes.color=j}}}export{te as fetch,oe as gltfToEngineResources,re as parseUrl};