设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 87|回复: 0

[制作教程] How to use PIXI ColorMatrixFilter

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    前天 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    4446

    主题

    864

    回帖

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    22899
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    28239

    灌水之王

    发表于 3 天前 | 显示全部楼层 |阅读模式
    Who this tutorial is for:

    This tutorial is for those who have been coding in MV/MZ, might have been messing around with PIXI's features, seen that one of the available filters is PIXI.filters.ColorMatrixFilter but have no idea what it means when PIXI's documentation says it's a "5x4 transformation matrix".

    I was unable to find an explanation online when I first discovered it so I could only use built-in presets. Much later, I took a class in linear algebra and now I was able to figure out what it does, and how it does. Here, I'll break down the basics of how to read this type of math.

    What is this filter?

    It converts the color of every pixel based on that pixel's red, green, blue, and alpha channels.

    Here's an example script call you can put into your game to apply a ColorMatrixFilter on to a map. This one inverts every color:

                    Code:       
    var scene = SceneManager._scene;scene.filters = [new PIXI.filters.ColorMatrixFilter()];scene.filters[0].matrix = [    -1, 0, 0, 0, 0.5,    0, -1, 0, 0, 0.5,    0, 0, -1, 0, 0.5,    0, 0, 0, 1, 0];


    And this is what the script call above does to my game:




    What do these numbers mean?

    Essentially, it's an abstracted equation.

    The first row (or the first five numbers) gives us an equation of what the new red component of each pixel should be.
    The second row (or the next five numbers) tells us what the new green color should be.
    The third row is for the blue channel.
    The fourth row is for the alpha channel.

    Values range from 0 to 1 (basically 0% through 100%). Each row takes in five arguments. They are in order: red, green, blue, alpha, constant.

                    Code:       
    [    -1, 0, 0, 0, 0.5,    0, -1, 0, 0, 0.5,    0, 0, -1, 0, 0.5,    0, 0, 0, 1, 0];


    So, the matrix from the above example code is a way of saying:
                    Code:       
    Red   = -1 * red +  0 * green +  0 * blue + 0 * alpha + 0.5Green =  0 * red + -1 * green +  0 * blue + 0 * alpha + 0.5Blue  =  0 * red +  0 * green + -1 * blue + 0 * alpha + 0.5Alpha =  0 * red +  0 * green +  0 * blue + 1 * alpha + 0

    So by inverting each color component with a -100% muliplication and adding 50%, I've created a color inversion filter!

    Other example matrices:


    Red-Green Colorblindness. This just blends the red and green channels together.
                    Code:       
    var scene = SceneManager._scene;scene.filters = [new PIXI.filters.ColorMatrixFilter()];scene.filters[0].matrix = [    0.5, 0.5, 0, 0, 0,    0.5, 0.5, 0, 0, 0,    0, 0, 1, 0, 0,    0, 0, 0, 1, 0];


    High Contrast
                    Code:       
    var scene = SceneManager._scene;scene.filters = [new PIXI.filters.ColorMatrixFilter()];scene.filters[0].matrix = [    2, 0, 0, 0, -1,    0, 2, 0, 0, -1,    0, 0, 2, 0, -1,    0, 0, 0, 1, 0];


    Increased saturation
                    Code:       
    var scene = SceneManager._scene;scene.filters = [new PIXI.filters.ColorMatrixFilter()];scene.filters[0].matrix = [    2, -0.5, -0.5, 0, 0,    -0.5, 2, -0.5, 0, 0,    -0.5, -0.5, 2, 0, 0,    0, 0, 0, 1, 0];


    You can even write these matrices as mathematical functions! Here's example code that you could save as a .js file and install as a plugin:

    Spoiler: Pulsation
    This one animates from normal contrast to high contrast:
                    Code:       
    (function(){var matrixAnimationCount = 0;getMatrixPulsation = function() {    matrixAnimationCount++;    const PERIOD = 300 / (2 * Math.PI);    const max = 3;    const min = 1;    const strength = (min - max) / 2;    var phase = matrixAnimationCount / PERIOD;    return getMatrixContrast(strength * (Math.cos(phase) + 1) + max);};getMatrixContrast = function(strength) {    var offset = 0.5 * (1 - strength);    return [        strength, 0, 0, 0, offset,        0, strength, 0, 0,  offset,        0, 0, strength, 0,  offset,        0, 0, 0, 1, 0    ]};_aliasSceneBaseUpdate = Scene_Base.prototype.update;_applyUpdatingFilter = function(method) {    Scene_Base.prototype.update = function() {        _aliasSceneBaseUpdate.call(this);        this.filters[0].matrix = method();    };}_applyUpdatingFilter(getMatrixPulsation);_aliasSceneBaseCreate = Scene_Base.prototype.create;Scene_Base.prototype.create = function() {    _aliasSceneBaseCreate.call(this);    var filter = new PIXI.filters.ColorMatrixFilter();    if (this.filters == null) {        this.filters = [filter];    } else {        this.filters = this.filters.concat([filter]);    }}})()


    Spoiler: Color Cycling 1
                    Code:       
    (function(){var matrixAnimationCount = 0;getMatrixTrippy = function() {    matrixAnimationCount++;    const PERIOD = 120;    var getAmount = function(phase) {        return Math.abs(1 - 2 * (phase % 1));    }    var phase = matrixAnimationCount / PERIOD;    var amounts = [0, getAmount(phase), getAmount(phase-0.5)];    return [        amounts[0], amounts[1], amounts[2], 0, 0,        amounts[2], amounts[0], amounts[1], 0, 0,        amounts[1], amounts[2], amounts[0], 0, 0,        0, 0, 0, 1, 0    ]};_aliasSceneBaseUpdate = Scene_Base.prototype.update;_applyUpdatingFilter = function(method) {    Scene_Base.prototype.update = function() {        _aliasSceneBaseUpdate.call(this);        this.filters[0].matrix = method();    };}_applyUpdatingFilter(getMatrixTrippy);_aliasSceneBaseCreate = Scene_Base.prototype.create;Scene_Base.prototype.create = function() {    _aliasSceneBaseCreate.call(this);    var filter = new PIXI.filters.ColorMatrixFilter();    if (this.filters == null) {        this.filters = [filter];    } else {        this.filters = this.filters.concat([filter]);    }}})()


    Spoiler: Color Cycling 2
                    Code:       
    (function(){var matrixAnimationCount = 0;getMatrixTrippy2 = function() {    matrixAnimationCount++;    const PERIOD = 120;    var getAmount = function(phase) {        return Math.max(0, Math.abs(1.5 - phase % 3) - 0.5);    }    var phase = 3 * matrixAnimationCount / PERIOD;    var amounts = [getAmount(phase), getAmount(phase-1), getAmount(phase-2)];    return [        getAmount(phase), getAmount(0.5 * phase + 1), getAmount(0.25 * phase + 2), 0, 0,        getAmount(phase - 1), getAmount(0.5 * phase + 2), getAmount(0.25 * phase), 0, 0,        getAmount(phase - 2), getAmount(0.5 * phase), getAmount(0.25 * phase + 1), 0, 0,        0, 0, 0, 1, 0    ]};_aliasSceneBaseUpdate = Scene_Base.prototype.update;_applyUpdatingFilter = function(method) {    Scene_Base.prototype.update = function() {        _aliasSceneBaseUpdate.call(this);        this.filters[0].matrix = method();    };}_applyUpdatingFilter(getMatrixTrippy2);_aliasSceneBaseCreate = Scene_Base.prototype.create;Scene_Base.prototype.create = function() {    _aliasSceneBaseCreate.call(this);    var filter = new PIXI.filters.ColorMatrixFilter();    if (this.filters == null) {        this.filters = [filter];    } else {        this.filters = this.filters.concat([filter]);    }}})()




    本贴来自国际rpgmaker官方论坛作者:Gensun处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/how-to-use-pixi-colormatrixfilter.181109/

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    x
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-7-14 14:28 , Processed in 0.125956 second(s), 56 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表