Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(w3ddevice): let metal mirror the sky

Olcay Seyganaddb1654aed0
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index d74f0b85c..66e1c39fe 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -48,6 +48,12 @@ static const float NORMAL_MAP_STRENGTH = 1.0f;
static const float NORMAL_MAP_HIGHLIGHT_POWER = 10.0f;
static const float NORMAL_MAP_HIGHLIGHT_SCALE = 0.9f;
+// How much of the sky a metal surface returns, and how dark the horizon is against straight up.
+// The sun's own dot on a hull is one small spot; the flank of a tank reads as metal because of what
+// it mirrors over its whole area, and from this camera that is nearly all sky.
+static const float SKY_REFLECTION_STRENGTH = 0.35f;
+static const float SKY_HORIZON_SHARE = 0.45f;
+
// The three stage counts are one count in three headers. The vertex constant block is copied
// wholesale out of the backend's own texture transforms, the generated pixel shader declares one
// sampler per texture the backend binds, and a mismatch is a silent overrun rather than a build
@@ -1630,6 +1636,25 @@ void DX11BackendClass::Upload_Constants()
pixel_block.ShadowSoftness[3] = ShadowSkyFill;
}
+ /* The sky a metal surface mirrors. There is no cubemap: the colour is the map's own sunlight,
+ which is what makes a night map's metal cold and a desert's warm without anything being
+ authored, and the direction it is brightest in is straight up in camera space. */
+ const float * sun_colour = Lights[0].Enabled ? Lights[0].Diffuse : NULL;
+ for (unsigned channel = 0; channel < 3; ++channel) {
+ pixel_block.Sky[channel] = (sun_colour != NULL) ? sun_colour[channel] : 1.0f;
+ }
+ pixel_block.Sky[3] = SKY_REFLECTION_STRENGTH;
+ const float world_up[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
+ transform_direction(world_up, View, pixel_block.SkyUp);
+ const float up_length = sqrtf(pixel_block.SkyUp[0] * pixel_block.SkyUp[0]
+ + pixel_block.SkyUp[1] * pixel_block.SkyUp[1] + pixel_block.SkyUp[2] * pixel_block.SkyUp[2]);
+ if (up_length > 0.0f) {
+ for (unsigned axis = 0; axis < 3; ++axis) {
+ pixel_block.SkyUp[axis] /= up_length;
+ }
+ }
+ pixel_block.SkyUp[3] = SKY_HORIZON_SHARE;
+
if ((!PixelConstantsHeld
|| memcmp(&HeldPixelConstants, &pixel_block, sizeof(pixel_block)) != 0)
&& SUCCEEDED(context->Map(PixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
index aa26be5b0..c20baa68f 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
@@ -289,6 +289,11 @@ private:
// caster and what it falls on, how many world units a unit of depth is, and how much of a
// wide shadow the sky fills back in.
float ShadowSoftness[4];
+ // What a metal surface mirrors: the map's own light as a sky colour with the share metal
+ // returns in its alpha, and which way up the world is in camera space with the horizon's
+ // share of the zenith colour in its own.
+ float Sky[4];
+ float SkyUp[4];
};
// A model under directional lights, drawn by generated programs.
bool Normal_Mapped() const;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
index 7dfec0c1c..b5bc32ffc 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
@@ -244,7 +244,9 @@ static void write_pixel_preamble(std::string & hlsl, bool bumped = false)
" row_major float4x4 ShadowFromClip;\n"
" float4 ShadowParameters;\n"
" float4 ShadowViewport;\n"
- " float4 ShadowSoftness;\n";
+ " float4 ShadowSoftness;\n"
+ " float4 Sky;\n"
+ " float4 SkyUp;\n";
hlsl += "};\n";
if (bumped) {
hlsl += "Texture2D NormalMap : register(t4);\n";
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
index d2a49d607..a8eadccf4 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
@@ -250,6 +250,17 @@ static void append_normal_mapped_lighting(std::string & hlsl, unsigned coordinat
" * pow(saturate(dot(bumped, normalize(to_light + to_eye))), NormalMapParameters.y);\n"
" }\n"
" highlight *= normal_texel.a * NormalMapParameters.z;\n"
+ /* What makes metal read as metal from a camera this far out is not the sun's own dot on it,
+ which is one small spot, but the sky it mirrors over its whole flank. There is no cubemap
+ here and none is needed: the sky over this game is a gradient and a sun, so the mirrored
+ direction is turned into a colour rather than looked up. Sky.rgb is the map's own light,
+ Sky.a how much of it metal returns, and the share grows towards the grazing angles where a
+ real surface turns into a mirror. The gloss map gates it, so cloth and sand get none. */
+ " float3 mirrored = reflect(-to_eye, bumped);\n"
+ " float sky_height = saturate(dot(mirrored, SkyUp.xyz) * 0.5 + 0.5);\n"
+ " float3 sky = lerp(Sky.rgb * SkyUp.w, Sky.rgb, sky_height);\n"
+ " float grazing = pow(1.0 - saturate(dot(bumped, to_eye)), 4.0);\n"
+ " highlight += sky * Sky.a * normal_texel.a * (0.3 + 0.7 * grazing);\n"
" input.Diffuse.rgb = saturate(input.LitMaterial * bumped_light + input.LitBase);\n",
coordinate_set, coordinate_set, coordinate_set, NORMAL_MAPPED_LIGHTS);
hlsl += line;
@@ -368,7 +379,7 @@ bool CombinerShader_Generate(const CombinerDescription & description, CombinerSh
NORMAL_MAPPED_LIGHTS, NORMAL_MAPPED_LIGHTS);
hlsl += line;
}
- if (description.ShadowReceiving) {
+ if (description.NormalMapped || description.ShadowReceiving) {
hlsl +=
" float4 TerrainSunDirection;\n"
// row_major for the reason the vertex half gives: every matrix the engine has is a
@@ -376,7 +387,11 @@ bool CombinerShader_Generate(const CombinerDescription & description, CombinerSh
" row_major float4x4 ShadowFromClip;\n"
" float4 ShadowParameters;\n"
" float4 ShadowViewport;\n"
- " float4 ShadowSoftness;\n";
+ " float4 ShadowSoftness;\n"
+ // The sky a metal surface mirrors, and which way up it is in camera space. Last in
+ // the block, so a program that wants it declares everything in front of it.
+ " float4 Sky;\n"
+ " float4 SkyUp;\n";
}
hlsl += "};\n";
if (description.NormalMapped) {