# Beginner: how to run examples from 'The Book of Shaders'

**URL:** https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682
**Category:** Coding Questions
**Created:** [October 20, 2018, 12:00pm UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682 "2018-10-20T12:00:25Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)
#### Post date: [October 20, 2018, 12:00pm UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/1 "2018-10-20T12:00:25Z")

</div>

Hello and thank you,

I am an absolute beginner with minimal knowledge of coding and I started working trough the examples of ‘The Book of Shaders’ ( [https://thebookofshaders.com](https://thebookofshaders.com))  
I am trying to get the example code to work in Processing, for example:

```auto
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main() {
	vec2 st = gl_FragCoord.xy/u_resolution;
	gl_FragColor = vec4(st.x,0.0,0.0,1.0);
}

```

I placed a file named shader.frag in the ‘data’ folder of the sketch (but I don’t understand what purpose it’s used for):

```auto
PShader shader;

void setup() {
  size(640, 360, P2D);
  noStroke();

  shader = loadShader("shader.frag");
}

void draw() {
  shader.set("u_resolution", float(width), float(height));
  shader.set("u_mouse", float(mouseX), float(mouseY));
  shader.set("u_time", millis() / 1000.0);
  shader(shader);
  rect(0,0,width,height);
}

```

When running the first mentioned piece of code, there are a bunch of warnings obviously.  
I have tried different setups, for example placing the ‘void main()’ method after the ‘void draw()’ method but anyhow, commands like vec2, fragcoord etc are not recognized.

I’m stuck and I would really appreciate some pointers in the right direction. How can I run the example shaders from ‘The Book of Shaders’ in Processing?

Thank you!

---

<div class="post-metadata">

### Author: ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)
#### Post date: [October 20, 2018, 3:22pm UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/2 "2018-10-20T15:22:09Z")

</div>

Hello and welcome @Hieronymus!  
The issue is that you’re combining GLSL code with Processing code. The following is GLSL code:

```auto
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main() {
	vec2 st = gl_FragCoord.xy/u_resolution;
	gl_FragColor = vec4(st.x,0.0,0.0,1.0);
}

```

This code is stored in a file called `shader.frag` You can simply open a notepad file, paste in the code there and save it as `shader.frag`

This is processing code that will use the shader written previously and then run it in processing:

```java
PShader shader;

void setup() {
  size(640, 360, P2D);
  noStroke();

  shader = loadShader("shader.frag");
}

void draw() {
  shader.set("u_resolution", float(width), float(height));
  shader.set("u_mouse", float(mouseX), float(mouseY));
  shader.set("u_time", millis() / 1000.0);
  shader(shader);
  rect(0,0,width,height);
}

```

When you save this code in processing, say you name it `ProcessingCode`, you will have a `ProcessingCode` folder that contains your sketch as `ProcessingCode.pde`. Normally in that folder, you have a `data` folder where you store things that you will load later into processing. Notice the statement in `void setup()` block that says `shader = loadShader("shader.frag");` You are essentially loading the shader stored in the `data` folder into processing that way.

It’s great that you’re excited to get started with shaders, but I would recommend spending some time playing with Processing a little bit more before delving into GLSL. If you’re aiming to learn GLSL only, it might get a bit frustrating to debug in processing, so I would recommend you play with it more in the code editor that comes with thebookofshaders first.

---

<div class="post-metadata">

### Author: ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)
#### Post date: [October 20, 2018, 5:52pm UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/3 "2018-10-20T17:52:36Z")

</div>

@WakeMeAtThree , thanks a million!

It’s a bit clearer now, so I’m going to take some time to work with more examples. Yes, I am very excited about this subject and I will definitely spend more time with Processing too!

cheers

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [October 21, 2018, 2:28am UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/4 "2018-10-21T02:28:21Z")

</div>

i understand that you do all that already, the processing code and the  
/data/shader.frag file.

and when i try that i get following error ( info):  
`The shader doesn't have a uniform called "u_mouse" OR the uniform was removed during compilation because it was unused. The shader doesn't have a uniform called "u_time" OR the uniform was removed during compilation because it was unused.`  
//\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
also i try  
[https://processing.org/tutorials/pshader/](https://processing.org/tutorials/pshader/)  
and load all the files:

> **[codeanticode/pshader-tutorials](https://github.com/codeanticode/pshader-tutorials)**
>
> Tutorial for the shader API in Processing 2.0. Contribute to codeanticode/pshader-tutorials development by creating an account on GitHub.

  
as zip  
and try  
**Ex\_09\_1\_allshaders**  
so it can be also a great resource for you, even is is also older it runs at v3.4

---

<div class="post-metadata">

### Author: ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)
#### Post date: [October 21, 2018, 9:18am UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/5 "2018-10-21T09:18:34Z")

</div>

Very cool, thank you for the resource.More examples to play with 😌

---

<div class="post-metadata">

### Author: ![PHuzzeyMusic](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@PHuzzeyMusic](https://discourse.processing.org/u/PHuzzeyMusic)
#### Post date: [October 22, 2018, 2:41am UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/6 "2018-10-22T02:41:29Z")

</div>

A little late, but if you would like to use the more current versions of openGL use [https://processing.org/reference/settings\_.html](https://processing.org/reference/settings_.html) in your sketch. Then you can specify which version of openGL to use:

```auto
void settings(){
    size(1280, 720, P3D);
    PGOGL.profile = 4;// 450 is the version I can use on my laptop
}
//then in glsl files declare a version
#version 450 // or whatever version is compatible with your system

```

if you run shaders in processing without a version I think it defaults to 1.2 which may not be what you want if you have looked at any current openGL docs.  
Also Visual Studio Community 2017 has a glsl extension that is really good for code completion and error detection.

---

<div class="post-metadata">

### Author: ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)
#### Post date: [October 22, 2018, 8:09am UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/7 "2018-10-22T08:09:02Z")

</div>

Thank you @PHuzzeyMusic  
My laptop has Version 4.0 The examples seem to work except for a few commands, which is ok for now. I have decided to spend more time on the basics before continuing with shaders.

---

<div class="post-metadata">

### Author: ![L0RDZed](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/l0rdzed/32/1136_2.png) [@L0RDZed](https://discourse.processing.org/u/L0RDZed)
#### Post date: [June 11, 2021, 8:19pm UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/8 "2021-06-11T20:19:27Z")

</div>

Hey, I’m only posting this because when I googled the error this was one of a few results which poked up.

I had the error message

`The shader doesn't have a uniform called "u_mouse" OR the uniform was removed during compilation because it was unused. The shader doesn't have a uniform called "u_time" OR the uniform was removed during compilation because it was unused.`

And I struggled for ages.

And it was a simple fix. I had defined these variables but not called them.

```auto
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main() {
//u_resolution, u_mouse, u_time are defined above and need to be 
//called here. At the moment only u_resolution (st.x) is being called.
//It is the first argument for vec4.
//The processing sketch will throw the error that u_mouse and u_time are not being used.
//The sketch will still run - so if you don't need to use them it's fine.
//To get ride of the "u_mouse" error in Processing I would have to use the variable.
//Simple but it took me a whole day to wrap my head around it and if anyone else ends up here.
//I hope it helps.

	vec2 st = gl_FragCoord.xy/u_resolution;
    vec2 ms = gl_FragCoord.xy/u_mouse;
// if i use ms.x or ms.y in vec4 e.g. vec4(ms.x,ms.y,0.0,1.0) then the mouse
// will be called and the error will resolve. But u_resolution will throw an error.
	gl_FragColor = vec4(st.x,0.0,0.0,1.0);
}

```

---

<div class="post-metadata">

### Author: ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)
#### Post date: [June 11, 2021, 8:34pm UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/9 "2021-06-11T20:34:44Z")

</div>

Very cool of you to post your solution here even though the thread is a couple of years old. Thanks!

---

<div class="post-metadata">

### Author: ![L0RDZed](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/l0rdzed/32/1136_2.png) [@L0RDZed](https://discourse.processing.org/u/L0RDZed)
#### Post date: [June 11, 2021, 11:21pm UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/10 "2021-06-11T23:21:50Z")

</div>

haha just doing my netizen duty of care for old forum topics 😛 Hope you had luck with the shaders after this! I’m just getting into them.

---

<div class="post-metadata">

### Author: ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)
#### Post date: [June 12, 2021, 10:26am UTC](https://discourse.processing.org/t/beginner-how-to-run-examples-from-the-book-of-shaders/4682/11 "2021-06-12T10:26:35Z")

</div>

Ha, I didn’t further dive into the realm of shaders yet but I do continue to use Processing though!
