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