# Using an if statement in a material fragment script

**URL:** <https://forum.defold.com/t/using-an-if-statement-in-a-material-fragment-script/66621>\
**Category:** Questions\
**Created:** [October 16, 2020, 3:20am UTC](https://forum.defold.com/t/using-an-if-statement-in-a-material-fragment-script/66621 "2020-10-16T03:20:50Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [October 16, 2020, 7:08am UTC](https://forum.defold.com/t/using-an-if-statement-in-a-material-fragment-script/66621/3 "2020-10-16T07:08:16Z")

</div>

Yes, syntax is like in C++ :

`if(){}`

or

`if(){}else if(){} else{}`

To do what you want (pallete modifications) take a default **sprite.fp** as a base of your new fragment program:

```
varying mediump vec2 var_texcoord0;

uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    gl_FragColor = texture2D(texture_sampler, var_texcoord0.xy) * tint_pm;
}

```

above, the color of the pixel is taken (from **texture2D** ) and only multiplied by **tint\_pm** and written to **gl\_FragColor** which is an output color.

You want to take a color from texture and not write it to the output, but to a variable, that you can operate on, so make a vector called **color\_of\_pixel** and take a color from **texture2D** :

```
lowp vec4 color_of_pixel = texture2D(texture_sampler, var_texcoord0.xy);

```

Then you want to modify the color if it meets your requirement, so here you can write for example:

```
if (color_of_pixel == base_hair_color) {
    pixel_color = my_new_hair;
}

```

^ This will take the pixels that has the same color as **base\_hair\_color** (e.g. black) and change its color to my\_new\_hair e.g.white;

Don’t forget to define the colors you used in if statement on top of the program or pass it using a shader constant (more advanced).

```
lowp vec4 base_hair_color = vec4(0.0, 0.0, 0.0, 1.0); // black - i just wrote it on top of program
lowp vec4 my_new_hair = vec4(1.0, 1.0, 1.0, 1.0); // white- i just wrote it on top of program

```

Write as many if statement as you need using `else if` construct  
In the end write the color\_of\_pixel to the output variable called **gl\_FragColor** and eventually multiply it by the tint as in the original sprite fragment program.

The whole program can look like this:

```
varying mediump vec2 var_texcoord0;

uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;

lowp vec4 base_hair_color = vec4(0.0, 0.0, 0.0, 1.0); // black - i just wrote it on top of program
lowp vec4 my_new_hair = vec4(1.0, 1.0, 1.0, 1.0); // white- i just wrote it on top of program

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    lowp vec4 color_of_pixel = texture2D(texture_sampler, var_texcoord0.xy);
    if (color_of_pixel == base_hair_color) {
        pixel_color = my_new_hair;
    }
    gl_FragColor = color_of_pixel * tint_pm;
}

```

Then add such program to a new material and apply this material to a sprite (with black hair 😃 )  
Run it and hair should be white 😉

This is the most simplest version and will work for all black pixels, so I;m using special colors for sprites I want such modifications to have (aka only hero sprite:)

 ![png](https://forum-defold.b-cdn.net/uploads/default/original/3X/f/3/f379e5ed93e6e541b2a8ddd6ac0bca9ddafa33fe.png)

You see here I used a specific color pallete - cloak, gloves, sword and jacket are modifiable and use basic colors (red, green, blue, magenta, cyan, yellow, black, white) so in shader program for that particular sprite there is no option that when I change color for cloak, some other colors will be changed, because there is no other part using pure red nor black color 😉 Tinker a little bit with it and maybe you won’t need to recolour your sprites 😉 just getting RGB values of your current sprites will be enough (I did that for the first version, something like: _if (color == vec4(55/255, 60/255, 0/255, 1.0)_)

If you will have any questions, don’t hesitate to ask 😉

---

_[View the full topic](https://forum.defold.com/t/using-an-if-statement-in-a-material-fragment-script/66621)._
