//**************************************************************************/ // Copyright 2014 Autodesk, Inc. // All rights reserved. // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. //**************************************************************************/ // World-view-projection transformation. uniform mat4 gWVPXf : WorldViewProjection; // Target size. uniform vec2 gTargetSize : ViewportPixelSize; // The single filter input, i.e. the image to be filtered. uniform texture2D gInputTex : InputTexture; // Filter input sampler. uniform sampler2D gInputSamp = sampler_state { Texture = ; //MinFilter = Point; //MagFilter = Point; //MipFilter = Point; }; // The edge thickness. uniform float gThickness = 1.5f; // The edge detection threshold. uniform float gThreshold = 0.2; // Vertex shader input structure. attribute VS_INPUT { vec4 Pos : POSITION; vec2 UV : TEXCOORD0; }; // Vertex shader output structure. attribute VS_TO_PS { vec2 VSUV : TEXCOORD0; }; // Vertex shader. GLSLShader VS_FilterEdgeDetect { void main() { gl_Position = gWVPXf*Pos; // Pass the texture coordinates unchanged. VSUV = UV; } } // Pixel shader output structure. attribute pixelOut { vec4 colorOut: COLOR0; } // Pixel shader. GLSLShader PS_FilterEdgeDetect { // Gets the grayscale value of a color, i.e. the average of the RGB components. float GetGray(vec4 c) { return dot(c.xyz, vec3(0.33333, 0.33333, 0.33333)); } void main() { vec2 ox = vec2(gThickness/gTargetSize.x,0.0); vec2 oy = vec2(0.0,gThickness/gTargetSize.y); vec2 uv = VSUV; vec2 PP = uv - oy; vec4 CC = texture2D(gInputSamp,PP-ox); float g00 = GetGray(CC); CC = texture2D(gInputSamp,PP); float g01 = GetGray(CC); CC = texture2D(gInputSamp,PP+ox); float g02 = GetGray(CC); PP = uv; CC = texture2D(gInputSamp,PP-ox); float g10 = GetGray(CC); CC = texture2D(gInputSamp,PP); float g11 = GetGray(CC); CC = texture2D(gInputSamp,PP+ox); float g12 = GetGray(CC); PP = uv + oy; CC = texture2D(gInputSamp,PP-ox); float g20 = GetGray(CC); CC = texture2D(gInputSamp,PP); float g21 = GetGray(CC); CC = texture2D(gInputSamp,PP+ox); float g22 = GetGray(CC); float K00 = -1; float K01 = -2; float K02 = -1; float K10 = 0; float K11 = 0; float K12 = 0; float K20 = 1; float K21 = 2; float K22 = 1; float sx = 0; float sy = 0; sx += g00 * K00; sx += g01 * K01; sx += g02 * K02; sx += g10 * K10; sx += g11 * K11; sx += g12 * K12; sx += g20 * K20; sx += g21 * K21; sx += g22 * K22; sy += g00 * K00; sy += g01 * K10; sy += g02 * K20; sy += g10 * K01; sy += g11 * K11; sy += g12 * K21; sy += g20 * K02; sy += g21 * K12; sy += g22 * K22; float dist = sqrt(sx*sx+sy*sy); if (dist > gThreshold) { colorOut = vec4(0,0,0,1); } else { colorOut = texture2D(gInputSamp, uv); } } } // The main technique. technique Main { pass p0 { VertexShader (in VS_INPUT, out VS_TO_PS) = VS_FilterEdgeDetect; PixelShader (in VS_TO_PS, out pixelOut) = PS_FilterEdgeDetect; } }