Visible Mixin

Visible pawns have a mesh and a material and they handle inserting and removing themselves from the scene. They should only be used with a companion mixin that has the method "global" that supplies them with a 4x4 transform. Make sure the companion mixin is added first so it will be updated first. Note that destroying a pawn will not clean up the mesh and the material because they may be used by multiple pawns.

# PM_Visible

export const PM_Visible = superclass => class extends superclass { destroy() { super.destroy(); const render = this.service("RenderManager"); for (const layerName in render.layers) { const layer = render.layers[layerName]; if (layer.has(this)) render.dirtyLayer(layerName); render.layers[layerName].delete(this); } } addToLayers(...names) { const render = this.service("RenderManager"); names.forEach(name => { if (!render.layers[name]) render.layers[name] = new Set(); render.layers[name].add(this); render.dirtyLayer(name); }); } removeFromLayers(...names) { const render = this.service("RenderManager"); names.forEach(name => { if (!render.layers[name]) return; render.layers[name].delete(this); if (render.layers[name].size === 0) { delete render.layers[name]; } render.dirtyLayer(name); }); } layers() { let result = []; const render = this.service("RenderManager"); for (const layerName in render.layers) { const layer = render.layers[layerName]; if (layer.has(this)) result.push(layerName); } return result; } };

# PM_WebGLVisible

export const PM_WebGLVisible = superclass => class extends PM_Visible(superclass) { constructor(...args) { super(...args); this.listen("viewGlobalChanged", this.refreshDrawTransform); } destroy() { super.destroy(); if (this.draw) this.service('WebGLRenderManager').scene.removeDrawCall(this.draw); } refreshDrawTransform() { if (this.draw) this.draw.transform.set(this.global); } setDrawCall(draw) { if (this.draw === draw) return; const scene = this.service('WebGLRenderManager').scene; if (this.draw) scene.removeDrawCall(this.draw); this.draw = draw; if (this.draw) { this.draw.transform.set(this.global || m4_identity()); scene.addDrawCall(this.draw); } } };