1#import "Common/ShaderLib/MultiSample.glsllib" 2 3uniform COLORTEXTURE m_Texture; 4uniform DEPTHTEXTURE m_DepthTexture; 5 6uniform sampler2D m_NormalsTexture; 7uniform vec2 g_Resolution; 8 9uniform vec4 m_EdgeColor; 10 11uniform float m_EdgeWidth; 12uniform float m_EdgeIntensity; 13 14uniform float m_NormalThreshold; 15uniform float m_DepthThreshold; 16 17uniform float m_NormalSensitivity; 18uniform float m_DepthSensitivity; 19 20in vec2 texCoord; 21out vec4 outFragColor; 22 23vec4 fetchNormalDepth(vec2 tc){ 24 vec4 nd; 25 nd.xyz = texture2D(m_NormalsTexture, tc).rgb; 26 nd.w = fetchTextureSample(m_DepthTexture, tc,0).r; 27 return nd; 28} 29 30void main(){ 31 vec3 color = getColor(m_Texture, texCoord).rgb; 32 33 vec2 edgeOffset = vec2(m_EdgeWidth) / textureSize(m_NormalsTexture, 0); 34 vec4 n1 = fetchNormalDepth(texCoord + vec2(-1.0, -1.0) * edgeOffset); 35 vec4 n2 = fetchNormalDepth(texCoord + vec2( 1.0, 1.0) * edgeOffset); 36 vec4 n3 = fetchNormalDepth(texCoord + vec2(-1.0, 1.0) * edgeOffset); 37 vec4 n4 = fetchNormalDepth(texCoord + vec2( 1.0, -1.0) * edgeOffset); 38 39 // Work out how much the normal and depth values are changing. 40 vec4 diagonalDelta = abs(n1 - n2) + abs(n3 - n4); 41 42 float normalDelta = dot(diagonalDelta.xyz, vec3(1.0)); 43 float depthDelta = diagonalDelta.w; 44 45 // Filter out very small changes, in order to produce nice clean results. 46 normalDelta = clamp((normalDelta - m_NormalThreshold) * m_NormalSensitivity, 0.0, 1.0); 47 depthDelta = clamp((depthDelta - m_DepthThreshold) * m_DepthSensitivity, 0.0, 1.0); 48 49 // Does this pixel lie on an edge? 50 float edgeAmount = clamp(normalDelta + depthDelta, 0.0, 1.0) * m_EdgeIntensity; 51 52 // Apply the edge detection result to the main scene color. 53 //color *= (1.0 - edgeAmount); 54 color = mix (color,m_EdgeColor.rgb,edgeAmount); 55 56 outFragColor = vec4(color, 1.0); 57} 58