1 | {"version":3,"file":"BatchShaderGenerator.mjs","sources":["../../src/batch/BatchShaderGenerator.ts"],"sourcesContent":["import { Matrix } from '@pixi/math';\nimport { Program } from '../shader/Program';\nimport { Shader } from '../shader/Shader';\nimport { UniformGroup } from '../shader/UniformGroup';\n\n/**\n * Helper that generates batching multi-texture shader. Use it with your new BatchRenderer\n * @memberof PIXI\n */\nexport class BatchShaderGenerator\n{\n /** Reference to the vertex shader source. */\n public vertexSrc: string;\n\n /** Reference to the fragment shader template. Must contain \"%count%\" and \"%forloop%\". */\n public fragTemplate: string;\n\n programCache: {[key: number]: Program};\n defaultGroupCache: {[key: number]: UniformGroup};\n\n /**\n * @param vertexSrc - Vertex shader\n * @param fragTemplate - Fragment shader template\n */\n constructor(vertexSrc: string, fragTemplate: string)\n {\n this.vertexSrc = vertexSrc;\n this.fragTemplate = fragTemplate;\n\n this.programCache = {};\n this.defaultGroupCache = {};\n\n if (!fragTemplate.includes('%count%'))\n {\n throw new Error('Fragment template must contain \"%count%\".');\n }\n\n if (!fragTemplate.includes('%forloop%'))\n {\n throw new Error('Fragment template must contain \"%forloop%\".');\n }\n }\n\n generateShader(maxTextures: number): Shader\n {\n if (!this.programCache[maxTextures])\n {\n const sampleValues = new Int32Array(maxTextures);\n\n for (let i = 0; i < maxTextures; i++)\n {\n sampleValues[i] = i;\n }\n\n this.defaultGroupCache[maxTextures] = UniformGroup.from({ uSamplers: sampleValues }, true);\n\n let fragmentSrc = this.fragTemplate;\n\n fragmentSrc = fragmentSrc.replace(/%count%/gi, `${maxTextures}`);\n fragmentSrc = fragmentSrc.replace(/%forloop%/gi, this.generateSampleSrc(maxTextures));\n\n this.programCache[maxTextures] = new Program(this.vertexSrc, fragmentSrc);\n }\n\n const uniforms = {\n tint: new Float32Array([1, 1, 1, 1]),\n translationMatrix: new Matrix(),\n default: this.defaultGroupCache[maxTextures],\n };\n\n return new Shader(this.programCache[maxTextures], uniforms);\n }\n\n generateSampleSrc(maxTextures: number): string\n {\n let src = '';\n\n src += '\\n';\n src += '\\n';\n\n for (let i = 0; i < maxTextures; i++)\n {\n if (i > 0)\n {\n src += '\\nelse ';\n }\n\n if (i < maxTextures - 1)\n {\n src += `if(vTextureId < ${i}.5)`;\n }\n\n src += '\\n{';\n src += `\\n\\tcolor = texture2D(uSamplers[${i}], vTextureCoord);`;\n src += '\\n}';\n }\n\n src += '\\n';\n src += '\\n';\n\n return src;\n }\n}\n"],"names":[],"mappings":";;;;AASO,MAAM,qBACb;AAAA;AAAA;AAAA;AAAA;AAAA,EAcI,YAAY,WAAmB,cAC/B;AAOI,QANA,KAAK,YAAY,WACjB,KAAK,eAAe,cAEpB,KAAK,eAAe,CAAC,GACrB,KAAK,oBAAoB,CAAA,GAErB,CAAC,aAAa,SAAS,SAAS;AAE1B,YAAA,IAAI,MAAM,2CAA2C;AAG3D,QAAA,CAAC,aAAa,SAAS,WAAW;AAE5B,YAAA,IAAI,MAAM,6CAA6C;AAAA,EAErE;AAAA,EAEA,eAAe,aACf;AACI,QAAI,CAAC,KAAK,aAAa,WAAW,GAClC;AACU,YAAA,eAAe,IAAI,WAAW,WAAW;AAEtC,eAAA,IAAI,GAAG,IAAI,aAAa;AAE7B,qBAAa,CAAC,IAAI;AAGjB,WAAA,kBAAkB,WAAW,IAAI,aAAa,KAAK,EAAE,WAAW,aAAa,GAAG,EAAI;AAEzF,UAAI,cAAc,KAAK;AAET,oBAAA,YAAY,QAAQ,aAAa,GAAG,WAAW,EAAE,GAC/D,cAAc,YAAY,QAAQ,eAAe,KAAK,kBAAkB,WAAW,CAAC,GAEpF,KAAK,aAAa,WAAW,IAAI,IAAI,QAAQ,KAAK,WAAW,WAAW;AAAA,IAC5E;AAEA,UAAM,WAAW;AAAA,MACb,MAAM,IAAI,aAAa,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;AAAA,MACnC,mBAAmB,IAAI,OAAO;AAAA,MAC9B,SAAS,KAAK,kBAAkB,WAAW;AAAA,IAAA;AAG/C,WAAO,IAAI,OAA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