@csi-foxbyte/cityjson-to-3d-tiles
Version:
A Node.js library that converts CityJSON files into Cesium 3D Tiles—complete with automatic texture atlas packing, Basis compression, three LOD levels, and customizable threading.
2 lines • 2.73 kB
JavaScript
import {getBBoxesFromMeshes}from'./helpers.js';import F from'proj4';import {Matrix4,Float32BufferAttribute,Vector3}from'three';import E from'eigen';async function _(d,e){await E.ready;const b=E.Matrix,n=d.length;if(n<4||e.length!==n)throw new Error("Need at least 4 corresponding points for a 3D transformation.");let f=[],g=[];for(let c=0;c<n;c++){const[l,u,N]=d[c],[z,k,v]=e[c];f.push([l,u,N,1,0,0,0,0,0,0,0,0]),g.push([z]),f.push([0,0,0,0,l,u,N,1,0,0,0,0]),g.push([k]),f.push([0,0,0,0,0,0,0,0,l,u,N,1]),g.push([v]);}const s=new b(f),S=new b(g),M=s.transpose(),I=M.matMul(s).inverse(),B=M.matMul(S),r=I.matMul(B),p=r.get(0,0),y=r.get(1,0),w=r.get(2,0),x=r.get(3,0),h=r.get(4,0),A=r.get(5,0),P=r.get(6,0),i=r.get(7,0),m=r.get(8,0),o=r.get(9,0),T=r.get(10,0),t=r.get(11,0);return E.GC.flush(),[p,y,w,x,h,A,P,i,m,o,T,t,0,0,0,1]}async function Q(d,e,b,n,f,g){let s=null;for(let m=0;m<16;m++)try{await new Promise(o=>setTimeout(o,m*100*Math.random())),s=await g.get("SELECT doc, srcSRS, arrayIndex, id from instancedData WHERE arrayIndex = ?",[n.template]);break}catch(o){console.error({e:o});}if(!s)throw new Error(`No geometry template found for "${n.template}"!`);const S=await f.readBinary(s.doc),M=s.srcSRS,O=F(M,b),I=S.getRoot(),B=new Matrix4(...n.transformationMatrix);let r=null;const p=e.vertices[n.boundaries[0]],y=[p[0]*e.transform.scale[0]+e.transform.translate[0],p[1]*e.transform.scale[1]+e.transform.translate[1],p[2]*e.transform.scale[2]+e.transform.translate[2]],w=[],x=[],h=[],A=new Set;for(const m of I.listMeshes())for(const o of m.listPrimitives()){A.add(o.getMaterial()?.getName()??"-");const T=o.getAttribute("POSITION")?.getArray();if(!T)continue;const t=new Float32BufferAttribute(T,3);for(let a=0;a<t.array.length;a+=3)x.push([t.array[a],t.array[a+1],t.array[a+2]]);t.applyMatrix4(B);for(let a=0;a<t.array.length;a+=3){const[c,l,u]=O.forward([t.array[a]+y[0],t.array[a+1]+y[1],t.array[a+2]+y[2]]);r||(r=new Vector3(c,l,u)),t.array[a]=c-r.x,t.array[a+1]=u-r.z,t.array[a+2]=-(l-r.y),h.push([c,u,-l]);}w.push({position:t.array}),o.getAttribute("POSITION").setArray(t.array);}const P=new Matrix4(...await _(x.slice(0,Math.min(20,x.length)),h.slice(0,Math.min(20,h.length))));if(!r)throw new Error("FISHY!");const{cartographicBox:i}=getBBoxesFromMeshes(w,r);return [{cartographicBoxMaxX:i.max.x,cartographicBoxMaxY:i.max.y,cartographicBoxMaxZ:i.max.z,cartographicBoxMinX:i.min.x,cartographicBoxMinY:i.min.y,cartographicBoxMinZ:i.min.z,id:d,refId:s.id.toString(),transformationMatrix:P.toArray(),isInstanced:true,collectedTextures:[],texturePaths:Array.from(A),serializedDoc:void 0}]}export{Q as buildGeometryInstance};//# sourceMappingURL=buildGeometryInstance.js.map
//# sourceMappingURL=buildGeometryInstance.js.map