phaser4-rex-plugins
Version:
2 lines (1 loc) • 18.2 kB
JavaScript
var t,e;t=void 0,e=function(){const t=Phaser.GameObjects.GameObject;let e=class extends t{};const r=Phaser.GameObjects.Components;Phaser.Class.mixin(e,[r.AlphaSingle,r.BlendMode,r.Depth,r.Flip,r.Mask,r.Origin,r.RenderNodes,r.Size,r.Texture,r.Transform,r.Visible,r.ScrollFactor]);const s=Phaser.GameObjects.GetCalcMatrix;var i={multiTexturing:!1,smoothPixelArt:!1},a={renderWebGL:function(t,e,r,a){var n=r.camera;if(n.addToRenderList(e),!e.skipRender()){var h,o=s(e,n,a,!r.useCanvas).calc;e.dirty&&e.updateBuffers();var l=e.texture;h=l&&null!==l.smoothPixelArt?l.smoothPixelArt:e.scene.sys.game.config.smoothPixelArt,i.smoothPixelArt=h,(e.customRenderNodes.BatchHandler||e.defaultRenderNodes.BatchHandler).batchTriangles(r,e,o,e.texture.source[0].glTexture,e.vertexBuffer,e.uvBuffer,e.colorBuffer,e.alphaBuffer,e.alpha,e.tintFill,i,e.debugCallback)}},renderCanvas:function(t,e,r,s){},skipRender:function(){return 0===this.faces.length}};const n=Phaser.Math.RotateAround;var h={};const o=Phaser.Math.Linear,l=Phaser.Math.RotateAround;class c{constructor(){this.parent=void 0,this.name="",this.u=0,this.v=0,this.frameU=0,this.frameV=0,this.frameX=0,this.frameY=0,this._dx=0,this._dy=0,this.localX=0,this.localY=0,this.alpha=1,this.color=16777215}setParent(t){return this.parent=t,this}setName(t){return this.name=t,this}get frameU(){return this._frameU}set frameU(t){this._frameU!==t&&(this._frameU=t,this.parent&&this.parent.setUVDirtyFlag())}get frameV(){return this._frameV}set frameV(t){this._frameV!==t&&(this._frameV=t,this.parent&&this.parent.setUVDirtyFlag())}get frameX(){return this._frameX}set frameX(t){this._frameX!==t&&(this._frameX=t,this._localX=t+this._dx,this.parent&&this.parent.setVertexDirtyFlag())}get frameY(){return this._frameY}set frameY(t){this._frameY!==t&&(this._frameY=t,this._localY=t+this._dy,this.parent&&this.parent.setVertexDirtyFlag())}get localX(){return this._localX}set localX(t){this._localX!==t&&(this._localX=t,this._dx=t-this._frameX,this.parent&&this.parent.setVertexDirtyFlag())}get localY(){return this._localY}set localY(t){this._localY!==t&&(this._localY=t,this._dy=t-this._frameY,this.parent&&this.parent.setVertexDirtyFlag())}get alpha(){return this._alpha}set alpha(t){this._alpha!==t&&(this._alpha=t,this.parent&&this.parent.setAlphaDirtyFlag())}get color(){return this._color}set color(t){this._color!==t&&(this._color=t,this.parent&&this.parent.setColorDirtyFlag())}setUV(t,e){return this.u=t,this.v=e,this}setFrameUV(t,e,r,s){return this.frameU=o(t,r,this.u),this.frameV=o(e,s,this.v),this}setFrameSize(t,e){return this.frameX=this.u*t,this.frameY=this.v*e,this}resetPosition(){return this.localX=this.frameX,this.localY=this.frameY,this}setLocalPosition(t,e){return this.localX=t,this.localY=e,this}rotateAround(t,e,r){return u.x=this.localX,u.y=this.localY,l(u,t,e,r),this.localX=u.x,this.localY=u.y,this}setAlpha(t){return this.alpha=t,this}setColor(t){return this.color=t,this}getWorldXY(t){return this.parent?function(t,e,r,s){void 0===s?s={}:!0===s&&(s=h);var i=t.displayOriginX,a=t.displayOriginY;return s.x=e-i,s.y=r-a,n(s,0,0,t.rotation),s.x*=t.scaleX,s.y*=t.scaleY,s.x+=t.x,s.y+=t.y,s}(this.parent,this.localX,this.localY,t):null}setWorldXY(t,e){var r=function(t,e,r,s){void 0===s?s={}:!0===s&&(s=h);var i=t.displayOriginX,a=t.displayOriginY;return s.x=e-t.x,s.y=r-t.y,s.x/=t.scaleX,s.y/=t.scaleY,n(s,0,0,-t.rotation),s.x+=i,s.y+=a,s}(this.parent,t,e,!0);return this.setLocalPosition(r.x,r.y),this}setPosition(t,e){return this.setWorldXY(t,e),this}get x(){return this.parent?this.getWorldXY(!0).x:null}set x(t){this.setWorldXY(t,this.y)}get y(){return this.parent?this.getWorldXY(!0).y:null}set y(t){this.setWorldXY(this.x,t)}}var u={};const f=Phaser.Geom.Triangle.InCenter;var d={},p={},v=new Phaser.Geom.Triangle;const m=Phaser.Math.RadToDeg,g=Phaser.Math.DegToRad;class y{constructor(t,e,r){void 0===t&&(t=new c),void 0===e&&(e=new c),void 0===r&&(r=new c),this.parent=void 0,this.name="",this.vertices=[t,e,r],this._localOffsetX=0,this._localOffsetY=0,this._rotation=0,this._alpha=1,this._color=16777215}setParent(t){return this.parent=t,this.vertices[0].setParent(t),this.vertices[1].setParent(t),this.vertices[2].setParent(t),this}setName(t){return this.name=t,this}get vertex0(){return this.vertices[0]}set vertex0(t){this.vertices[0]=t}get vertex1(){return this.vertices[1]}set vertex1(t){this.vertices[1]=t}get vertex2(){return this.vertices[2]}set vertex2(t){this.vertices[2]=t}get localOffsetX(){return this._localOffsetX}set localOffsetX(t){t!==this._localOffsetX&&(this._localOffsetX=t,this.updateVerticesPosition())}get localOffsetY(){return this._localOffsetY}set localOffsetY(t){t!==this._localOffsetY&&(this._localOffsetY=t,this.updateVerticesPosition())}get rotation(){return this._rotation}set rotation(t){if(t!==this._rotation){this._rotation=t;var e,r,s=(e=this,void 0===(r=!0)?r={}:!0===r&&(r=p),d.x1=e.vertices[0].localX,d.y1=e.vertices[0].localY,d.x2=e.vertices[1].localX,d.y2=e.vertices[1].localY,d.x3=e.vertices[2].localX,d.y3=e.vertices[2].localY,f(d,r)),i=s.x,a=s.y;this.vertices[0].rotateAround(i,a,t),this.vertices[1].rotateAround(i,a,t),this.vertices[2].rotateAround(i,a,t)}}get angle(){return m(this._rotation)}set angle(t){this.rotation=g(t)}get alpha(){return this._alpha}set alpha(t){this._alpha!==t&&(this._alpha=t,this.vertices[0].setAlpha(t),this.vertices[1].setAlpha(t),this.vertices[2].setAlpha(t))}get color(){return this._color}set color(t){this._color!==t&&(this._color=t,this.vertices[0].setColor(t),this.vertices[1].setColor(t),this.vertices[2].setColor(t))}get isPositionModified(){return 0!==this._localOffsetX||0!==this._localOffsetY||0!==this._rotation}setUV(t,e,r,s,i,a){return this.vertices[0].setUV(t,e),this.vertices[1].setUV(r,s),this.vertices[2].setUV(i,a),this}setFrameUV(t,e,r,s){return this.vertices[0].setFrameUV(t,e,r,s),this.vertices[1].setFrameUV(t,e,r,s),this.vertices[2].setFrameUV(t,e,r,s),this}setFrameSize(t,e){for(var r=0,s=this.vertices.length;r<s;r++)this.vertices[r].setFrameSize(t,e);return this.isPositionModified&&this.updateVerticesPosition(),this}setLocalOffset(t,e){return this.localOffsetX=t,this.localOffsetY=e,this}resetVerticesPosition(){for(var t=0,e=this.vertices.length;t<e;t++)this.vertices[t].resetPosition();return this.isPositionModified&&this.updateVerticesPosition(),this}updateVerticesPosition(){for(var t=this._localOffsetX,e=this._localOffsetY,r=this.vertices,s=0,i=r.length;s<i;s++){var a=r[s];a.setLocalPosition(a.frameX+t,a.frameY+e)}var n=this.rotation;return this._rotation=0,this.rotation=n,this}setRotation(t){return this.rotation=t,this}setAngle(t){return this.angle=t,this}setAlpha(t){return this.alpha=t,this}setColor(t){return this.color=t,this}contains(t,e){return r=t,s=e,i=this.vertices,a=i[0],n=i[1],h=i[2],v.setTo(a.localX,a.localY,n.localX,n.localY,h.localX,h.localY),v.contains(r,s);var r,s,i,a,n,h}}const x=Phaser.Utils.Array.GetFirst;var F={clear(){return this.faces.length=0,this.vertices.length=0,this.setFaceCountDirtyFlag(),this},createVertex(t,e){void 0===t&&(t=0),void 0===e&&(e=0);var r=new c;return r.setUV(t,e),r},createFace:(t,e,r)=>new y(t,e,r),addFace(t){if(this.faces.includes(t))return this;t.setParent(this),this.faces.push(t),this.setFaceCountDirtyFlag();var e=this.frame;t.setFrameSize(e.cutWidth,e.cutHeight).setFrameUV(e.u0,e.v0,e.u1,e.v1).resetVerticesPosition();var r=this.vertices;return t.vertices.forEach((function(t){r.includes(t)||r.push(t)})),this},addFaces(t){for(var e=0,r=t.length;e<r;e++)this.addFace(t[e]);return this},resetFaceSize(){for(var t=this.frame,e=t.realWidth,r=t.realHeight,s=this.faces,i=0,a=s.length;i<a;i++)s[i].setFrameSize(e,r);return this},addGridFaces(t,e,r){return function(t,e,r,s){if(function(t){if("object"!=typeof t||t.nodeType||t===t.window)return!1;try{if(t.constructor&&!{}.hasOwnProperty.call(t.constructor.prototype,"isPrototypeOf"))return!1}catch(t){return!1}return!0}(e)){var i=e;e=i.columns,r=i.rows,s=i.sharedVertexMode}void 0===e&&(e=1),void 0===r&&(r=1),void 0===s&&(s=!1);var a,n,h,o,l,c=[];if(s){a=[];for(var u=0;u<=r;u++)for(var f=0;f<=e;f++){var d=t.createVertex(f/e,u/r);a.push(d)}}for(u=0;u<r;u++)for(f=0;f<e;f++)if(s){var p=u*(e+1)+f,v=p+1,m=(u+1)*(e+1)+f,g=m+1,y=a[p],x=a[v],F=a[m],_=a[g];l=t.createFace(y,_,F),t.addFace(l),c.push(l),l=t.createFace(y,x,_),t.addFace(l),c.push(l)}else{var P=f/e,V=(f+1)/e,Y=u/r,X=(u+1)/r;n=t.createVertex(P,Y),h=t.createVertex(P,X),o=t.createVertex(V,X),l=t.createFace(n,h,o),t.addFace(l),c.push(l),n=t.createVertex(P,Y),h=t.createVertex(V,X),o=t.createVertex(V,Y),l=t.createFace(n,h,o),t.addFace(l),c.push(l)}s&&t.vertices.sort((function(t,e){return t.v===e.v?t.u-e.u:t.v-e.v}))}(this,t,e,r),this},getVertexByName(t){return x(this.vertices,"name",t)},getFaceByName(t){return x(this.faces,"name",t)},resetVerticesPosition(){for(var t=this.vertices,e=0,r=t.length;e<r;e++)t[e].resetPosition();return this}},_={clearDirtyFlag(){return this.dirtyFlags=0,this},setFaceCountDirtyFlag(){return this.dirtyFlags|=31,this},setUVDirtyFlag(){return this.dirtyFlags|=2,this},setVertexDirtyFlag(){return this.dirtyFlags|=4,this},setAlphaDirtyFlag(){return this.dirtyFlags|=8,this},setColorDirtyFlag(){return this.dirtyFlags|=16,this}},P={updateBuffers(){return this.dirty?(1&this.dirtyFlags&&this.resizeBuffers(),2&this.dirtyFlags&&this.updateUVBuffer(),4&this.dirtyFlags&&this.updateVertexBuffer(),8&this.dirtyFlags&&this.updateAlphaBuffer(),16&this.dirtyFlags&&this.updateColorBuffer(),this.clearDirtyFlag(),this):this},resizeBuffers(){var t=this.faces.length;return this.vertexBuffer=new Float32Array(6*t),this.uvBuffer=new Float32Array(6*t),this.colorBuffer=new Uint32Array(3*t),this.alphaBuffer=new Float32Array(3*t),this},updateUVBuffer(){for(var t,e,r=this.uvBuffer,s=this.faces,i=0,a=s.length;i<a;i++)e=s[i].vertices,r[t=6*i]=e[0].frameU,r[t+1]=e[0].frameV,r[t+2]=e[1].frameU,r[t+3]=e[1].frameV,r[t+4]=e[2].frameU,r[t+5]=e[2].frameV;return this},updateVertexBuffer(){for(var t,e,r=this.vertexBuffer,s=this.faces,i=0,a=s.length;i<a;i++)e=s[i].vertices,r[t=6*i]=e[0].localX,r[t+1]=e[0].localY,r[t+2]=e[1].localX,r[t+3]=e[1].localY,r[t+4]=e[2].localX,r[t+5]=e[2].localY;return this},updateAlphaBuffer(){for(var t,e,r=this.alphaBuffer,s=this.faces,i=0,a=s.length;i<a;i++)e=s[i].vertices,r[t=3*i]=e[0].alpha,r[t+1]=e[1].alpha,r[t+2]=e[2].alpha;return this},updateColorBuffer(){for(var t,e,r=this.colorBuffer,s=this.faces,i=0,a=s.length;i<a;i++)e=s[i].vertices,r[t=3*i]=e[0].color,r[t+1]=e[1].color,r[t+2]=e[2].color;return this}},V={setTintFill(t){return void 0===t&&(t=!1),this.tintFill=t,this},setTint(t){return this.tint=t,this},clearTint(){return this.setTint(16777215),this}},Y={setDebug(t,e){return this.debugGraphic=t,this.debugCallback=t||e?e||this.renderDebugVerts:null,this},renderDebugVerts(t,e,r){for(var s=t.debugGraphic,i=0;i<e;i+=6){var a=r[i+0],n=r[i+1],h=r[i+2],o=r[i+3],l=r[i+4],c=r[i+5];s.lineBetween(a,n,h,o),s.lineBetween(h,o,l,c),s.lineBetween(l,c,a,n)}}};const X=Phaser.GameObjects.Components.TransformMatrix,A=Phaser.Math.TransformXY;var B,O,b={},T={getFaceAt(t,e,r){for(var s=function(t,e,r,s,i){void 0===s&&(s=t.scene.cameras.main),void 0===i?i={}:!0===i&&(i=b);var a=s.scrollX,n=s.scrollY,h=e+a*t.scrollFactorX-a,o=r+n*t.scrollFactorY-n;return t.parentContainer?(void 0===B&&(B=new X,O=new X),t.getWorldTransformMatrix(B,O),B.applyInverse(h,o,i)):A(h,o,t.x,t.y,t.rotation,t.scaleX,t.scaleY,i),i.x+=t.displayOriginX,i.y+=t.displayOriginY,i}(this,t,e,r,!0),i=s.x,a=s.y,n=this.faces,h=0,o=n.length;h<o;h++){var l=n[h];if(l.contains(i,a))return l}return null},hasFaceAt(t,e,r){return!!this.getFaceAt(t,e,r)}},C=function(t,e,r,s){for(var i=s.faces,a=0,n=i.length;a<n;a++){var h=i[a];if(h.contains(e,r))return s.input.hitFace=h,!0}return s.input.hitFace=null,!1},U=function(t,e,r,s){var i=this.input.hitFace;this.emit("face.pointerdown",i,t,e,r,s)},D=function(t,e,r,s){var i=this.input.hitFace;this.emit("face.pointerup",i,t,e,r,s),this.input.hitFace=null},w=function(t,e,r,s){var i=this.input.hitFace,a=this.input.prevHitFace;i!==a?(a&&this.emit("face.pointerout",a,t,s),i&&this.emit("face.pointerover",i,t,s),this.input.prevHitFace=i):this.emit("face.pointermove",i,t,e,r,s)},M=function(t,e){var r=this.input.prevHitFace;r&&this.emit("face.pointerout",r,t,e),this.input.hitFace=null,this.input.prevHitFace=null},R={setFaceInteractive:function(t){if(!this.input)return void 0===t&&(t={}),t.hitArea={},t.hitAreaCallback=C,this.setInteractive(t).on("pointerdown",U,this).on("pointerup",D,this).on("pointermove",w,this).on("pointerover",w,this).on("pointerout",M,this),this}};Object.assign(R,F,_,P,V,Y,T);const G=Phaser.Renderer.WebGL.Shaders.MultiFrag,S=Phaser.Renderer.WebGL.Shaders.MultiVert,j=Phaser.Renderer.WebGL.ShaderAdditionMakers,W=j.MakeApplyTint,N=j.MakeDefineTexCount,H=j.MakeGetTexCoordOut,L=j.MakeGetTexRes,k=j.MakeSmoothPixelArt,z=j.MakeGetTexture,I=Phaser.Renderer.WebGL.Utils,E=Phaser.Renderer.WebGL.RenderNodes.BatchHandlerQuad,Q=I.getTintAppendFloatAlpha;class q extends E{constructor(t,e){super(t,e),this.renderOptions.multiTexturing=!0}_generateElementIndices(t){for(var e=new ArrayBuffer(5*t*2),r=new Uint16Array(e),s=0,i=0;i<t;i++){var a=3*i;r[s++]=a,r[s++]=a,r[s++]=a+1,r[s++]=a+2,r[s++]=a+2}return e}batchTriangles(t,e,r,s,i,a,n,h,o,l,c,u){0===this.instanceCount&&this.manager.setCurrentBatchNode(this,t);var f=i.length/(2*this.verticesPerInstance);if(f>this.instancesPerBatch)throw new Error("rexBatchHandlerTriangle: Vertex count exceeds maximum per batch ("+this.maxVerticesPerBatch+")");this.instanceCount+f>this.instancesPerBatch&&this.run(t),this.updateRenderOptions(c),this._renderOptionsChanged&&(this.run(t),this.updateShaderConfig());var d,p=this.batchTextures(s),v=this.instanceCount*this.floatsPerInstance,m=this.vertexBufferLayout.buffer,g=m.viewF32,y=m.viewU32;u&&(d=[]);for(var x=r.a,F=r.b,_=r.c,P=r.d,V=r.e,Y=r.f,X=-e.displayOriginX,A=-e.displayOriginY,B=i.length,O=0;O<B;O+=6){for(var b=0;b<3;b++){var T=O+2*b,C=i[T],U=i[T+1],D=(C+=X)*x+(U+=A)*_+V,w=C*F+U*P+Y,M=O/2+b,R=Q(n[M],h[M]*o);g[v++]=D,g[v++]=w,g[v++]=a[T],g[v++]=a[T+1],g[v++]=p,g[v++]=l,y[v++]=R,d&&d.push(D,w)}this.instanceCount++,this.currentBatchEntry.count++}u&&u.call(e,e,B,d)}}q.prototype.defaultConfig={name:"rexBatchHandlerTriangle",verticesPerInstance:3,indicesPerInstance:5,shaderName:"REXTRI",vertexSource:S,fragmentSource:G,shaderAdditions:[H(),L(!0),k(!0),N(1),z(),W()],vertexBufferLayout:{usage:"DYNAMIC_DRAW",layout:[{name:"inPosition",size:2},{name:"inTexCoord",size:2},{name:"inTexDatum"},{name:"inTintEffect"},{name:"inTint",size:4,type:"UNSIGNED_BYTE",normalized:!0}]}};const J=Phaser.Game;var K=function(t){return t instanceof J};const Z=Phaser.Scene;var $=function(t){return t instanceof Z},tt=function(t,e,r){var s,i=(s=t,null==s||"object"!=typeof s?null:K(s)?s:K(s.game)?s.game:$(s)?s.sys.game:$(s.scene)?s.scene.sys.game:void 0).renderer.renderNodes;i.hasNode(e,!0)||i.addNodeConstructor(e,r)};const et=new Phaser.Structs.Map([["BatchHandler","rexBatchHandlerTriangles"]]);class rt extends e{constructor(t,e,r,s,i){void 0===e&&(e=0),void 0===r&&(r=0),void 0===s&&(s="__DEFAULT"),super(t,"rexMeshImage"),this.dirtyFlags=0,this.vertices=[],this.faces=[],this.vertexBuffer=null,this.uvBuffer=null,this.alphaBuffer=null,this.colorBuffer=null,this.tintFill=!1,this.debugCallback=null,this.debugGraphic=null,this.setTexture(s,i),this.setPosition(e,r),this.setSizeToFrame(),this.setOriginFromFrame(),tt(t,"rexBatchHandlerTriangles",q),this.initRenderNodes(this._defaultRenderNodesMap)}get _defaultRenderNodesMap(){return et}get dirty(){return 0!==this.dirtyFlags}get frame(){return this._frame}set frame(t){if(this._frame!==t){this._frame=t;var e=this.faces;if(e){var r=t?t.u0:0,s=t?t.v0:0,i=t?t.u1:0,a=t?t.v1:0,n=t?t.cutWidth:0,h=t?t.cutHeight:0,o=this._frameWidthSave!==n||this._frameHeightSave!==h;this._frameWidthSave=n,this._frameHeightSave=h;for(var l=0,c=e.length;l<c;l++){var u=e[l];u.setFrameUV(r,s,i,a),o&&u.setFrameSize(n,h)}}}}get tint(){return this.faces.length>0?this.faces[0].color:16777215}set tint(t){for(var e=this.faces,r=0,s=e.length;r<s;r++)e[r].setColor(t)}addedToScene(){this.scene.sys.updateList.add(this)}removedFromScene(){this.scene.sys.updateList.remove(this)}}Object.assign(rt.prototype,a,R);var st={play(t,e){return this.anims.play(t,e)},playReverse(t,e){return this.anims.playReverse(t,e)},playAfterDelay(t,e){return this.anims.playAfterDelay(t,e)},playAfterRepeat(t,e){return this.anims.playAfterRepeat(t,e)},chain(t){return this.anims.chain(t)},stop(){return this.anims.stop()},stopAfterDelay(t){return this.anims.stopAfterDelay(t)},stopAfterRepeat(t){return this.anims.stopAfterRepeat(t)},stopOnFrame(t){return this.anims.stopOnFrame(t)}},it={};Object.assign(it,st);const at=Phaser.Animations.AnimationState;class nt extends rt{constructor(t,e,r,s,i){super(t,e,r,s,i),this.type="rexMeshSprite",this.anims=new at(this)}preUpdate(t,e){this.anims.update(t,e)}preDestroy(){this.anims.destroy(),this.anims=void 0}}function ht(t,e,r,s){var i=new nt(this.scene,t,e,r,s);return this.scene.add.existing(i),i}Object.assign(nt.prototype,it);const ot=Phaser.Utils.Objects.GetAdvancedValue,lt=Phaser.GameObjects.BuildGameObject;function ct(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var r=ot(t,"key",null),s=ot(t,"frame",null),i=new nt(this.scene,0,0,r,s);return lt(this.scene,i,t),i}var ut=function(t){return null==t||""===t||0===t.length};class ft extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexMesh",ht,ct)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,e,r,s){if(void 0===s&&(s="."),"object"==typeof t)if(ut(e)){if(null==r)return;"object"==typeof r&&(t=r)}else{"string"==typeof e&&(e=e.split(s));var i=e.pop(),a=function(t,e,r){var s=t;if(ut(e));else{var i;"string"==typeof e&&(e=e.split("."));for(var a=0,n=e.length;a<n;a++){var h;null!=s[i=e[a]]&&"object"==typeof s[i]||(h=a===n-1?void 0===r?{}:r:{},s[i]=h),s=s[i]}}return s}(t,e);a[i]=r}}(window,"RexPlugins.GameObjects.Mesh",nt),ft},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexmeshplugin=e();