Files

1 line
6.4 KiB
Plaintext
Raw Permalink Normal View History

2025-01-04 00:34:03 +01:00
{"version":3,"file":"MeshMaterial.mjs","sources":["../src/MeshMaterial.ts"],"sourcesContent":["import { Color, Matrix, Program, Shader, TextureMatrix } from '@pixi/core';\nimport fragment from './shader/mesh.frag';\nimport vertex from './shader/mesh.vert';\n\nimport type { ColorSource, Texture, utils } from '@pixi/core';\n\nexport interface IMeshMaterialOptions\n{\n alpha?: number;\n tint?: ColorSource;\n pluginName?: string;\n program?: Program;\n uniforms?: utils.Dict<unknown>;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface MeshMaterial extends GlobalMixins.MeshMaterial {}\n\n/**\n * Slightly opinionated default shader for PixiJS 2D objects.\n * @memberof PIXI\n */\nexport class MeshMaterial extends Shader\n{\n /**\n * TextureMatrix instance for this Mesh, used to track Texture changes.\n * @readonly\n */\n public readonly uvMatrix: TextureMatrix;\n\n /**\n * `true` if shader can be batch with the renderer's batch system.\n * @default true\n */\n public batchable: boolean;\n\n /**\n * Renderer plugin for batching.\n * @default 'batch'\n */\n public pluginName: string;\n\n // Internal-only properties\n _tintRGB: number;\n\n /**\n * Only do update if tint or alpha changes.\n * @private\n * @default false\n */\n private _colorDirty: boolean;\n private _alpha: number;\n private _tintColor: Color;\n\n /**\n * @param uSampler - Texture that material uses to render.\n * @param options - Additional options\n * @param {number} [options.alpha=1] - Default alpha.\n * @param {PIXI.ColorSource} [options.tint=0xFFFFFF] - Default tint.\n * @param {string} [options.pluginName='batch'] - Renderer plugin for batching.\n * @param {PIXI.Program} [options.program=0xFFFFFF] - Custom program.\n * @param {object} [options.uniforms] - Custom uniforms.\n */\n constructor(uSampler: Texture, options?: IMeshMaterialOptions)\n {\n const uniforms = {\n uSampler,\n alpha: 1,\n uTextureMatrix: Matrix.IDENTITY,\n uColor: new Float32Array([1, 1, 1, 1]),\n };\n\n // Set defaults\n options = Object.assign({\n tint: 0xFFFFFF,\n alpha: 1,\n pluginName: 'batch',\n }, options);\n\n if (options.uniforms)\n {\n Object.assign(uniforms, options.uniforms);\n }\n\n super(options.program || Program.from(vertex, fragment), uniforms);\n\n this._colorDirty = false;\n\n this.uvMatrix = new TextureMatrix(uSampler);\n this.batchable = options.program === undefined;\n this.pluginName = options.pluginName;\n\n this._tintColor = new Color(options.tint);\n this._tintRGB = this._tintColor.toLittleEndianNumber();\n this._colorDirty = true;\n this.alpha = options.alpha;\n }\n\n /** Reference to the texture being rendered. */\n get texture(): Texture\n {\n return this.uniforms.uSampler;\n }\n set texture(value: Texture)\n {\n if (this.uniforms.uSampler !== value)\n {\n if (!this.uniforms.uSampler.baseTexture.alphaMode !== !value.baseTexture.alphaMode)\n {\n this._colorDirty = true;\n }\n\n this.uniforms.uSampler = value;\n this.uvMatrix.texture = value;\n }\n }\n\n /**\n * This gets automatically set by the object using this.\n * @default 1\n */\n set alpha(value: number)\n {\n if (value === this._alpha) return;\n\n this._alpha = value;\n this._colorDirty = true;\n }\n get alpha(): number\n {\n return this._alpha;\n }\n\n /**\n * Multiply tint for the material.\n * @default 0xFFFFFF\n */\n set tint(value: ColorSource)\n {\n if (value === this.tint) return;\n\n this._tintColor.setValue(value);\n this._tintRGB = this._tintColor.toLittleEndianNumber();\n this._colorDi