🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Need ideas for an GLSL Shader

Started by
5 comments, last by trsh 4 years, 6 months ago

I need to figure out a Shader (vert, frag) that as further away from edges creates more white color. So it it's like a IN gradient. Shapes and angles can be different, even unregular forms. Any ideas?

Advertisement

Ah and in one pass I must do this :/

Are these 3D shapes? Those squares are two triangles? What then defines an edge, a change in normal from its neighbouring triangle?

One way is if the two triangles that make the square have their own vertices. If each triangle has its own vertices and they're not indexed, then you can pass the vertice position (that is twice, once interpolated into SV_POSITION or whatever GLSL call it and then again uninterpolated) and compare distance from edge in the fragment shader (point distance from line). If the triangles are indexed then you can pass the uninterpolated vertice positions from the the domain shader or whatever GLSL call it. I do that kind of stuff a lot in geometry shader for debugging (doing things like wireframe over the solid mesh).

Here's some terrain rendered a simpler way but less precise at corners because of interpolation (it might be good enough for what you want):

Each vertice gets it own value, like this:

Simply render the colour based on the lowest value. The above screen shot is literally just:

	float lowest = input.vA;
	if (input.vB < lowest)
		lowest = input.vB;
	if (input.vC < lowest)
		lowest = input.vC;

	lowest = saturate(lowest * 6);
	color = float4(lowest, lowest, lowest, 1);

Forgive the HLSL I don't know GLSL off the top of my head.

The right tool for this is to use signed distance fields (SDF). For each binary shape, you would precompute an image that encodes the distance to the nearest surface boundary, and use that image as input to your pixel shader. Points inside the shape would have a negative distance (encoded as value less than 0.5), and points outside would be positive (>0.5). With this information you can render smooth outlines that behave similar to vector graphics (i.e. no pixelation when zoomed), as well as a number of special effects (shadow, outline, inflation/contraction). The approach was first popularized by Valve in their 2007 paper.

Basically, your shader would sample this distance field texture, convert the values to the right range, then apply some logic:
* If distance >0, then set output alpha to 0.
* if distance near 0, then use smoothstep() function to do antialiasing of the alpha.
* If distance <0, use alpha = 1 and linearly interpolate between the border and inside colors based on distance.

Thank you guys for your directions :)

Sadly I just realized I need that for iregular and 3d forms. So sound like mission impossible

This topic is closed to new replies.

Advertisement