40 votes
Accepted

How to do color post processing in WebGL, when you can not read the color of the current pixel?

This is not a WebGL 2 restriction. This is fundamentally how GPUs and their 3D rendering APIs all work. Because they're designed to render many triangles and fragments in parallel, they structurally ...
DMGregory's user avatar
  • 130k
29 votes
Accepted

How do I write a shader that lights up when objects are near a surface?

A general outline: Create a depth map of your scene without the shield. You can get this effectively for free, since transparent objects are often rendered in a later pass anyway. Otherwise, you can ...
Yousef Amar's user avatar
12 votes
Accepted

How to make a 2D neon-like trail effect in Unity

To achieve this effect my friend has created a custom sprite, we played around with all sorts of sprites and you can customize this part to your liking. The shader I've written for this effect, takes ...
John Hamilton's user avatar
7 votes
Accepted

How to pass a big (60+) amount of variables to an HLSL pixel shader?

Use several constant buffers and group variables together based on how often they change. If your variables are fairly static ( or just huge ) you may be better off converting values into a texture ...
cfehunter's user avatar
  • 246
7 votes
Accepted

How do I calculate UV space from world space in the fragment shader?

Do I understand correctly that you want an effect somewhat like this? (If I'm way off, you may need to edit your question to include more description of what you're trying to do and why) Here I have ...
DMGregory's user avatar
  • 130k
6 votes
Accepted

Pixelation shader explanation?

This is a pixelation shader, it seems. What it does is, it divides the screen into tiny rectangles, the side of each rectangle is determined by the values dx and dy. The smaller the value of ...
The Light Spark's user avatar
5 votes

How do I write a shader that lights up when objects are near a surface?

It's just using the depth map. It renders the world then renders the shield and takes a difference between the shield's rendered z value and the depth buffer z value to tint the pixel more white.
Sirisian's user avatar
  • 584
5 votes

How do I wrap textures inside shader GLSL?

For people who come here from Google (like me), the shader version is: fract (tex_coord.st) this will loop coordinates in range 0.0 - 1.0
PiotrK's user avatar
  • 367
5 votes
Accepted

Does it make sense to do more calculations in the fragment shader if there are more vertices than pixels?

The calculation falls apart like so: If every one of those 3 million polygons rasterizes to at least one pixel on average, then doing the operation in the fragment shader still results in it being ...
DMGregory's user avatar
  • 130k
4 votes

How do I wrap textures inside shader GLSL?

You don't set these values in the shader. You set them in your program, generally when first creating the texture (though it can be set or changed whenever). If you've already uploaded your texture ...
Fibbs's user avatar
  • 504
4 votes
Accepted

Is there a way to get what pixel is being processed within the fragment shader?

There is, in the fragment shader, a variable called gl_FragCoord. This contains the coordinates of the currently executing fragment shader. Note that (by default) a pixel at pixel coordinates (x,y) ...
Elva's user avatar
  • 3,203
4 votes
Accepted

Super formula on shader

Instead of atan(sf.y/sf.x), use atan(sf.y,sf.x). This works because it takes the sign into consideration. Dividing the two ...
vero's user avatar
  • 155
4 votes
Accepted

GLSL: Issue replacing ternary operator with mix

The main issue is that (ONE3-b)/a may cause a division by zero. This causes the result to be NaN (Not A Number). In that case mix will also return NaN because ...
Stephane Hockenhull's user avatar
4 votes
Accepted

HLSL texture sampler always returns white

The problem is this: the compiler optimizes the effect file. So in your code only ONE sampler is created. However in reality you are using TWO textures. The first one, being the Texture provided as ...
Felsir's user avatar
  • 4,037
4 votes
Accepted

How to make far block graphically better?

What you see is there because the textures are in a higher resolution than what they get drawn in. This results in a very pixelated look. To "fix" this you can generate mipmaps. Mipmaps are the ...
Bálint's user avatar
  • 14.8k
4 votes
Accepted

Depth Intersection Shader

This issue exists with the original shader as well, but is less apparent with the sphere mesh. Notice how it looks alright when the model's vertices are close to the intersecting object. But as they ...
jhoffman0x's user avatar
4 votes
Accepted

Is linear filtering possible on depth textures in OpenGL?

Shadow maps, the way you're using them, don't filter well. The linear filtering does what it's supposed to, it's just not the thing you actually want for this context. Take a sample point halfway ...
DMGregory's user avatar
  • 130k
3 votes
Accepted

Fragment shader, tint color problem

The problem was there gl_FragColor = (1.0, 0 , 0 ,1.0);//vec4(texVal.rgb, texVal.a)*(1.0, 0 , 0 ,1.0); it miss the vec4 name ...
D4rWiNS's user avatar
  • 293
3 votes

Shadertoy getting help moving to glsl

You have two options: Use gl_FragCoord and pass a uniform variable for iResolution. Everything else will work exactly like in ...
Wumpf's user avatar
  • 451
3 votes
Accepted

Stencil buffer VS conditional discard in fragment shader

Modern hardware performs early stencil testing using the same sort of tile-based approach as early depth. It can reject large groups of fragments before they make it to the fragment shader stage. That ...
Andon M. Coleman's user avatar
3 votes
Accepted

Procedural Hatching

Yes, in fact I'm not sure of any other way you'd do it other than in the fragment shader. Here's some examples I found: This ShaderToy uses a noise texture and another function to generate the ...
Cobertos's user avatar
  • 1,634
3 votes

GLSL - skewed parallax occlusion mapping

normalMapping should use TBN, Parallax Mapping should use transpose(TBN), and viewDirection should mul transpose(TBN).
玩雪明's user avatar
3 votes

GLSL Fragment Shader compiles fine on certain computers

Remove the 'f' on your float constants. It's not part of the GLSL language. Source: GLSL Specification If you do not put a #version NNN pragma at the top of your ...
Stephane Hockenhull's user avatar
3 votes
Accepted

How can I create an outline shader for a plane?

If all we need is an outline around a rectangle, we can compute this analytically with the math of signed distance fields. :) ...
DMGregory's user avatar
  • 130k
3 votes
Accepted

How to implement multicolored flashing sprite effect like in Contra and the Messenger?

I think you could achieve this effect by using a rotating color palette. You can do this in GLSL by passing in a grayscale image and a 1D texture containing the color palette. You'd sample the ...
user1118321's user avatar
  • 2,622
3 votes
Accepted

My Single Pass Gaussian Blur Looks Awful

Your sampling points are most likely messed up because you are not taking into account that texture texel size is in range [0; 1]. You need to divide offset coordinates by texture resolution. We can ...
Ocelot's user avatar
  • 1,424
3 votes
Accepted

Shader vs Working with Pixels

There are a few benefits when using a shader: Speed The GPU is faster in manipulating pixels. Ofcourse you can 'preprocess' the texture and that will be fine. It is under the assumption that you ...
Felsir's user avatar
  • 4,037
3 votes
Accepted

How to decorate the floor with a grid?

One way to do a square pattern in glsl is with mod: ...
Theraot's user avatar
  • 24.4k
3 votes
Accepted

Unity mesh shader for outlining colour changes

What you're finding here is that drawing the line based on your height above/below the boundary gives inconsistent results: in places where the height changes quickly (steep slopes), the outline ends ...
DMGregory's user avatar
  • 130k

Only top scored, non community-wiki answers of a minimum length are eligible