@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 6.84 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import e from"../../../Color.js";import{colorGamma as t}from"../../../colorUtils.js";import r from"../../../request.js";import o from"../../../core/Error.js";import{getOrCreateMapValue as n}from"../../../core/MapUtils.js";import{hasScaling as s,rad2deg as i}from"../../../core/mathUtils.js";import{normalFromMat4 as a,fromMat4 as l}from"../../../core/libs/gl-matrix-2/math/mat3.js";import{create as c}from"../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{fromValues as u}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{fromValues as f}from"../../../core/libs/gl-matrix-2/factories/vec4f64.js";import m from"../MeshComponent.js";import p from"../MeshMaterialMetallicRoughness.js";import d from"../MeshTexture.js";import x from"../MeshTextureTransform.js";import{MeshVertexAttributes as g}from"../MeshVertexAttributes.js";import{selectVertexSpace as T}from"../meshVertexSpaceUtils.js";import{BufferViewVec4f as h,BufferViewVec4u8 as v,BufferViewVec4u16 as w,BufferViewVec3u8 as j,BufferViewVec3f as b,BufferViewVec3u16 as y,BufferViewVec3f64 as C,BufferViewVec2f as M}from"../buffer/BufferView.js";import{d as R,c as A,e as E,l as B}from"../../../chunks/vec3.js";import{a as F,n as $,l as S}from"../../../chunks/vec4.js";import{createBuffer as U}from"../buffer/utils.js";import{convertVertexSpace as k}from"./vertexSpaceConversion.js";import{DefaultLoadingContext as V}from"../../../views/3d/glTF/DefaultLoadingContext.js";import{loadGLTF as D}from"../../../views/3d/glTF/loader.js";import{convertPrimitiveToTriangles as I}from"../../../views/3d/glTF/internal/indexUtils.js";import{isEncodedMeshTexture as L}from"../../../views/3d/glTF/internal/resourceUtils.js";import{TextureWrapMode as O}from"../../../views/webgl/enums.js";import{f as P}from"../../../chunks/vec33.js";import{f as q}from"../../../chunks/vec43.js";import{n as _,f as z}from"../../../chunks/vec2.js";async function G(e,t,r){const n=new V(N(r)),s=(await D(n,t,r,!0)).model,i=s.lods.shift(),a=new Map,l=new Map;s.textures.forEach(((e,t)=>a.set(t,J(e)))),s.materials.forEach(((e,t)=>l.set(t,W(e,a))));const c=H(i);for(const o of c.parts)X(c,o,l);const{position:u,normal:f,tangent:m,color:p,texCoord0:d}=c.vertexAttributes,x=T(e,r),h=e.spatialReference.isGeographic?T(e):x,v=k({vertexAttributes:{position:u.typedBuffer,normal:f?.typedBuffer,tangent:m?.typedBuffer},vertexSpace:h,spatialReference:e.spatialReference},x,{allowBufferReuse:!0,sourceUnit:r?.unitConversionDisabled?void 0:"meters"});if(!v)throw new o("load-gltf-mesh:vertex-space-projection",`Failed to load mesh from glTF because we could not convert the vertex space from ${h.type} to ${x.type}`);return{transform:null,vertexSpace:x,components:c.components,spatialReference:e.spatialReference,vertexAttributes:new g({...v,color:p?.typedBuffer,uv:d?.typedBuffer})}}function N(e){const t=e?.resolveFile;return t?{busy:!1,request:async(e,o,n)=>{const s=t?.(e)??e,i="image"===o?"image":"binary"===o||"image+type"===o?"array-buffer":"json";return(await r(s,{responseType:i,signal:n?.signal,timeout:0})).data}}:null}function K(e,t){if(null==e)return"-";const r=e.typedBuffer;return`${n(t,r.buffer,(()=>t.size))}/${r.byteOffset}/${r.byteLength}`}function Q(e){return null!=e?e.toString():"-"}function H(e){let t=0;const has={color:!1,tangent:!1,normal:!1,texCoord0:!1},r=new Map,o=new Map,s=[];for(const i of e.parts){const{position:e,normal:a,color:l,tangent:c,texCoord0:u}=i.attributes,f=`\n ${K(e,r)}/\n ${K(a,r)}/\n ${K(l,r)}/\n ${K(c,r)}/\n ${K(u,r)}/\n ${Q(i.transform)}\n `;let m=!1;const p=n(o,f,(()=>(m=!0,{start:t,length:e.count})));m&&(t+=e.count),a&&(has.normal=!0),l&&(has.color=!0),c&&(has.tangent=!0),u&&(has.texCoord0=!0),s.push({gltf:i,writeVertices:m,region:p})}return{vertexAttributes:{position:U(C,t),normal:has.normal?U(b,t):null,tangent:has.tangent?U(h,t):null,color:has.color?U(v,t):null,texCoord0:has.texCoord0?U(M,t):null},parts:s,components:[]}}function J(e){return new d({data:(L(e.data),e.data),wrap:ee(e.parameters.wrap)})}function W(t,r){const o=new e(oe(t.color,t.opacity)),n=t.emissiveFactor?new e(ne(t.emissiveFactor)):null,s=e=>e?new x({scale:e.scale?[e.scale[0],e.scale[1]]:[1,1],rotation:i(e.rotation??0),offset:e.offset?[e.offset[0],e.offset[1]]:[0,0]}):null;return new p({color:o,colorTexture:r.get(t.colorTexture),normalTexture:r.get(t.normalTexture),emissiveColor:n,emissiveTexture:r.get(t.emissiveTexture),occlusionTexture:r.get(t.occlusionTexture),alphaMode:Z(t.alphaMode),alphaCutoff:t.alphaCutoff,doubleSided:t.doubleSided,metallic:t.metallicFactor,roughness:t.roughnessFactor,metallicRoughnessTexture:r.get(t.metallicRoughnessTexture),colorTextureTransform:s(t.colorTextureTransform),normalTextureTransform:s(t.normalTextureTransform),occlusionTextureTransform:s(t.occlusionTextureTransform),emissiveTextureTransform:s(t.emissiveTextureTransform),metallicRoughnessTextureTransform:s(t.metallicRoughnessTextureTransform)})}function X(e,t,r){t.writeVertices&&Y(e,t);const{indices:o,attributes:n,primitiveType:s,material:i}=t.gltf;let a=I(o||n.position.count,s);const l=t.region.start;if(l){const e=new Uint32Array(a);for(let t=0;t<a.length;t++)e[t]+=l;a=e}e.components.push(new m({name:t.gltf.name,faces:a,material:r.get(i),shading:n.normal?"source":"flat",trustSourceNormals:!0}))}function Y(e,t){const{position:r,normal:o,tangent:n,color:i,texCoord0:u}=e.vertexAttributes,f=t.region.start,{attributes:m,transform:p}=t.gltf,d=m.position.count;if(R(r.slice(f,d),m.position,p),null!=m.normal&&null!=o){const e=a(c(),p),t=o.slice(f,d);A(t,m.normal,e),s(e)&&E(t,t)}else null!=o&&P(o,0,0,1,{dstIndex:f,count:d});if(null!=m.tangent&&null!=n){const e=l(c(),p),t=n.slice(f,d);F(t,m.tangent,e),s(e)&&$(t,t)}else null!=n&&q(n,0,0,1,1,{dstIndex:f,count:d});if(null!=m.texCoord0&&null!=u?_(u.slice(f,d),m.texCoord0):null!=u&&z(u,0,0,{dstIndex:f,count:d}),null!=m.color&&null!=i){const e=m.color,t=i.slice(f,d);if(4===e.elementCount)e instanceof h?S(t,e,1,255):(e instanceof v||e instanceof w)&&S(t,e,1/255,255);else{q(t,255,255,255,255);const r=j.fromTypedArray(t.typedBuffer,t.typedBufferStride);e instanceof b?B(r,e,1,255):(e instanceof j||e instanceof y)&&B(r,e,1/255,255)}}else null!=i&&q(i.slice(f,d),255,255,255,255)}function Z(e){switch(e){case"OPAQUE":return"opaque";case"MASK":return"mask";case"BLEND":return"blend"}}function ee(e){return{horizontal:te(e.s),vertical:te(e.t)}}function te(e){switch(e){case O.CLAMP_TO_EDGE:return"clamp";case O.MIRRORED_REPEAT:return"mirror";case O.REPEAT:return"repeat"}}function re(e){return e**(1/t)*255}function oe(e,t){return f(re(e[0]),re(e[1]),re(e[2]),t)}function ne(e){return u(re(e[0]),re(e[1]),re(e[2]))}export{G as loadGLTFMesh};