- Home /
Unity 3D fragment shader - distance from pixel to vertex?
I'm trying to create a star field fragment shader in which the vertices of a mesh are treated as stars. I want the color of the pixel to go between 0,0,0,1 and 1,1,1,1 as the distance from the pixel (interpolated from the position output of the vertex shader) to the vertex (output from the vertex shader in some way that will prevent it from getting interpolated) goes between some value to 0. I want the value to be calculated based on the z value of the vertex.
Is this possible? How can it be done?
I'm having trouble coming up with the distance from the interpolated vertex position to the non-interpolated one in the fragment shader. Maybe it could be done by somehow getting the screen coordinate of the vertex in the vertex shader, and passing that as a color value or something? Because I seem to be able to access the screen coordinates for a fragment, but not the world coordinates. (If I try to use anything that has position semantics in the fragment shader, I get an error, as I guess you'd expect.)
Additionally, I'd really like to also pass a (star) color value and size value along with a vertex, from the vertex shader, and have those affect the output of the fragment shader.
Your answer
Follow this Question
Related Questions
Need a Unique Shader--Transparent + Translucent? + Rim Shading, Glow, Self-Illum? 0 Answers
Dynamically animating mesh according to heightmap image sequence 1 Answer
how can i catch the sky box and do a reflection in object 0 Answers
Alpha Transparency Shader Issue 1 Answer
CG - Using Matrix as texcoord 1 Answer