npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xeokit-xkt-utils

v0.0.1

Published

JavaScript utilities to create .XKT files

Downloads

18

Readme

xeokit-xkt-utils

JavaScript tools to generate .XKT files

Spatial partitioning

Resources

Contents

Features

Support for Large WGS84 Coordinates

Using graphics APIs such as WebGL, graphics processing units (GPUs) internally operate on single precision 32-bit floating-point numbers.

Single precision values are generally said to have seven accurate decimal digits; therefore as your numbers become larger, the numbers are less accurately represented.

xeokit-xkt-utils improves the accuracy of the math executed on the GPU beyond the GPU's single precision limitations by using relative-to-eye coordinates [TODO]

Compressed Geometry

Usage

Classes

xeokit-xkt-utils provides five javaScript classes from which we can build an in-memory "document-object model" that represents the contents of an .xkt file.

  • XKTModel represents an .xkt model, providing methods through which we can create 3D objects within the model.
  • XKTPrimitive represents an individual mesh, which has vertex positions, vertex normals, triangle indices, edge indices, an RGB color, and an opacity.
  • XKTPrimitiveInstance is an association class that represents the use of an XKTPrimitive by an XKTEntity.
  • XKTEntity represents a 3D object, which has a unique ID, and one or more PrimitiveInstances.
  • XKTTile represents a box-shaped region within the XKTModel. Each XKTTile has one or more XKTEntitys, a World-space axis-aligned bounding box (AABB) that encloses the XKTEntitys, and a decoding matrix to de-quantize the vertex positions belonging to the primitives instanced by the entities.

Class diagram

Functions

xeokit-xkt-utils also provides functions for loading, serializing and testing XKTModels:

Building an XKTModel

In the example below, we'll programmatically build a simple XKTModel resembling this table:

Spatial partitioning

const {XKTModel, loadGLTFIntoXKTModel, writeXKTModelToArrayBuffer} = require("./xeokit-xkt-utils.cjs.js");

const xktModel = new XKTModel();

xktModel.createPrimitive({
     primitiveId: "legPrimitive",
     primitiveType: "triangles",
     positions: [
         1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1,
         -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1,
         -1, -1, -1, -1, -1, 1, -1, 1, 1, -1
     ],
     normals: [
         0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
         -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, -1, 0, 0, -1, 0, 0,
         -1, 0, 0, -1
     ],
     indices: [
         0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19,
         20, 21, 22, 20, 22, 23
     ],
     color: [255, 0, 0],
     opacity: 255
 });

xktModel.createEntity({
     entityId: "leg1",
     primitiveIds: ["legPrimitive"],
     position: [-4, -6, -4],
     scale: [1, 3, 1],
     rotation: [0, 0, 0]
 });

xktModel.createEntity({
     entityId: "leg2",
     primitiveIds: ["legPrimitive"],
     position: [4, -6, -4],
     scale: [1, 3, 1],
     rotation: [0, 0, 0]
 });

xktModel.createEntity({
     entityId: "leg3",
     primitiveIds: ["legPrimitive"],
     position: [4, -6, 4],
     scale: [1, 3, 1],
     rotation: [0, 0, 0]
 });

xktModel.createEntity({
     entityId: "leg4",
     primitiveIds: ["legPrimitive"],
     position: [-4, -6, 4],
     scale: [1, 3, 1],
     rotation: [0, 0, 0]
 });

xktModel.createEntity({
     entityId: "top",
     primitiveIds: ["legPrimitive"],
     position: [0, -3, 0],
     scale: [6, 0.5, 6],
     rotation: [0, 0, 0]
 });

Once we've built our XKTModel we need to finalize it:

xktModel.finalize();

Serializing the XKTModel to an ArrayBuffer

Next, we'll use writeXKTModelToArrayBuffer to serialize our XKTModel to an ArrayBuffer:

const xktArrayBuffer = writeXKTModelToArrayBuffer(xktModel);

Validating the ArrayBuffer

Now we'll use validateXKTArrayBuffer to validate the ArrayBuffer against our XKTModel. If this function finds any errors, it will log them to the console and return false. Otherwise, it will return true to indicate that the ArrayBuffer is correct.

const xktArrayBufferValid = validateXKTArrayBuffer(xktArrayBuffer, xktModel);

if (!xktArrayBufferValid) {
    console.error("XKT array buffer is invalid!");
}

Loading the ArrayBuffer into a Viewer

Let's now create a Viewer and load the ArrayBuffer into it using an XKTLoaderPlugin:

const viewer = new Viewer({
    canvasId: "myCanvas"
});

const xktLoader = new XKTLoaderPlugin(viewer);

xktLoader.load({
    id: "myModel",
    xkt: xktArrayBuffer
});

The XKTLoaderPlugin can also load our ArrayBuffer from a file.

Finally, we'll fit the model in view:

viewer.cameraFlight.flyTo(viewer.scene);

Loading glTF into an XKTModel

Let's use loadGLTFIntoXKTModel to parse glTF into an XKTModel.

We'll also use the classes and functions introduced in the previous examples to serialize the XKTModel to an ArrayBuffer, then validate the ArrayBuffer and load it into a Viewer.

utils.loadJSON("./models/gltf/MAP/MAP.gltf", (json) => {

        const xktModel = new XKTModel();

        loadGLTFIntoXKTModel(json, xktModel, {basePath: ""}).then(() => {
      
            const xktArrayBuffer = writeXKTModelToArrayBuffer(xktModel);

            const xktArrayBufferValid = validateXKTArrayBuffer(xktArrayBuffer, xktModel);

            const viewer = new Viewer({
                canvasId: "myCanvas"
            });

            const xktLoader = new XKTLoaderPlugin(viewer);

            xktLoader.load({
                id: "myModel",
                xkt: xktArrayBuffer
            });

            viewer.cameraFlight.flyTo(viewer.scene);
        });
    },
    (errMsg) => {  });

Using in node.js

In the example below, we'll load the contents of a glTF file, then use loadGLTFIntoXKTModel to parse the glTF into an XKTModel, then we'll use writeXKTModelToArrayBuffer to serialize our XKTModel to an ArrayBuffer, which we finally write to an .xkt file.

const fs = require('fs');

const {XKTModel, loadGLTFIntoXKTModel, writeXKTModelToArrayBuffer} = require("./xeokit-xkt-utils.cjs.js");

const gltfPath = "./myModel.gltf";
const xktPath = "./myModel.xkt";

const gltfText = await new Promise((resolve, reject) => {
    fs.readFile(gltfPath, (error, gltfText) => {
        if (error !== null) {
            reject(error);
            return;
        }
        resolve(gltfText);
    });
});

const gltf = JSON.parse(gltfText);

const xktModel = new XKTModel();

loadGLTFIntoXKTModel(gltf, xktModel, {basePath: "./"});

const xktArrayBuffer = writeXKTModelToArrayBuffer(xktModel);
    
await new Promise((resolve, reject) => {
    fs.writeFile(xktPath, Buffer.from(xktArrayBuffer), (error) => {
        if (error !== null) {
            console.error(`Unable to write to file at path: ${xktPath}`);
            reject(error);
            return;
        }
        resolve();
    });
});