@xeokit/xeokit-convert
Version:
JavaScript utilities to create .XKT files
55 lines (54 loc) • 3.24 kB
TypeScript
/**
* @desc Parses STL file data into an {@link XKTModel}.
*
* * Supports binary and ASCII STL formats.
* * Option to create a separate {@link XKTEntity} for each group of faces that share the same vertex colors.
* * Option to smooth face-aligned normals loaded from STL.
* * Option to reduce XKT file size by ignoring STL normals and relying on xeokit to auto-generate them.
*
* ## Usage
*
* In the example below we'll create an {@link XKTModel}, then load an STL model into it.
*
* ````javascript
* utils.loadArraybuffer("./models/stl/binary/spurGear.stl", async (data) => {
*
* const xktModel = new XKTModel();
*
* parseSTLIntoXKTModel({data, xktModel});
*
* xktModel.finalize();
* });
* ````
*
* @param {Object} params Parsing params.
* @param {ArrayBuffer|String} [params.data] STL file data. Can be binary or string.
* @param {Boolean} [params.autoNormals=false] When true, the parser will ignore the STL geometry normals, and the STL
* data will rely on the xeokit ````Viewer```` to automatically generate them. This has the limitation that the
* normals will be face-aligned, and therefore the ````Viewer```` will only be able to render a flat-shaded representation
* of the STL.
* Overrides ````smoothNormals```` when ````true````. This ignores the normals in the STL, and loads no
* normals from the STL into the {@link XKTModel}, resulting in the XKT file storing no normals for the STL model. The
* xeokit-sdk will then automatically generate the normals within its shaders. The disadvantages are that auto-normals
* may slow rendering down a little bit, and that the normals can only be face-aligned (and thus rendered using flat
* shading). The advantages, however, are a smaller XKT file size, and the ability to apply certain geometry optimizations
* during parsing, such as removing duplicated STL vertex positions, that are not possible when normals are loaded
* for the STL vertices.
* @param {Boolean} [params.smoothNormals=true] When true, automatically converts face-oriented STL normals to vertex normals, for a smooth appearance. Ignored if ````autoNormals```` is ````true````.
* @param {Number} [params.smoothNormalsAngleThreshold=20] This is the threshold angle between normals of adjacent triangles, below which their shared wireframe edge is not drawn.
* @param {Boolean} [params.splitMeshes=true] When true, creates a separate {@link XKTEntity} for each group of faces that share the same vertex colors. Only works with binary STL (ie. when ````data```` is an ArrayBuffer).
* @param {XKTModel} [params.xktModel] XKTModel to parse into.
* @param {Object} [params.stats] Collects statistics.
* @param {function} [params.log] Logging callback.
@returns {Promise} Resolves when STL has been parsed.
*/
export function parseSTLIntoXKTModel({ data, splitMeshes, autoNormals, smoothNormals, smoothNormalsAngleThreshold, xktModel, stats, log }: {
data?: string | ArrayBuffer | undefined;
autoNormals?: boolean | undefined;
smoothNormals?: boolean | undefined;
smoothNormalsAngleThreshold?: number | undefined;
splitMeshes?: boolean | undefined;
xktModel?: any;
stats?: Object | undefined;
log?: Function | undefined;
}): Promise<any>;