Reference Source

src/plugins/GroundShadowPlugin/GroundShadowPlugin.js

import {Plugin} from "../../viewer/Plugin.js";
import {ObjectsMemento} from "../../viewer/scene/mementos/ObjectsMemento.js";
import {CameraMemento} from "../../viewer/scene/mementos/CameraMemento.js";
import {ModelGroundShadow} from "./GroundShadow.js";




/**
 * @desc A {@link Viewer} plugin that provides methods for visualizing IfcBuildingStoreys.
 *
 *  <a href="https://xeokit.github.io/xeokit-sdk/examples/#GroundShadow_GroundShadowPlugin_recipe2"><img src="http://xeokit.io/img/docs/GroundShadowPlugin/minimap.gif"></a>
 *
 * [[Run this example](https://xeokit.github.io/xeokit-sdk/examples/#GroundShadow_GroundShadowPlugin_recipe2)]
 *
 * ## Overview
 *

 */
class GroundShadowPlugin extends Plugin {

    /**
     * @constructor
     *
     * @param {Viewer} viewer The Viewer.
     * @param {Object} cfg  Plugin configuration.
     * @param {String} [cfg.id="GroundShadow"] Optional ID for this plugin, so that we can find it within {@link Viewer#plugins}.
     */
    constructor(viewer, cfg = {}) {

        super("GroundShadow", viewer);

        this._objectsMemento = new ObjectsMemento();

        this._cameraMemento = new CameraMemento();

        /**
         * A {@link ModelGroundShadow} for each model.
         *
         * These are created and destroyed automatically as models are loaded and destroyed.
         *
         * @type {{String:ModelGroundShadow}}
         */
        this.groundShadows = {};

        this._onModelLoaded = this.viewer.scene.on("modelLoaded", (modelId) => {

            const groundShadow = new ModelGroundShadow(this, )
        });
    }

    _registerModel(modelId) {
        const viewer = this.viewer;
        const scene = viewer.scene;
        const metaScene = viewer.metaScene;
        const metaModel = metaScene.metaModels[modelId];
        const model = scene.models[modelId];
        if (!metaModel || !metaModel.rootMetaObject) {
            return;
        }
        const storeyIds = metaModel.rootMetaObject.getObjectIDsInSubtreeByType(["IfcBuildingStorey"]);
        for (let i = 0, len = storeyIds.length; i < len; i++) {
            const storeyId = storeyIds[i];
            const metaObject = metaScene.metaObjects[storeyId];
            const childObjectIds = metaObject.getObjectIDsInSubtree();
            const aabb = scene.getAABB(childObjectIds);
            const numObjects = (Math.random() > 0.5) ? childObjectIds.length : 0;
            const storey = new Dimensions(this, aabb, modelId, storeyId, numObjects);
            storey._onModelDestroyed = model.once("destroyed", () => {
                this._deregisterModel(modelId);
                this.fire("storeys", this.storeys);
            });
            this.storeys[storeyId] = storey;
            if (!this.modelStoreys[modelId]) {
                this.modelStoreys[modelId] = {};
            }
            this.modelStoreys[modelId][storeyId] = storey;
        }
    }

    _deregisterModel(modelId) {
        const storeys = this.modelStoreys[modelId];
        if (storeys) {
            const scene = this.viewer.scene;
            for (let storyObjectId in storeys) {
                if (storeys.hasOwnProperty(storyObjectId)) {
                    const storey = storeys[storyObjectId];
                    const model = scene.models[storey.modelId];
                    if (model) {
                        model.off(storey._onModelDestroyed);
                    }
                    delete this.storeys[storyObjectId];
                }
            }
            delete this.modelStoreys[modelId];
        }
    }
    /**
     * Destroys this GroundShadowPlugin.
     */
    destroy() {
        super.destroy();
    }
}

export {GroundShadowPlugin}