@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 4.64 kB
JavaScript
import{isSome as e}from"../../../../core/maybe.js";import{WGLSymbologyType as t}from"./enums.js";import r from"./MemoryRequirements.js";import s from"./TileBufferData.js";import i from"./TileDisplayData.js";import{allocateTypedArrayBuffer as o,copyMeshData as a,forEachGeometryType as n}from"./Utils.js";import f from"./WGLDisplayRecord.js";import{DisplayRecordReader as l}from"./cpuMapped/DisplayRecordReader.js";import{VertexBuffers as u}from"./mesh/VertexBuffer.js";import{VertexVectors as c}from"./mesh/VertexVector.js";import m from"./util/Writer.js";const d=new r,p=new r,D=1.5,h=5;function x(e,t){const r={};for(const s in e){const i={data:o(t,e[s]),stride:e[s]};r[s]=i}return r}function y(e){return[e.fill||{},e.line||{},e.icon||{},e.text||{},e.label||{}]}function B(e){const t=[[],[],[],[],[]],r=e;for(const s of r)for(const e of s.displayRecords)t[e.geometryType].push(e);return t}class g{constructor(){this.tileDisplayData=null,this.tileBufferData=null}reshuffle(){if(d.reset(),!this.tileDisplayData)return;const e=B(this.tileDisplayData.displayObjects);for(const s of e)for(const e of s)e&&d.needMore(e.geometryType,e.meshData?e.meshData.vertexCount:e.vertexCount,e.meshData?e.meshData.indexData.length:e.indexCount);const t=e.length,r=new s;for(let s=0;s<t;++s){r.geometries[s].indexBuffer=new Uint32Array(Math.round(D*d.indicesFor(s)));const e=[],t=this.tileBufferData?.geometries[s].vertexBuffer;if(!t)continue;for(const r in t)e.push(t[r].stride);const i=g._computeVertexAlignment(e),a=Math.round(D*d.verticesFor(s)),n=g._align(a,i);for(const f in t){const e=t[f].stride;r.geometries[s].vertexBuffer[f]={stride:e,data:o(n,e)}}}p.reset(),this.tileDisplayData.displayList?.clear();for(let s=0;s<t;++s){const t=e[s];for(const e of t){if(e.meshData)e.writeMeshDataToBuffers(p.verticesFor(s),r.geometries[s].vertexBuffer,p.indicesFor(s),r.geometries[s].indexBuffer),e.meshData=null;else{const t=this.tileBufferData?.geometries[s];if(t){const i=t.vertexBuffer,o=t.indexBuffer,n=r.geometries[s].vertexBuffer,f=r.geometries[s].indexBuffer,l=p.verticesFor(s),u=p.indicesFor(s);a(l,u,n,f,e,i,o),e.vertexFrom=l,e.indexFrom=u}}p.needMore(s,e.vertexCount,e.indexCount)}}const{displayList:i,displayObjects:n}=this.tileDisplayData;if(i)for(const s of n)i.addToList(s.displayRecords);this.tileBufferData=r}getStrides(){const e=[],t=this.tileBufferData?.geometries;if(!t)return e;for(let r=0;r<t.length;++r){const s=t[r];e[r]={};for(const t in s.vertexBuffer)e[r][t]=s.vertexBuffer[t].stride}return e}clone(){const e=new g;return e.tileBufferData=this.tileBufferData?.clone()??null,e.tileDisplayData=this.tileDisplayData?.clone()??null,e}_guessSize(){const e=this.tileDisplayData?.displayObjects??[],t=Math.min(e.length,4),r=12,s=40;let i=0;for(let o=0;o<t;o++)i=Math.max(i,e[o].displayRecords.length);return 2*(e.length*r+e.length*i*s)}serialize(){const e=this.tileBufferData.serialize(),t=this.tileBufferData.getBuffers(),r=this.tileDisplayData.serialize(new m(Int32Array,this._guessSize())).buffer();return t.push(r),{result:{displayData:r,bufferData:e},transferList:t}}static fromVertexData(r,s){const i={},o=new Map;for(const e of s)o.set(e.id,e);return n((s=>{const a=r.data[s];if(e(a)){const e=l.from(a.records).getCursor();for(;e.next();){const t=e.id,r=e.materialKey,i=e.indexFrom,a=e.indexCount,n=e.vertexFrom,l=e.vertexCount,u=o.get(t),c=new f(t,s,r);c.indexFrom=i,c.indexCount=a,c.vertexFrom=n,c.vertexCount=l,u.displayRecords.push(c)}i[s]=u.fromVertexData(a,s)}else i[s]=new c(s,0,t.DEFAULT).intoBuffers()})),g.fromMeshData({displayObjects:s,vertexBuffersMap:i})}static fromMeshData(e){const t=new g,r=new i,o=new s;r.displayObjects=e.displayObjects;for(const s in e.vertexBuffersMap){const t=e.vertexBuffersMap[s];o.geometries[s].indexBuffer=t.indexBuffer,o.geometries[s].vertexBuffer=t.namedBuffers}return t.tileDisplayData=r,t.tileBufferData=o,t}static bind(e,t){const r=new g;return r.tileDisplayData=e,r.tileBufferData=t,r}static create(e,t){const r=new g;r.tileDisplayData=new i,r.tileDisplayData.displayObjects=e;const o=[0,0,0,0,0],a=[0,0,0,0,0],n=[[],[],[],[],[]];for(const s of e)for(const e of s.displayRecords)n[e.geometryType].push(e),o[e.geometryType]+=e.meshData.vertexCount,a[e.geometryType]+=e.meshData.indexData.length;const l=new s,u=y(t);for(let s=0;s<h;s++){const e=new Uint32Array(a[s]),t=x(u[s],o[s]);f.writeAllMeshDataToBuffers(n[s],t,e),l.geometries[s]={indexBuffer:e,vertexBuffer:t}}return r.tileBufferData=l,r}static _align(e,t){const r=e%t;return 0===r?e:e+(t-r)}static _computeVertexAlignment(e){let t=!1,r=!1;for(const s of e)s%4==2?t=!0:s%4!=0&&(r=!0);return r?4:t?2:1}}export{g as TileData};