UNPKG

@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.55 kB
import {getBBoxesFromMeshes}from'./helpers.js';import j from'proj4';import {Matrix4,Float32BufferAttribute,Vector3}from'three';import N from'eigen';async function _(x,o){await N.ready;const y=N.Matrix,e=x.length;if(e<4||o.length!==e)throw new Error("Need at least 4 corresponding points for a 3D transformation.");let m=[],u=[];for(let s=0;s<e;s++){const[c,P,E]=x[s],[z,v,D]=o[s];m.push([c,P,E,1,0,0,0,0,0,0,0,0]),u.push([z]),m.push([0,0,0,0,c,P,E,1,0,0,0,0]),u.push([v]),m.push([0,0,0,0,0,0,0,0,c,P,E,1]),u.push([D]);}const i=new y(m),w=new y(u),h=i.transpose(),T=h.matMul(i).inverse(),I=h.matMul(w),r=T.matMul(I),l=r.get(0,0),f=r.get(1,0),b=r.get(2,0),p=r.get(3,0),g=r.get(4,0),S=r.get(5,0),n=r.get(6,0),B=r.get(7,0),d=r.get(8,0),M=r.get(9,0),t=r.get(10,0),a=r.get(11,0);return N.GC.flush(),[l,f,b,p,g,S,n,B,d,M,t,a,0,0,0,1]}async function Q(x,o,y,e,m,u){const i=await u.get("SELECT doc2, srcSRS, arrayIndex, id from instancedData WHERE arrayIndex = ?",[e.template]);if(!i)throw new Error(`No geometry template found for "${e.template}"!`);const w=await m.readBinary(i.doc2),h=i.srcSRS,O=j(h,y),T=w.getRoot(),I=new Matrix4(...e.transformationMatrix);let r=null;const l=o.vertices[e.boundaries[0]],f=[l[0]*o.transform.scale[0]+o.transform.translate[0],l[1]*o.transform.scale[1]+o.transform.translate[1],l[2]*o.transform.scale[2]+o.transform.translate[2]],b=[],p=[],g=[];for(const B of T.listMeshes())for(const d of B.listPrimitives()){const M=d.getAttribute("POSITION")?.getArray();if(!M)continue;const t=new Float32BufferAttribute(M,3);for(let a=0;a<t.array.length;a+=3)p.push([t.array[a],t.array[a+1],t.array[a+2]]);t.applyMatrix4(I);for(let a=0;a<t.array.length;a+=3){const[A,s,c]=O.forward([t.array[a]+f[0],t.array[a+1]+f[1],t.array[a+2]+f[2]]);r||(r=new Vector3(A,s,c)),t.array[a]=A-r.x,t.array[a+1]=c-r.z,t.array[a+2]=-(s-r.y),g.push([A,c,-s]);}b.push({position:t.array}),d.getAttribute("POSITION").setArray(t.array);}const S=new Matrix4(...await _(p.slice(0,Math.min(20,p.length)),g.slice(0,Math.min(20,g.length))));if(!r)throw new Error("FISHY!");const{cartographicBox:n}=getBBoxesFromMeshes(b,r);return [{cartographicBoxMaxX:n.max.x,cartographicBoxMaxY:n.max.y,cartographicBoxMaxZ:n.max.z,cartographicBoxMinX:n.min.x,cartographicBoxMinY:n.min.y,cartographicBoxMinZ:n.min.z,id:x,refId:i.id.toString(),transformationMatrix:S.toArray(),isInstanced:true,collectedTextures:[],texturePaths:[],serializedDoc:void 0}]}export{Q as buildGeometryInstance};//# sourceMappingURL=buildGeometryInstance.js.map //# sourceMappingURL=buildGeometryInstance.js.map