Jump to content

Shader Tutorial


Andy90
 Share

Go to solution Solved by klepto2,

Recommended Posts

I don't think there's an official tutorial or anything but I made a tutorial on how to convert from Shadertoy to Leadwerks. If you look around the community you can find things here and there.  Also I made a blog with a bunch of different shaders you can grab. Once you get the hang of them you should be able to figure them out.
 

 

...and the blog:

 

 

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...
On 9/30/2023 at 12:27 AM, havenphillip said:

I don't think there's an official tutorial or anything but I made a tutorial on how to convert from Shadertoy to Leadwerks. If you look around the community you can find things here and there.  Also I made a blog with a bunch of different shaders you can grab. Once you get the hang of them you should be able to figure them out.
 

...and the blog:

I made this tutorial but my shader ist just black

Fragment Shader

#version 400

out vec4 fragData0;

uniform vec2 buffersize;
uniform float currenttime;
uniform sampler2D texture0;


void main()
{
	vec2 q = gl_FragCoord.xy/buffersize.xy;
    vec2 p = -1.0+2.0*q;
	p.x *= gl_FragCoord.x/buffersize.y;

    vec3 col = vec3(0.0, 0.0, 0.0);
	
	// Rain
	vec2 st =  p * vec2(.5, .01)+vec2(currenttime*.3-q.y*.6*-0.0, currenttime*.3);
    float f = floor(mod(currenttime/9., 2.0));
	f = texture(texture0, st).y * texture(texture0, st*.773).x * 1.55;
	f = clamp(pow(abs(f), 23.0) * 13.0, 0.0, q.y*.14);
	col += f;
    fragData0=vec4(col, 1.0 );
}

Vertex Shader
 

#version 400

uniform mat4 projectionmatrix;
uniform mat4 drawmatrix;
uniform vec2 offset;
uniform vec2 position[4];

in vec3 vertex_position;

void main(void)
{
    gl_Position = projectionmatrix * (drawmatrix * vec4(position[gl_VertexID]+offset, 0.0, 1.0));
}

and here is the source Simple rain (shadertoy.com)

Link to comment
Share on other sites

Ok so you set the "col" to vec3(0,0,0) that's where the black is coming from. You're sort of just trying to add the effect over a black screen. The screencolor texture for post effect shaders is texture1. That gives you the standard screen.  Set it instead to:

 

uniform sampler2D texture1; //color

...

vec3 col = texture(texture1, q ).xyz;
 

  • Like 1
Link to comment
Share on other sites

 

1 hour ago, havenphillip said:

Ok so you set the "col" to vec3(0,0,0) that's where the black is coming from. You're sort of just trying to add the effect over a black screen. The screencolor texture for post effect shaders is texture1. That gives you the standard screen.  Set it instead to:

uniform sampler2D texture1; //color

...

vec3 col = texture(texture1, q ).xyz;
 

While this is true, it isn't needed in this case. The shader on shadertoy also just uses a black background, which might be what he currently wants. The blending with the actual background is not yet the problem.  The problem is, that the calculated value for 'f' is just to low to be recognized in the output. a multiplication by 2.0 or 4.0 will move the rain into a more visible spectrum. 

Another problem might be the pfx lua script. This would be nice to see as well to figure out if the noise texture is correctly bound.

  • Like 2
  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

1 minute ago, havenphillip said:

@klepto2 I'm quite certain you know more about this stuff so I defer to you. I just assumed. If it was me I'd try to find a rain shader that uses noise instead of going through a script with the noise buffer.

yeah, that is what most users do with shadertoy shaders. But gpu noise is far too expansive to be used in realtime scenarios (espacially when it is a game or so) It is always better to use a noise texture than to calculate the noise with the various available noise methods for glsl. 

1. Performance: noise calculation on the gpu is much (and i mean very much) slower than a simple texture lookup.

2. low end hardware: most noise mehtods have something like this: 

float hash( vec2 p ) {
	float h = dot(p,vec2(127.1,311.7));	
    return fract(sin(h)*43758.5453123);
}

but this will result in unexpected results on low end hardware (espacially integrated gfx)

  • Like 2
  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

Okey i miss the noise texture. So in this case i need to create the shader itself and a lua post effect wich loads the noise texture in the shader itself right? But how can i set the texture? I searched within the documentation but there is nothing to set a texture.

Link to comment
Share on other sites

  • Solution

 

Try this: i have added the diffuse layer, so the background is visible.

1. I changed the noise texture to the one used by shadertoy (using other textures might lead to artifacts)

2. I corrected the aspect ratio calculation you had p.x*= gl_FragCoord.x / buffersize.y; where is should be  p.x*= buffersize.x / buffersize.y;

Effect.zip

  • Like 1
  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

the noise texture was ok, the other noise texture i use is closer to the original.

The artifacts came from this line: 

p.x *= gl_FragCoord.x/buffersize.y;

this: should have been:

p.x *= buffersize.x/buffersize.y;

this line calculates the aspect ratio and if you use gl_FragCoord.x it is dependend on the pixel position which leads to the artifacts you have seen.

  • Like 2
  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

Ah, okay, this makes sense. I'm relatively new to shaders. It's a fascinating aspect of game development, so I'm trying to grasp what's happening rather than just copying and pasting. I believe that being able to create your own shaders is a significant skill that opens up a world of possibilities. Thanks all again for your help.

  • Like 1
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.

 Share

×
×
  • Create New...