UNPKG

@inweb/viewer-three

Version:

JavaScript library for rendering CAD and BIM files in a browser using Three.js

2 lines (1 loc) 23.2 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("@inweb/viewer-three"),require("three")):"function"==typeof define&&define.amd?define(["@inweb/viewer-three","three"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ODA.Three,e.THREE)}(this,(function(e,t){"use strict";class r extends t.Loader{constructor(e){super(e),this.littleEndian=!0}load(e,r,i,n){const s=this,a=new t.FileLoader(s.manager);a.setPath(s.path),a.setResponseType("arraybuffer"),a.setRequestHeader(s.requestHeader),a.setWithCredentials(s.withCredentials),a.load(e,(function(t){try{r(s.parse(t))}catch(t){n?n(t):console.error(t),s.manager.itemError(e)}}),i,n)}_getDataView(e,t,r,i){switch(r){case"F":return 8===i?e.getFloat64(t,this.littleEndian):e.getFloat32(t,this.littleEndian);case"I":return 1===i?e.getInt8(t):2===i?e.getInt16(t,this.littleEndian):e.getInt32(t,this.littleEndian);case"U":return 1===i?e.getUint8(t):2===i?e.getUint16(t,this.littleEndian):e.getUint32(t,this.littleEndian)}}parse(e){const r=function(e){const t={},r=new Uint8Array(e);let i="",n="",s=0,a=!1;const o=r.length;for(;s<o&&!1===a;){const e=String.fromCharCode(r[s++]);"\n"===e||"\r"===e?(n.trim().toLowerCase().startsWith("data")&&(a=!0),n=""):n+=e,i+=e}const l=i.search(/[\r\n]DATA\s(\S*)\s/i),c=/[\r\n]DATA\s(\S*)\s/i.exec(i.slice(l-1));if(t.data=c[1],t.headerLen=c[0].length+l,t.str=i.slice(0,t.headerLen),t.str=t.str.replace(/#.*/gi,""),t.version=/^VERSION (.*)/im.exec(t.str),t.fields=/^FIELDS (.*)/im.exec(t.str),t.size=/^SIZE (.*)/im.exec(t.str),t.type=/^TYPE (.*)/im.exec(t.str),t.count=/^COUNT (.*)/im.exec(t.str),t.width=/^WIDTH (.*)/im.exec(t.str),t.height=/^HEIGHT (.*)/im.exec(t.str),t.viewpoint=/^VIEWPOINT (.*)/im.exec(t.str),t.points=/^POINTS (.*)/im.exec(t.str),null!==t.version&&(t.version=parseFloat(t.version[1])),t.fields=null!==t.fields?t.fields[1].split(" "):[],null!==t.type&&(t.type=t.type[1].split(" ")),null!==t.width&&(t.width=parseInt(t.width[1])),null!==t.height&&(t.height=parseInt(t.height[1])),null!==t.viewpoint&&(t.viewpoint=t.viewpoint[1]),null!==t.points&&(t.points=parseInt(t.points[1],10)),null===t.points&&(t.points=t.width*t.height),null!==t.size&&(t.size=t.size[1].split(" ").map((function(e){return parseInt(e,10)}))),null!==t.count)t.count=t.count[1].split(" ").map((function(e){return parseInt(e,10)}));else{t.count=[];for(let e=0,r=t.fields.length;e<r;e++)t.count.push(1)}t.offset={};let h=0;for(let e=0,r=t.fields.length;e<r;e++)"ascii"===t.data?t.offset[t.fields[e]]=e:(t.offset[t.fields[e]]=h,h+=t.size[e]*t.count[e]);return t.rowSize=h,t}(e),i=[],n=[],s=[],a=[],o=[],l=new t.Color;if("ascii"===r.data){const c=r.offset,h=(new TextDecoder).decode(e).slice(r.headerLen).split("\n");for(let e=0,u=h.length;e<u;e++){if(""===h[e])continue;const u=h[e].split(" ");if(void 0!==c.x&&(i.push(parseFloat(u[c.x])),i.push(parseFloat(u[c.y])),i.push(parseFloat(u[c.z]))),void 0!==c.rgb){const e=r.fields.findIndex((e=>"rgb"===e)),i=r.type[e],n=parseFloat(u[c.rgb]);let a=n;if("F"===i){const e=new Float32Array(1);e[0]=n,a=new Int32Array(e.buffer)[0]}const o=(a>>16&255)/255,h=(a>>8&255)/255,f=(255&a)/255;l.setRGB(o,h,f,t.SRGBColorSpace),s.push(l.r,l.g,l.b)}void 0!==c.normal_x&&(n.push(parseFloat(u[c.normal_x])),n.push(parseFloat(u[c.normal_y])),n.push(parseFloat(u[c.normal_z]))),void 0!==c.intensity&&a.push(parseFloat(u[c.intensity])),void 0!==c.label&&o.push(parseInt(u[c.label]))}}if("binary_compressed"===r.data){const c=new Uint32Array(e.slice(r.headerLen,r.headerLen+8)),h=c[0],u=c[1],f=function(e,t){const r=e.length,i=new Uint8Array(t);let n,s,a,o=0,l=0;do{if(n=e[o++],n<32){if(n++,l+n>t)throw new Error("Output buffer is not large enough");if(o+n>r)throw new Error("Invalid compressed data");do{i[l++]=e[o++]}while(--n)}else{if(s=n>>5,a=l-((31&n)<<8)-1,o>=r)throw new Error("Invalid compressed data");if(7===s&&(s+=e[o++],o>=r))throw new Error("Invalid compressed data");if(a-=e[o++],l+s+2>t)throw new Error("Output buffer is not large enough");if(a<0)throw new Error("Invalid compressed data");if(a>=l)throw new Error("Invalid compressed data");do{i[l++]=i[a++]}while(2+--s)}}while(o<r);return i}(new Uint8Array(e,r.headerLen+8,h),u),d=new DataView(f.buffer),p=r.offset;for(let e=0;e<r.points;e++){if(void 0!==p.x){const t=r.fields.indexOf("x"),n=r.fields.indexOf("y"),s=r.fields.indexOf("z");i.push(this._getDataView(d,r.points*p.x+r.size[t]*e,r.type[t],r.size[t])),i.push(this._getDataView(d,r.points*p.y+r.size[n]*e,r.type[n],r.size[n])),i.push(this._getDataView(d,r.points*p.z+r.size[s]*e,r.type[s],r.size[s]))}if(void 0!==p.rgb){const i=r.fields.indexOf("rgb"),n=d.getUint8(r.points*p.rgb+r.size[i]*e+2)/255,a=d.getUint8(r.points*p.rgb+r.size[i]*e+1)/255,o=d.getUint8(r.points*p.rgb+r.size[i]*e+0)/255;l.setRGB(n,a,o,t.SRGBColorSpace),s.push(l.r,l.g,l.b)}if(void 0!==p.normal_x){const t=r.fields.indexOf("normal_x"),i=r.fields.indexOf("normal_y"),s=r.fields.indexOf("normal_z");n.push(this._getDataView(d,r.points*p.normal_x+r.size[t]*e,r.type[t],r.size[t])),n.push(this._getDataView(d,r.points*p.normal_y+r.size[i]*e,r.type[i],r.size[i])),n.push(this._getDataView(d,r.points*p.normal_z+r.size[s]*e,r.type[s],r.size[s]))}if(void 0!==p.intensity){const t=r.fields.indexOf("intensity");a.push(this._getDataView(d,r.points*p.intensity+r.size[t]*e,r.type[t],r.size[t]))}if(void 0!==p.label){const t=r.fields.indexOf("label");o.push(d.getInt32(r.points*p.label+r.size[t]*e,this.littleEndian))}}}if("binary"===r.data){const c=new DataView(e,r.headerLen),h=r.offset;for(let e=0,u=0;e<r.points;e++,u+=r.rowSize){if(void 0!==h.x){const e=r.fields.indexOf("x"),t=r.fields.indexOf("y"),n=r.fields.indexOf("z");i.push(this._getDataView(c,u+h.x,r.type[e],r.size[e])),i.push(this._getDataView(c,u+h.y,r.type[t],r.size[t])),i.push(this._getDataView(c,u+h.z,r.type[n],r.size[n]))}if(void 0!==h.rgb){const e=c.getUint8(u+h.rgb+2)/255,r=c.getUint8(u+h.rgb+1)/255,i=c.getUint8(u+h.rgb+0)/255;l.setRGB(e,r,i,t.SRGBColorSpace),s.push(l.r,l.g,l.b)}if(void 0!==h.normal_x){const e=r.fields.indexOf("normal_x"),t=r.fields.indexOf("normal_y"),i=r.fields.indexOf("normal_z");n.push(this._getDataView(c,u+h.normal_x,r.type[e],r.size[e])),n.push(this._getDataView(c,u+h.normal_y,r.type[t],r.size[t])),n.push(this._getDataView(c,u+h.normal_z,r.type[i],r.size[i]))}if(void 0!==h.intensity){const e=r.fields.indexOf("intensity");a.push(this._getDataView(c,u+h.intensity,r.type[e],r.size[e]))}void 0!==h.label&&o.push(c.getInt32(u+h.label,this.littleEndian))}}const c=new t.BufferGeometry;i.length>0&&c.setAttribute("position",new t.Float32BufferAttribute(i,3)),n.length>0&&c.setAttribute("normal",new t.Float32BufferAttribute(n,3)),s.length>0&&c.setAttribute("color",new t.Float32BufferAttribute(s,3)),a.length>0&&c.setAttribute("intensity",new t.Float32BufferAttribute(a,1)),o.length>0&&c.setAttribute("label",new t.Int32BufferAttribute(o,1)),c.computeBoundingSphere();const h=new t.PointsMaterial({size:.005});return s.length>0&&(h.vertexColors=!0),new t.Points(c,h)}}const i={Box3:t.Box3,BufferAttribute:t.BufferAttribute,BufferGeometry:t.BufferGeometry,Color:t.Color,Euler:t.Euler,Float32BufferAttribute:t.Float32BufferAttribute,Group:t.Group,Line:t.Line,LineBasicMaterial:t.LineBasicMaterial,Matrix4:t.Matrix4,Mesh:t.Mesh,MeshLambertMaterial:t.MeshLambertMaterial,MeshStandardMaterial:t.MeshStandardMaterial,Points:t.Points,PointsMaterial:t.PointsMaterial,PerspectiveCamera:t.PerspectiveCamera,Scene:t.Scene,Vector3:t.Vector3};var n=class{providers;constructor(e){this.providers=e}async GetLayerByURI(e){let t=[];for(let r of this.providers){let i=await r.GetLayerByURI(e);if(!(i instanceof Error))return i;t.push(i)}return new Error(JSON.stringify(t))}},s=class{layers;constructor(){this.layers=new Map}GetLayerByURI(e){return this.layers.has(e)?Promise.resolve(this.layers.get(e)):new Error(`File with uri "${e}" not found`)}add(e){if(this.layers.has(e.header.id))throw new Error(`Inserting file with duplicate ID "${e.header.id}"`);return this.layers.set(e.header.id,e),this}AddAll(e){return e.forEach((e=>this.add(e))),this}};var a=class{layers;constructor(){this.layers=new Map}async FetchJson(e){let t=await fetch(e);if(!t.ok)return new Error(`Failed to fetch ${e}: ${t.status}`);try{return await t.json()}catch(t){return function(e){console.log(`${JSON.stringify(arguments)}`)}(e),new Error(`Failed to parse json at ${e}: ${t}`)}}async GetLayerByURI(e){if(!this.layers.has(e)){let t=await this.FetchJson(e);if(t instanceof Error)return new Error(`File with id "${e}" not found`);let r=t;return this.layers.set(e,r),r}return this.layers.get(e)}};function o(e,t,r){e.has(t)?e.get(t)?.push(r):e.set(t,[r])}var l=class extends Error{};function c(e){let t=new Map,r=new Map;e.forEach(((e,i)=>{Object.keys(e.inherits).forEach((n=>{o(t,i,e.inherits[n]),o(r,e.inherits[n],i)})),Object.keys(e.children).forEach((n=>{o(t,i,e.children[n]),o(r,e.children[n],i)}))}));let i=[...e.keys()],n={},s={};function a(e){if(n[e])return;if(s[e])throw new Error("CYCLE!");s[e]=!0;let r=t.get(e);r&&r.forEach((e=>a(e))),n[e]=!0}let l=new Set;try{i.forEach((e=>{r.has(e)||-1!==e.indexOf("/")||l.add(e),a(e)}))}catch(e){return null}return l}function h(e){return e.split("/")[0]}function u(e){let t=e.split("/");return t.shift(),t.join("/")}function f(e,t){if(""===t)return e;let r=t.split("/"),i=e.children.get(r[0]);return i?1===r.length?i:f(i,u(t)):null}function d(e,t){let r={path:e,children:{},inherits:{},attributes:{}};return t.forEach((e=>{Object.keys(e.children).forEach((t=>{r.children[t]=e.children[t]})),Object.keys(e.inherits).forEach((t=>{let i=e.inherits[t];null===i?delete r.inherits[t]:r.inherits[t]=i})),Object.keys(e.attributes).forEach((t=>{r.attributes[t]=e.attributes[t]}))})),r}function p(e,t){return w(e,{node:e,children:new Map,attributes:new Map},t)}function w(e,t,r){let i=r.get(e);var n,s,a;return i&&(n=i,s=t,a=r,Object.values(n.inherits).forEach((e=>{let t=f(p(h(e),a),u(e));if(!t)throw new Error(`Unknown node ${e}`);t.children.forEach(((e,t)=>{s.children.set(t,e)}));for(let[e,r]of t.attributes)s.attributes.set(e,r)})),Object.entries(n.children).forEach((([e,t])=>{if(null!==t){let r=f(p(h(t),a),u(t));if(!r)throw new Error(`Unknown node ${t}`);s.children.set(e,r)}else s.children.delete(e)})),Object.entries(n.attributes).forEach((([e,t])=>{s.attributes.set(e,t)}))),t.children.forEach(((t,i)=>{w(`${e}/${i}`,t,r)})),t}var y=class extends Error{};function b(e,t,r,i){if(!e.optional||void 0!==t)if(e.inherits&&e.inherits.forEach((n=>{let s=i[n];if(!s)throw new y(`Unknown inherited schema id "${e.inherits}"`);b(s.value,t,r,i)})),"Boolean"===e.dataType){if("boolean"!=typeof t)throw new y(`Expected "${t}" to be of type boolean`)}else if("String"===e.dataType){if("string"!=typeof t)throw new y(`Expected "${t}" to be of type string`)}else if("DateTime"===e.dataType){if("string"!=typeof t)throw new y(`Expected "${t}" to be of type date`)}else if("Enum"===e.dataType){if("string"!=typeof t)throw new y(`Expected "${t}" to be of type string`);if(!(1===e.enumRestrictions.options.filter((e=>e===t)).length))throw new y(`Expected "${t}" to be one of [${e.enumRestrictions.options.join(",")}]`)}else if("Integer"===e.dataType){if("number"!=typeof t)throw new y(`Expected "${t}" to be of type int`)}else if("Real"===e.dataType){if("number"!=typeof t)throw new y(`Expected "${t}" to be of type real`)}else if("Reference"===e.dataType){if("string"!=typeof t)throw new y(`Expected "${t}" to be of type string`)}else if("Object"===e.dataType){if("object"!=typeof t)throw new y(`Expected "${t}" to be of type object`);e.objectRestrictions&&Object.keys(e.objectRestrictions.values).forEach((n=>{let s=e.objectRestrictions.values[n].optional,a=Object.hasOwn(t,n);if(!s||a){if(!a)throw new y(`Expected "${t}" to have key ${n}`);b(e.objectRestrictions.values[n],t[n],r+"."+n,i)}}))}else{if("Array"!==e.dataType)throw new y(`Unexpected datatype ${e.dataType}`);if(!Array.isArray(t))throw new y(`Expected "${t}" to be of type array`);t.forEach((t=>{b(e.arrayRestrictions.value,t,r+".<array>.",i)}))}}function m(e,t){t.forEach((t=>{Object.keys(t.attributes).filter((e=>!e.startsWith("__internal"))).forEach((r=>{if(!e[r])throw new y(`Missing schema "${r}" referenced by ["${t.path}"].attributes`);let i=e[r],n=t.attributes[r];try{b(i.value,n,"",e)}catch(e){throw e instanceof y?new y(`Error validating ["${t.path}"].attributes["${r}"]: ${e.message}`):e}}))}))}function g(e){let t=new Map;return e.forEach((e=>{let r={path:e.path,children:e.children?e.children:{},inherits:e.inherits?e.inherits:{},attributes:e.attributes?e.attributes:{}};o(t,r.path,r)})),t}function E(e,t=!0,r=!0){let i=function(e){let t=new Map;for(let[r,i]of e)t.set(r,d(r,i));return t}(g(e.data));try{t&&m(e.schemas,i)}catch(e){throw e}return r?function(e){let t=c(e);if(!t)throw new l;let r={node:"",attributes:new Map,children:new Map};return t.forEach((t=>{r.children.set(t,p(t,e))})),r}(i):function(e){let t=c(e);if(!t)throw new l;return p([...t.values()][0],e)}(i)}function v(e){if(0===e.length)throw new Error("Trying to federate empty set of files");let t={header:e[0].header,schemas:{},data:[]};return e.forEach((e=>{Object.keys(e.schemas).forEach((r=>t.schemas[r]=e.schemas[r]))})),e.forEach((e=>{e.data.forEach((e=>t.data.push(e)))})),function(e,t=!1){let r={header:e.header,imports:[],schemas:e.schemas,data:[]};return g(e.data).forEach((e=>{let i=function(e,t=!1){let r={path:e[0].path,children:{},inherits:{},attributes:{}};if(e.forEach((e=>{Object.keys(e.children).forEach((t=>{r.children[t]=e.children[t]})),Object.keys(e.inherits).forEach((t=>{r.inherits[t]=e.inherits[t]})),Object.keys(e.attributes).forEach((t=>{r.attributes[t]=e.attributes[t]}))})),t){let e=!0;if(Object.keys(r.children).forEach((t=>{null!==r.children[t]&&(e=!1)})),Object.keys(r.inherits).forEach((t=>{null!==r.inherits[t]&&(e=!1)})),Object.keys(r.attributes).forEach((t=>{null!==r.attributes[t]&&(e=!1)})),e)return null}return r}(e,t);i&&r.data.push({path:i.path,children:i.children,inherits:i.inherits,attributes:i.attributes})})),r}(t)}var x,A,O=class{layers;tree;schemas;federated;constructor(e){this.layers=e,this.Compose()}GetLayerIds(){return this.layers.map((e=>e.header.id))}Compose(){this.federated=v(this.layers),this.schemas=this.federated.schemas,this.tree=E(this.federated)}GetFullTree(){return this.Compose(),this.tree}GetFederatedLayer(){return this.federated}GetSchemas(){return this.schemas}},F=class{provider;mainLayerId=null;constructor(e){this.provider=e}FromId(e){return this.mainLayerId=e,this}async Build(){if(!this.mainLayerId)throw new Error("no main layer ID specified");let e=await this.BuildLayerSet(this.mainLayerId);if(e instanceof Error)return e;try{return new O(e)}catch(e){return e}}async SatisfyDependencies(e,t,r){let i=[];for(const r of e.imports)if(!t.has(r.uri)){let e=await this.provider.GetLayerByURI(r.uri);if(e instanceof Error)return e;i.push(e),t.set(r.uri,!0)}let n=[];for(const e of i){n.push(e);let i=await this.SatisfyDependencies(e,t,r);if(i instanceof Error)return i;n.push(...i)}return n.forEach((e=>r.push(e))),n}async BuildLayerSet(e){let t=await this.provider.GetLayerByURI(e);if(t instanceof Error)return t;let r=[t],i=new Map;i.set(t.header.id,!0);let n=await this.SatisfyDependencies(t,i,r);return n instanceof Error?n:r}};function I(e,t,r){let i={name:e,attributes:{},children:[]};return t.children.forEach(((t,n)=>{i.children?.push(I(`${e}/${n}`,t,r))})),t.attributes.forEach(((e,t)=>{if(e&&"object"==typeof e&&!Array.isArray(e))Object.keys(e).forEach((r=>{i.attributes[`${t}::${r}`]=e[r]}));else{let n=r[t];if(n&&n.value.quantityKind){let r="",s=n.value.quantityKind;"Length"===s?r="m":"Volume"===s&&(r="m"+String.fromCodePoint(179)),i.attributes[t]=`${e} ${r}`}else i.attributes[t]=e}})),0===Object.keys(i.attributes).length&&delete i.attributes,i}async function z(e){let t={header:{...e[0].header},imports:e.map((e=>({uri:e.header.id}))),schemas:{},data:[]};t.header.id="USER_DEF";let r=new n([(new s).AddAll([t,...e]),new a]),i=await new F(r).FromId(t.header.id).Build();if(i instanceof Error)throw i;return i.GetFederatedLayer().data.forEach(((e,t)=>{e.attributes=e.attributes||{},e.attributes[`__internal_${t}`]=e.path})),I("",i.GetFullTree(),i.GetSchemas())}var B=[],L=!0,M={},S={};function _(e,t){return!(!e||!e.attributes)&&!!e.attributes[t]}function C(e){let t={color:new i.Color(.6,.6,.6),transparent:!1,opacity:1};for(let r of e){const e=r.attributes?r.attributes["bsi::ifc::presentation::diffuseColor"]:null;if(e){t.color=new i.Color(...e);const n=r.attributes["bsi::ifc::presentation::opacity"];n&&(t.transparent=!0,t.opacity=n);break}}return t}function $(e){let t=new Float32Array(e[0].attributes["usd::usdgeom::mesh::points"].flat()),r=new Uint16Array(e[0].attributes["usd::usdgeom::mesh::faceVertexIndices"]);const n=new i.BufferGeometry;var s;n.setAttribute("position",new i.BufferAttribute(t,3)),n.setIndex(new i.BufferAttribute(r,1)),n.computeVertexNormals();let a=function(e){for(let t of e){if(!t.attributes)continue;const e=t.attributes["gltf::material::pbrMetallicRoughness"],r=t.attributes["gltf::material::normalTexture"],n=t.attributes["gltf::material::occlusionTexture"],s=t.attributes["gltf::material::emissiveTexture"],a=t.attributes["gltf::material::emissiveFactor"],o=t.attributes["gltf::material::alphaMode"],l=t.attributes["gltf::material::alphaCutoff"],c=t.attributes["gltf::material::doubleSided"];if(!(e||r||n||s||a||o||l||c))continue;let h=new i.MeshStandardMaterial;if(h.color=new i.Color(1,1,1),h.metalness=1,h.roughness=1,e){let t=e.baseColorFactor;t&&(h.color=new i.Color(t[0],t[1],t[2]));let r=e.metallicFactor;void 0!==r&&(h.metalness=r);let n=e.roughnessFactor;void 0!==n&&(h.roughness=n)}return h.envMap=void 0,h.needsUpdate=!0,h.envMapRotation=new i.Euler(.5*Math.PI,0,0),h}}(e);if(a)s=a;else{const t=C(e);s=new i.MeshLambertMaterial({...t})}return new i.Mesh(n,s)}function j(e,t){const r=new i.PointsMaterial;return r.sizeAttenuation=!1,r.fog=!0,r.size=5,r.color=new i.Color(t?16777215:0),t&&(r.vertexColors=!0),new i.Points(e,r)}function D(e){let t;try{t=atob(e)}catch(e){throw new Error("base64 encoded string is invalid")}const r=new Uint8Array(t.length);for(let e=0;e<t.length;++e)r[e]=t.charCodeAt(e);return r.buffer}function T(e,t,n){const s=e[0];let a=new i.Group;if(_(s,"usd::usdgeom::visibility::visibility")){if("invisible"===s.attributes["usd::usdgeom::visibility::visibility"])return}else _(s,"usd::usdgeom::mesh::points")?a=$(e):_(s,"usd::usdgeom::basiscurves::points")?a=function(e){let t=new Float32Array(e[0].attributes["usd::usdgeom::basiscurves::points"].flat());const r=new i.BufferGeometry;r.setAttribute("position",new i.BufferAttribute(t,3));const n=C(e);let s=new i.LineBasicMaterial({...n});return s.color.multiplyScalar(.8),new i.Line(r,s)}(e):_(s,"pcd::base64")?a=function(e){const t=e[0].attributes["pcd::base64"],i=atob(t),n=i.length,s=new Uint8Array(n);for(let e=0;e<n;e++)s[e]=i.charCodeAt(e);const a=(new r).parse(s.buffer);return a.material.sizeAttenuation=!1,a.material.size=2,a}(e):_(s,"points::array::positions")?a=function(e){const t=new i.BufferGeometry,r=new Float32Array(e[0].attributes["points::array::positions"].flat());t.setAttribute("position",new i.Float32BufferAttribute(r,3));const n=e[0].attributes["points::array::colors"];if(n){const e=new Float32Array(n.flat());t.setAttribute("color",new i.Float32BufferAttribute(e,3))}return j(t,n)}(e):_(s,"points::base64::positions")&&(a=function(e){const t=new i.BufferGeometry,r=D(e[0].attributes["points::base64::positions"]);if(!r)return null;const n=new Float32Array(r);t.setAttribute("position",new i.Float32BufferAttribute(n,3));const s=e[0].attributes["points::base64::colors"];if(s){const e=D(s);if(e){const r=new Float32Array(e);t.setAttribute("color",new i.Float32BufferAttribute(r,3))}}return j(t,s)}(e));M[s.name]=a,S[s.name]=s,a.userData.path=s.name;for(let e of Object.entries(s.attributes||{}).filter((([e,t])=>e.startsWith("__internal_"))).map((([e,t])=>t)))(n[String(e)]=n[String(e)]||[]).push(s.name);if(t.add(a),e.length>1){a.matrixAutoUpdate=!1;let e=s.attributes&&s.attributes["usd::xformop::transform"]?s.attributes["usd::xformop::transform"].flat():null;if(e){let t=new i.Matrix4;t.set(...e),t.transpose(),a.matrix=t}}(s.children||[]).forEach((r=>T([r,...e],a||t,n)))}async function U(){if(x&&(x.children=[]),M={},S={},0===B.length)return;let e=null,t=B.map((e=>e[1]));if(e=await z(t),!e)return void console.error("No result from composition");x||(x=new i.Scene,(A=new i.PerspectiveCamera(75,window.innerWidth/window.innerHeight,.1,100)).up.set(0,0,1),A.position.set(50,50,50),A.lookAt(0,0,0),x.add(A));if(T([e],x,{}),L){const e=new i.Box3;if(e.setFromObject(x),!e.isEmpty()){let t=e.min.clone().add(e.max).multiplyScalar(.5),r=e.max.clone().sub(e.min).length();A.position.copy(t.clone().add(new i.Vector3(1,1,1).normalize().multiplyScalar(r))),A.far=3*r,A.updateProjectionMatrix(),L=!1}}}async function G(e,t){return B.push([t,e]),await U(),x}function R(){x=void 0,B.length=0,L=!0}class k extends t.Loader{load(e,r,i,n){const s=this.manager;s.itemStart(e);const a=t=>{r(t),s.itemEnd(e)},o=t=>{n?n(t):console.error(t),s.itemError(e),s.itemEnd(e)},l=new t.FileLoader(this.manager);l.setPath(this.path),l.setResponseType("json"),l.setRequestHeader(this.requestHeader),l.setWithCredentials(this.withCredentials),l.load(e,(e=>this.parse(e,a,o)),i,n)}parse(e,t,r){G(e).then((e=>t(e))).catch((e=>r(e))).finally((()=>R()))}}class V extends e.Loader{constructor(e){super(),this.viewer=e}isSupport(e,t){return("string"==typeof e||e instanceof globalThis.File||e instanceof ArrayBuffer)&&/(ifcx)$/i.test(t)}async load(t,r,i){const n=new e.GLTFLoadingManager(t,i),s=new k(n);s.setPath(n.path),s.setCrossOrigin(i.crossOrigin||s.crossOrigin),s.setWithCredentials(i.withCredentials||s.withCredentials);const a=await s.loadAsync(n.fileURL,(e=>{const{lengthComputable:r,loaded:i,total:n}=e,s=r?i/n:1;this.viewer.emitEvent({type:"geometryprogress",data:s,file:t})}));if(!this.viewer.scene)return this;let o=0;a.traverse((e=>{e.userData={handle:o,...e.userData},o++}));const l=new e.ModelImpl(a);return l.loader=this,l.viewer=this.viewer,this.viewer.scene.add(a),this.viewer.models.push(l),this.viewer.syncOptions(),this.viewer.syncOverlay(),this.viewer.update(),this.viewer.emitEvent({type:"databasechunk",data:a,file:t}),this}}class P extends e.Loader{constructor(e){super(),this.viewer=e}isSupport(e){return"object"==typeof e&&"string"==typeof e.type&&"function"==typeof e.download&&/.ifcx$/i.test(e.type)}async load(t){const r=await t.download((e=>{this.viewer.emitEvent({type:"geometryprogress",data:e,file:t})}),this.abortController.signal);if(!this.viewer.scene)return this;const i=new TextDecoder,n=JSON.parse(i.decode(r)),s=await G(n);R();let a=0;s.traverse((e=>{e.userData={handle:a,...e.userData},a++}));const o=new e.ModelImpl(s);return o.loader=this,o.viewer=this.viewer,this.viewer.scene.add(s),this.viewer.models.push(o),this.viewer.syncOptions(),this.viewer.syncOverlay(),this.viewer.update(),this.viewer.emitEvent({type:"databasechunk",data:s,file:t}),this}}e.loaders.registerLoader("ifcx-file",(e=>new V(e))),e.loaders.registerLoader("ifcx-cloud",(e=>new P(e)))}));