0

So I'm working on a WebGL code in JS that draws approximation of a given function using gl_POINTS, and I'm trying to make those points' colors blend with background color the further from camera they get. So whenever user moves or rotates camera, colors change. I thought of using Blending function, but supposedly I'm supposed to implement that feature mostly in fragment shader.

All I figured out so far is that fragment shader has vec2 gl_FragCoord built-in variable , but that doesn't seem to help much since it's only position on screen, doesn't really keep any z coordinate I could use. Am I missing something?

genpfault
  • 51,148
  • 11
  • 85
  • 139
toxing
  • 47
  • 7
  • gl_FragCoord does keep a z value. Whether that's useful or not depends on what you're trying to do. If you want help you probably need to post a [minimal complete verifiable example](https://meta.stackoverflow.com/questions/349789/how-do-i-create-a-minimal-complete-verifiable-example) in your question. [Here's an example of setting alpha based on Z in the vertex shader](https://www.vertexshaderart.com/art/tkkTQCvYJZZ7rzpLo). Of course you can pass Z to the fragment shader by varying to do things in the fragment shader or use `gl_FragCoord.z` – gman Jun 11 '18 at 02:40
  • yep you can modulate alpha channel of output color by distance in fragment shader and set up blending function to use alpha. [Just be sure your context has alpha channel bits](https://stackoverflow.com/a/50248477/2521214) ... – Spektre Jun 11 '18 at 07:58
  • What you will want to do is pass some value from the vertex shader into the fragment shader. Assuming you have the camera position in the vertex shader, you can compute the distance from the camera the point is. Then you can use this value such that the further the value is from the camera, the lower the opacity. As the other said make sure you set your blending function so that the alpha value affects the opacity. – Luple Jun 12 '18 at 22:26

0 Answers0