UNPKG

@xeokit/xeokit-convert

Version:

JavaScript utilities to create .XKT files

484 lines (483 loc) 21.6 kB
/** * A document model that represents the contents of an .XKT file. * * * An XKTModel contains {@link XKTTile}s, which spatially subdivide the model into axis-aligned, box-shaped regions. * * Each {@link XKTTile} contains {@link XKTEntity}s, which represent the objects within its region. * * Each {@link XKTEntity} has {@link XKTMesh}s, which each have a {@link XKTGeometry}. Each {@link XKTGeometry} can be shared by multiple {@link XKTMesh}s. * * Import models into an XKTModel using {@link parseGLTFJSONIntoXKTModel}, {@link parseIFCIntoXKTModel}, {@link parseCityJSONIntoXKTModel} etc. * * Build an XKTModel programmatically using {@link XKTModel#createGeometry}, {@link XKTModel#createMesh} and {@link XKTModel#createEntity}. * * Serialize an XKTModel to an ArrayBuffer using {@link writeXKTModelToArrayBuffer}. * * ## Usage * * See [main docs page](/docs/#javascript-api) for usage examples. * * @class XKTModel */ export class XKTModel { /** * Constructs a new XKTModel. * * @param {*} [cfg] Configuration * @param {Number} [cfg.edgeThreshold=10] * @param {Number} [cfg.minTileSize=500] * @param {Number} [cfg.maxIndicesForEdge=10000] */ constructor(cfg?: any); /** * The model's ID, if available. * * Will be "default" by default. * * @type {String} */ modelId: string; /** * The project ID, if available. * * Will be an empty string by default. * * @type {String} */ projectId: string; /** * The revision ID, if available. * * Will be an empty string by default. * * @type {String} */ revisionId: string; /** * The model author, if available. * * Will be an empty string by default. * * @property author * @type {String} */ author: string; /** * The date the model was created, if available. * * Will be an empty string by default. * * @property createdAt * @type {String} */ createdAt: string; /** * The application that created the model, if available. * * Will be an empty string by default. * * @property creatingApplication * @type {String} */ creatingApplication: string; /** * The model schema version, if available. * * In the case of IFC, this could be "IFC2x3" or "IFC4", for example. * * Will be an empty string by default. * * @property schema * @type {String} */ schema: string; /** * The XKT format version. * * @property xktVersion; * @type {number} */ xktVersion: number; /** * * @type {Number|number} */ edgeThreshold: number | number; /** * Minimum diagonal size of the boundary of an {@link XKTTile}. * * @type {Number|number} */ minTileSize: number | number; /** * Optional overall AABB that contains all the {@link XKTEntity}s we'll create in this model, if previously known. * * This is the AABB of a complete set of input files that are provided as a split-model set for conversion. * * This is used to help the {@link XKTTile.aabb}s within split models align neatly with each other, as we * build them with a k-d tree in {@link XKTModel#finalize}. Without this, the AABBs of the different parts * tend to misalign slightly, resulting in excess number of {@link XKTTile}s, which degrades memory and rendering * performance when the XKT is viewer in the xeokit Viewer. */ modelAABB: any; /** * Map of {@link XKTPropertySet}s within this XKTModel, each mapped to {@link XKTPropertySet#propertySetId}. * * Created by {@link XKTModel#createPropertySet}. * * @type {{String:XKTPropertySet}} */ propertySets: { String: XKTPropertySet; }; /** * {@link XKTPropertySet}s within this XKTModel. * * Each XKTPropertySet holds its position in this list in {@link XKTPropertySet#propertySetIndex}. * * Created by {@link XKTModel#finalize}. * * @type {XKTPropertySet[]} */ propertySetsList: XKTPropertySet[]; /** * Map of {@link XKTMetaObject}s within this XKTModel, each mapped to {@link XKTMetaObject#metaObjectId}. * * Created by {@link XKTModel#createMetaObject}. * * @type {{String:XKTMetaObject}} */ metaObjects: { String: XKTMetaObject; }; /** * {@link XKTMetaObject}s within this XKTModel. * * Each XKTMetaObject holds its position in this list in {@link XKTMetaObject#metaObjectIndex}. * * Created by {@link XKTModel#finalize}. * * @type {XKTMetaObject[]} */ metaObjectsList: XKTMetaObject[]; /** * The positions of all shared {@link XKTGeometry}s are de-quantized using this singular * de-quantization matrix. * * This de-quantization matrix is generated from the collective Local-space boundary of the * positions of all shared {@link XKTGeometry}s. * * @type {Float32Array} */ reusedGeometriesDecodeMatrix: Float32Array; /** * Map of {@link XKTGeometry}s within this XKTModel, each mapped to {@link XKTGeometry#geometryId}. * * Created by {@link XKTModel#createGeometry}. * * @type {{Number:XKTGeometry}} */ geometries: { Number: XKTGeometry; }; /** * List of {@link XKTGeometry}s within this XKTModel, in the order they were created. * * Each XKTGeometry holds its position in this list in {@link XKTGeometry#geometryIndex}. * * Created by {@link XKTModel#finalize}. * * @type {XKTGeometry[]} */ geometriesList: XKTGeometry[]; /** * Map of {@link XKTTexture}s within this XKTModel, each mapped to {@link XKTTexture#textureId}. * * Created by {@link XKTModel#createTexture}. * * @type {{Number:XKTTexture}} */ textures: { Number: XKTTexture; }; /** * List of {@link XKTTexture}s within this XKTModel, in the order they were created. * * Each XKTTexture holds its position in this list in {@link XKTTexture#textureIndex}. * * Created by {@link XKTModel#finalize}. * * @type {XKTTexture[]} */ texturesList: XKTTexture[]; /** * Map of {@link XKTTextureSet}s within this XKTModel, each mapped to {@link XKTTextureSet#textureSetId}. * * Created by {@link XKTModel#createTextureSet}. * * @type {{Number:XKTTextureSet}} */ textureSets: { Number: XKTTextureSet; }; /** * List of {@link XKTTextureSet}s within this XKTModel, in the order they were created. * * Each XKTTextureSet holds its position in this list in {@link XKTTextureSet#textureSetIndex}. * * Created by {@link XKTModel#finalize}. * * @type {XKTTextureSet[]} */ textureSetsList: XKTTextureSet[]; /** * Map of {@link XKTMesh}s within this XKTModel, each mapped to {@link XKTMesh#meshId}. * * Created by {@link XKTModel#createMesh}. * * @type {{Number:XKTMesh}} */ meshes: { Number: XKTMesh; }; /** * List of {@link XKTMesh}s within this XKTModel, in the order they were created. * * Each XKTMesh holds its position in this list in {@link XKTMesh#meshIndex}. * * Created by {@link XKTModel#finalize}. * * @type {XKTMesh[]} */ meshesList: XKTMesh[]; /** * Map of {@link XKTEntity}s within this XKTModel, each mapped to {@link XKTEntity#entityId}. * * Created by {@link XKTModel#createEntity}. * * @type {{String:XKTEntity}} */ entities: { String: XKTEntity; }; /** * {@link XKTEntity}s within this XKTModel. * * Each XKTEntity holds its position in this list in {@link XKTEntity#entityIndex}. * * Created by {@link XKTModel#finalize}. * * @type {XKTEntity[]} */ entitiesList: XKTEntity[]; /** * {@link XKTTile}s within this XKTModel. * * Created by {@link XKTModel#finalize}. * * @type {XKTTile[]} */ tilesList: XKTTile[]; /** * The axis-aligned 3D World-space boundary of this XKTModel. * * Created by {@link XKTModel#finalize}. * * @type {Float64Array} */ aabb: Float64Array; /** * Indicates if this XKTModel has been finalized. * * Set ````true```` by {@link XKTModel#finalize}. * * @type {boolean} */ finalized: boolean; /** * Maximum number of indices in a mesh. Above this, edges are not calculated. * * @type {Number} */ maxIndicesForEdge: number; /** * Creates an {@link XKTPropertySet} within this XKTModel. * * Logs error and does nothing if this XKTModel has been finalized (see {@link XKTModel#finalized}). * * @param {*} params Method parameters. * @param {String} params.propertySetId Unique ID for the {@link XKTPropertySet}. * @param {String} [params.propertySetType="default"] A meta type for the {@link XKTPropertySet}. * @param {String} [params.propertySetName] Human-readable name for the {@link XKTPropertySet}. Defaults to the ````propertySetId```` parameter. * @param {String[]} params.properties Properties for the {@link XKTPropertySet}. * @returns {XKTPropertySet} The new {@link XKTPropertySet}. */ createPropertySet(params: any): XKTPropertySet; /** * Creates an {@link XKTMetaObject} within this XKTModel. * * Logs error and does nothing if this XKTModel has been finalized (see {@link XKTModel#finalized}). * * @param {*} params Method parameters. * @param {String} params.metaObjectId Unique ID for the {@link XKTMetaObject}. * @param {String} params.propertySetIds ID of one or more property sets that contains additional metadata about * this {@link XKTMetaObject}. The property sets could be stored externally (ie not managed at all by the XKT file), * or could be {@link XKTPropertySet}s within {@link XKTModel#propertySets}. * @param {String} [params.metaObjectType="default"] A meta type for the {@link XKTMetaObject}. Can be anything, * but is usually an IFC type, such as "IfcSite" or "IfcWall". * @param {String} [params.metaObjectName] Human-readable name for the {@link XKTMetaObject}. Defaults to the ````metaObjectId```` parameter. * @param {String} [params.parentMetaObjectId] ID of the parent {@link XKTMetaObject}, if any. Defaults to the ````metaObjectId```` parameter. * @returns {XKTMetaObject} The new {@link XKTMetaObject}. */ createMetaObject(params: any): XKTMetaObject; _rootMetaObject: any; /** * Creates an {@link XKTTexture} within this XKTModel. * * Registers the new {@link XKTTexture} in {@link XKTModel#textures} and {@link XKTModel#texturesList}. * * Logs error and does nothing if this XKTModel has been finalized (see {@link XKTModel#finalized}). * * @param {*} params Method parameters. * @param {Number} params.textureId Unique ID for the {@link XKTTexture}. * @param {String} [params.src] Source of an image file for the texture. * @param {Buffer} [params.imageData] Image data for the texture. * @param {Number} [params.mediaType] Media type (ie. MIME type) of ````imageData````. Supported values are {@link GIFMediaType}, {@link PNGMediaType} and {@link JPEGMediaType}. * @param {Number} [params.width] Texture width, used with ````imageData````. Ignored for compressed textures. * @param {Number} [params.height] Texture height, used with ````imageData````. Ignored for compressed textures. * @param {Boolean} [params.compressed=true] Whether to compress the texture. * @param {Number} [params.minFilter=LinearMipMapNearestFilter] How the texture is sampled when a texel covers less than one pixel. Supported * values are {@link LinearMipmapLinearFilter}, {@link LinearMipMapNearestFilter}, {@link NearestMipMapNearestFilter}, * {@link NearestMipMapLinearFilter} and {@link LinearMipMapLinearFilter}. Ignored for compressed textures. * @param {Number} [params.magFilter=LinearMipMapNearestFilter] How the texture is sampled when a texel covers more than one pixel. Supported values * are {@link LinearFilter} and {@link NearestFilter}. Ignored for compressed textures. * @param {Number} [params.wrapS=RepeatWrapping] Wrap parameter for texture coordinate *S*. Supported values are {@link ClampToEdgeWrapping}, * {@link MirroredRepeatWrapping} and {@link RepeatWrapping}. Ignored for compressed textures. * @param {Number} [params.wrapT=RepeatWrapping] Wrap parameter for texture coordinate *T*. Supported values are {@link ClampToEdgeWrapping}, * {@link MirroredRepeatWrapping} and {@link RepeatWrapping}. Ignored for compressed textures. * {@param {Number} [params.wrapR=RepeatWrapping] Wrap parameter for texture coordinate *R*. Supported values are {@link ClampToEdgeWrapping}, * {@link MirroredRepeatWrapping} and {@link RepeatWrapping}. Ignored for compressed textures. * @returns {XKTTexture} The new {@link XKTTexture}. */ createTexture(params: any): XKTTexture; /** * Creates an {@link XKTTextureSet} within this XKTModel. * * Registers the new {@link XKTTextureSet} in {@link XKTModel#textureSets} and {@link XKTModel#.textureSetsList}. * * Logs error and does nothing if this XKTModel has been finalized (see {@link XKTModel#finalized}). * * @param {*} params Method parameters. * @param {Number} params.textureSetId Unique ID for the {@link XKTTextureSet}. * @param {*} [params.colorTextureId] ID of *RGBA* base color {@link XKTTexture}, with color in *RGB* and alpha in *A*. * @param {*} [params.metallicRoughnessTextureId] ID of *RGBA* metal-roughness {@link XKTTexture}, with the metallic factor in *R*, and roughness factor in *G*. * @param {*} [params.normalsTextureId] ID of *RGBA* normal {@link XKTTexture}, with normal map vectors in *RGB*. * @param {*} [params.emissiveTextureId] ID of *RGBA* emissive {@link XKTTexture}, with emissive color in *RGB*. * @param {*} [params.occlusionTextureId] ID of *RGBA* occlusion {@link XKTTexture}, with occlusion factor in *R*. * @returns {XKTTextureSet} The new {@link XKTTextureSet}. */ createTextureSet(params: any): XKTTextureSet; /** * Creates an {@link XKTGeometry} within this XKTModel. * * Registers the new {@link XKTGeometry} in {@link XKTModel#geometries} and {@link XKTModel#geometriesList}. * * Logs error and does nothing if this XKTModel has been finalized (see {@link XKTModel#finalized}). * * @param {*} params Method parameters. * @param {Number} params.geometryId Unique ID for the {@link XKTGeometry}. * @param {String} params.primitiveType The type of {@link XKTGeometry}: "triangles", "lines" or "points". * @param {Float64Array} params.positions Floating-point Local-space vertex positions for the {@link XKTGeometry}. Required for all primitive types. * @param {Number[]} [params.normals] Floating-point vertex normals for the {@link XKTGeometry}. Only used with triangles primitives. Ignored for points and lines. * @param {Number[]} [params.colors] Floating-point RGBA vertex colors for the {@link XKTGeometry}. Required for points primitives. Ignored for lines and triangles. * @param {Number[]} [params.colorsCompressed] Integer RGBA vertex colors for the {@link XKTGeometry}. Required for points primitives. Ignored for lines and triangles. * @param {Number[]} [params.uvs] Floating-point vertex UV coordinates for the {@link XKTGeometry}. Alias for ````uv````. * @param {Number[]} [params.uv] Floating-point vertex UV coordinates for the {@link XKTGeometry}. Alias for ````uvs````. * @param {Number[]} [params.colorsCompressed] Integer RGBA vertex colors for the {@link XKTGeometry}. Required for points primitives. Ignored for lines and triangles. * @param {Uint32Array} [params.indices] Indices for the {@link XKTGeometry}. Required for triangles and lines primitives. Ignored for points. * @param {Number} [params.edgeThreshold=10] * @param {Number} [params.meshTreshold=100000] The threshold for size of the mesh. Only used for triangles primitives.} * @returns {XKTGeometry} The new {@link XKTGeometry}. */ createGeometry(params: any): XKTGeometry; _createDefaultIndices(numIndices: any): number[]; /** * Creates an {@link XKTMesh} within this XKTModel. * * An {@link XKTMesh} can be owned by one {@link XKTEntity}, which can own multiple {@link XKTMesh}es. * * Registers the new {@link XKTMesh} in {@link XKTModel#meshes} and {@link XKTModel#meshesList}. * * @param {*} params Method parameters. * @param {Number} params.meshId Unique ID for the {@link XKTMesh}. * @param {Number} params.geometryId ID of an existing {@link XKTGeometry} in {@link XKTModel#geometries}. * @param {Number} [params.textureSetId] Unique ID of an {@link XKTTextureSet} in {@link XKTModel#textureSets}. * @param {Float32Array} params.color RGB color for the {@link XKTMesh}, with each color component in range [0..1]. * @param {Number} [params.metallic=0] How metallic the {@link XKTMesh} is, in range [0..1]. A value of ````0```` indicates fully dielectric material, while ````1```` indicates fully metallic. * @param {Number} [params.roughness=1] How rough the {@link XKTMesh} is, in range [0..1]. A value of ````0```` indicates fully smooth, while ````1```` indicates fully rough. * @param {Number} params.opacity Opacity factor for the {@link XKTMesh}, in range [0..1]. * @param {Float64Array} [params.matrix] Modeling matrix for the {@link XKTMesh}. Overrides ````position````, ````scale```` and ````rotation```` parameters. * @param {Number[]} [params.position=[0,0,0]] Position of the {@link XKTMesh}. Overridden by the ````matrix```` parameter. * @param {Number[]} [params.scale=[1,1,1]] Scale of the {@link XKTMesh}. Overridden by the ````matrix```` parameter. * @param {Number[]} [params.rotation=[0,0,0]] Rotation of the {@link XKTMesh} as Euler angles given in degrees, for each of the X, Y and Z axis. Overridden by the ````matrix```` parameter. * @returns {XKTMesh} The new {@link XKTMesh}. */ createMesh(params: any): XKTMesh; /** * Creates an {@link XKTEntity} within this XKTModel. * * Registers the new {@link XKTEntity} in {@link XKTModel#entities} and {@link XKTModel#entitiesList}. * * Logs error and does nothing if this XKTModel has been finalized (see {@link XKTModel#finalized}). * * @param {*} params Method parameters. * @param {String} params.entityId Unique ID for the {@link XKTEntity}. * @param {String[]} params.meshIds IDs of {@link XKTMesh}es used by the {@link XKTEntity}. Note that each {@link XKTMesh} can only be used by one {@link XKTEntity}. * @returns {XKTEntity} The new {@link XKTEntity}. */ createEntity(params: any): XKTEntity; /** * Creates a default {@link XKTMetaObject} for each {@link XKTEntity} that does not already have one. */ createDefaultMetaObjects(): void; /** * Finalizes this XKTModel. * * After finalizing, we may then serialize the model to an array buffer using {@link writeXKTModelToArrayBuffer}. * * Logs error and does nothing if this XKTModel has already been finalized. * * Internally, this method: * * * for each {@link XKTEntity} that doesn't already have a {@link XKTMetaObject}, creates one with {@link XKTMetaObject#metaObjectType} set to "default" * * sets each {@link XKTEntity}'s {@link XKTEntity#hasReusedGeometries} true if it shares its {@link XKTGeometry}s with other {@link XKTEntity}s, * * creates each {@link XKTEntity}'s {@link XKTEntity#aabb}, * * creates {@link XKTTile}s in {@link XKTModel#tilesList}, and * * sets {@link XKTModel#finalized} ````true````. */ finalize(): Promise<void>; _removeUnusedTextures(): void; _compressTextures(): Promise<any>; _bakeSingleUseGeometryPositions(): void; _bakeAndOctEncodeNormals(): void; _createEntityAABBs(): void; _createKDTree(): KDNode; _insertEntityIntoKDTree(kdNode: any, entity: any): void; _createTilesFromKDTree(rootKDNode: any): void; _createTilesFromKDNode(kdNode: any): void; /** * Creates a tile from the given entities. * * For each single-use {@link XKTGeometry}, this method centers {@link XKTGeometry#positions} to make them relative to the * tile's center, then quantizes the positions to unsigned 16-bit integers, relative to the tile's boundary. * * @param kdNode */ _createTileFromEntities(kdNode: any): void; _createReusedGeometriesDecodeMatrix(): void; _flagSolidGeometries(): void; } import { XKTPropertySet } from "./XKTPropertySet.js"; import { XKTMetaObject } from "./XKTMetaObject.js"; import { XKTGeometry } from './XKTGeometry.js'; import { XKTTexture } from "./XKTTexture.js"; import { XKTTextureSet } from "./XKTTextureSet.js"; import { XKTMesh } from './XKTMesh.js'; import { XKTEntity } from './XKTEntity.js'; import { XKTTile } from './XKTTile.js'; import { KDNode } from "./KDNode.js";