SHARE
TWEET

Outline.hlsl

a guest Jul 6th, 2019 248 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float3 DecodeViewNormalStereo( float4 enc4 )
  2. {
  3.     float kScale = 1.7777;
  4.     float3 nn = enc4.xyz*float3(2*kScale,2*kScale,0) + float3(-kScale,-kScale,1);
  5.     float g = 2.0 / dot(nn.xyz,nn.xyz);
  6.     float3 n;
  7.     n.xy = g*nn.xy;
  8.     n.z = g-1;
  9.     return n;
  10. }
  11.  
  12. float3 DecodeNormal( float4 enc)
  13. {
  14.     return DecodeViewNormalStereo (enc);
  15. }
  16.  
  17. void Outline_float(float4 ScreenPos, float Scale, float DepthThreshold, float NormalThreshold, float2 Texel, Texture2D DepthNormalsTexture, Texture2D DepthTexture, SamplerState Sampler, out float Out)
  18. {
  19.     float halfScaleFloor = floor(Scale * 0.5);
  20.     float halfScaleCeil = ceil(Scale * 0.5);
  21.  
  22.     float2 bottomLeftUV = ScreenPos.xy - float2(Texel.x, Texel.y) * halfScaleFloor;
  23.     float2 topRightUV = ScreenPos.xy + float2(Texel.x, Texel.y) * halfScaleCeil;
  24.     float2 bottomRightUV = ScreenPos.xy + float2(Texel.x * halfScaleCeil, -Texel.y * halfScaleFloor);
  25.     float2 topLeftUV = ScreenPos.xy + float2(-Texel.x * halfScaleFloor, Texel.y * halfScaleCeil);
  26.  
  27.     // Depth from DepthTexture
  28.     float depth0 =SAMPLE_TEXTURE2D(DepthTexture, Sampler, bottomLeftUV).r;
  29.     float depth1 =SAMPLE_TEXTURE2D(DepthTexture, Sampler, topRightUV).r;
  30.     float depth2 =SAMPLE_TEXTURE2D(DepthTexture, Sampler, bottomRightUV).r;
  31.     float depth3 =SAMPLE_TEXTURE2D(DepthTexture, Sampler, bottomLeftUV).r;
  32.  
  33.     float depthFiniteDifference0 = depth1 - depth0;
  34.     float depthFiniteDifference1 = depth3 - depth2;
  35.  
  36.     float edgeDepth = sqrt(pow(depthFiniteDifference0, 2) + pow(depthFiniteDifference1, 2)) * 100;
  37.  
  38.     float newDepthThreshold = DepthThreshold * depth0;
  39.     edgeDepth = edgeDepth > newDepthThreshold ? 1 : 0;
  40.  
  41.  
  42.     // Normals extracted from DepthNormalsTexture
  43.     float3 normal0 = DecodeNormal(SAMPLE_TEXTURE2D(DepthNormalsTexture, Sampler, bottomLeftUV));
  44.     float3 normal1 = DecodeNormal(SAMPLE_TEXTURE2D(DepthNormalsTexture, Sampler, topRightUV));
  45.     float3 normal2 = DecodeNormal(SAMPLE_TEXTURE2D(DepthNormalsTexture, Sampler, bottomRightUV));
  46.     float3 normal3 = DecodeNormal(SAMPLE_TEXTURE2D(DepthNormalsTexture, Sampler, topLeftUV));
  47.  
  48.     float3 normalFiniteDifference0 = normal1 - normal0;
  49.     float3 normalFiniteDifference1 = normal3 - normal2;
  50.  
  51.     float edgeNormal = sqrt(dot(normalFiniteDifference0, normalFiniteDifference0) + dot(normalFiniteDifference1, normalFiniteDifference1));
  52.     edgeNormal = edgeNormal > NormalThreshold ? 1 : 0;
  53.    
  54.  
  55.     // Combined
  56.     float edge = max(edgeDepth, edgeNormal);
  57.  
  58.     Out = edge;
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top