|
Hi, I'd like to write a shader effect, where a transparent wall "glows" locally when a ball comes near it. By "glow" I mean simply make the transparent wall locally opaque. In my current implementation that wall doesn't glow correctly. In a script attached to the wall I pass the ball position to the wall-shader:
So far I've written this shader code:
Any idea what's wrong with my code? Isn't fragPos interpolated across the triangle?
(comments are locked)
|
|
Looks like your checking the distance in the pixel shader, after coordinates have already been transformed into eye / screen space. You need to convert the point into the wall's local co-ordinate system, and then do the distance calculation in the vertext shader instead. Convert the position of the ball into local space before sending to the shader:
And the shader changes: Of course, fragPos is no longer needed and should be removed from the v2f structure.
Apr 13 '10 at 02:23 AM
Daniel Brauer
Thanks a lot for your suggestion, which works pretty fine for well tesselated objects. But in my case I have a quite big wall consisting of only two triangles. If I calculate the distance in the vertex shader and the ball hits the wall in the middle, vertices are too far away so the wall doesn't get opaque at the hitpoint. That was the reason why I wanted to compute the distance in the fragment shader. I thought I had to get everything into eye coordinates...?
Apr 13 '10 at 07:20 AM
drhenry
BTW: converting the ball position into the local coordinate system of the wall did the trick for me (see Horsman's code above). I made mainly two mistakes in my code: 1) I thought I should pass global coordinates to the shader (thus I had the coordinates of the vertex and the ball in separate coordinates systems) 2) fragPos and ball position must be multiplied by the modelview matrix (not the model view PROJECTION matrix), if you do distance calculation in the fragment shader.
Apr 13 '10 at 08:25 AM
drhenry
(comments are locked)
|
|
Thanks to Horsman. I changed a bit of his code, so that it works fine even for objects of arbitrary triangulation:
Performance would be better, if the ball position in eye coordinates would not be computed in the vertex shader (=matrix multiplication for each vertex) but in a Unity script (=only one matrix multiplication). See http://forum.unity3d.com/viewtopic.php?t=49037 for an even better solution. The ModelView matrix multiplications in the vertex shader can be omitted, if _BallPos is in the local coordinate system of the wall. Then even ballPos in the v2f struct can be omitted.
May 05 '10 at 07:29 AM
drhenry
(comments are locked)
|
