- Home /
Getting the world position of the pixel in the fragment shader.
Hello guys !
I'm struggling with a custom shader :
Shader "Custom/HaloEffect" {
Properties{
_Position("Position", Vector) = (.0, .0, .0)
_HaloColor("Halo Color", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader {
Tags { "Queue"="Transparent" "Render"="Transparent" "IgnoreProjector"="True"}
LOD 200
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 vertex : SV_POSITION;
float4 color : COLOR0;
};
uniform float3 _Position;
uniform float4 _HaloColor;
v2f vert(appdata v) {
v2f o;
o.color = _HaloColor;
o.color.a = 1 - distance(mul(unity_ObjectToWorld, v.vertex), _Position) ;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target {
return i.color;
}
ENDCG
}
}
FallBack "Diffuse"
}
The desired behaviour would have been to smootly set the alpha around the world coordinates named "_Position" on the mesh that would execute this shader. In this version the results are pretty near of what I expected. But the distances calculus are operated only on the vertices and the final interpolation is weird.
As you can see here the _Position is between 4 vertices of the plane:
And here is on one vertex:
I think It does the right work regarding what I wrote. But, what I wished was a kind of a smooth circle.
I think I can managed to obtain this by doing the distance calculus in the Fragment Shader.
But I dont know how to get the world coordinates of the current pixel.
I try to compute an inverse of the MVP Matrix applied to the SV_POSITION, but it turns really bad.
Is the SV_POSITION is the same that gl_FragCoord ?
If I'am totally on the wrong way I wish you guys would tell me.
Please help me find a solution.
Answer by Namey5 · Nov 16, 2016 at 10:56 PM
Shader "Custom/HaloEffect" {
Properties{
_Position("Position", Vector) = (.0, .0, .0)
_HaloColor("Halo Color", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader {
Tags { "Queue"="Transparent" "Render"="Transparent" "IgnoreProjector"="True"}
LOD 200
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD0;
};
uniform float3 _Position;
uniform float4 _HaloColor;
v2f vert(appdata v) {
v2f o;
o.worldPos = mul (unity_ObjectToWorld, v.vertex);
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 col = _HaloColor;
col.a = 1 - distance(i.worldPos), _Position) ;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
Does it not work to just move the calculations into the fragment shader?
Thanks !
It works Like a charm In my attemps to pass a position from the vertex shader to the fragment shader I used
the semantics POSITION, unfortunately that wasn't the right way to do it.
Thanks to you know I have a better understanding of shaders, I know what TEXCOORD0 is.
And it make perfecty sense.
Your answer
Follow this Question
Related Questions
Problem with alpha coloured shader 1 Answer
custom Standard Shader alpha issues. 1 Answer
How do you make an X-ray vision following the mouse? 1 Answer
Is there a way to set an alpha color? 2 Answers
Multiple Cars not working 1 Answer