UNPKG

pixi-cull

Version:

a library to visibly cull objects designed to work with pixi.js

3 lines (2 loc) 8.62 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Cull={})}(this,(function(t){"use strict";var e=function(){return(e=Object.assign||function(t){for(var e,i=1,s=arguments.length;i<s;i++)for(var r in e=arguments[i])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)},i={visible:"visible",dirtyTest:!1},s=function(){function t(t){void 0===t&&(t={}),t=e(e({},i),t),this.dirtyTest=void 0===t.dirtyTest||t.dirtyTest,this.lists=[[]]}return t.prototype.addList=function(t,e){this.lists.push(t),e&&(t.staticObject=!0);for(var i=t.length,s=0;s<i;s++)this.updateObject(t[s]);return t},t.prototype.removeList=function(t){var e=this.lists.indexOf(t);return-1===e||this.lists.splice(e,1),t},t.prototype.add=function(t,e){return e&&(t.staticObject=!0),(this.dirtyTest||e)&&this.updateObject(t),this.lists[0].push(t),t},t.prototype.remove=function(t){var e=this.lists[0].indexOf(t);return-1===e||this.lists[0].splice(e,1),t},t.prototype.cull=function(t,e){e||this.updateObjects();for(var i=0,s=this.lists;i<s.length;i++)for(var r=s[i],h=r.length,n=0;n<h;n++){var o=r[n],a=o.AABB;o.visible=a.x+a.width>t.x&&a.x<t.x+t.width&&a.y+a.height>t.y&&a.y<t.y+t.height}},t.prototype.updateObjects=function(){if(this.dirtyTest)for(var t=0,e=this.lists;t<e.length;t++){if(!(n=e[t]).staticObject)for(var i=n.length,s=0;s<i;s++){!(a=n[s]).staticObject&&a.dirty&&(this.updateObject(a),a.dirty=!1)}}else for(var r=0,h=this.lists;r<h.length;r++){var n;if(!(n=h[r]).staticObject){var o=n.length;for(s=0;s<o;s++){var a;(a=n[s]).staticObject||this.updateObject(a)}}}},t.prototype.updateObject=function(t){var e=t.getLocalBounds();t.AABB=t.AABB||{x:0,y:0,width:0,height:0},t.AABB.x=t.x+(e.x-t.pivot.x)*Math.abs(t.scale.x),t.AABB.y=t.y+(e.y-t.pivot.y)*Math.abs(t.scale.y),t.AABB.width=e.width*Math.abs(t.scale.x),t.AABB.height=e.height*Math.abs(t.scale.y)},t.prototype.query=function(t){for(var e=[],i=0,s=this.lists;i<s.length;i++)for(var r=0,h=s[i];r<h.length;r++){var n=h[r],o=n.AABB;o&&o.x+o.width>t.x&&o.x-o.width<t.x+t.width&&o.y+o.height>t.y&&o.y-o.height<t.y+t.height&&e.push(n)}return e},t.prototype.queryCallback=function(t,e){for(var i=0,s=this.lists;i<s.length;i++)for(var r=0,h=s[i];r<h.length;r++){var n=h[r],o=n.AABB;if(o&&o.x+o.width>t.x&&o.x-o.width<t.x+t.width&&o.y+o.height>t.y&&o.y-o.height<t.y+t.height&&e(n))return!0}return!1},t.prototype.stats=function(){for(var t=0,e=0,i=0,s=this.lists;i<s.length;i++){s[i].forEach((function(i){t+=i.visible?1:0,e++}))}return{total:e,visible:t,culled:e-t}},t}(),r={xSize:1e3,ySize:1e3,simpleTest:!0,dirtyTest:!0},h=function(){function t(t){this.xSize=1e3,this.ySize=1e3,this.simpleTest=!0,this.dirtyTest=!0,(t=e(e({},r),t))&&void 0!==t.size?this.xSize=this.ySize=t.size:(this.xSize=t.xSize,this.ySize=t.ySize),this.simpleTest=t.simpleTest,this.dirtyTest=t.dirtyTest,this.width=this.height=0,this.hash={},this.containers=[],this.elements=[]}return t.prototype.add=function(t,e){return t.spatial={hashes:[]},this.dirtyTest&&(t.dirty=!0),e&&(t.staticObject=!0),this.updateObject(t),this.elements.push(t),t},t.prototype.remove=function(t){return this.elements.splice(this.elements.indexOf(t),1),this.removeFromHash(t),t},t.prototype.addContainer=function(t,e){for(var i=this,s=function(t){t.spatial={hashes:[]},i.updateObject(t)},r=function(t){i.removeFromHash(t)},h=t.children.length,n=0;n<h;n++){var o=t.children[n];o.spatial={hashes:[]},this.updateObject(o)}t.cull={},this.containers.push(t),t.on("childAdded",s),t.on("childRemoved",r),t.cull.added=s,t.cull.removed=r,e&&(t.cull.static=!0)},t.prototype.removeContainer=function(t){var e=this;return this.containers.splice(this.containers.indexOf(t),1),t.children.forEach((function(t){return e.removeFromHash(t)})),t.off("childAdded",t.cull.added),t.off("removedFrom",t.cull.removed),delete t.cull,t},t.prototype.cull=function(t,e,i){return e||this.updateObjects(),this.invisible(),(i?this.queryCallbackAll(t,this.simpleTest,i):this.query(t,this.simpleTest)).forEach((function(t){return t.visible=!0})),this.lastBuckets},t.prototype.invisible=function(){for(var t=this.elements.length,e=0;e<t;e++)this.elements[e].visible=!1;for(var i=0,s=this.containers;i<s.length;i++){var r=s[i],h=r.children.length;for(e=0;e<h;e++)r.children[e].visible=!1}},t.prototype.updateObjects=function(){if(this.dirtyTest){for(var t=this.elements.length,e=0;e<t;e++){(n=this.elements[e]).dirty&&(this.updateObject(n),n.dirty=!1)}for(var i=0,s=this.containers;i<s.length;i++){if(!(l=s[i]).cull.static){var r=l.children.length;for(e=0;e<r;e++){(n=l.children[e]).dirty&&(this.updateObject(n),n.dirty=!1)}}}}else{var h=this.elements.length;for(e=0;e<h;e++){var n;(n=this.elements[e]).staticObject||this.updateObject(n)}for(var o=0,a=this.containers;o<a.length;o++){var l;if(!(l=a[o]).cull.static){var c=l.children.length;for(e=0;e<c;e++)this.updateObject(l.children[e])}}}},t.prototype.updateObject=function(t){var e,i=t.getLocalBounds();e=t.AABB={x:t.x+(i.x-t.pivot.x)*t.scale.x,y:t.y+(i.y-t.pivot.y)*t.scale.y,width:i.width*t.scale.x,height:i.height*t.scale.y};var s=t.spatial;s||(s=t.spatial={hashes:[]});var r=this.getBounds(e),h=r.xStart,n=r.yStart,o=r.xEnd,a=r.yEnd;if(s.xStart!==h||s.yStart!==n||s.xEnd!==o||s.yEnd!==a){s.hashes.length&&this.removeFromHash(t);for(var l=n;l<=a;l++)for(var c=h;c<=o;c++){var u=c+","+l;this.insert(t,u),s.hashes.push(u)}s.xStart=h,s.yStart=n,s.xEnd=o,s.yEnd=a}},t.prototype.getBuckets=function(t){void 0===t&&(t=1);var e=[];for(var i in this.hash){var s=this.hash[i];s.length>=t&&e.push(s)}return e},t.prototype.getBounds=function(t){return{xStart:Math.floor(t.x/this.xSize),yStart:Math.floor(t.y/this.ySize),xEnd:Math.floor((t.x+t.width)/this.xSize),yEnd:Math.floor((t.y+t.height)/this.ySize)}},t.prototype.insert=function(t,e){this.hash[e]?this.hash[e].push(t):this.hash[e]=[t]},t.prototype.removeFromHash=function(t){for(var e=t.spatial;e.hashes.length;){var i=e.hashes.pop(),s=this.hash[i];s.splice(s.indexOf(t),1)}},t.prototype.neighbors=function(t){var e=this,i=[];return t.spatial.hashes.forEach((function(t){return i=i.concat(e.hash[t])})),i},t.prototype.query=function(t,e){void 0===e&&(e=!0);for(var i=0,s=[],r=this.getBounds(t),h=r.xStart,n=r.yStart,o=r.xEnd,a=r.yEnd,l=n;l<=a;l++)for(var c=h;c<=o;c++){var u=this.hash[c+","+l];if(u){if(e)for(var d=u.length,f=0;f<d;f++){var p=u[f],y=p.AABB;y.x+y.width>t.x&&y.x<t.x+t.width&&y.y+y.height>t.y&&y.y<t.y+t.height&&s.push(p)}else s=s.concat(u);i++}}return this.lastBuckets=i,s},t.prototype.queryCallbackAll=function(t,e,i){void 0===e&&(e=!0);for(var s=0,r=[],h=this.getBounds(t),n=h.xStart,o=h.yStart,a=h.xEnd,l=h.yEnd,c=o;c<=l;c++)for(var u=n;u<=a;u++){var d=this.hash[u+","+c];if(d){if(e)for(var f=d.length,p=0;p<f;p++){var y=(x=d[p]).AABB;y.x+y.width>t.x&&y.x<t.x+t.width&&y.y+y.height>t.y&&y.y<t.y+t.height&&(r.push(x),i(x))}else{r=r.concat(d);for(var v=0,g=d;v<g.length;v++){var x;i(x=g[v])}}s++}}return this.lastBuckets=s,r},t.prototype.queryCallback=function(t,e,i){void 0===i&&(i=!0);for(var s=this.getBounds(t),r=s.xStart,h=s.yStart,n=s.xEnd,o=s.yEnd,a=h;a<=o;a++)for(var l=r;l<=n;l++){var c=this.hash[l+","+a];if(c)for(var u=0;u<c.length;u++){var d=c[u];if(i){var f=d.AABB;if(f.x+f.width>f.x&&f.x<f.x+f.width&&f.y+f.height>f.y&&f.y<f.y+f.height&&e(d))return!0}else if(e(d))return!0}}return!1},t.prototype.stats=function(){for(var t=0,e=0,i=this.elements.length,s=0;s<i;s++){t+=this.elements[s].visible?1:0,e++}for(var r=0,h=this.containers;r<h.length;r++){var n=h[r],o=n.children.length;for(s=0;s<o;s++){t+=n.children[s].visible?1:0,e++}}return{buckets:this.lastBuckets,total:e,visible:t,culled:e-t}},t.prototype.getNumberOfBuckets=function(){return Object.keys(this.hash).length},t.prototype.getAverageSize=function(){var t=0;for(var e in this.hash)t+=this.hash[e].length;return t/this.getBuckets().length},t.prototype.getLargest=function(){var t=0;for(var e in this.hash)this.hash[e].length>t&&(t=this.hash[e].length);return t},t.prototype.getWorldBounds=function(){var t=1/0,e=1/0,i=0,s=0;for(var r in this.hash){var h=r.split(","),n=parseInt(h[0]),o=parseInt(h[1]);t=n<t?n:t,e=o<e?o:e,i=n>i?n:i,s=o>s?o:s}return{xStart:t,yStart:e,xEnd:i,yEnd:s}},t.prototype.getSparseness=function(t){for(var e=0,i=0,s=t?this.getBounds(t):this.getWorldBounds(),r=s.xStart,h=s.yStart,n=s.xEnd,o=s.yEnd,a=h;a<o;a++)for(var l=r;l<n;l++)e+=this.hash[l+","+a]?1:0,i++;return e/i},t}();t.Simple=s,t.SpatialHash=h,Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=pixi-cull.min.js.map