import{Object3D,Color}from"three";import{definePlugin}from"../../core/extension";import{createColorHandler}from"../../webgl/tool";import{toArray}from"../../common/util";export default definePlugin("color",{destroyed(){this.dispose()},setup(a={},b){let{clear:c,drawColor:d,dispose:e}=createColorHandler({color:"#FF0000",opacity:.5,...a}),f=new Set;const g=a=>{let c=toArray(a),d=[];return c.forEach(a=>{if("string"==typeof a){let c=b.get(a);c&&c.model&&d.push(c.model)}else a&&a.model?d.push(a.model):a instanceof Object3D?d.push(a):a&&d.push(a)}),d},h=(a,b)=>{let c=[];return a.forEach(a=>{if(a.isInstancedMesh){const c=a,d=c.instancedId;if(void 0!==d){const a=new Color(b);c.setColorAt(d,a),c.instanceColor&&(c.instanceColor.needsUpdate=!0)}}else c.push(a)}),c};return{set(b,c){let e=g(b);if(0<e.length){const b=(null===c||void 0===c?void 0:c.color)||a.color||"#FF0000";let g=h(e,b);0<g.length&&d(g,c),e.forEach(a=>f.add(a))}},clear(a){let b=a===void 0?Array.from(f):g(a).filter(a=>f.has(a));if(0<b.length){let d=h(b,16777215);0<d.length&&c(d),a===void 0?f.clear():b.forEach(a=>f.delete(a))}},dispose(){this.clear(),e()}}}});