Jump to content

Tint to color filter


dmko
 Share

Recommended Posts

I'd like to tint a sprite to a solid color, i.e. if it's a texture just turn all the opaque pixels to that particular value (Sprite.tint to white does not do this)

From looking at http://pixijs.github.io/pixi-filters/examples/ it seems there's no built-in filter to do this. Do I need to write a new filter/shader for v4 in order to accomplish this?

Not too scared, but just wanted to make sure this particular path hasn't been beaten down already ;)

Thanks!

Link to comment
Share on other sites

If you need 2d canvas support also you could do that by rendering the textures to offscreen canvas like this.
 

var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
var img = someImageElement;
canvas.width = img.width;
canvas.height = img.height;
context.fillStyle = "#FFFFFF"; //Replace with wanted color.
context.fillRect(0,0,img.width, img.height);
context.globalCompositeOperation = "destination-in";
context.drawImage(img, 0,0, img.width, img.height);

var baseTexture = new BaseTexture(canvas);
var texture = new Texture(baseTexture);
var sprite = new Sprite(texture);
stage.addChild(sprite);

If you're using spritesheet then you could build new textures with frame definitions from that spritesheet and just have them use the canvas as basetexture.

Link to comment
Share on other sites

OK cool, I got it to work... calling this "colorfill".. no doubt there's a better way of inlining the shader code, but I didn't want to worry about browser issues etc...

I used an "loose augmentation" structure so I can add more filters, and use the DefaultVert across them

usage: 

var filter = new MY_SHADERS.ColorFill(0xFF0000); //fill with any hex color

item.filters = [filter];


colorfill frag shader code:
 

var MY_SHADERS = (function(exports) {
    var ColorFill = function(hexVal) {
        var str = "";

        str += "precision mediump float;";

        str += "varying vec2 vTextureCoord;";
        str += "uniform sampler2D uSampler;";
        str += "uniform vec3 rgbColor;";


        str += "void main(void) {";

        str += "gl_FragColor = texture2D(uSampler, vTextureCoord);";
        str += "gl_FragColor.r = rgbColor.r * gl_FragColor.a;";
        str += "gl_FragColor.g = rgbColor.g * gl_FragColor.a;";
        str += "gl_FragColor.b = rgbColor.b * gl_FragColor.a;";
        str += "}";

        PIXI.Filter.call(this, MY_SHADERS.DefaultVert, str);

        if (hexVal !== undefined) {
            this.hexColor = hexVal;
        }
    }

    ColorFill.prototype = Object.create(PIXI.Filter.prototype);
    ColorFill.prototype.constructor = ColorFill;
    Object.defineProperties(ColorFill.prototype, {
        rgbColor: {
            get: function() {
                return this.uniforms.rgbColor;
            },
            set: function(value) {
                this.uniforms.rgbColor = value;
            }
        },

        hexColor: {
            get: function() {
                return PIXI.utils.rgb2hex(this.uniforms.rgbColor);
            },
            set: function(value) {
                this.uniforms.rgbColor = PIXI.utils.hex2rgb(value);
            }
        },
    });

    exports.ColorFill = ColorFill;

    return exports;

}(MY_SHADERS || {}));

Default vert shader code:
 

//taken from pixi filters repo
var MY_SHADERS = (function(exports) {
    var str = "";

    str += "attribute vec2 aVertexPosition;";
    str += "attribute vec2 aTextureCoord;";

    str += "uniform mat3 projectionMatrix;";

    str += "varying vec2 vTextureCoord;";

    str += "void main(void)";
    str += "{";
    str += "gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);";
    str += "vTextureCoord = aTextureCoord;";
    str += "}";

    exports.DefaultVert = str;

    return exports;

}(MY_SHADERS || {}));

 

Edited by dmko
couldn't read js in post
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...