UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 6.47 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{i8888to32 as t}from"../../webgl/number.js";import{sortByAlignment as e}from"../../webgl/shaderGraph/techniques/mesh/AlignedVertexSpec.js";import{DataType as o}from"../../../../webgl/enums.js";import{fromLayouts as r,fromLayout as n}from"../../../../webgl/VertexAttributeLocations.js";import{VertexElementDescriptor as s}from"../../../../webgl/VertexElementDescriptor.js";class i{constructor(t){this._precisionFactors={},this._key=t}get key(){return this._key}get type(){return 7&this._key}defines(){return[]}getStride(){return this._geometryLayout||this._buildMaterialInfo(),this._stride}getAttributeLocations(){return this._geometryLayout||this._buildMaterialInfo(),this._locations}getPrecisionFactors(){return this._precisionFactors||this._buildMaterialInfo(),this._precisionFactors}get geometryLayout(){return this._geometryLayout||this._buildMaterialInfo(),this._geometryLayout}getEncodingInfos(){return this._propertyEncodingInfo||this._buildMaterialInfo(),this._propertyEncodingInfo}getUniforms(){return this._uniforms||this._buildMaterialInfo(),this._uniforms}getUsedAttributes(){return this._usedAttributes||this._buildMaterialInfo(),this._usedAttributes}encodeAttributes(t,e,o,r){const n=this.propertyInfos,s=this.getEncodingInfos(),i=[],a=i.length,c=Math.ceil(this._ddStrideInBytes/4);for(let l=0;l<c;l++)i.push(0);let u=0,h=null;for(const l of Object.keys(s)){const c=s[l],{type:y,precisionFactor:d,isLayout:p}=n[l];h===y&&32!==u||(u=0,h=y);const _=p?o.getLayoutProperty(l):o.getPaintProperty(l),m=_.interpolator?.getInterpolationRange(e);let f=0;for(const o of c){const{offset:n}=o,s=a+Math.floor(n/4),c=r??_.getValue(m?m[f]:e,t),h=d||1;switch(y){case 0:case 1:i[s]|=this._encodeByte(c*h,0)<<u,u+=8;break;case 2:case 3:i[s]|=this._encodeShort(c*h,0)<<u,u+=16;break;case 4:case 5:{const t=this._encodeByte(c*h,0),e=this._encodeByte(c*h,8);i[s]|=t<<u,i[s]|=e<<u,u+=16}break;case 6:case 7:i[s]|=this._encodeShort(c*h,0),i[s]|=this._encodeShort(c*h,16);break;case 8:case 9:i[s]|=this._encodeByte(c*h,0),i[s]|=this._encodeByte(c*h,8),i[s]|=this._encodeByte(c*h,16),i[s]|=this._encodeByte(c*h,24);break;case 10:i[s]=this._encodeColor(c);break;case 11:case 12:this._encodePattern(s,i,c);break;default:throw new Error("Unsupported encoding type")}f++}}return i}getAtributeState(t){let e=0;const o=3+2*t;return e|=this._bit(o),e|=this._bit(o+1)<<1,e}static{this._encodingInfo={0:{dataType:o.BYTE,bytesPerElement:1,count:1,normalized:!1},1:{dataType:o.UNSIGNED_BYTE,bytesPerElement:1,count:1,normalized:!1},2:{dataType:o.SHORT,bytesPerElement:2,count:1,normalized:!1},3:{dataType:o.UNSIGNED_SHORT,bytesPerElement:2,count:1,normalized:!1},4:{dataType:o.BYTE,bytesPerElement:1,count:2,normalized:!1},5:{dataType:o.UNSIGNED_BYTE,bytesPerElement:1,count:2,normalized:!1},6:{dataType:o.SHORT,bytesPerElement:2,count:2,normalized:!1},7:{dataType:o.UNSIGNED_SHORT,bytesPerElement:2,count:2,normalized:!1},8:{dataType:o.BYTE,bytesPerElement:1,count:4,normalized:!1},9:{dataType:o.UNSIGNED_BYTE,bytesPerElement:1,count:4,normalized:!1},10:{dataType:o.UNSIGNED_BYTE,bytesPerElement:1,count:4,normalized:!0},11:{dataType:o.UNSIGNED_SHORT,bytesPerElement:2,count:4,normalized:!1},12:{dataType:o.UNSIGNED_SHORT,bytesPerElement:2,count:4,normalized:!1}}}_getLayoutElementsForPropertyState(t,e,o){const r=o[t],n=r.name,{count:s,dataType:a,normalized:c}=i._encodingInfo[r.type],{attribueIdx:u,precisionFactor:h}=r,l=h||1,y=[{location:u[0],name:n,propertyName:t,count:s,type:a,normalized:c,precisionFactor:l}];if(this._precisionFactors[n]=l,2===e){const e=`${n}To`;y.push({location:u[1],name:e,propertyName:t,count:s,type:a,normalized:c,precisionFactor:l}),this._precisionFactors[e]=l}return y}_buildMaterialInfo(){const t=[];this._propertyEncodingInfo={};const e={},o=this.properties,r=this.propertyInfos;let n=-1;for(const i of o){n++;const o=r[i].name;e[o]=!1,e[`${o}To`]=!1;const s=this.getAtributeState(n);if(0===s||3===s)continue;const a=this._getLayoutElementsForPropertyState(i,s,r);t.push(...a)}for(const i of t)e[i.name]=!0;const s=u(t);this._buildLayout(s),this._buildShaderUniforms(),this._usedAttributes=e}_buildLayout(t){this.opacityLayout?this._locations=r([this.baseGeometryLayout,this.opacityLayout]):this._locations=n(this.baseGeometryLayout);const e=this.baseGeometryLayout;let o=e[0].stride;const i=[];let u=0;for(const r of t)u+=a(r.type)*r.count;o=c(o+u);for(const r of e)i.push(new s(r.name,r.count,r.type,r.offset,o,r.normalized));let h=0;for(const r of t)i.push(new s(r.name,r.count,r.type,e[0].stride+h,o,r.normalized)),void 0===this._propertyEncodingInfo[r.propertyName]&&(this._propertyEncodingInfo[r.propertyName]=[]),this._propertyEncodingInfo[r.propertyName].push({offset:h}),h+=a(r.type)*r.count;this._ddStrideInBytes=h,this._geometryLayout=i;const l=this._locations;for(const r of t)l.set(r.name,r.location);this._stride=o}_buildShaderUniforms(){const t=[],e=this.properties,o=this.propertyInfos;let r=-1;for(const n of e){r++;const{name:e,type:s,isLayout:i}=o[n];switch(this.getAtributeState(r)){case 0:t.push({name:e,getValue:(t,e,o,r)=>{const a=i?t.getLayoutValue(n,e):t.getPaintValue(n,e);switch(s){case 11:{const o=t.getDashKey(a,t.getLayoutValue("line-cap",e)),n=r.getMosaicItemPosition(o,!1);if(null==n)return null;const{tl:s,br:i}=n;return[s[0],i[1],i[0],s[1]]}case 12:{const t=r.getMosaicItemPosition(a,!n.includes("line-"));if(null==t)return null;const{tl:e,br:o}=t;return[e[0],o[1],o[0],e[1]]}case 10:{const t=a[3];return[t*a[0],t*a[1],t*a[2],t]}default:return a}}});break;case 2:{const o=`${e}Mix`;t.push({name:o,getValue:(t,e,o,r)=>(i?t.getLayoutProperty(n):t.getPaintProperty(n)).interpolator.interpolationUniformValue(o,e)})}}}this._uniforms=t}_bit(t){return(this._key&1<<t)>>t}_encodeColor(e){const o=255*e[3];return t(e[0]*o,e[1]*o,e[2]*o,o)}_encodePattern(t,e,o){if(!o?.rect)return;const r=2,n=o.rect,s=o.width,i=o.height;e[t]=this._encodeShort(n.x+r,0),e[t]|=this._encodeShort(n.y+r+i,16),e[t+1]=this._encodeShort(n.x+r+s,0),e[t+1]|=this._encodeShort(n.y+r,16)}_encodeByte(t,e){return(255&t)<<e}_encodeShort(t,e){return(65535&t)<<e}}const a=t=>{switch(t){case o.FLOAT:case o.INT:case o.UNSIGNED_INT:return 4;case o.SHORT:case o.UNSIGNED_SHORT:case o.HALF_FLOAT:return 2;case o.BYTE:case o.UNSIGNED_BYTE:return 1}},c=t=>t+3&-4;function u(t){const o=e(t),r=[];let n=0;for(const e of o)r.push({...e,offset:n}),n+=a(e.type)*e.count;return r}export{i as VTLMaterial};