   [XNA] edge detection problem
  I have a toon + edge detection shader in my game project. I used the example from this website for that.But for some reason the edge detection doesn't always render the edge properly. I was hoping anyone here had an idea of what the problem could be.Take a look at the following screenshots. You can clearly see that the right image renders WITH edge detection, while the left image does it without. I all depends on where the camera is rotated around the character. I just have no idea why it sometimes doesn't render the edge and sometimes it does.Screenshot:http://i44.tinypic.com/11c9or4.png   
  depens on what project u moved from my guess<br><br>for more info it would better if u past shader code and &#111;n what api it worked   ;Quote:Original post by e3d_ALiVEdepens on what project u moved from my guess<br><br>for more info it would better if u past shader code and &#111;n what api it worked<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>I'm working with XNA 3.1. Here's the shader code i'm using for th edge detection:<br><br>I've already tried to play with the Threshold settings, it does optimize it a bit, but that &#111;nly gives me a bery thin, light edge around my character and &#111;n other areas a normal thick &#111;ne.<br><br>BTW, not sure what you mean with 'texel mapping'. So i can't tell if that's the problem.<br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br>float4x4 World;<br>float4x4 View;<br>float4x4 Projection;<br><br><span class="cpp-keyword">float</span> EdgeWidth = <span class="cpp-number">0</span>.8f;<br><span class="cpp-keyword">float</span> EdgeIntensity = <span class="cpp-number">1</span>;<br><br><span class="cpp-comment">// How sensitive should the edge detection be to tiny variations in the input data?</span><br><span class="cpp-comment">// Smaller settings will make it pick up more subtle edges, while larger values get</span><br><span class="cpp-comment">// rid of unwanted noise.</span><br><span class="cpp-keyword">float</span> NormalThreshold = <span class="cpp-number">0</span>.<span class="cpp-number">5</span>;<br><span class="cpp-keyword">float</span> DepthThreshold = <span class="cpp-number">0</span>.<span class="cpp-number">1</span>;<br><br><span class="cpp-comment">// How dark should the edges get in response to changes in the input data?</span><br><span class="cpp-keyword">float</span> NormalSensitivity = <span class="cpp-number">1</span>;<br><span class="cpp-keyword">float</span> DepthSensitivity = <span class="cpp-number">10</span>;<br><br><span class="cpp-comment">// Pass in the current screen resolution.</span><br>float2 ScreenResolution;<br><br>texture SceneTexture;<br><br><span class="cpp-comment">//—————————-</span><br><span class="cpp-comment">// Structures</span><br><span class="cpp-comment">//—————————-</span><br><br>sampler SceneSampler : <span class="cpp-keyword">register</span>(s0) = sampler_state<br>{<br>    Texture = (SceneTexture);<br>    <br>    MinFilter = Linear;<br>    MagFilter = Linear;<br>    <br>    AddressU = Clamp;<br>    AddressV = Clamp;<br>};<br><br><br><span class="cpp-comment">// This texture contains normals (in the color channels) and depth (in alpha)</span><br><span class="cpp-comment">// for the main scene image. Differences in the normal and depth data are used</span><br><span class="cpp-comment">// to detect where the edges of the model are.</span><br>texture NormalDepthTexture;<br><br>sampler NormalDepthSampler : <span class="cpp-keyword">register</span>(s1) = sampler_state<br>{<br>    Texture = (NormalDepthTexture);<br>    <br>    MinFilter = Linear;<br>    MagFilter = Linear;<br>    <br>    AddressU = Clamp;<br>    AddressV = Clamp;<br>};<br><br><span class="cpp-comment">//—————————-</span><br><span class="cpp-comment">// Pixel Shader</span><br><span class="cpp-comment">//—————————-</span><br><br>float4 PixelShader(float2 texCoord : TEXCOORD0) : COLOR0<br>{<br>    <span class="cpp-comment">// Look up the original color from the main scene.</span><br>    float3 scene = tex2D(SceneSampler, texCoord);<br>    <br>    <span class="cpp-comment">// Apply the edge detection filter?</span><br>    <span class="cpp-comment">// Look up four values from the normal/depth texture, offset along the</span><br>    <span class="cpp-comment">// four diagonals from the pixel we are currently shading.</span><br>    float2 edgeOffset = EdgeWidth / ScreenResolution;<br>    <br>    float4 n1 = tex2D(NormalDepthSampler, texCoord + float2(-<span class="cpp-number">1</span>, -<span class="cpp-number">1</span>) * edgeOffset);<br>    float4 n2 = tex2D(NormalDepthSampler, texCoord + float2( <span class="cpp-number">1</span>,  <span class="cpp-number">1</span>) * edgeOffset);<br>    float4 n3 = tex2D(NormalDepthSampler, texCoord + float2(-<span class="cpp-number">1</span>,  <span class="cpp-number">1</span>) * edgeOffset);<br>    float4 n4 = tex2D(NormalDepthSampler, texCoord + float2( <span class="cpp-number">1</span>, -<span class="cpp-number">1</span>) * edgeOffset);<br><br>    <span class="cpp-comment">// Work out how much the normal and depth values are changing.</span><br>    float4 diagonalDelta = abs(n1 - n2) + abs(n3 - n4);<br><br>    <span class="cpp-keyword">float</span> normalDelta = dot(diagonalDelta.xyz, <span class="cpp-number">1</span>);<br>    <span class="cpp-keyword">float</span> depthDelta = diagonalDelta.w;<br>    <br>    <span class="cpp-comment">// Filter out very small changes, in order to produce nice clean results.</span><br>    normalDelta = saturate((normalDelta - NormalThreshold) * NormalSensitivity);<br>    depthDelta = saturate((depthDelta - DepthThreshold) * DepthSensitivity);<br><br>    <span class="cpp-comment">// Does this pixel lie on an edge?</span><br>    <span class="cpp-keyword">float</span> edgeAmount = saturate(normalDelta + depthDelta) * EdgeIntensity;<br>    <br>    <span class="cpp-comment">// Apply the edge detection result to the main scene color.</span><br>    scene *= (<span class="cpp-number">1</span> - edgeAmount);<br><br>    <span class="cpp-keyword">return</span> float4(scene, <span class="cpp-number">1</span>);<br>}<br><br><span class="cpp-comment">//—————————-</span><br><span class="cpp-comment">// Technique</span><br><span class="cpp-comment">//—————————-</span><br><br>technique EdgeDetect<br>{<br>    pass P0<br>    {<br>   PixelShader = compile ps_2_0 PixelShader();<br>    }<br>}<br><br></pre></div><!–ENDSCRIPT–> ;If it works best for camera positions close to the starting camera position, it might help to check if the NormalDepthTexture is being updated as the camera moves. It's a bit of a wild guess, but I think the information in this texture is only 'valid' for the camera position it has been rendered from.;  How are you storing depth in your normal + depth texture?   ;  A cross (4 or 5 fetches) or sobel filter (9 fetches) against depth information can do it all with just depth.  This will get you the exterior silhouettes fairly accurately.Edges on convex junctions (say the corner of a 90 degree wall) need another approach. A similar filter pattern, but using dot products against the normals if available, or comparing material ids for equality if you have those available.   