2020-08-11 21:08:49 +02:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// LandBump.fx
|
|
|
|
|
|
|
|
// Land bumpmapping and lighting shader.
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
#include "../fxh/SystemVariables.fxh"
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
//float4x4 g_texGenMatrix2 : TexGenTransform2; // Shore texgen
|
2020-08-11 21:08:49 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Texture samplers
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
sampler g_LandTextureSampler =
|
|
|
|
sampler_state
|
|
|
|
{
|
2020-11-06 20:22:56 +01:00
|
|
|
Texture = <g_texture0>;
|
2020-08-11 21:08:49 +02:00
|
|
|
MipFilter = LINEAR;
|
|
|
|
MinFilter = LINEAR;
|
|
|
|
MagFilter = LINEAR;
|
|
|
|
};
|
|
|
|
|
|
|
|
sampler g_LandBumpTextureSampler =
|
|
|
|
sampler_state
|
|
|
|
{
|
2020-11-06 20:22:56 +01:00
|
|
|
Texture = <g_texture1>;
|
2020-08-11 21:08:49 +02:00
|
|
|
MipFilter = LINEAR;
|
|
|
|
MinFilter = LINEAR;
|
|
|
|
MagFilter = LINEAR;
|
|
|
|
};
|
|
|
|
|
2020-11-05 11:17:12 +01:00
|
|
|
//sampler g_LandDetailTextureSampler =
|
|
|
|
//sampler_state
|
|
|
|
//{
|
|
|
|
// Texture = <g_LandDetailTexture>;
|
|
|
|
// MipFilter = LINEAR;
|
|
|
|
// MinFilter = LINEAR;
|
|
|
|
// MagFilter = LINEAR;
|
|
|
|
//};
|
|
|
|
//
|
|
|
|
//sampler g_ShoreTextureSampler =
|
|
|
|
//sampler_state
|
|
|
|
//{
|
|
|
|
// Texture = <g_LandBumpTexture>;
|
|
|
|
// MipFilter = LINEAR;
|
|
|
|
// MinFilter = LINEAR;
|
|
|
|
// MagFilter = LINEAR;
|
|
|
|
//};
|
2020-08-11 21:08:49 +02:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Vertex shader output structure
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
struct VS_OUTPUT_BUMP
|
|
|
|
{
|
|
|
|
float4 Position : POSITION;
|
|
|
|
float4 LandBumpDiffuse : COLOR1;
|
|
|
|
float4 LandDiffuse : COLOR0;
|
|
|
|
float2 LandBumpTextureUV : TEXCOORD0;
|
|
|
|
float2 LandTextureUV : TEXCOORD1;
|
|
|
|
float3 WorldPos : TEXCOORD2;
|
|
|
|
float3 Normal : TEXCOORD3;
|
2020-11-06 20:22:56 +01:00
|
|
|
//float3 ShoreTextureUV : TEXCOORD4;
|
2020-08-11 21:08:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
float4 bx2(float4 x)
|
|
|
|
{
|
|
|
|
return float4(2.0f * x.xyzw - 1.0f);
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:01:11 +02:00
|
|
|
VS_OUTPUT_BUMP LandBumpVS(
|
|
|
|
float4 vPos : POSITION,
|
|
|
|
float3 vNormal : NORMAL,
|
|
|
|
float4 vDiffuse : COLOR0,
|
|
|
|
float4 vDiffuse2 : COLOR1)
|
2020-08-11 21:08:49 +02:00
|
|
|
{
|
|
|
|
VS_OUTPUT_BUMP Output;
|
|
|
|
|
|
|
|
// Transform the position from object space to homogeneous projection space
|
2020-11-06 20:22:56 +01:00
|
|
|
Output.Position = mul(vPos, g_WorldViewProjection);
|
2020-08-11 21:08:49 +02:00
|
|
|
|
|
|
|
Output.LandBumpDiffuse = vDiffuse2 * .5f;
|
|
|
|
Output.LandBumpDiffuse.a = 1.0f;
|
|
|
|
|
|
|
|
Output.LandDiffuse.rgb = vDiffuse;
|
|
|
|
Output.LandDiffuse.a = 1.0f;
|
|
|
|
|
|
|
|
Output.WorldPos = mul(vPos, g_World);
|
|
|
|
|
|
|
|
// Set dynamically generated tex coords
|
2020-11-06 20:22:56 +01:00
|
|
|
Output.LandBumpTextureUV = mul(vPos, g_texGenMatrix0);
|
|
|
|
Output.LandTextureUV = mul(vPos, g_texGenMatrix1);
|
|
|
|
//Output.ShoreTextureUV = mul(vPos, g_texGenMatrix2);
|
2020-08-11 21:08:49 +02:00
|
|
|
|
|
|
|
// Transform the normal from object space to world space
|
|
|
|
Output.Normal = normalize(mul(vNormal, (float3x3)g_World)); // normal (world space)
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 LandBumpPS(VS_OUTPUT_BUMP input) : COLOR0
|
|
|
|
{
|
|
|
|
float4 normal = bx2(tex2D(g_LandBumpTextureSampler, input.LandBumpTextureUV));
|
|
|
|
float4 normalcol = bx2(input.LandDiffuse);
|
|
|
|
|
|
|
|
float3 normalMap;
|
|
|
|
normalMap = saturate((float4)dot((float3)normal, (float3)normalcol)).xyz;
|
|
|
|
float3 finalColor = 2.0 * (normalMap * (tex2D(g_LandTextureSampler, input.LandTextureUV)) + input.LandBumpDiffuse);
|
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
for (int i = 0; i < g_numPointLights; i++)
|
2020-08-11 21:08:49 +02:00
|
|
|
{
|
2020-11-06 20:22:56 +01:00
|
|
|
// Get light direction for this fragment
|
|
|
|
float3 lightDir = normalize(input.WorldPos - g_PointLightPosition[i]); // per pixel diffuse lighting
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
// Note: Non-uniform scaling not supported
|
|
|
|
float diffuseLighting = saturate(dot(input.Normal, -lightDir));
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
// Introduce fall-off of light intensity
|
|
|
|
diffuseLighting *= (g_PointLightRangeSquared[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos));
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
float4 diffuseColor = diffuseLighting * g_PointLightDiffuse[i];
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
finalColor += diffuseColor;
|
2020-08-11 21:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return float4(finalColor, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
technique LandBump
|
|
|
|
{
|
|
|
|
pass P0
|
|
|
|
{
|
|
|
|
VertexShader = compile vs_2_0 LandBumpVS();
|
2020-09-28 08:01:11 +02:00
|
|
|
PixelShader = compile ps_2_0 LandBumpPS();
|
2020-08-11 21:08:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Vertex shader output structure
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
struct VS_OUTPUT
|
|
|
|
{
|
|
|
|
float4 Position : POSITION;
|
|
|
|
float4 Diffuse : COLOR0;
|
|
|
|
float2 TextureUV : TEXCOORD0;
|
|
|
|
float3 Normal : TEXCOORD1;
|
|
|
|
float3 WorldPos : TEXCOORD2;
|
|
|
|
};
|
|
|
|
|
2020-09-28 08:01:11 +02:00
|
|
|
VS_OUTPUT LandscapeVS(
|
|
|
|
float4 vPos : POSITION,
|
|
|
|
float3 vNormal : NORMAL,
|
|
|
|
float4 vDiffuse : COLOR0)
|
2020-08-11 21:08:49 +02:00
|
|
|
{
|
|
|
|
VS_OUTPUT Output;
|
|
|
|
|
|
|
|
// Transform the position from object space to homogeneous projection space
|
2020-11-06 20:22:56 +01:00
|
|
|
Output.Position = mul(vPos, g_WorldViewProjection);
|
2020-08-11 21:08:49 +02:00
|
|
|
|
|
|
|
// Transform the normal from object space to world space
|
|
|
|
Output.Normal = normalize(mul(vNormal, (float3x3)g_World)); // normal (world space)
|
|
|
|
|
|
|
|
Output.Diffuse.rgb = vDiffuse;
|
|
|
|
Output.Diffuse.a = 1.0f;
|
|
|
|
|
|
|
|
Output.WorldPos = mul(vPos, g_World);
|
|
|
|
|
|
|
|
// Set dynamically generated tex coords
|
2020-11-06 20:22:56 +01:00
|
|
|
Output.TextureUV = mul(vPos, g_texGenMatrix0);
|
2020-08-11 21:08:49 +02:00
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 LandscapePS(VS_OUTPUT input) : COLOR0
|
|
|
|
{
|
|
|
|
float4 finalColor = 0;
|
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
for (int i = 0; i < g_numPointLights; i++)
|
2020-08-11 21:08:49 +02:00
|
|
|
{
|
2020-11-06 20:22:56 +01:00
|
|
|
// Get light direction for this fragment
|
|
|
|
float3 lightDir = normalize(input.WorldPos - g_PointLightPosition[i]); // per pixel diffuse lighting
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
// Note: Non-uniform scaling not supported
|
|
|
|
float diffuseLighting = saturate(dot(input.Normal, -lightDir));
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
// Introduce fall-off of light intensity
|
|
|
|
diffuseLighting *= (g_PointLightRangeSquared[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos));
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
float4 diffuseColor = diffuseLighting * g_PointLightDiffuse[i];
|
2020-08-11 21:08:49 +02:00
|
|
|
|
2020-11-06 20:22:56 +01:00
|
|
|
finalColor += diffuseColor;
|
2020-08-11 21:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float3 texel = tex2D(g_LandTextureSampler, input.TextureUV);
|
|
|
|
return float4(saturate((texel.xyz + input.Diffuse) + (finalColor)), 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
technique Landscape
|
|
|
|
{
|
|
|
|
pass P0
|
|
|
|
{
|
|
|
|
VertexShader = compile vs_2_0 LandscapeVS();
|
|
|
|
PixelShader = compile ps_2_0 LandscapePS();
|
|
|
|
}
|
|
|
|
}
|