0001: 
0002: //-----------------------------------------------------------------------------
0003: // Global variables
0004: //-----------------------------------------------------------------------------
0005: float4 MaterialAmbientColor = {0.3, 0.3, 0.3, 1.0};
0006: float4 MaterialDiffuseColor = {0.7, 0.7, 0.7, 0.0};
0007: 
0008: #define LIGHT_DIR  0.0,10.0,0.0
0009: float3 LightDir = normalize(float3(LIGHT_DIR));
0010: float4 LightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient
0011: float4 LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
0012: 
0013: float4x4 mWorld;
0014: float4x4 mWorldViewProjection;
0015: 
0016: float fCurvature = 1.0;
0017: 
0018: 
0019: 
0020: //-----------------------------------------------------------------------------
0021: // Texture samplers
0022: //-----------------------------------------------------------------------------
0023: texture RenderTargetTexture;
0024: sampler RenderTargetSampler = 
0025: sampler_state
0026: {
0027:     Texture = <RenderTargetTexture>;
0028:     MinFilter = LINEAR;
0029:     MagFilter = LINEAR;
0030: 
0031:     AddressU = Clamp;
0032:     AddressV = Clamp;
0033: };
0034: 
0035: texture MeshTexture;
0036: sampler MeshTextureSampler = 
0037: sampler_state
0038: {
0039:     Texture = <MeshTexture>;
0040:     MinFilter = LINEAR;
0041:     MagFilter = LINEAR;
0042:     MipFilter = NONE;
0043: 
0044:     AddressU = Clamp;
0045:     AddressV = Clamp;
0046: };
0047: 
0048: 
0049: 
0050: 
0051: 
0052: 
0053: //-----------------------------------------------------------------------------
0054: //-----------------------------------------------------------------------------
0055: float3 GetDiffuse(float3 normalW )
0056: {
0057:     return MaterialDiffuseColor * LightDiffuse * max(0,dot(normalW, LightDir))
0058:            + MaterialAmbientColor * LightAmbient;
0059: }
0060: 
0061: 
0062: //-----------------------------------------------------------------------------
0063: // Vertex shader output structure
0064: //-----------------------------------------------------------------------------
0065: struct VS_OUTPUT
0066: {
0067:     float4 Position : POSITION;
0068:     float2 TextureUV : TEXCOORD0;
0069:     float4 Diffuse : TEXCOORD1;
0070: };
0071: 
0072: 
0073: //-----------------------------------------------------------------------------
0074: // Name: SceneVertexShader
0075: // Type: Vertex shader                                      
0076: // Desc: In addition to standard transform and lighting, it calculates the blur
0077: //       factor of the vertex and outputs this as a texture coord.
0078: //-----------------------------------------------------------------------------
0079: VS_OUTPUT SceneVertexShader( float4 vPos : POSITION, 
0080:                              float3 vNormal : NORMAL,
0081:                              float2 vTexCoord0 : TEXCOORD0 )
0082: {
0083:     VS_OUTPUT Output;
0084:     float3 vViewPosition;
0085:     float3 vWorldNormal;
0086:     float  fBlurFactor;
0087:   
0088:     // tranform vertex position into screen space
0089:     Output.Position = mul(vPos, mWorldViewProjection);
0090:     
0091:     // Compute simple lighting equation
0092:     vWorldNormal = mul(vNormal, (float3x3)mWorld);       
0093:     Output.Diffuse = float4(GetDiffuse(vWorldNormal), 1.0);
0094:     
0095:     // Just copy the texture coordinate through
0096:     Output.TextureUV = vTexCoord0;
0097:     
0098:     return Output;    
0099: }
0100: 
0101: 
0102: //-----------------------------------------------------------------------------
0103: // Name: ScenePixelShader
0104: // Type: Pixel shader
0105: // Desc: This shader simply outputs the pixel's color 
0106: //-----------------------------------------------------------------------------
0107: float4 ScenePixelShader( VS_OUTPUT In ) : COLOR
0108: {
0109:     return tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
0110: }
0111: 
0112: 
0113: //-----------------------------------------------------------------------------
0114: // Name: TechScene
0115: // Type: Technique                                     
0116: // Desc: Renders the scene's color to the render target and stores the
0117: //       depth information as a blur factor in the alpha channel.
0118: //-----------------------------------------------------------------------------
0119: technique TechScene
0120: {
0121:     pass P0
0122:     {        
0123:         VertexShader = compile vs_1_1 SceneVertexShader();
0124:         PixelShader  = compile ps_2_0 ScenePixelShader();
0125:     }
0126: }
0127: 
0128: 
0129: 
0130: float4 FishEyePS( in float2 OriginalUV : TEXCOORD0 ) : COLOR 
0131: {
0132:     half2 pos = OriginalUV * 2.0 - 1.0;
0133:     half p = length(pos);
0134:     pos = (1 + fCurvature * p * p)/(1+2*fCurvature) * pos;
0135: 
0136:     return tex2D(RenderTargetSampler, pos * 0.5 + 0.5);
0137: }
0138: 
0139: 
0140: //-----------------------------------------------------------------------------
0141: // Name: TechFishEye
0142: // Type: Technique
0143: //-----------------------------------------------------------------------------
0144: technique TechFishEye
0145: {
0146:     pass P0
0147:     {        
0148:         PixelShader = compile ps_2_0 FishEyePS();
0149:         
0150:         ZEnable = false;
0151:         AlphaBlendEnable = false;
0152:     }
0153: }
0154: 
0155: