UNPKG

12.6 kBSource Map (JSON)View Raw
1{"version":3,"file":"uniformParsers.js","sources":["../../../src/shader/utils/uniformParsers.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\n// Parsers, each one of these will take a look at the type of shader property and uniform.\n// if they pass the test function then the code function is called that returns a the shader upload code for that uniform.\n// Shader upload code is automagically generated with these parsers.\n// If no parser is valid then the default upload functions are used.\n// exposing Parsers means that custom upload logic can be added to pixi's shaders.\n// A good example would be a pixi rectangle can be directly set on a uniform.\n// If the shader sees it it knows how to upload the rectangle structure as a vec4\n// format is as follows:\n//\n// {\n// test: (data, uniform) => {} <--- test is this code should be used for this uniform\n// code: (name, uniform) => {} <--- returns the string of the piece of code that uploads the uniform\n// codeUbo: (name, uniform) => {} <--- returns the string of the piece of code that uploads the\n// uniform to a uniform buffer\n// }\n\nexport interface IUniformParser\n{\n test(data: unknown, uniform: any): boolean;\n code(name: string, uniform: any): string;\n codeUbo?(name: string, uniform: any): string;\n}\n\nexport const uniformParsers: IUniformParser[] = [\n\n // a float cache layer\n {\n test: (data: any): boolean =>\n data.type === 'float' && data.size === 1 && !data.isArray,\n code: (name: string): string =>\n `\n if(uv[\"${name}\"] !== ud[\"${name}\"].value)\n {\n ud[\"${name}\"].value = uv[\"${name}\"]\n gl.uniform1f(ud[\"${name}\"].location, uv[\"${name}\"])\n }\n `,\n },\n // handling samplers\n {\n test: (data: any, uniform: any): boolean =>\n // eslint-disable-next-line max-len,no-eq-null,eqeqeq\n (data.type === 'sampler2D' || data.type === 'samplerCube' || data.type === 'sampler2DArray') && data.size === 1 && !data.isArray && (uniform == null || uniform.castToBaseTexture !== undefined),\n code: (name: string): string => `t = syncData.textureCount++;\n\n renderer.texture.bind(uv[\"${name}\"], t);\n\n if(ud[\"${name}\"].value !== t)\n {\n ud[\"${name}\"].value = t;\n gl.uniform1i(ud[\"${name}\"].location, t);\\n; // eslint-disable-line max-len\n }`,\n },\n // uploading pixi matrix object to mat3\n {\n test: (data: any, uniform: any): boolean =>\n data.type === 'mat3' && data.size === 1 && !data.isArray && uniform.a !== undefined,\n code: (name: string): string =>\n\n // TODO and some smart caching dirty ids here!\n `\n gl.uniformMatrix3fv(ud[\"${name}\"].location, false, uv[\"${name}\"].toArray(true));\n `,\n codeUbo: (name: string): string =>\n `\n var ${name}_matrix = uv.${name}.toArray(true);\n\n data[offset] = ${name}_matrix[0];\n data[offset+1] = ${name}_matrix[1];\n data[offset+2] = ${name}_matrix[2];\n \n data[offset + 4] = ${name}_matrix[3];\n data[offset + 5] = ${name}_matrix[4];\n data[offset + 6] = ${name}_matrix[5];\n \n data[offset + 8] = ${name}_matrix[6];\n data[offset + 9] = ${name}_matrix[7];\n data[offset + 10] = ${name}_matrix[8];\n `\n ,\n\n },\n // uploading a pixi point as a vec2 with caching layer\n {\n test: (data: any, uniform: any): boolean =>\n data.type === 'vec2' && data.size === 1 && !data.isArray && uniform.x !== undefined,\n code: (name: string): string =>\n `\n cv = ud[\"${name}\"].value;\n v = uv[\"${name}\"];\n\n if(cv[0] !== v.x || cv[1] !== v.y)\n {\n cv[0] = v.x;\n cv[1] = v.y;\n gl.uniform2f(ud[\"${name}\"].location, v.x, v.y);\n }`,\n codeUbo: (name: string): string =>\n `\n v = uv.${name};\n\n data[offset] = v.x;\n data[offset+1] = v.y;\n `\n },\n // caching layer for a vec2\n {\n test: (data: any): boolean =>\n data.type === 'vec2' && data.size === 1 && !data.isArray,\n code: (name: string): string =>\n `\n cv = ud[\"${name}\"].value;\n v = uv[\"${name}\"];\n\n if(cv[0] !== v[0] || cv[1] !== v[1])\n {\n cv[0] = v[0];\n cv[1] = v[1];\n gl.uniform2f(ud[\"${name}\"].location, v[0], v[1]);\n }\n `,\n },\n // upload a pixi rectangle as a vec4 with caching layer\n {\n test: (data: any, uniform: any): boolean =>\n data.type === 'vec4' && data.size === 1 && !data.isArray && uniform.width !== undefined,\n\n code: (name: string): string =>\n `\n cv = ud[\"${name}\"].value;\n v = uv[\"${name}\"];\n\n if(cv[0] !== v.x || cv[1] !== v.y || cv[2] !== v.width || cv[3] !== v.height)\n {\n cv[0] = v.x;\n cv[1] = v.y;\n cv[2] = v.width;\n cv[3] = v.height;\n gl.uniform4f(ud[\"${name}\"].location, v.x, v.y, v.width, v.height)\n }`,\n codeUbo: (name: string): string =>\n `\n v = uv.${name};\n\n data[offset] = v.x;\n data[offset+1] = v.y;\n data[offset+2] = v.width;\n data[offset+3] = v.height;\n `\n },\n // upload a pixi color as vec4 with caching layer\n {\n test: (data: any, uniform: any): boolean =>\n data.type === 'vec4' && data.size === 1 && !data.isArray && uniform.red !== undefined,\n\n code: (name: string): string =>\n `\n cv = ud[\"${name}\"].value;\n v = uv[\"${name}\"];\n\n if(cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue || cv[3] !== v.alpha)\n {\n cv[0] = v.red;\n cv[1] = v.green;\n cv[2] = v.blue;\n cv[3] = v.alpha;\n gl.uniform4f(ud[\"${name}\"].location, v.red, v.green, v.blue, v.alpha)\n }`,\n codeUbo: (name: string): string =>\n `\n v = uv.${name};\n\n data[offset] = v.red;\n data[offset+1] = v.green;\n data[offset+2] = v.blue;\n data[offset+3] = v.alpha;\n `\n },\n // upload a pixi color as a vec3 with caching layer\n {\n test: (data: any, uniform: any): boolean =>\n data.type === 'vec3' && data.size === 1 && !data.isArray && uniform.red !== undefined,\n\n code: (name: string): string =>\n `\n cv = ud[\"${name}\"].value;\n v = uv[\"${name}\"];\n\n if(cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue || cv[3] !== v.a)\n {\n cv[0] = v.red;\n cv[1] = v.green;\n cv[2] = v.blue;\n \n gl.uniform3f(ud[\"${name}\"].location, v.red, v.green, v.blue)\n }`,\n codeUbo: (name: string): string =>\n `\n v = uv.${name};\n\n data[offset] = v.red;\n data[offset+1] = v.green;\n data[offset+2] = v.blue;\n `\n },\n\n // a caching layer for vec4 uploading\n {\n test: (data: any): boolean =>\n data.type === 'vec4' && data.size === 1 && !data.isArray,\n code: (name: string): string =>\n `\n cv = ud[\"${name}\"].value;\n v = uv[\"${name}\"];\n\n if(cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3])\n {\n cv[0] = v[0];\n cv[1] = v[1];\n cv[2] = v[2];\n cv[3] = v[3];\n\n gl.uniform4f(ud[\"${name}\"].location, v[0], v[1], v[2], v[3])\n }`,\n },\n];\n\n"],"names":[],"mappings":";;;;AAwBO,MAAM,cAAmC,GAAA;AAAA,EAG5C;AAAA,IACI,IAAA,EAAM,CAAC,IAAA,KACH,IAAK,CAAA,IAAA,KAAS,WAAW,IAAK,CAAA,IAAA,KAAS,CAAK,IAAA,CAAC,IAAK,CAAA,OAAA;AAAA,IACtD,IAAA,EAAM,CAAC,IACH,KAAA,CAAA;AAAA,mBAAA,EACS,IAAkB,CAAA,WAAA,EAAA,IAAA,CAAA;AAAA;AAAA,oBAAA,EAEjB,IAAsB,CAAA,eAAA,EAAA,IAAA,CAAA;AAAA,iCAAA,EACT,IAAwB,CAAA,iBAAA,EAAA,IAAA,CAAA;AAAA;AAAA,YAAA,CAAA;AAAA,GAGvD;AAAA,EAEA;AAAA,IACI,IAAA,EAAM,CAAC,IAAW,EAAA,OAAA,KAEb,MAAK,IAAS,KAAA,WAAA,IAAe,IAAK,CAAA,IAAA,KAAS,aAAiB,IAAA,IAAA,CAAK,SAAS,gBAAqB,KAAA,IAAA,CAAK,SAAS,CAAK,IAAA,CAAC,KAAK,OAAY,KAAA,OAAA,IAAW,IAAQ,IAAA,OAAA,CAAQ,iBAAsB,KAAA,KAAA,CAAA,CAAA;AAAA,IAC1L,IAAA,EAAM,CAAC,IAAyB,KAAA,CAAA;AAAA;AAAA,sCAEA,EAAA,IAAA,CAAA;AAAA;AAAA,mBAEnB,EAAA,IAAA,CAAA;AAAA;AAAA,oBAEC,EAAA,IAAA,CAAA;AAAA,iCACa,EAAA,IAAA,CAAA;AAAA;AAAA,aAAA,CAAA;AAAA,GAE/B;AAAA,EAEA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