2020-11-09 03:02:39 +01:00
|
|
|
#include "../fxh/Constants.fxh"
|
|
|
|
#include "../fxh/Lighting.fxh"
|
2020-12-01 02:40:33 +01:00
|
|
|
#include "../fxh/Transform.fxh"
|
|
|
|
#include "../fxh/Fog.fxh"
|
2020-11-05 11:17:12 +01:00
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
shared texture g_Texture0;
|
|
|
|
shared texture g_Texture1;
|
2020-11-09 03:02:39 +01:00
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
shared DirectionalLightInfo g_DirectionalLights;
|
2020-11-09 03:02:39 +01:00
|
|
|
shared Material g_Material;
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
shared WorldTransforms g_WorldTransforms;
|
|
|
|
shared float4x4 g_EnvironmentTextureTransform;
|
2020-11-09 03:02:39 +01:00
|
|
|
|
|
|
|
shared float4 g_TextureFactor;
|
2020-12-01 02:40:33 +01:00
|
|
|
shared BlendStageInfo g_BlendStages;
|
|
|
|
|
|
|
|
shared ViewTransforms g_ViewTransforms;
|
|
|
|
|
|
|
|
shared ColorMixMode g_ColorMixMode;
|
2020-11-09 03:02:39 +01:00
|
|
|
|
|
|
|
sampler g_ObjTextureSampler : register(s0) =
|
2020-11-05 11:17:12 +01:00
|
|
|
sampler_state
|
|
|
|
{
|
2020-11-09 03:02:39 +01:00
|
|
|
Texture = <g_Texture0>;
|
2020-11-05 11:17:12 +01:00
|
|
|
MipFilter = LINEAR;
|
|
|
|
MinFilter = LINEAR;
|
|
|
|
MagFilter = LINEAR;
|
|
|
|
};
|
2020-11-03 10:46:00 +01:00
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
sampler g_EnvTextureSampler =
|
|
|
|
sampler_state
|
2020-11-03 10:46:00 +01:00
|
|
|
{
|
2020-12-01 02:40:33 +01:00
|
|
|
Texture = <g_Texture1>;
|
|
|
|
MipFilter = LINEAR;
|
|
|
|
MinFilter = LINEAR;
|
|
|
|
MagFilter = LINEAR;
|
|
|
|
};
|
2020-11-03 10:46:00 +01:00
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
shared FogParams g_Fog;
|
2020-11-03 10:46:00 +01:00
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
// =======================================================
|
|
|
|
// Pixel and vertex lighting techniques
|
|
|
|
//
|
2020-11-03 10:46:00 +01:00
|
|
|
|
|
|
|
Lighting DoDirectionalLight(float3 worldPos, float3 N, int i)
|
|
|
|
{
|
|
|
|
Lighting Out;
|
2020-12-01 02:40:33 +01:00
|
|
|
Out.Diffuse = CalculateDirectionalDiffuse(
|
2020-11-05 11:17:12 +01:00
|
|
|
N,
|
2020-12-01 02:40:33 +01:00
|
|
|
-g_DirectionalLights.Direction[i],
|
|
|
|
g_DirectionalLights.Diffuse[i]);
|
|
|
|
Out.Specular = CalculateDirectionalSpecular(
|
2020-11-05 11:17:12 +01:00
|
|
|
worldPos,
|
2020-12-01 02:40:33 +01:00
|
|
|
g_ViewTransforms.CameraPosition.xyz,
|
2020-11-05 11:17:12 +01:00
|
|
|
N,
|
2020-12-01 02:40:33 +01:00
|
|
|
-g_DirectionalLights.Direction[i],
|
|
|
|
g_DirectionalLights.Specular[i],
|
|
|
|
g_Material.Power);
|
2020-11-03 10:46:00 +01:00
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
Lighting ComputeLighting(float3 worldPos, float3 N)
|
|
|
|
{
|
|
|
|
Lighting finalLighting = (Lighting)0;
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
for (int i = 0; i < g_DirectionalLights.Count; i++)
|
2020-11-03 10:46:00 +01:00
|
|
|
{
|
2020-11-05 11:17:12 +01:00
|
|
|
Lighting lighting = DoDirectionalLight(worldPos, N, i);
|
|
|
|
finalLighting.Diffuse += lighting.Diffuse;
|
|
|
|
finalLighting.Specular += lighting.Specular;
|
2020-11-03 10:46:00 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
float4 ambient = g_Material.Ambient * g_DirectionalLights.Ambient;
|
2020-11-03 10:46:00 +01:00
|
|
|
float4 diffuse = g_Material.Diffuse * finalLighting.Diffuse;
|
|
|
|
float4 specular = g_Material.Specular * finalLighting.Specular;
|
|
|
|
|
2020-11-09 03:02:39 +01:00
|
|
|
finalLighting.Diffuse = saturate(ambient + diffuse + g_Material.Emissive);
|
2020-11-03 10:46:00 +01:00
|
|
|
finalLighting.Specular = saturate(specular);
|
|
|
|
|
|
|
|
return finalLighting;
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:17:12 +01:00
|
|
|
struct PixelLightingVSOutput
|
|
|
|
{
|
|
|
|
float4 Pos : POSITION;
|
|
|
|
float2 Tex0 : TEXCOORD0;
|
|
|
|
float3 Normal : TEXCOORD1;
|
|
|
|
float3 WorldPos : TEXCOORD2;
|
2020-12-01 02:40:33 +01:00
|
|
|
float3 EnvMapTex : TEXCOORD3;
|
|
|
|
float4 BumpColor : TEXCOORD4;
|
|
|
|
float Fog : FOG;
|
2020-11-05 11:17:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
float4 GetColorArg(int colorArg, float4 textureColor, float4 diffuseColor)
|
|
|
|
{
|
|
|
|
float4 result;
|
|
|
|
if (colorArg == D3DTA_TEXTURE) result = textureColor;
|
|
|
|
else if (colorArg == D3DTA_DIFFUSE) result = diffuseColor;
|
2020-11-09 03:02:39 +01:00
|
|
|
else if (colorArg == D3DTA_TFACTOR) result = g_TextureFactor;
|
2020-11-05 11:17:12 +01:00
|
|
|
else result = float4(1.f, 1.f, 1.f, 1.f);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 Modulate(int stageIndex, float4 textureColor, float4 diffuseColor, float factor)
|
|
|
|
{
|
2020-12-01 02:40:33 +01:00
|
|
|
float4 left = GetColorArg(g_BlendStages.BlendStages[stageIndex].colorArg1, textureColor, diffuseColor);
|
|
|
|
float4 right = GetColorArg(g_BlendStages.BlendStages[stageIndex].colorArg2, textureColor, diffuseColor);
|
2020-11-05 11:17:12 +01:00
|
|
|
|
|
|
|
return (left * right) * factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 ProcessStages(float4 textureColor, float4 diffuseColor)
|
|
|
|
{
|
|
|
|
float4 output = 0;
|
2020-12-01 02:40:33 +01:00
|
|
|
for (int i = 0; i < g_BlendStages.Count; i++)
|
2020-11-05 11:17:12 +01:00
|
|
|
{
|
2020-12-01 02:40:33 +01:00
|
|
|
if (g_BlendStages.BlendStages[i].colorOp == D3DTOP_MODULATE4X || g_BlendStages.BlendStages[i].colorOp == D3DTOP_MODULATE)
|
2020-11-05 11:17:12 +01:00
|
|
|
{
|
2020-12-01 02:40:33 +01:00
|
|
|
float modulateFactor =
|
|
|
|
(4.0f * (g_BlendStages.BlendStages[i].colorOp == D3DTOP_MODULATE4X)) +
|
|
|
|
(1.0f * (g_BlendStages.BlendStages[i].colorOp == D3DTOP_MODULATE));
|
|
|
|
|
|
|
|
output += Modulate(i, textureColor, diffuseColor, modulateFactor);
|
2020-11-05 11:17:12 +01:00
|
|
|
}
|
2020-12-01 02:40:33 +01:00
|
|
|
else if (g_BlendStages.BlendStages[i].colorOp == D3DTOP_DOTPRODUCT3)
|
2020-11-05 11:17:12 +01:00
|
|
|
{
|
2020-12-01 02:40:33 +01:00
|
|
|
output = float4(1, 0, 0, 1);
|
2020-11-05 11:17:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PixelLightingPSOutput
|
|
|
|
{
|
|
|
|
float4 Diffuse : COLOR0;
|
|
|
|
};
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
PixelLightingVSOutput PixelLightingVS(
|
|
|
|
float4 vPosition : POSITION0,
|
|
|
|
float3 vNormal : NORMAL0,
|
|
|
|
float4 color : COLOR0,
|
|
|
|
float2 tc : TEXCOORD0)
|
|
|
|
{
|
|
|
|
// Simple transform, pre-compute as much as we can for the pixel shader
|
|
|
|
PixelLightingVSOutput output = (PixelLightingVSOutput)0;
|
|
|
|
|
|
|
|
vNormal = normalize(vNormal);
|
|
|
|
output.Pos = mul(vPosition, g_WorldTransforms.WorldViewProjection);
|
|
|
|
output.Normal = mul(vNormal, (float3x3)g_WorldTransforms.World);
|
|
|
|
output.WorldPos = mul(vPosition, g_WorldTransforms.World);
|
|
|
|
output.Tex0 = tc;
|
|
|
|
output.BumpColor = color;
|
|
|
|
|
|
|
|
float3 P = mul(vPosition, g_WorldTransforms.WorldView);
|
|
|
|
float d = length(P);
|
|
|
|
output.Fog = CalculateFogFactor(g_Fog.FogMax, g_Fog.FogMin, d);
|
|
|
|
|
|
|
|
if (g_ColorMixMode.EnvironmentMappingEnabled)
|
|
|
|
{
|
|
|
|
// Generate cube texture coordinates
|
|
|
|
// DX9 FFP formula: R = 2(E dot N) * N - E
|
|
|
|
float3 E = normalize(g_ViewTransforms.CameraPosition.xyz - output.WorldPos);
|
|
|
|
float3 N = mul(vNormal, (float3x3)g_WorldTransforms.WorldView);
|
|
|
|
float4 R = float4((2.f * dot(E, N) * N - E), 0);
|
|
|
|
output.EnvMapTex = mul(g_EnvironmentTextureTransform, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 PixelLightingPS(PixelLightingVSOutput input) : COLOR0
|
2020-11-05 11:17:12 +01:00
|
|
|
{
|
|
|
|
float4 color = tex2D(g_ObjTextureSampler, input.Tex0);
|
|
|
|
|
|
|
|
Lighting lighting = ComputeLighting(input.WorldPos, input.Normal);
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
// Emulate FFP texture stages
|
|
|
|
float4 finalColor = ProcessStages(color, lighting.Diffuse) + lighting.Specular;
|
|
|
|
|
|
|
|
// Apply cubic environment mapping if enabled
|
|
|
|
if (g_ColorMixMode.EnvironmentMappingEnabled)
|
|
|
|
{
|
|
|
|
float4 envMapColor = texCUBE(g_EnvTextureSampler, input.EnvMapTex);
|
|
|
|
|
|
|
|
if (g_BlendStages.BlendStages[1].colorOp == D3DTOP_BLENDFACTORALPHA)
|
|
|
|
finalColor = (finalColor * g_TextureFactor.a) + (envMapColor * (1 - g_TextureFactor.a));
|
|
|
|
else if (g_BlendStages.BlendStages[1].colorOp == D3DTOP_BLENDCURRENTALPHA)
|
|
|
|
finalColor = float4(1, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply linear pixel fog
|
|
|
|
if (g_Fog.Enabled)
|
|
|
|
{
|
|
|
|
finalColor = ApplyPixelFog(finalColor, input.Fog, g_Fog.Color);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (input.BumpColor.r != 0)
|
|
|
|
{
|
|
|
|
finalColor = float4(1, 1, 1, 1);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return finalColor;
|
2020-11-05 11:17:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
technique PixelLighting
|
|
|
|
{
|
|
|
|
pass P0
|
|
|
|
{
|
|
|
|
PixelShader = compile ps_3_0 PixelLightingPS();
|
|
|
|
VertexShader = compile vs_3_0 PixelLightingVS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:02:39 +01:00
|
|
|
struct VertexLightingVSOutput
|
|
|
|
{
|
|
|
|
float4 Pos : POSITION;
|
|
|
|
float4 Diffuse : COLOR0;
|
|
|
|
float4 Specular : COLOR1;
|
|
|
|
float2 Tex0 : TEXCOORD0;
|
|
|
|
float4 BumpColor : TEXCOORD1;
|
|
|
|
float Fog : FOG;
|
|
|
|
};
|
|
|
|
|
|
|
|
VertexLightingVSOutput VertexLightingVS(
|
|
|
|
float4 vPosition : POSITION0,
|
|
|
|
float3 vNormal : NORMAL0,
|
|
|
|
float4 color : COLOR0,
|
2020-12-01 02:40:33 +01:00
|
|
|
float2 tc : TEXCOORD0,
|
|
|
|
float fog : FOG)
|
2020-11-09 03:02:39 +01:00
|
|
|
{
|
|
|
|
VertexLightingVSOutput output;
|
2020-12-01 02:40:33 +01:00
|
|
|
output.Pos = mul(vPosition, g_WorldTransforms.WorldViewProjection);
|
2020-11-09 03:02:39 +01:00
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
float4 worldPos = mul(vPosition, g_WorldTransforms.World);
|
|
|
|
float3 normal = mul(normalize(vNormal), (float3x3)g_WorldTransforms.World);
|
2020-11-09 03:02:39 +01:00
|
|
|
|
|
|
|
Lighting lighting = ComputeLighting(worldPos, normal);
|
|
|
|
|
|
|
|
output.Diffuse = lighting.Diffuse;
|
|
|
|
output.Specular = lighting.Specular;
|
|
|
|
output.BumpColor = color;
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
output.Fog = fog;
|
|
|
|
output.Tex0 = tc;
|
2020-11-09 03:02:39 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
technique VertexLighting
|
|
|
|
{
|
|
|
|
pass P0
|
|
|
|
{
|
2020-12-01 02:40:33 +01:00
|
|
|
VertexShader = compile vs_2_0 VertexLightingVS();
|
2020-11-09 03:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:17:12 +01:00
|
|
|
// =======================================================
|
|
|
|
// Color per vertex
|
2020-11-03 10:46:00 +01:00
|
|
|
//
|
2020-11-05 11:17:12 +01:00
|
|
|
|
|
|
|
struct ColorPerVertexVSOutput
|
|
|
|
{
|
|
|
|
float4 Pos : POSITION;
|
|
|
|
float2 Tex0 : TEXCOORD0;
|
|
|
|
float4 Color : COLOR0;
|
2020-11-06 20:22:56 +01:00
|
|
|
float Fog : FOG;
|
2020-11-05 11:17:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0
|
|
|
|
{
|
|
|
|
float4 color = tex2D(g_ObjTextureSampler, input.Tex0); //* input.Color;
|
|
|
|
return color;
|
2020-11-03 10:46:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 11:17:12 +01:00
|
|
|
ColorPerVertexVSOutput ColorPerVertexVS(
|
|
|
|
float4 vPosition : POSITION0,
|
|
|
|
float2 tc : TEXCOORD0,
|
2020-11-06 20:22:56 +01:00
|
|
|
float4 color : COLOR0,
|
|
|
|
float fog : FOG)
|
2020-11-03 10:46:00 +01:00
|
|
|
{
|
2020-11-05 11:17:12 +01:00
|
|
|
// Simple transform, pre-compute as much as we can for the pixel shader
|
|
|
|
ColorPerVertexVSOutput output = (ColorPerVertexVSOutput)0;
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
output.Pos = mul(vPosition, g_WorldTransforms.WorldViewProjection);
|
2020-11-05 11:17:12 +01:00
|
|
|
output.Tex0 = tc;
|
|
|
|
output.Color = color;
|
2020-11-06 20:22:56 +01:00
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
float3 P = mul(vPosition, g_WorldTransforms.WorldView); //position in view space
|
2020-11-06 20:22:56 +01:00
|
|
|
float d = length(P);
|
|
|
|
|
2020-12-01 02:40:33 +01:00
|
|
|
output.Fog = fog;
|
2020-11-05 11:17:12 +01:00
|
|
|
return output;
|
2020-11-03 10:46:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 11:17:12 +01:00
|
|
|
technique ColorPerVertex
|
2020-11-03 10:46:00 +01:00
|
|
|
{
|
2020-11-05 11:17:12 +01:00
|
|
|
pass P0
|
|
|
|
{
|
|
|
|
//PixelShader = compile ps_3_0 ColorPerVertexPS();
|
2020-12-01 02:40:33 +01:00
|
|
|
VertexShader = compile vs_2_0 ColorPerVertexVS();
|
2020-11-05 11:17:12 +01:00
|
|
|
}
|
2020-11-03 10:46:00 +01:00
|
|
|
}
|