# Shaders for beginners

**URL:** <https://forum.defold.com/t/shaders-for-beginners/66622>\
**Category:** The Defoldmine\
**Created:** [October 16, 2020, 12:04pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622 "2020-10-16T12:04:27Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![SpencerWF](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/spencerwf/32/21158_2.png) [@SpencerWF](https://forum.defold.com/u/SpencerWF)\
**Post date:** [December 28, 2020, 9:48pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/21 "2020-12-28T21:48:43Z")

</div>

This was a great post! I was hoping to also figure out how to tell what the vec4 values of a pixel is in the shader, so that I could change just the pixels that are of a particular color?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 28, 2020, 9:55pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/22 "2020-12-28T21:55:22Z")

</div>

Check this post

> [@Using an if statement in a material fragment script](https://forum.defold.com/t/using-an-if-statement-in-a-material-fragment-script/66621/3):
>
> 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,…

---

<div class="post-metadata">

**Author:** ![gameoffate2018](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gameoffate2018/32/20065_2.png) [@gameoffate2018](https://forum.defold.com/u/gameoffate2018)\
**Post date:** [March 5, 2021, 11:51pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/23 "2021-03-05T23:51:28Z")

</div>

Hey!

I was trying to follow the flashing sprite tutorial, I got it working but when I did an HTML 5 build I got an error saying it didn’t recognize some of the operations (see attached screen shots). It looks to be some sort of conversion error, I’m super confused by it. Do you know what I could have done wrong or how to fix it?

 ![flashing_sprite_error](https://forum-defold-uploads.b-cdn.net/original/3X/e/b/ebbfd17d8e06c7c824f69407d96b3a82e9aa007f.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [March 6, 2021, 1:57am UTC](https://forum.defold.com/t/shaders-for-beginners/66622/24 "2021-03-06T01:57:30Z")

</div>

Be careful with mixing ints (like 1 2 3 4) and floats (like 0.1 0.5 1.9) in shader programs (which have more strict rules for types compared to Lua). Generally you want everything to be a float. So if you want to have a 1 you need to write it as either 1.0 or 1. as shorthand (but I prefer to include the 0 for the sake of visual clarity).

---

<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:** [March 6, 2021, 7:22am UTC](https://forum.defold.com/t/shaders-for-beginners/66622/25 "2021-03-06T07:22:32Z")

</div>

You are right! I corrected the code snippets (there were comparisons to 0 and 1 instead of 0.0 and 1.0\>

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [March 6, 2021, 9:55am UTC](https://forum.defold.com/t/shaders-for-beginners/66622/26 "2021-03-06T09:55:37Z")

</div>

Also note:

1. Comparing two floats is kind of a strange condition… Float are “approximations” of real numbers. It is better to replace a = b by abs(a - b) \< some small number.

2. IF statement in shaders should be avoided… as in any parallel computation. But this is a long story.

Ciao!

---

<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:** [March 6, 2021, 6:01pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/27 "2021-03-06T18:01:29Z")

</div>

Oh yes, I agree, but I still aimed to make it as simple and understandable as possible 😅 The whole example is only to highlight some features and understanding of how it works and it shouldn’ be surely used in production, as I mentioned 😉 Do you think I should make it in other way? Or mention some more details or warnings?

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [March 6, 2021, 6:27pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/28 "2021-03-06T18:27:22Z")

</div>

> [@Pawel](#):
>
> lowp vec4 color\_of\_pixel = texture2D(texture\_sampler, var\_texcoord0.xy); if(color\_of\_pixel.a != 0.0) // when alpha value of the color is not 0 (not transparent) (you can get components of this vector like .r, .b, .g and .a) { color\_of\_pixel = vec4(1.,1.,1.,1.); // then it’s your character - turn the color to white } gl\_FragColor = color\_of\_pixel; // write the color\_of\_pixel to the output gl\_FragColor

No, no. The tutorial is perfect!

If you want to remove the if statement then I think this should work:

```
varying mediump vec2 var_texcoord0; // as it was
uniform lowp sampler2D texture_sampler; // as it was

void main(){
   float alpha = texture2D(texture_sampler, var_texcoord0).a;
   gl_FragColor = vec4(1.0, 1.0, 1.0, alpha)
}

```

However I have not tested it…

Ciao!

---

<div class="post-metadata">

**Author:** ![gameoffate2018](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gameoffate2018/32/20065_2.png) [@gameoffate2018](https://forum.defold.com/u/gameoffate2018)\
**Post date:** [March 6, 2021, 7:37pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/29 "2021-03-06T19:37:49Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Dong\_Wang](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dong_wang/32/28542_2.png) [@Dong\_Wang](https://forum.defold.com/u/Dong_Wang)\
**Post date:** [May 15, 2022, 3:11pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/30 "2022-05-15T15:11:48Z")

</div>

That’s awesome article. I am making fruit slot machine game. When I try to make fruits only visible in rectangle area, the solution I can find is using shader program to change the color of the pixel. Here is the code I use in fp:

```C++
varying mediump vec4 position;
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);
	if(position.y >= 465.0 && position.y <= 815.0) {
		gl_FragColor = texture2D(texture_sampler, var_texcoord0.xy) * tint_pm;
	} else {
		gl_FragColor = texture2D(texture_sampler, var_texcoord0.xy) * vec4(1.0, 1.0, 1.0, 0.0);
	}
}

```

I got error says Input of fragment shader ‘position’ not written by vertex shader.

Can I use the varying position to check whether it in the rectangle area? Any help will be appreciate.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [May 15, 2022, 5:23pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/31 "2022-05-15T17:23:19Z")

</div>

> [@Dong\_Wang](#):
>
> I got error says Input of fragment shader ‘position’ not written by vertex shader

What does the vertex shader look like?

---

<div class="post-metadata">

**Author:** ![Dong\_Wang](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dong_wang/32/28542_2.png) [@Dong\_Wang](https://forum.defold.com/u/Dong_Wang)\
**Post date:** [May 15, 2022, 10:16pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/32 "2022-05-15T22:16:14Z")

</div>

I am using the default build in sprite.vp.

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [May 16, 2022, 7:12am UTC](https://forum.defold.com/t/shaders-for-beginners/66622/33 "2022-05-16T07:12:21Z")

</div>

The default sprite.vp doesn’t write to any position varying afaik. You can either:  
Copy the sprite.vp and paste:

`
uniform highp mat4 view_proj;
// positions are in world space
attribute highp vec4 position;
attribute mediump vec2 texcoord0;
varying mediump vec2 var_texcoord0;
varying mediump vec4 position;
`

`void main()
{
gl_Position = view_proj * vec4(position.xyz, 1.0);
var_texcoord0 = texcoord0;
position = position;
}
`

The two things added are:  
varying mediump vec4 position;

and position = position;

OR:  
You can use gl\_FragCoord in your conditional ([gl\_FragCoord - OpenGL 4 Reference Pages](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_FragCoord.xhtml))

* * *

To clarify, the issue you are facing is that the vertex shader only binds the “attribute” of the mesh vertices which is usually the components of the triangles such as position, uv, color and so forth. The data between the vertex and fragment stages are not written automatically, that needs to be specified explicitly by specifying (and setting) these varying variables. In newer versions of GLSL, these variables are named “input” and “output” between stages, which I think is more understandable than “attribute” and “varying”. Just ask if you get stuck!

---

<div class="post-metadata">

**Author:** ![Dong\_Wang](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dong_wang/32/28542_2.png) [@Dong\_Wang](https://forum.defold.com/u/Dong_Wang)\
**Post date:** [May 16, 2022, 11:18am UTC](https://forum.defold.com/t/shaders-for-beginners/66622/34 "2022-05-16T11:18:45Z")

</div>

Thx for your reply. I got error from the vp code you provided. It says  
ERROR:GRAPHICS: ERROR: 0:8: Regular non-array variable ‘position’ may not be redeclared  
ERROR: 0:14: Left-hand-side of assignment must not be read-only.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [May 16, 2022, 11:30am UTC](https://forum.defold.com/t/shaders-for-beginners/66622/35 "2022-05-16T11:30:58Z")

</div>

@Dong_Wang please share .vp, .fp and .material file that you use.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [May 16, 2022, 11:35am UTC](https://forum.defold.com/t/shaders-for-beginners/66622/36 "2022-05-16T11:35:15Z")

</div>

> [@jhonny.goransson](#):
>
> attribute highp vec4 position;  
> varying mediump vec4 position;

> Regular non-array variable ‘position’ may not be redeclared

You cannot have two variables declared with the same name.

Rename the added one to `var_position`, and use that in your .fp file instead:

```lua
varying mediump vec2 var_texcoord0;
varying mediump vec4 var_position;

```

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [May 16, 2022, 3:51pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/37 "2022-05-16T15:51:30Z")

</div>

Right, I didn’t read too carefully 😅

---

<div class="post-metadata">

**Author:** ![netox](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/netox/32/30603_2.png) [@netox](https://forum.defold.com/u/netox)\
**Post date:** [May 9, 2024, 1:15pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/38 "2024-05-09T13:15:45Z")

</div>

No words to say how great is this article! Thank you so much!  
I am trying to back to game development, working in a turn based game(just started this week), I will do a post on this soon to try and keep myself motivated 😅

I added these shaders to the damage and they work perfectly for my needs.  
 ![video1_TSM-ezgif.com-video-to-gif-converter](https://defold-forum-bucket.nbg1.your-objectstorage.com/original/3X/e/5/e5180f563ed14b6711682d122991a18aca4b2bc0.gif)

---

<div class="post-metadata">

**Author:** ![schlista](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/schlista/32/44619_2.png) [@schlista](https://forum.defold.com/u/schlista)\
**Post date:** [May 17, 2024, 8:01pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/39 "2024-05-17T20:01:39Z")

</div>

Thank you! Finally a shader article written so newbs to shaders can follow along! Amazing work.

---

<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:** [June 1, 2026, 12:44pm UTC](https://forum.defold.com/t/shaders-for-beginners/66622/40 "2026-06-01T12:44:14Z")

</div>

This post was created a while ago, but since Defold introduced the new modern SPIR-V based rendering pipeline we recommend everyone to use it and write shaders in at least GLSL version 140 (OpenGL 3.1 / ES 3.0+ compatible) **GLSL version 140** (OpenGL 3.1 / ES 3.0+ compatible), the article on website is now updated:

> **[Shaders for Beginners | Defold](https://defold.com/2020/10/19/Shaders-for-beginners/)**
>
> In this guest post we invited Pawel Jarosz, developer of Witchcrafter - Empire Legends, to share his experience learning shader programming.

* * *

Btw, also the Shadertoy tutorial is updated, and additionally it uses the `Time` constant:

> **[Shadertoy to Defold tutorial](https://defold.com/tutorials/shadertoy/)**
>
> In this tutorial you will convert a shader from shadertoy.com to Defold.

[Previous page](https://forum.defold.com/t/shaders-for-beginners/66622.md?page=1)
