port from perforce
This commit is contained in:
BIN
aiwaz/Demo/Data/ChamferBox.blu
Normal file
BIN
aiwaz/Demo/Data/ChamferBox.blu
Normal file
Binary file not shown.
BIN
aiwaz/Demo/Data/Distortion.jpg
Normal file
BIN
aiwaz/Demo/Data/Distortion.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 51 KiB |
202
aiwaz/Demo/Data/NormalOutput.fx
Normal file
202
aiwaz/Demo/Data/NormalOutput.fx
Normal file
@@ -0,0 +1,202 @@
|
||||
matrix WorldMatrix : WorldMatrix;
|
||||
matrix ViewMatrix : ViewMatrix;
|
||||
matrix ProjectionMatrix : ProjectionMatrix;
|
||||
float3 CameraPosition : CamPos;
|
||||
float3 CameraDirection : CamDir;
|
||||
float3 LightPos : LightPosition;
|
||||
float4 LightColor : LightColor;
|
||||
|
||||
Texture2D Diffuse : Diffuse;
|
||||
Texture2D Normal : Normal;
|
||||
SamplerState samLinear
|
||||
{
|
||||
Filter = ANISOTROPIC;
|
||||
AddressU = Wrap;
|
||||
AddressV = Wrap;
|
||||
};
|
||||
|
||||
DepthStencilState RenderWithStencilState
|
||||
{
|
||||
StencilEnable = false;
|
||||
DepthEnable = TRUE;
|
||||
DepthWriteMask = ALL;
|
||||
};
|
||||
|
||||
|
||||
RasterizerState EnableCulling
|
||||
{
|
||||
CullMode = BACK;
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct COLOR_PAIR
|
||||
{
|
||||
float4 Diffuse : COLOR0;
|
||||
float4 Specular : COLOR1;
|
||||
};
|
||||
|
||||
COLOR_PAIR CalculatePointLight(float3 vPosition, float3 vNormal, float3 vViewDir, float3 vLightPos, float LightRange, float4 LightColor, float2 SpecularValue)
|
||||
{
|
||||
COLOR_PAIR rResult = (COLOR_PAIR)0;
|
||||
|
||||
float3 vLightDir = normalize(vLightPos - vPosition); // calc LightDirection
|
||||
float fDiffuse = dot(vNormal, vLightDir); // get diffuse
|
||||
|
||||
float3 vReflect = reflect(vNormal, vLightDir);
|
||||
float fSpecular = dot(vReflect, vViewDir);
|
||||
if (fSpecular > 0)
|
||||
fSpecular = pow( abs(fSpecular), SpecularValue.x) * SpecularValue.y;
|
||||
else
|
||||
fSpecular = 0;
|
||||
|
||||
float fAtten = 1.0f;
|
||||
float LD = length(vPosition - vLightPos);
|
||||
if(LD > LightRange)
|
||||
{
|
||||
fAtten = 0.f;
|
||||
}
|
||||
else
|
||||
{
|
||||
fAtten *= 1.f/(0 +
|
||||
1*LD +
|
||||
0*LD*LD);
|
||||
fAtten = min(1.0f, fAtten);
|
||||
}
|
||||
|
||||
rResult.Diffuse = max(0.0f, fAtten * fDiffuse * LightColor);
|
||||
rResult.Specular = max(0.0f, fAtten * fSpecular * LightColor);
|
||||
|
||||
return rResult;
|
||||
}
|
||||
|
||||
|
||||
struct VS_OUTPUT
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float3 Norm : NORMAL;
|
||||
float2 Tex : TEXCOORD0;
|
||||
float3 WorldPos : TEXCOORD1;
|
||||
float3 ViewDir : TEXCOORD2;
|
||||
};
|
||||
|
||||
struct VSN_OUTPUT
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float3 Norm : NORMAL;
|
||||
float2 Tex : TEXCOORD0;
|
||||
float3 WorldPos : TEXCOORD1;
|
||||
float3 ViewDir : TEXCOORD2;
|
||||
|
||||
float3 Tangent : TEXCOORD3;
|
||||
float3 Binormal : TEXCOORD4;
|
||||
};
|
||||
|
||||
|
||||
VS_OUTPUT VS(float4 Pos : POSITION, float3 Norm : NORMAL, float2 Tex : TEXCOORD)
|
||||
{
|
||||
VS_OUTPUT output = (VS_OUTPUT)0;
|
||||
output.Pos = mul( Pos, WorldMatrix );
|
||||
output.Pos = mul( output.Pos, ViewMatrix );
|
||||
output.Pos = mul( output.Pos, ProjectionMatrix );
|
||||
output.Norm = mul(float4(Norm, 0.0), WorldMatrix).xyz;
|
||||
output.Tex = Tex;
|
||||
output.WorldPos = mul(Pos, WorldMatrix).xyz;
|
||||
output.ViewDir = mul(Pos, WorldMatrix).xyz - CameraPosition;
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
VSN_OUTPUT NormalMappingVS(float4 Pos : POSITION, float3 Norm : NORMAL, float3 Tangent : TANGENT, float2 Tex : TEXCOORD)
|
||||
{
|
||||
VSN_OUTPUT output = (VSN_OUTPUT)0;
|
||||
output.Pos = mul( Pos, WorldMatrix );
|
||||
output.Pos = mul( output.Pos, ViewMatrix );
|
||||
output.Pos = mul( output.Pos, ProjectionMatrix );
|
||||
|
||||
output.Norm = mul(float4(Norm, 0.0), WorldMatrix).xyz;
|
||||
output.Tangent = mul(float4(Tangent, 0.0), WorldMatrix).xyz;
|
||||
output.Binormal = cross(output.Norm, output.Tangent);
|
||||
|
||||
output.Tex = Tex;
|
||||
output.WorldPos = mul(Pos, WorldMatrix).xyz;
|
||||
output.ViewDir = mul(Pos, WorldMatrix).xyz - CameraPosition;
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
float4 NormalMappingPS( VSN_OUTPUT input) : SV_Target
|
||||
{
|
||||
|
||||
float3x3 objToTangentSpace = float3x3( normalize(input.Tangent), normalize(input.Binormal), normalize(input.Norm) );
|
||||
|
||||
float4 base = Diffuse.Sample(samLinear, input.Tex);
|
||||
|
||||
float4 bump = Normal.Sample(samLinear, input.Tex);
|
||||
bump.xyz = normalize(bump.xyz * 2 - 1);
|
||||
|
||||
float3 normal = mul(objToTangentSpace, bump.xyz);
|
||||
float3 lightpos = mul(objToTangentSpace, LightPos);
|
||||
|
||||
COLOR_PAIR LightResult = CalculatePointLight(input.WorldPos, normal, normalize(input.ViewDir), lightpos, 1000.0f, LightColor, float2(16.0f, 1.0f));
|
||||
float4 color = Diffuse.Sample(samLinear, input.Tex) * LightResult.Diffuse + LightResult.Specular;
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
float4 PS( VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
COLOR_PAIR LightResult = CalculatePointLight(input.WorldPos, normalize(input.Norm), normalize(input.ViewDir), LightPos, 1000.0f, LightColor, float2(16.0f, 1.0f));
|
||||
float4 color = Diffuse.Sample(samLinear, input.Tex) * LightResult.Diffuse + LightResult.Specular;
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
float4 LightPS( VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
return float4(LightColor.x, LightColor.y, LightColor.z,1);
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
technique10 Render
|
||||
{
|
||||
pass P0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VS() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, PS() ) );
|
||||
|
||||
SetDepthStencilState( RenderWithStencilState, 0 );
|
||||
SetBlendState( NULL, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
SetRasterizerState( EnableCulling );
|
||||
}
|
||||
}
|
||||
|
||||
technique10 NormalMapping
|
||||
{
|
||||
pass P0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, NormalMappingVS() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, NormalMappingPS() ) );
|
||||
|
||||
SetDepthStencilState( RenderWithStencilState, 0 );
|
||||
SetBlendState( NULL, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
SetRasterizerState( EnableCulling );
|
||||
}
|
||||
}
|
||||
|
||||
technique10 Light
|
||||
{
|
||||
pass P0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VS() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, LightPS() ) );
|
||||
|
||||
SetDepthStencilState( RenderWithStencilState, 0 );
|
||||
SetBlendState( NULL, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
SetRasterizerState( EnableCulling );
|
||||
}
|
||||
}
|
||||
184
aiwaz/Demo/Data/PostProcess.fx
Normal file
184
aiwaz/Demo/Data/PostProcess.fx
Normal file
@@ -0,0 +1,184 @@
|
||||
static const float PixelKernel[13] = { -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
|
||||
static const float BlurWeights[13] = { 0.002216, 0.008764, 0.026995, 0.064759, 0.120985, 0.176033, 0.199471, 0.176033, 0.120985, 0.064759, 0.026995, 0.008764, 0.002216 };
|
||||
|
||||
float CurrentTime : Time;
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// States
|
||||
//--------------------------------------------------------------------------------------
|
||||
BlendState NoBlending
|
||||
{
|
||||
BlendEnable[0] = FALSE;
|
||||
RenderTargetWriteMask[0] = 0x0F;
|
||||
};
|
||||
|
||||
DepthStencilState RenderWithoutStencilState
|
||||
{
|
||||
DepthEnable = false;
|
||||
DepthWriteMask = ZERO;
|
||||
DepthFunc = Always;
|
||||
};
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Textures
|
||||
//--------------------------------------------------------------------------------------
|
||||
Texture2D ColorMap : ColorMap;
|
||||
Texture2D Calculated : InputTexture;
|
||||
Texture2D DistortMap : DistortMap;
|
||||
SamplerState samLinear
|
||||
{
|
||||
Filter = ANISOTROPIC;
|
||||
AddressU = Wrap;
|
||||
AddressV = Wrap;
|
||||
};
|
||||
|
||||
SamplerState samLinearClamp
|
||||
{
|
||||
Filter = ANISOTROPIC;
|
||||
AddressU = Clamp;
|
||||
AddressV = Clamp;
|
||||
};
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
struct VS_OUTPUT
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float2 Tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Vertex Shader
|
||||
//--------------------------------------------------------------------------------------
|
||||
VS_OUTPUT VS(float4 Pos : POSITION, float3 Norm : NORMAL, float2 Tex : TEXCOORD)
|
||||
{
|
||||
VS_OUTPUT output = (VS_OUTPUT)0;
|
||||
output.Pos = Pos;
|
||||
output.Pos.w = 1.0f;
|
||||
output.Tex = Tex;
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Pixel Shader
|
||||
//--------------------------------------------------------------------------------------
|
||||
float4 IntensityPS( VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
float4 color = ColorMap.Sample(samLinear, input.Tex);
|
||||
|
||||
float3 ColorToIntensity = float3(0.294, 0.582, 0.114);
|
||||
color.rgb *= ColorToIntensity;
|
||||
color.rgb = color.r + color.g + color.b;
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
float4 ShowTexturePS( VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
float4 color = Calculated.Sample(samLinear, input.Tex);
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
float4 ComposePS( VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
const float DistortTextureScale = 8.0f;
|
||||
const float DistortStrength = 10.0f;
|
||||
const float DistortSpeed = 0.25f;
|
||||
const float3 ColorToIntensity = float3(0.294, 0.582, 0.114);
|
||||
|
||||
float2 distort = (DistortMap.Sample(samLinear, DistortTextureScale * input.Tex + float2(sin(CurrentTime * DistortSpeed), cos(CurrentTime * DistortSpeed))).xy - 0.5f) * 1.0f;
|
||||
|
||||
// get DDX DDY of the input texture
|
||||
float2 CalculatedSize;
|
||||
ColorMap.GetDimensions(CalculatedSize.x, CalculatedSize.y);
|
||||
CalculatedSize = 1.0f / CalculatedSize;
|
||||
|
||||
float4 bloom = Calculated.Sample(samLinear, input.Tex);
|
||||
|
||||
// bloom step
|
||||
bloom = pow(bloom, 2.0f) * 2.0f;
|
||||
|
||||
float4 intensity = bloom;
|
||||
intensity.rgb *= ColorToIntensity;
|
||||
intensity.rgb = intensity.r + intensity.g + intensity.b;
|
||||
float4 color = ColorMap.Sample(samLinearClamp, input.Tex + distort * CalculatedSize * intensity * DistortStrength);
|
||||
|
||||
return color + bloom;
|
||||
}
|
||||
|
||||
const float pi4 = 3.141 / 4.0;
|
||||
float4 BlurPS( VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
// get DDX DDY of the input texture
|
||||
float2 ddxy;
|
||||
Calculated.GetDimensions(ddxy.x, ddxy.y);
|
||||
ddxy = 1.0f / ddxy;
|
||||
|
||||
// Blur
|
||||
float4 color = 0;
|
||||
float2 SamplePos;
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
SamplePos = input.Tex + ddxy * 4.0 * float2(sin(pi4 * i), cos(pi4 * i));
|
||||
color += Calculated.Sample(samLinearClamp, SamplePos);
|
||||
}
|
||||
|
||||
return color / 8.0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
technique10 Intensity
|
||||
{
|
||||
pass P0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VS() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, IntensityPS() ) );
|
||||
SetDepthStencilState( NULL, 0 );
|
||||
SetBlendState( NULL, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
technique10 ShowTexture
|
||||
{
|
||||
pass P0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VS() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, ShowTexturePS() ) );
|
||||
SetDepthStencilState( NULL, 0 );
|
||||
SetBlendState( NULL, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
technique10 Blur
|
||||
{
|
||||
pass P0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VS() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, BlurPS() ) );
|
||||
SetDepthStencilState( NULL, 0 );
|
||||
SetBlendState( NULL, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
technique10 Compose
|
||||
{
|
||||
pass P0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VS() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, ComposePS() ) );
|
||||
SetDepthStencilState( NULL, 0 );
|
||||
SetBlendState( NULL, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
}
|
||||
}
|
||||
319
aiwaz/Demo/Data/ShadowVolume.fx
Normal file
319
aiwaz/Demo/Data/ShadowVolume.fx
Normal file
@@ -0,0 +1,319 @@
|
||||
#define NOVERTEX 0xfffffffe
|
||||
|
||||
struct VSSceneIn
|
||||
{
|
||||
float4 Pos : POSITION;
|
||||
float3 Norm : NORMAL;
|
||||
float2 Tex : TEXCOORD;
|
||||
};
|
||||
|
||||
struct PSSceneIn
|
||||
{
|
||||
float4 Pos : SV_Position;
|
||||
float4 Color : COLOR0;
|
||||
};
|
||||
|
||||
struct GSShadowIn
|
||||
{
|
||||
float3 Pos : POS;
|
||||
float3 Norm : NORMAL;
|
||||
};
|
||||
|
||||
struct PSShadowIn
|
||||
{
|
||||
float4 Pos : SV_Position;
|
||||
};
|
||||
|
||||
|
||||
matrix WorldMatrix : WorldMatrix;
|
||||
matrix ViewMatrix : ViewMatrix;
|
||||
matrix ProjectionMatrix : ProjectionMatrix;
|
||||
|
||||
float3 LightPos : LightPosition;
|
||||
float ExtrudeAmt = 10.0f;
|
||||
float ExtrudeBias = 0.0f;
|
||||
float4 ShadowColor = float4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
DepthStencilState DisableDepth
|
||||
{
|
||||
DepthEnable = FALSE;
|
||||
DepthWriteMask = ZERO;
|
||||
};
|
||||
|
||||
DepthStencilState EnableDepth
|
||||
{
|
||||
DepthEnable = TRUE;
|
||||
DepthWriteMask = ALL;
|
||||
};
|
||||
|
||||
DepthStencilState TwoSidedStencil
|
||||
{
|
||||
DepthEnable = true;
|
||||
DepthWriteMask = ZERO;
|
||||
DepthFunc = Less;
|
||||
|
||||
// Setup stencil states
|
||||
StencilEnable = true;
|
||||
StencilReadMask = 0xFFFFFFFF;
|
||||
StencilWriteMask = 0xFFFFFFFF;
|
||||
|
||||
BackFaceStencilFunc = Always;
|
||||
BackFaceStencilDepthFail = Incr;
|
||||
BackFaceStencilPass = Keep;
|
||||
BackFaceStencilFail = Keep;
|
||||
|
||||
FrontFaceStencilFunc = Always;
|
||||
FrontFaceStencilDepthFail = Decr;
|
||||
FrontFaceStencilPass = Keep;
|
||||
FrontFaceStencilFail = Keep;
|
||||
};
|
||||
|
||||
|
||||
DepthStencilState RenderNonShadows
|
||||
{
|
||||
DepthEnable = true;
|
||||
DepthWriteMask = ZERO;
|
||||
DepthFunc = Less_Equal;
|
||||
|
||||
StencilEnable = true;
|
||||
StencilReadMask = 0xFFFFFFFF;
|
||||
StencilWriteMask = 0x0;
|
||||
|
||||
FrontFaceStencilFunc = NOT_EQUAL;
|
||||
FrontFaceStencilPass = Keep;
|
||||
FrontFaceStencilFail = ZERO;
|
||||
|
||||
BackFaceStencilFunc = Never;
|
||||
BackFaceStencilPass = ZERO;
|
||||
BackFaceStencilFail = ZERO;
|
||||
};
|
||||
|
||||
BlendState DisableFrameBuffer
|
||||
{
|
||||
BlendEnable[0] = FALSE;
|
||||
RenderTargetWriteMask[0] = 0x0;
|
||||
};
|
||||
|
||||
BlendState EnableFrameBuffer
|
||||
{
|
||||
BlendEnable[0] = FALSE;
|
||||
RenderTargetWriteMask[0] = 0x0F;
|
||||
};
|
||||
|
||||
BlendState SrcAlphaBlending
|
||||
{
|
||||
AlphaToCoverageEnable = FALSE;
|
||||
BlendEnable[0] = TRUE;
|
||||
SrcBlend = SRC_ALPHA;
|
||||
DestBlend = INV_SRC_ALPHA;
|
||||
BlendOp = ADD;
|
||||
SrcBlendAlpha = ZERO;
|
||||
DestBlendAlpha = ZERO;
|
||||
BlendOpAlpha = ADD;
|
||||
RenderTargetWriteMask[0] = 0x0F;
|
||||
};
|
||||
|
||||
|
||||
BlendState AdditiveBlending
|
||||
{
|
||||
AlphaToCoverageEnable = FALSE;
|
||||
BlendEnable[0] = TRUE;
|
||||
SrcBlend = ONE;
|
||||
DestBlend = ONE;
|
||||
BlendOp = SUBTRACT;
|
||||
SrcBlendAlpha = ZERO;
|
||||
DestBlendAlpha = ZERO;
|
||||
BlendOpAlpha = ADD;
|
||||
RenderTargetWriteMask[0] = 0x0F;
|
||||
};
|
||||
|
||||
RasterizerState DisableCulling
|
||||
{
|
||||
CullMode = NONE;
|
||||
};
|
||||
|
||||
RasterizerState EnableCulling
|
||||
{
|
||||
CullMode = BACK;
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// VS for sending information to the shadow GS
|
||||
//
|
||||
GSShadowIn VSShadowmain( VSSceneIn input )
|
||||
{
|
||||
GSShadowIn output = (GSShadowIn)0.0;
|
||||
|
||||
//output our position in world space
|
||||
float4 pos = mul( float4(input.Pos.xyz,1), WorldMatrix );
|
||||
output.Pos = pos.xyz;
|
||||
|
||||
//world space normal
|
||||
output.Norm = mul( input.Norm, (float3x3)WorldMatrix );
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
PSSceneIn VSScenemain( VSSceneIn input )
|
||||
{
|
||||
PSSceneIn output = (PSSceneIn)0.0;
|
||||
|
||||
//output our final position in clipspace
|
||||
output.Pos = mul(input.Pos, WorldMatrix);
|
||||
output.Pos = mul(output.Pos, ViewMatrix);
|
||||
output.Pos = mul(output.Pos, ProjectionMatrix);
|
||||
|
||||
//world space normal
|
||||
float3 norm = mul( input.Norm, (float3x3)WorldMatrix );
|
||||
|
||||
//find the light dir
|
||||
float3 wpos = mul( input.Pos, (float3x3)WorldMatrix );
|
||||
|
||||
float3 lightDir = normalize( LightPos - wpos );
|
||||
float lightLenSq = length(LightPos - wpos);
|
||||
|
||||
output.Color = saturate(dot(lightDir,norm)) * ShadowColor * 8.0f/lightLenSq;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// PS for rendering lit and textured triangles
|
||||
float4 PSScenemain(PSSceneIn input) : SV_Target
|
||||
{
|
||||
return input.Color;
|
||||
}
|
||||
|
||||
// Helper to detect a silhouette edge and extrude a volume from it
|
||||
void DetectAndProcessSilhouette(float3 N, // Un-normalized triangle normal
|
||||
GSShadowIn v1, // Shared vertex
|
||||
GSShadowIn v2, // Shared vertex
|
||||
GSShadowIn vAdj, // Adjacent triangle vertex
|
||||
inout TriangleStream<PSShadowIn> ShadowTriangleStream // triangle stream
|
||||
)
|
||||
{
|
||||
float3 NAdj = cross(v2.Pos - vAdj.Pos, v1.Pos - vAdj.Pos);
|
||||
|
||||
float fDot = dot(normalize(N), normalize(NAdj));
|
||||
if (fDot < 0.0)
|
||||
{
|
||||
float3 outpos[4];
|
||||
float3 extrude1 = normalize(v1.Pos - LightPos);
|
||||
float3 extrude2 = normalize(v2.Pos - LightPos);
|
||||
|
||||
outpos[0] = v1.Pos + ExtrudeBias * extrude1;
|
||||
outpos[1] = v1.Pos + ExtrudeAmt * extrude1;
|
||||
outpos[2] = v2.Pos + ExtrudeBias * extrude2;
|
||||
outpos[3] = v2.Pos + ExtrudeAmt * extrude2;
|
||||
|
||||
// Extrude silhouette to create two new triangles
|
||||
PSShadowIn Out;
|
||||
for(int v = 0; v < 4; v++)
|
||||
{
|
||||
Out.Pos = mul(float4(outpos[v], 1.0f), ViewMatrix);
|
||||
Out.Pos = mul(Out.Pos, ProjectionMatrix);
|
||||
|
||||
ShadowTriangleStream.Append(Out);
|
||||
}
|
||||
ShadowTriangleStream.RestartStrip();
|
||||
}
|
||||
}
|
||||
|
||||
// GS for generating shadow volumes
|
||||
[maxvertexcount(18)]
|
||||
void GSShadowmain(triangleadj GSShadowIn In[6], inout TriangleStream<PSShadowIn> ShadowTriangleStream)
|
||||
{
|
||||
// Compute un-normalized triangle normal
|
||||
float3 N = normalize(cross(In[2].Pos - In[0].Pos, In[4].Pos - In[0].Pos));
|
||||
|
||||
// Compute direction from this triangle to the light
|
||||
float3 lightDir[3];
|
||||
lightDir[0] = normalize(LightPos - In[0].Pos);
|
||||
lightDir[1] = normalize(LightPos - In[2].Pos);
|
||||
lightDir[2] = normalize(LightPos - In[4].Pos);
|
||||
|
||||
//if we're facing the light
|
||||
if(dot(N, lightDir[0]) > 0.0f || dot(N, lightDir[1]) > 0.0f || dot(N, lightDir[2]) > 0.0f)
|
||||
{
|
||||
// For each edge of the triangle, determine if it is a silhouette edge
|
||||
DetectAndProcessSilhouette(lightDir[0], In[0], In[2], In[1], ShadowTriangleStream);
|
||||
DetectAndProcessSilhouette(lightDir[1], In[2], In[4], In[3], ShadowTriangleStream);
|
||||
DetectAndProcessSilhouette(lightDir[2], In[4], In[0], In[5], ShadowTriangleStream);
|
||||
|
||||
//near cap
|
||||
PSShadowIn Out;
|
||||
for(int v = 0; v < 6; v += 2)
|
||||
{
|
||||
float3 extrude = normalize(In[v].Pos - LightPos);
|
||||
|
||||
float3 Pos = In[v].Pos + ExtrudeBias * extrude;
|
||||
Out.Pos = mul(float4(Pos, 1.0f), ViewMatrix);
|
||||
Out.Pos = mul(Out.Pos, ProjectionMatrix);
|
||||
ShadowTriangleStream.Append(Out);
|
||||
}
|
||||
ShadowTriangleStream.RestartStrip();
|
||||
|
||||
//far cap (reverse the order)
|
||||
for(int v2 = 4; v2 >= 0; v2 -= 2)
|
||||
{
|
||||
float3 extrude = normalize(In[v2].Pos - LightPos);
|
||||
|
||||
float3 Pos = In[v2].Pos + ExtrudeAmt * extrude;
|
||||
Out.Pos = mul(float4(Pos, 1.0f), ViewMatrix);
|
||||
Out.Pos = mul(Out.Pos, ProjectionMatrix);
|
||||
ShadowTriangleStream.Append( Out );
|
||||
}
|
||||
ShadowTriangleStream.RestartStrip();
|
||||
}
|
||||
}
|
||||
|
||||
// PS for rendering shadow scene
|
||||
float4 PSShadowmain(PSShadowIn input) : SV_Target
|
||||
{
|
||||
return float4(0.3,0,0,0.25);
|
||||
}
|
||||
|
||||
// RenderShadow - extrudes shadows from geometry
|
||||
technique10 CastShadows
|
||||
{
|
||||
pass p0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VSShadowmain() ) );
|
||||
SetGeometryShader( CompileShader( gs_4_0, GSShadowmain() ) );
|
||||
SetPixelShader( CompileShader( ps_4_0, PSShadowmain() ) );
|
||||
|
||||
SetBlendState( DisableFrameBuffer, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
SetDepthStencilState( TwoSidedStencil, 1 ); //state, stencilref
|
||||
SetRasterizerState( DisableCulling );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
technique10 ShowShadowVolume
|
||||
{
|
||||
pass p0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VSShadowmain() ) );
|
||||
SetGeometryShader( CompileShader( gs_4_0, GSShadowmain() ) );
|
||||
SetPixelShader( CompileShader( ps_4_0, PSShadowmain() ) );
|
||||
|
||||
SetBlendState( SrcAlphaBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
SetDepthStencilState( TwoSidedStencil, 1 ); //state, stencilref
|
||||
SetRasterizerState( DisableCulling );
|
||||
}
|
||||
}
|
||||
|
||||
technique10 ReceiveShadows
|
||||
{
|
||||
pass p0
|
||||
{
|
||||
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
|
||||
SetGeometryShader( NULL );
|
||||
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );
|
||||
|
||||
SetBlendState( SrcAlphaBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
|
||||
SetDepthStencilState( RenderNonShadows, 0 ); //state, stencilref
|
||||
SetRasterizerState( EnableCulling );
|
||||
}
|
||||
}
|
||||
BIN
aiwaz/Demo/Data/Wall.jpg
Normal file
BIN
aiwaz/Demo/Data/Wall.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 449 KiB |
BIN
aiwaz/Demo/Data/Wall2.jpg
Normal file
BIN
aiwaz/Demo/Data/Wall2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 156 KiB |
BIN
aiwaz/Demo/Data/Wall2Normal.jpg
Normal file
BIN
aiwaz/Demo/Data/Wall2Normal.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 64 KiB |
BIN
aiwaz/Demo/Data/WallNormal.jpg
Normal file
BIN
aiwaz/Demo/Data/WallNormal.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 181 KiB |
Reference in New Issue
Block a user