UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 6.41 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import e from"../../../Color.js";import{colorGamma as t}from"../../../colorUtils.js";import r from"../../../core/Error.js";import{getOrCreateMapValue as o}from"../../../core/MapUtils.js";import{rad2deg as n,hasScaling as s}from"../../../core/mathUtils.js";import{normalFromMat4 as i,fromMat4 as a}from"../../../core/libs/gl-matrix-2/math/mat3.js";import{create as l}from"../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{fromValues as c}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{fromValues as f}from"../../../core/libs/gl-matrix-2/factories/vec4f64.js";import u from"../MeshComponent.js";import m from"../MeshMaterialMetallicRoughness.js";import p from"../MeshTexture.js";import d from"../MeshTextureTransform.js";import x from"../MeshVertexAttributes.js";import{selectVertexSpace as g}from"../meshVertexSpaceUtils.js";import{BufferViewVec4f as h,BufferViewVec4u8 as T,BufferViewVec4u16 as v,BufferViewVec3u8 as w,BufferViewVec3f as j,BufferViewVec3u16 as b,BufferViewVec2f as y,BufferViewVec3f64 as C}from"../buffer/BufferView.js";import{d as M,c as R,e as S,l as B}from"../../../chunks/vec3.js";import{a as F,n as $,l as A}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 E}from"../../../views/3d/glTF/loader.js";import{convertPrimitiveToTriangles as D}from"../../../views/3d/glTF/internal/indexUtils.js";import{isEncodedMeshTexture as I}from"../../../views/3d/glTF/internal/resourceUtils.js";import{f as L}from"../../../chunks/vec32.js";import{f as z}from"../../../chunks/vec42.js";import{n as K,f as N}from"../../../chunks/vec2.js";async function O(e,t,o){const n=new V(o?.resolveFile),s=await E(n,t,o),i=s.model,a=i.lods.shift(),l=new Map,c=new Map;i.textures.forEach((e,t)=>l.set(t,P(e))),i.materials.forEach((e,t)=>c.set(t,Q(e,l)));const f=H(a);for(const r of f.parts)J(f,r,c);const{position:u,normal:m,tangent:p,color:d,texCoord0:h}=f.vertexAttributes,T=g(e,o),v=e.spatialReference.isGeographic?g(e):T,w=k({vertexAttributes:{position:u.typedBuffer,normal:m?.typedBuffer,tangent:p?.typedBuffer},vertexSpace:v,spatialReference:e.spatialReference},T,{allowBufferReuse:!0,sourceUnit:o?.unitConversionDisabled?void 0:"meters"});if(!w)throw new r("load-gltf-mesh:vertex-space-projection",`Failed to load mesh from glTF because we could not convert the vertex space from ${v.type} to ${T.type}`);return{mesh:{transform:null,vertexSpace:T,components:f.components,spatialReference:e.spatialReference,vertexAttributes:new x({...w,color:d?.typedBuffer,uv:h?.typedBuffer})},meta:s.meta}}function q(e,t){if(null==e)return"-";const r=e.typedBuffer;return`${o(t,r.buffer,()=>t.size)}/${r.byteOffset}/${r.byteLength}`}function G(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,n=new Map,s=[];for(const i of e.parts){const{position:e,normal:a,color:l,tangent:c,texCoord0:f}=i.attributes,u=`\n ${q(e,r)}/\n ${q(a,r)}/\n ${q(l,r)}/\n ${q(c,r)}/\n ${q(f,r)}/\n ${G(i.transform)}\n `;let m=!1;const p=o(n,u,()=>(m=!0,{start:t,length:e.count}));m&&(t+=e.count),a&&(has.normal=!0),l&&(has.color=!0),c&&(has.tangent=!0),f&&(has.texCoord0=!0),s.push({gltf:i,writeVertices:m,region:p})}return{vertexAttributes:{position:U(C,t),normal:has.normal?U(j,t):null,tangent:has.tangent?U(h,t):null,color:has.color?U(T,t):null,texCoord0:has.texCoord0?U(y,t):null},parts:s,components:[]}}function P(e){return new p({data:(I(e.data),e.data),wrap:Y(e.parameters.wrap)})}function Q(t,r){const o=new e(ee(t.color,t.opacity)),s=t.emissiveFactor?new e(te(t.emissiveFactor)):null,i=e=>e?new d({scale:e.scale?[e.scale[0],e.scale[1]]:[1,1],rotation:n(e.rotation??0),offset:e.offset?[e.offset[0],e.offset[1]]:[0,0]}):null;return new m({color:o,colorTexture:r.get(t.colorTexture),normalTexture:r.get(t.normalTexture),emissiveColor:s,emissiveStrength:t.emissiveStrengthKHR,emissiveTexture:r.get(t.emissiveTexture),occlusionTexture:r.get(t.occlusionTexture),alphaMode:X(t.alphaMode),alphaCutoff:t.alphaCutoff,doubleSided:t.doubleSided,metallic:t.metallicFactor,roughness:t.roughnessFactor,metallicRoughnessTexture:r.get(t.metallicRoughnessTexture),colorTextureTransform:i(t.colorTextureTransform),normalTextureTransform:i(t.normalTextureTransform),occlusionTextureTransform:i(t.occlusionTextureTransform),emissiveTextureTransform:i(t.emissiveTextureTransform),metallicRoughnessTextureTransform:i(t.metallicRoughnessTextureTransform)})}function J(e,t,r){t.writeVertices&&W(e,t);const{indices:o,attributes:n,primitiveType:s,material:i}=t.gltf;let a=D(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 u({name:t.gltf.name,faces:a,material:r.get(i),shading:n.normal?"source":"flat",trustSourceNormals:!0}))}function W(e,t){const{position:r,normal:o,tangent:n,color:c,texCoord0:f}=e.vertexAttributes,u=t.region.start,{attributes:m,transform:p}=t.gltf,d=m.position.count;if(M(r.slice(u,d),m.position,p),null!=m.normal&&null!=o){const e=i(l(),p),t=o.slice(u,d);R(t,m.normal,e),s(e)&&S(t,t)}else null!=o&&L(o,0,0,1,{dstIndex:u,count:d});if(null!=m.tangent&&null!=n){const e=a(l(),p),t=n.slice(u,d);F(t,m.tangent,e),s(e)&&$(t,t)}else null!=n&&z(n,0,0,1,1,{dstIndex:u,count:d});if(null!=m.texCoord0&&null!=f?K(f.slice(u,d),m.texCoord0):null!=f&&N(f,0,0,{dstIndex:u,count:d}),null!=m.color&&null!=c){const e=m.color,t=c.slice(u,d);if(4===e.elementCount)e instanceof h?A(t,e,1,255):(e instanceof T||e instanceof v)&&A(t,e,1/255,255);else{z(t,255,255,255,255);const r=w.fromTypedArray(t.typedBuffer,t.typedBufferStride);e instanceof j?B(r,e,1,255):(e instanceof w||e instanceof b)&&B(r,e,1/255,255)}}else null!=c&&z(c.slice(u,d),255,255,255,255)}function X(e){switch(e){case"OPAQUE":return"opaque";case"MASK":return"mask";case"BLEND":return"blend"}}function Y(e){return{horizontal:Z(e.s),vertical:Z(e.t)}}function Z(e){switch(e){case 33071:return"clamp";case 33648:return"mirror";case 10497:return"repeat"}}function _(e){return e**(1/t)*255}function ee(e,t){return f(_(e[0]),_(e[1]),_(e[2]),t)}function te(e){return c(_(e[0]),_(e[1]),_(e[2]))}export{O as loadGLTFMesh};