hello, i am a newbee to unity, i am now working with edge detection problem.
I find that using the difference(ddx, ddy) of face nomal(object space) can easily generate a plausible result. And the object space normal is rotation invariant.
object-space face normal
But when i use ddx/ddy to generate difference of face normal, it always return zero value, why would this happen?
Here is the code snippet of my shader, i generate a face normal in geometry shader.
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float2 uvLM : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float2 uv : TEXCOORD0;
float2 uvLM : TEXCOORD1;
float4 positionWSAndFogFactor : TEXCOORD2;
half3 normalWS : TEXCOORD3;
#if _NORMALMAP
half3 tangentWS : TEXCOORD4;
half3 bitangentWS : TEXCOORD5;
#endif
#ifdef _MAIN_LIGHT_SHADOWS
float4 shadowCoord : TEXCOORD6;
#endif
float3 normalOS : TEXCOORD7;
float4 positionCS : SV_POSITION;
};
Varyings vert(Attributes input)
{
Varyings output;
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS.xyz, input.tangentOS);
float fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
// output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
output.uv = input.uv;
output.uvLM = input.uvLM.xy * unity_LightmapST.xy + unity_LightmapST.zw;
output.positionWSAndFogFactor = float4(vertexInput.positionWS, fogFactor);
output.normalWS = vertexNormalInput.normalWS;
#ifdef _NORMALMAP
output.tangentWS = vertexNormalInput.tangentWS;
output.bitangentWS = vertexNormalInput.bitangentWS;
#endif
#ifdef _MAIN_LIGHT_SHADOWS
output.shadowCoord = GetShadowCoord(vertexInput);
#endif
output.positionCS = vertexInput.positionCS;
// store the object space position
output.normalOS = input.positionOS.xyz;
return output;
}
[maxvertexcount(3)]
void geom(triangle Varyings input[3], inout TriangleStream<Varyings> outputStream)
{
Varyings output = (Varyings)0;
Varyings top[3];
float3 dir[3];
[unroll(3)]
for (int k = 0; k < 3; ++k)
{
dir[k] = input[(k + 1) % 3].normalOS.xyz - input[k].normalOS.xyz;
}
// generate a face normal
float3 faceNorm = normalize(cross(dir[0], -dir[2]));
[unroll(3)]
for (int i = 0; i < 3; ++i)
{
top _= input*;*_
<em>_top*.normalOS = faceNorm;*_</em>
<em><em>_outputStream.Append(top*);*_</em></em>
<em><em>_*}*_</em></em>
<em><em>_*outputStream.RestartStrip();*_</em></em>
<em><em>_*}*_</em></em>
<em><em><em>*half4 frag(Varyings input) : SV_Target*</em></em></em>
<em><em>_*{*_</em></em>
<em><em>_*// draw out line*_</em></em>
<em><em>_*float3 diff = fwidth(input.normalOS.xyz);*_</em></em>
<em><em>_*float intensity = min(diff.x + diff.y + diff.z, 1.0);*_</em></em>
<em><em>_*intensity = 1.0 - intensity;*_</em></em>
<em><em>_*return half4(intensity, intensity, intensity, 1.0);*_</em></em>
<em><em>_*}*_</em></em>
<em><em>_*ENDHLSL*_</em></em>
<em><em>_*```*_</em></em>
<em><em>_*![8454488--1121864--blank-white.PNG|547x547](upload://fDQjB9gbYONH68sE74O495p0Xnn.png)*_</em></em>
<em><em>_*result of my shader.*_</em></em>