Well it's something ig
This commit is contained in:
@@ -116,6 +116,10 @@ float3 Shade(VS_OUTPUT input)
|
||||
}
|
||||
|
||||
float4 PS(VS_OUTPUT input) : SV_TARGET{
|
||||
//Check if gDiffuseMap sample has a valid value, otherwise render red
|
||||
if (gDiffuseMap.Sample(gSampleState, input.TexCoord).a == 0.f)
|
||||
return float4(1.f, 0.f, 0.f, 1.f);
|
||||
|
||||
return gDiffuseMap.Sample(gSampleState, input.TexCoord);
|
||||
}
|
||||
|
||||
|
||||
111
project/resources/SuperSimpleDiffuse.fx
Normal file
111
project/resources/SuperSimpleDiffuse.fx
Normal file
@@ -0,0 +1,111 @@
|
||||
SamplerState gSampleState : SampleState;
|
||||
RasterizerState gRasterizerState : RastState;
|
||||
|
||||
// Transformations
|
||||
float4x4 gWorldViewProj : WorldViewProjection;
|
||||
float4x4 gWorldMatrix : WorldMatrix;
|
||||
float4x4 gLightViewProj : LightViewProjection;
|
||||
|
||||
// Textures
|
||||
texture2D gDiffuseMap : DiffuseMap;
|
||||
texture2D gNormalMap : Normal;
|
||||
texture2D gSpecularMap : Specular;
|
||||
texture2D gGlossMap : Gloss;
|
||||
texture2D gShadowMap : ShadowMap;
|
||||
|
||||
// Light properties
|
||||
float3 gLightDirection : LightDirection;
|
||||
float3 gLightColor : LightColor;
|
||||
float3 gCameraPosition : CameraPosition;
|
||||
bool gUseNormal : UseNormal;
|
||||
|
||||
// Constants
|
||||
static const float3 gAmbient = float3(.03f, .03f, .03f);
|
||||
static const float gLightIntensity = 7.f;
|
||||
static const float PI = 3.14159f;
|
||||
static const float gSpecularReflectance = 1.f;
|
||||
static const float gShininess = 25.f;
|
||||
|
||||
// Shadow bias to reduce artifacts
|
||||
static const float gShadowBias = 0.005f;
|
||||
|
||||
// Input/Output structures
|
||||
struct VS_INPUT {
|
||||
float3 Position : POSITION;
|
||||
float2 TexCoord : TEXCOORD;
|
||||
float3 Normal : NORMAL;
|
||||
float3 Tangent : TANGENT;
|
||||
};
|
||||
|
||||
struct VS_OUTPUT {
|
||||
float4 Position : SV_POSITION;
|
||||
float4 WorldPosition : WORLDPOSITION;
|
||||
float2 TexCoord : TEXCOORD;
|
||||
float3 Normal : NORMAL;
|
||||
float3 Tangent : TANGENT;
|
||||
float4 LightPosition : TEXCOORD1;
|
||||
};
|
||||
|
||||
VS_OUTPUT VS(VS_INPUT input) {
|
||||
VS_OUTPUT output = (VS_OUTPUT)0;
|
||||
|
||||
output.Position = mul(float4(input.Position, 1.f), gWorldViewProj);
|
||||
output.WorldPosition = mul(float4(input.Position, 1.f), gWorldMatrix);
|
||||
output.TexCoord = input.TexCoord;
|
||||
output.Normal = mul(input.Normal, (float3x3)gWorldMatrix);
|
||||
output.Tangent = mul(input.Tangent, (float3x3)gWorldMatrix);
|
||||
output.LightPosition = mul(float4(input.Position, 1.f), gLightViewProj);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
float ShadowCalculation(float4 lightPos) {
|
||||
// Transform light position to shadow map space
|
||||
float2 shadowCoord = lightPos.xy / lightPos.w;
|
||||
shadowCoord = shadowCoord * 0.5f + 0.5f;
|
||||
|
||||
// Sample the shadow map
|
||||
float shadowDepth = gShadowMap.Sample(gSampleState, shadowCoord).r;
|
||||
|
||||
// Compare depth and apply bias
|
||||
if (shadowDepth + gShadowBias < lightPos.z / lightPos.w) {
|
||||
return 0.0f; // In shadow
|
||||
}
|
||||
return 1.0f; // Not in shadow
|
||||
}
|
||||
|
||||
float4 PS(VS_OUTPUT input) : SV_TARGET {
|
||||
// Normalize inputs
|
||||
float3 normal = normalize(input.Normal);
|
||||
float3 lightDir = normalize(gLightDirection);
|
||||
|
||||
// Diffuse lighting
|
||||
float NdotL = max(dot(normal, -lightDir), 0.0f);
|
||||
float3 diffuse = gLightColor * NdotL;
|
||||
|
||||
// Shadows
|
||||
float shadowFactor = ShadowCalculation(input.LightPosition);
|
||||
|
||||
// Ambient + Diffuse * Shadow
|
||||
float3 finalColor = gAmbient + (diffuse * shadowFactor);
|
||||
// float3 finalColor = float3(shadowFactor,shadowFactor,shadowFactor);
|
||||
|
||||
return float4(finalColor, 1.0f);
|
||||
}
|
||||
|
||||
DepthStencilState gDepthStencilState {
|
||||
DepthEnable = true;
|
||||
DepthWriteMask = ALL;
|
||||
DepthFunc = LESS;
|
||||
StencilEnable = true;
|
||||
};
|
||||
|
||||
technique11 DefaultTechnique {
|
||||
pass P0 {
|
||||
SetDepthStencilState(gDepthStencilState, 0);
|
||||
SetRasterizerState(gRasterizerState);
|
||||
SetVertexShader(CompileShader(vs_5_0, VS()));
|
||||
SetGeometryShader(NULL);
|
||||
SetPixelShader(CompileShader(ps_5_0, PS()));
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,15 @@ float4x4 gLightWorldViewProj : WorldViewProjection;
|
||||
|
||||
float4x4 gWorldViewProj: WorldViewProjection; //For compatibility sake
|
||||
|
||||
Texture2D gShadowMap : register(t0); // Shadow map texture
|
||||
SamplerState gShadowMapSampler : register(s0); // Shadow map sampler
|
||||
Texture2D gShadowMap : ShadowMap;
|
||||
SamplerState gShadowMapSampler : ShadowMapSampler;
|
||||
|
||||
struct VS_Input
|
||||
{
|
||||
struct VS_Input {
|
||||
float3 Position : POSITION;
|
||||
float2 TexCoord : TEXCOORD;
|
||||
float3 Normal : NORMAL;
|
||||
float3 Tangent : TANGENT;
|
||||
};
|
||||
|
||||
struct VS_Output
|
||||
{
|
||||
float4 Position : SV_POSITION;
|
||||
|
||||
Reference in New Issue
Block a user