UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

186 lines (145 loc) 4.43 kB
import { System } from "../../../../ecs/System.js"; import { HighlightRenderElementSource } from "../plugin/HighlightRenderElementSource.js"; import Highlight from "../Highlight.js"; import { OutlineRenderPlugin } from "../plugin/OutlineRenderPlugin.js"; import { ShadedGeometry } from "../../mesh-v2/ShadedGeometry.js"; import { SGThreeObjectCache } from "../../mesh-v2/render/SGThreeObjectCache.js"; class Supplier extends HighlightRenderElementSource { /** * * @param {ShadedGeometryHighlightSystem} system */ constructor(system) { super(); /** * * @type {ShadedGeometryHighlightSystem} * @private */ this.__system = system; /** * * @type {ObjectPoolFactory<HighlightRenderElement>} * @private */ this.__pool = null; /** * * @type {HighlightRenderGroup} * @private */ this.__group = null; /** * * @type {SGThreeObjectCache} * @private */ this.__object_cache = SGThreeObjectCache.INSTANCE; } collect(pool, group) { this.__pool = pool; this.__group = group; const system = this.__system; /** * * @type {EntityManager} */ const em = system.entityManager; const dataset = em.dataset; if (dataset === null) { return; } dataset.traverseEntities([Highlight, ShadedGeometry], this.__visit, this); } /** * * @param {Highlight} highlight * @param {ShadedGeometry} sg * @param {number} entity * @private */ __visit(highlight, sg, entity) { // TODO cull by visibility // if (!sg.getFlag(ShadedGeometryFlags.InView)) { // return; // } const elements = highlight.elements; if (elements.isEmpty()) { // no elements return; } const object = this.__object_cache.get(sg); object.matrixWorld.elements = sg.transform; const renderElement = this.__pool.create(); renderElement.definitions = elements.asArray(); renderElement.object = object; this.__group.elements.add(renderElement); } } export class ShadedGeometryHighlightSystem extends System { /** * * @param {Engine} engine * @constructor */ constructor(engine) { super(); /** * * @type {Engine} * @private */ this.__engine = engine; this.dependencies = [Highlight, ShadedGeometry]; this.__element_source = new Supplier(this); /** * * @type {Reference<OutlineRenderPlugin>} * @private */ this.__plugin_renderer_ref = null; } /** * * @param {EntityManager} entityManager * @param {function} readyCallback * @param {function} errorCallback */ shutdown(entityManager, readyCallback, errorCallback) { try { if (this.__plugin_renderer_ref !== null) { this.__plugin_renderer_ref.getValue().removeSource(this.__element_source); this.__plugin_renderer_ref.release(); this.__plugin_renderer_ref = null; } readyCallback(); } catch (e) { errorCallback(); } } /** * * @param {EntityManager} entityManager * @param {function} readyCallback * @param {function} errorCallback */ startup(entityManager, readyCallback, errorCallback) { /** * * @type {EntityManager} */ this.entityManager = entityManager; const engine = this.__engine; engine.plugins.acquire(OutlineRenderPlugin) .then(ref => { this.__plugin_renderer_ref = ref; /** * * @type {OutlineRenderPlugin} */ const plugin = ref.getValue(); plugin.addSource(this.__element_source); }, errorCallback) .then(readyCallback); } }