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

178 lines
3.9 KiB
HLSL
Raw Normal View History

2020-08-11 21:08:49 +02:00
//--------------------------------------------------------------------------------------
2020-12-01 02:40:33 +01:00
// Ocean.fx
2020-08-11 21:08:49 +02:00
// Ocean water reflection shader.
//--------------------------------------------------------------------------------------
2020-12-01 02:40:33 +01:00
#include "../fxh/Transform.fxh"
#include "../fxh/Fog.fxh"
2020-08-11 21:08:49 +02:00
2020-12-01 02:40:33 +01:00
shared texture g_Texture0; // Seabed texture
shared texture g_Texture1; // Environment texture
shared WorldTransforms g_WorldTransforms;
shared ViewTransforms g_ViewTransforms;
shared FogParams g_Fog;
2020-08-11 21:08:49 +02:00
struct PS_INPUT
{
float4 color : COLOR0;
float4 texCoord0 : TEXCOORD0;
float4 texCoord1 : TEXCOORD1;
2020-12-01 02:40:33 +01:00
float Fog : FOG;
};
sampler g_SeabedSampler : register(s0) = sampler_state
{
Texture = <g_Texture0>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
2020-08-11 21:08:49 +02:00
};
2020-12-01 02:40:33 +01:00
sampler g_EnvironmentSampler = sampler_state
{
Texture = <g_Texture1>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
2020-08-11 21:08:49 +02:00
float4 MainPS(const PS_INPUT input) : COLOR0
{
// Interpolate linearly between Environment Texture, seabed, and inverted alpha of current pixel
2020-12-01 02:40:33 +01:00
float4 result = lerp(texCUBE(g_EnvironmentSampler, input.texCoord1), tex2D(g_SeabedSampler, input.texCoord0.xy), 1-input.color.a);
// Note: fog disabled for now as it doesn't quite match up with FFP fog.
/*
if (g_Fog.Enabled)
{
result = ApplyPixelFog(result, input.Fog, g_Fog.Color);
}
*/
2020-08-11 21:08:49 +02:00
2020-12-01 02:40:33 +01:00
return result;
}
2020-08-11 21:08:49 +02:00
2020-12-01 02:40:33 +01:00
// C44: (dynamic)
2020-08-11 21:08:49 +02:00
float4 const44 : register(c44);
struct VS_OUTPUT
{
float4 pos : POSITION;
2020-12-01 02:40:33 +01:00
float4 texCoord0 : TEXCOORD0;
2020-08-11 21:08:49 +02:00
float4 texCoord1 : TEXCOORD1;
2020-12-01 02:40:33 +01:00
float4 color : COLOR0;
float Fog : FOG;
2020-08-11 21:08:49 +02:00
};
struct VS_INPUT
{
float4 pos : POSITION;
2020-12-01 02:40:33 +01:00
float4 texCoord0 : TEXCOORD0;
2020-08-11 21:08:49 +02:00
};
VS_OUTPUT MainVS(const VS_INPUT input)
{
2020-12-01 02:40:33 +01:00
VS_OUTPUT output = (VS_OUTPUT)0;
2020-08-11 21:08:49 +02:00
2020-12-01 02:40:33 +01:00
output.pos = mul(input.pos, g_WorldTransforms.WorldViewProjection);
2020-08-11 21:08:49 +02:00
// add r0, v0, -c44
2020-12-01 02:40:33 +01:00
float4 r0 : register(r0) = input.pos + -const44;
2020-08-11 21:08:49 +02:00
// mov r0.z, -r0.z
r0.z = -r0.z;
// dp3 r0.w, r0, r0
r0.w = dot(r0.xyz, r0.xyz);
// rsq r0.w, r0.w
r0.w = rsqrt(r0.w);
// mul oT1.x, -r0, r0.w
// mul oT1.y, r0, r0.w
// mul oT1.z, r0, r0.w
// mov oT1.w, c27.y
2020-12-01 02:40:33 +01:00
output.texCoord1.x = r0.w * -r0.x;
output.texCoord1.y = r0.w * r0.y;
output.texCoord1.z = r0.w * r0.z;
output.texCoord1.w = 1.0f;
//rcp r0.w, r0.w
r0.w = 1.0f / r0.w;
//mul r0.w, r0.w, c45.x
//max r0.x, r0.w, c27.x
//min oD0.w, r0.w, c27.y
//mov oT0, v2
r0.w = r0.w * 0.00033333333f;
output.color.a = min(r0.w, 1.0);
output.texCoord0 = input.texCoord0;
/*
float3 P = mul(input.pos, g_WorldTransforms.WorldView);
float d = length(P);
output.Fog = CalculateFogFactor(g_Fog.FogMax, g_Fog.FogMin, d);
*/
2020-08-11 21:08:49 +02:00
return output;
}
2020-12-01 02:40:33 +01:00
technique ReflectionLow
2020-08-11 21:08:49 +02:00
{
2020-12-01 02:40:33 +01:00
pass P0
2020-08-11 21:08:49 +02:00
{
2020-12-01 02:40:33 +01:00
VertexShader = compile vs_2_0 MainVS();
PixelShader = compile ps_2_0 MainPS();
2020-08-11 21:08:49 +02:00
}
}
2020-12-01 02:40:33 +01:00
struct ReflectionHighVSOutput
{
float4 pos : POSITION;
float4 texCoord0 : TEXCOORD0;
float4 texCoord1 : TEXCOORD1;
float4 color : COLOR0;
float Fog : FOG;
float3 Normal : TEXCOORD2;
float3 Reflection : TEXCOORD3;
};
struct ReflectionHighVSInput
{
float4 pos : POSITION;
float4 texCoord0 : TEXCOORD0;
float3 Normal : NORMAL0;
};
ReflectionHighVSOutput ReflectionHighVS(const ReflectionHighVSInput input)
{
ReflectionHighVSOutput output = (ReflectionHighVSOutput)0;
output.pos = mul(input.pos, g_WorldTransforms.WorldViewProjection);
output.Normal = normalize(input.Normal);
float3 normal = normalize(mul(input.Normal, g_WorldTransforms.WorldInverseTranspose));
float3 viewDirection = g_ViewTransforms.CameraPosition - mul(input.pos, g_WorldTransforms.World);
output.Reflection = reflect(-normalize(viewDirection), normal);
return output;
}
float4 ReflectionHighPS(ReflectionHighVSOutput input) : COLOR0
{
return texCUBE(g_EnvironmentSampler, normalize(input.Reflection));
}
technique ReflectionHigh
{
pass P0
{
VertexShader = compile vs_2_0 ReflectionHighVS();
PixelShader = compile ps_2_0 ReflectionHighPS();
//VertexShader = compile vs_2_0 MainVS();
//PixelShader = compile ps_2_0 MainPS();
}
}