1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-22 06:05:38 +01:00
GiantsTools/Shaders/fx/Water.fx

109 lines
3.0 KiB
HLSL
Raw Normal View History

2020-08-11 21:08:49 +02:00
//--------------------------------------------------------------------------------------
// Water.fx
// Very basic shader that implements D3DTOP_ADDSMOOTH functionality in HLSL, replicating
// the original fixed-function water rendering behavior.
//--------------------------------------------------------------------------------------
2020-12-01 02:40:33 +01:00
#include "../fxh/Transform.fxh"
#include "../fxh/Lighting.fxh"
#include "../fxh/Fog.fxh"
shared texture g_Texture0;
2020-12-01 02:40:33 +01:00
shared WorldTransforms g_WorldTransforms;
shared ViewTransforms g_ViewTransforms;
shared float4 g_TextureFactor;
2020-08-11 21:08:49 +02:00
2020-12-01 02:40:33 +01:00
const float3 g_WaterNormal = float3(0, 0, 1); // Water is always a flat plane currently, so we can just hardcode this to (0, 0, 1)
shared Material g_WaterMaterial;
shared PointLightInfo g_PointLights;
shared DirectionalLightInfo g_DirectionalLights;
shared FogParams g_Fog;
2020-08-11 21:08:49 +02:00
sampler2D g_WaterTextureSampler =
sampler_state
{
Texture = <g_Texture0>;
2020-08-11 21:08:49 +02:00
};
struct VS_OUTPUT
{
float4 Position : POSITION; // vertex position
float2 TextureUV : TEXCOORD0; // vertex texture coords
float4 Color : COLOR0;
2020-12-01 02:40:33 +01:00
float Fog : FOG;
float3 WorldPos : TEXCOORD1;
/*float3 Normal : TEXCOORD1;*/
float3 ViewDirection : TEXCOORD2;
2020-08-11 21:08:49 +02:00
};
VS_OUTPUT RenderSceneVS(float4 inPos : POSITION,
float4 inDiffuse : COLOR0,
2020-12-01 02:40:33 +01:00
float4 inTextureUV : TEXCOORD0
/*float3 Normal : NORMAL*/)
2020-08-11 21:08:49 +02:00
{
VS_OUTPUT Output;
2020-12-01 02:40:33 +01:00
Output.Position = mul(inPos, g_WorldTransforms.WorldViewProjection);
2020-08-11 21:08:49 +02:00
Output.TextureUV = inTextureUV;
Output.Color = inDiffuse;
2020-12-01 02:40:33 +01:00
float3 P = mul(inPos, g_WorldTransforms.WorldView);
float d = length(P);
Output.Fog = CalculateFogFactor(g_Fog.FogMax, g_Fog.FogMin, d);
//Output.Normal = mul(Normal, (float3x3)g_WorldTransforms.World);
Output.WorldPos = mul(inPos, g_WorldTransforms.World);
Output.ViewDirection = g_ViewTransforms.CameraPosition - Output.WorldPos;
2020-08-11 21:08:49 +02:00
return Output;
}
float4 RenderScenePS(VS_OUTPUT input) : COLOR0
2020-12-01 02:40:33 +01:00
{
2020-08-11 21:08:49 +02:00
float4 texel = tex2D(g_WaterTextureSampler, input.TextureUV);
float4 result = saturate(texel + (1 - texel) * g_TextureFactor); // equivalent to saturate((texel + g_TextureFactor) - (texel * g_TextureFactor));
2020-12-01 02:40:33 +01:00
float3 reflection = -reflect(normalize(-g_DirectionalLights.Direction[g_DirectionalLights.SunIndex]), g_WaterNormal);
float specular = dot(normalize(reflection), normalize(input.ViewDirection));
if (specular > 0.0f)
{
specular = pow(specular, g_WaterMaterial.Power);
result = saturate(result + (specular));
}
for (int i = 0; i < g_PointLights.Count; i++)
{
float3 worldPos = g_PointLights.Position[i] - input.WorldPos;
float3 lightDirection = normalize(input.WorldPos - (g_PointLights.Position[i]));
result += CalculatePointDiffuse(
worldPos,
g_WaterNormal,
-lightDirection,
g_PointLights.Range[i],
g_PointLights.Diffuse[i]);
}
if (g_Fog.Enabled)
{
result = ApplyPixelFog(result, input.Fog, g_Fog.Color);
}
result.a = input.Color.a * g_TextureFactor.a;
2020-08-11 21:08:49 +02:00
return result;
}
technique RenderScene
{
pass P0
{
2020-12-01 02:40:33 +01:00
VertexShader = compile vs_3_0 RenderSceneVS();
PixelShader = compile ps_3_0 RenderScenePS();
2020-08-11 21:08:49 +02:00
}
}