Skip to content

Commit afa3c44

Browse files
authored
Shader cleanup (FAForever#6452)
I had a look at the shaders again for integration in the map editor. During this I noticed some improvements that should be made to make the code more readable
1 parent fcbe00c commit afa3c44

File tree

2 files changed

+59
-70
lines changed

2 files changed

+59
-70
lines changed

effects/terrain.fx

+48-48
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ float WaterElevation;
3434
float WaterElevationDeep;
3535
float WaterElevationAbyss;
3636

37-
// masks of stratum layers 1 - 4
37+
// masks of stratum layers 0 - 3 (lower stratum has no mask)
3838
texture UtilityTextureA;
3939

40-
// masks of stratum layers 5 - 8
40+
// masks of stratum layers 4 - 7
4141
texture UtilityTextureB;
4242

4343
// red: wave normal strength
@@ -389,16 +389,16 @@ float ComputeShadow( float4 vShadowCoord )
389389
}
390390

391391
// apply the water color
392-
float3 ApplyWaterColor(float terrainHeight, float depth, float3 inColor)
392+
float3 ApplyWaterColor(float terrainHeight, float waterDepth, float3 color)
393393
{
394-
if (depth > 0) {
394+
if (waterDepth > 0) {
395395
// With this extra check we get rid of unwanted coloration on steep cliffs when zoomed in,
396396
// but we prevent that terrain tesselation swallows too much of the water when zoomed out
397397
float opacity = saturate(smoothstep(10, 200, CameraPosition.y - WaterElevation) + step(terrainHeight, WaterElevation));
398-
float4 wcolor = tex1D(WaterRampSampler, depth);
399-
inColor = lerp(inColor.xyz, wcolor.xyz, wcolor.w * opacity);
398+
float4 waterColor = tex1D(WaterRampSampler, waterDepth);
399+
color = lerp(color.xyz, waterColor.rgb, waterColor.a * opacity);
400400
}
401-
return inColor;
401+
return color;
402402
}
403403

404404
float3 ApplyWaterColorExponentially(float3 viewDirection, float terrainHeight, float waterDepth, float3 color)
@@ -422,19 +422,19 @@ float3 ApplyWaterColorExponentially(float3 viewDirection, float terrainHeight, f
422422
}
423423

424424
// calculate the lit pixels
425-
float4 CalculateLighting( float3 inNormal, float3 inViewPosition, float3 inAlbedo, float specAmount, float waterDepth, float4 inShadow, uniform bool inShadows)
425+
float4 CalculateLighting( float3 inNormal, float3 worldTerrain, float3 inAlbedo, float specAmount, float waterDepth, float4 shadowCoords, uniform bool inShadows)
426426
{
427427
float4 color = float4( 0, 0, 0, 0 );
428428

429-
float shadow = ( inShadows && ( 1 == ShadowsEnabled ) ) ? ComputeShadow( inShadow ) : 1;
429+
float shadow = ( inShadows && ( 1 == ShadowsEnabled ) ) ? ComputeShadow(shadowCoords) : 1;
430430
if (IsExperimentalShader()) {
431-
float3 position = TerrainScale * inViewPosition;
431+
float3 position = TerrainScale * worldTerrain;
432432
float mapShadow = tex2D(UpperAlbedoSampler, position.xy).w;
433433
shadow = shadow * mapShadow;
434434
}
435435

436436
// calculate some specular
437-
float3 viewDirection = normalize(inViewPosition.xzy-CameraPosition);
437+
float3 viewDirection = normalize(worldTerrain.xzy-CameraPosition);
438438

439439
float SunDotNormal = dot( SunDirection, inNormal);
440440
float3 R = SunDirection - 2.0f * SunDotNormal * inNormal;
@@ -445,9 +445,9 @@ float4 CalculateLighting( float3 inNormal, float3 inViewPosition, float3 inAlbed
445445
color.rgb = light * inAlbedo;
446446

447447
if (IsExperimentalShader()) {
448-
color.rgb = ApplyWaterColorExponentially(-viewDirection, inViewPosition.z, waterDepth, color);
448+
color.rgb = ApplyWaterColorExponentially(-viewDirection, worldTerrain.z, waterDepth, color);
449449
} else {
450-
color.rgb = ApplyWaterColor(inViewPosition.z, waterDepth, color);
450+
color.rgb = ApplyWaterColor(worldTerrain.z, waterDepth, color);
451451
}
452452

453453
color.a = 0.01f + (specular*SpecularColor.w);
@@ -782,11 +782,11 @@ float4 TerrainNormalsPS( VS_OUTPUT inV ) : COLOR
782782
// sample all the textures we'll need
783783
float4 mask = saturate(tex2D( UtilitySamplerA, inV.mTexWT * TerrainScale));
784784

785-
float4 lowerNormal = normalize(tex2D( LowerNormalSampler, inV.mTexWT * TerrainScale * LowerNormalTile ) * 2 - 1);
786-
float4 stratum0Normal = normalize(tex2D( Stratum0NormalSampler, inV.mTexWT * TerrainScale * Stratum0NormalTile ) * 2 - 1);
787-
float4 stratum1Normal = normalize(tex2D( Stratum1NormalSampler, inV.mTexWT * TerrainScale * Stratum1NormalTile ) * 2 - 1);
788-
float4 stratum2Normal = normalize(tex2D( Stratum2NormalSampler, inV.mTexWT * TerrainScale * Stratum2NormalTile ) * 2 - 1);
789-
float4 stratum3Normal = normalize(tex2D( Stratum3NormalSampler, inV.mTexWT * TerrainScale * Stratum3NormalTile ) * 2 - 1);
785+
float4 lowerNormal = normalize(tex2D( LowerNormalSampler, inV.mTexWT * TerrainScale * LowerNormalTile ) * 2 - 1);
786+
float4 stratum0Normal = normalize(tex2D(Stratum0NormalSampler, inV.mTexWT * TerrainScale * Stratum0NormalTile) * 2 - 1);
787+
float4 stratum1Normal = normalize(tex2D(Stratum1NormalSampler, inV.mTexWT * TerrainScale * Stratum1NormalTile) * 2 - 1);
788+
float4 stratum2Normal = normalize(tex2D(Stratum2NormalSampler, inV.mTexWT * TerrainScale * Stratum2NormalTile) * 2 - 1);
789+
float4 stratum3Normal = normalize(tex2D(Stratum3NormalSampler, inV.mTexWT * TerrainScale * Stratum3NormalTile) * 2 - 1);
790790

791791
// blend all normals together
792792
float4 normal = lowerNormal;
@@ -890,13 +890,13 @@ float4 TerrainBasisPSBiCubic( VS_OUTPUT inV ) : COLOR
890890
float4 TerrainPS( VS_OUTPUT inV, uniform bool inShadows ) : COLOR
891891
{
892892
// sample all the textures we'll need
893-
float4 mask = saturate(tex2Dproj( UtilitySamplerA, inV.mTexWT * TerrainScale)* 2 - 1 );
894-
float4 upperAlbedo = tex2Dproj( UpperAlbedoSampler, inV.mTexWT * TerrainScale* UpperAlbedoTile );
895-
float4 lowerAlbedo = tex2Dproj( LowerAlbedoSampler, inV.mTexWT * TerrainScale* LowerAlbedoTile );
896-
float4 stratum0Albedo = tex2Dproj( Stratum0AlbedoSampler, inV.mTexWT * TerrainScale* Stratum0AlbedoTile );
897-
float4 stratum1Albedo = tex2Dproj( Stratum1AlbedoSampler, inV.mTexWT * TerrainScale* Stratum1AlbedoTile );
898-
float4 stratum2Albedo = tex2Dproj( Stratum2AlbedoSampler, inV.mTexWT * TerrainScale* Stratum2AlbedoTile );
899-
float4 stratum3Albedo = tex2Dproj( Stratum3AlbedoSampler, inV.mTexWT * TerrainScale* Stratum3AlbedoTile );
893+
float4 mask = saturate(tex2D( UtilitySamplerA, inV.mTexWT * TerrainScale) * 2 - 1);
894+
float4 upperAlbedo = tex2D( UpperAlbedoSampler, inV.mTexWT * TerrainScale * UpperAlbedoTile);
895+
float4 lowerAlbedo = tex2D( LowerAlbedoSampler, inV.mTexWT * TerrainScale * LowerAlbedoTile);
896+
float4 stratum0Albedo = tex2D(Stratum0AlbedoSampler, inV.mTexWT * TerrainScale * Stratum0AlbedoTile);
897+
float4 stratum1Albedo = tex2D(Stratum1AlbedoSampler, inV.mTexWT * TerrainScale * Stratum1AlbedoTile);
898+
float4 stratum2Albedo = tex2D(Stratum2AlbedoSampler, inV.mTexWT * TerrainScale * Stratum2AlbedoTile);
899+
float4 stratum3Albedo = tex2D(Stratum3AlbedoSampler, inV.mTexWT * TerrainScale * Stratum3AlbedoTile);
900900

901901
float3 normal = SampleScreen(NormalSampler, inV.mTexSS).xyz*2-1;
902902

@@ -909,7 +909,7 @@ float4 TerrainPS( VS_OUTPUT inV, uniform bool inShadows ) : COLOR
909909
albedo.xyz = lerp( albedo.xyz, upperAlbedo.xyz, upperAlbedo.w );
910910

911911
// get the water depth
912-
float waterDepth = tex2Dproj( UtilitySamplerC, inV.mTexWT * TerrainScale).g;
912+
float waterDepth = tex2D( UtilitySamplerC, inV.mTexWT * TerrainScale).g;
913913

914914
// calculate the lit pixel
915915
float4 outColor = CalculateLighting( normal, inV.mTexWT.xyz, albedo.xyz, 1-albedo.w, waterDepth, inV.mShadow, inShadows);
@@ -921,19 +921,19 @@ float4 TerrainAlbedoXP( VS_OUTPUT pixel) : COLOR
921921
{
922922
float4 position = TerrainScale*pixel.mTexWT;
923923

924-
float4 mask0 = saturate(tex2Dproj(UtilitySamplerA,position)*2-1);
925-
float4 mask1 = saturate(tex2Dproj(UtilitySamplerB,position)*2-1);
924+
float4 mask0 = saturate(tex2D(UtilitySamplerA,position)*2-1);
925+
float4 mask1 = saturate(tex2D(UtilitySamplerB,position)*2-1);
926926

927-
float4 lowerAlbedo = tex2Dproj(LowerAlbedoSampler,position*LowerAlbedoTile);
928-
float4 stratum0Albedo = tex2Dproj(Stratum0AlbedoSampler,position*Stratum0AlbedoTile);
929-
float4 stratum1Albedo = tex2Dproj(Stratum1AlbedoSampler,position*Stratum1AlbedoTile);
930-
float4 stratum2Albedo = tex2Dproj(Stratum2AlbedoSampler,position*Stratum2AlbedoTile);
931-
float4 stratum3Albedo = tex2Dproj(Stratum3AlbedoSampler,position*Stratum3AlbedoTile);
932-
float4 stratum4Albedo = tex2Dproj(Stratum4AlbedoSampler,position*Stratum4AlbedoTile);
933-
float4 stratum5Albedo = tex2Dproj(Stratum5AlbedoSampler,position*Stratum5AlbedoTile);
934-
float4 stratum6Albedo = tex2Dproj(Stratum6AlbedoSampler,position*Stratum6AlbedoTile);
935-
float4 stratum7Albedo = tex2Dproj(Stratum7AlbedoSampler,position*Stratum7AlbedoTile);
936-
float4 upperAlbedo = tex2Dproj(UpperAlbedoSampler,position*UpperAlbedoTile);
927+
float4 lowerAlbedo = tex2D(LowerAlbedoSampler,position*LowerAlbedoTile);
928+
float4 stratum0Albedo = tex2D(Stratum0AlbedoSampler,position*Stratum0AlbedoTile);
929+
float4 stratum1Albedo = tex2D(Stratum1AlbedoSampler,position*Stratum1AlbedoTile);
930+
float4 stratum2Albedo = tex2D(Stratum2AlbedoSampler,position*Stratum2AlbedoTile);
931+
float4 stratum3Albedo = tex2D(Stratum3AlbedoSampler,position*Stratum3AlbedoTile);
932+
float4 stratum4Albedo = tex2D(Stratum4AlbedoSampler,position*Stratum4AlbedoTile);
933+
float4 stratum5Albedo = tex2D(Stratum5AlbedoSampler,position*Stratum5AlbedoTile);
934+
float4 stratum6Albedo = tex2D(Stratum6AlbedoSampler,position*Stratum6AlbedoTile);
935+
float4 stratum7Albedo = tex2D(Stratum7AlbedoSampler,position*Stratum7AlbedoTile);
936+
float4 upperAlbedo = tex2D(UpperAlbedoSampler,position*UpperAlbedoTile);
937937

938938
float4 albedo = lowerAlbedo;
939939
albedo = lerp(albedo,stratum0Albedo,mask0.x);
@@ -958,7 +958,7 @@ float4 TerrainAlbedoXP( VS_OUTPUT pixel) : COLOR
958958
light = LightingMultiplier*light + ShadowFillColor*(1-light);
959959
albedo.rgb = light * ( albedo.rgb + specular.rgb );
960960

961-
float waterDepth = tex2Dproj(UtilitySamplerC,pixel.mTexWT*TerrainScale).g;
961+
float waterDepth = tex2D(UtilitySamplerC,pixel.mTexWT*TerrainScale).g;
962962
albedo.rgb = ApplyWaterColor(pixel.mTexWT.z, waterDepth, albedo.rgb);
963963

964964
return float4(albedo.rgb, 0.01f);
@@ -2075,17 +2075,17 @@ float4 TerrainPBRAlbedoPS ( VS_OUTPUT inV) : COLOR
20752075
// Layer| Albedo stratum | Normal stratum
20762076
// | R | G | B | A | R | G | B | A |
20772077
// ---- --- --- --- --- --- --- --- ---
2078-
// | L | R G B unused | X Y Z unused |
2078+
// | L | R G B specularity | X Y Z unused |
20792079
// ---- --- --- --- --- --- --- --- ---
2080-
// | S0 | R G B unused | X Y Z unused |
2081-
// | S1 | R G B unused | X Y Z unused |
2082-
// | S2 | R G B unused | X Y Z unused |
2083-
// | S3 | R G B unused | X Y Z unused |
2080+
// | S0 | R G B specularity | X Y Z unused |
2081+
// | S1 | R G B specularity | X Y Z unused |
2082+
// | S2 | R G B specularity | X Y Z unused |
2083+
// | S3 | R G B specularity | X Y Z unused |
20842084
// ---- --- --- --- --- --- --- --- ---
2085-
// | S4 | R G B unused | X Y Z unused |
2086-
// | S5 | R G B unused | X Y Z unused |
2087-
// | S6 | R G B unused | X Y Z unused |
2088-
// | S7 | R G B unused | X Y Z unused |
2085+
// | S4 | R G B specularity | X Y Z unused |
2086+
// | S5 | R G B specularity | X Y Z unused |
2087+
// | S6 | R G B specularity | X Y Z unused |
2088+
// | S7 | R G B specularity | X Y Z unused |
20892089
// ---- --- --- --- --- --- --- --- ---
20902090
// | U | normal.x normal.z unused shadow |
20912091

effects/water2.fx

+11-22
Original file line numberDiff line numberDiff line change
@@ -131,35 +131,26 @@ sampler2D MaskSampler = sampler_state
131131
AddressV = CLAMP;
132132
};
133133

134-
//
135134
// surface water color
136-
//
137135
float3 waterColor = float3(0,0.7,1.5);
138136
float3 waterColorLowFi = float3(0.7647,0.8784,0.9647);
139137

140138
// these actually get overridden by the map
141139
float2 waterLerp = 0.3;
142140
float refractionScale = 0.015;
143141

144-
//
145142
// fresnel parameters
146143
// both are unused by the shaders
147-
//
148144
float fresnelBias = 0.1;
149145
float fresnelPower = 1.5;
150146

151147
// these actually get overridden by the map
152148
float unitreflectionAmount = 0.5;
153149
float skyreflectionAmount = 1.5;
154150

155-
156-
//
157151
// 4 repeat rates for the 4 wave texture layers
158-
//
159152
float4 normalRepeatRate = float4(0.0009, 0.009, 0.05, 0.5);
160153

161-
162-
//
163154
// 4 vectors of normal movements
164155
float2 normal1Movement = float2(0.5, -0.95);
165156
float2 normal2Movement = float2(0.05, -0.095);
@@ -176,9 +167,6 @@ float sunReflectionAmount = 5;
176167
// unused by the shaders
177168
float SunGlow;
178169

179-
///
180-
///
181-
///
182170

183171
struct LOWFIDELITY_VERTEX
184172
{
@@ -432,19 +420,19 @@ float4 HighFidelityPS( VS_OUTPUT inV,
432420

433421
// we need to exclude the unit refraction above the water line. This creats small areas with
434422
// no refraction, but the water color in the next step will make this mostly unnoticeable
435-
refractedPixels.xyz = lerp(refractedPixels, backGroundPixels, saturate(refractedPixels.a * 255)).xyz;
423+
refractedPixels.rgb = lerp(refractedPixels, backGroundPixels, saturate(refractedPixels.a * 255)).rgb;
436424
// we want to lerp in the water color based on depth, but clamped
437-
float waterLerp = clamp(waterDepth, waterLerp.x, waterLerp.y);
438-
refractedPixels.xyz = lerp(refractedPixels.xyz, waterColor, waterLerp);
425+
float factor = clamp(waterDepth, waterLerp.x, waterLerp.y);
426+
refractedPixels.rgb = lerp(refractedPixels.rgb, waterColor, factor);
439427

440428
// We can't compute wich part of the unit we would hit with our reflection vector,
441429
// so we have to resort to an approximation using the refractionPos
442-
float4 reflectedPixels = tex2D(ReflectionSampler, refractionPos);
430+
float4 unitReflections = tex2D(ReflectionSampler, refractionPos);
443431

444432
float4 skyReflection = texCUBE(SkySampler, R);
445433
// The alpha channel acts as a mask for unit parts above the water and probably
446434
// uses unitReflectionAmount as the positive value of the mask
447-
reflectedPixels.xyz = lerp(skyReflection.xyz, reflectedPixels.xyz, saturate(reflectedPixels.a));
435+
float3 reflections = lerp(skyReflection.rgb, unitReflections.rgb, saturate(unitReflections.a));
448436

449437
// Schlick approximation for fresnel
450438
float NDotV = saturate(dot(viewVector, N));
@@ -453,21 +441,22 @@ float4 HighFidelityPS( VS_OUTPUT inV,
453441
// the default value of 1.5 is way to high, but we want to preserve manually set values in existing maps
454442
if (skyreflectionAmount == 1.5)
455443
skyreflectionAmount = 1.0;
456-
refractedPixels = lerp(refractedPixels, reflectedPixels, saturate(fresnel * skyreflectionAmount));
444+
float3 water = lerp(refractedPixels, reflections, saturate(fresnel * skyreflectionAmount));
457445

458446
// add in the sun reflection
459447
float3 sunReflection = calculateSunReflection(R, viewVector, N);
460448
// the sun shouldn't be visible where a unit reflection is
461-
sunReflection *= (1 - saturate(reflectedPixels.a * 4));
449+
sunReflection *= (1 - saturate(unitReflections.a * 4));
462450
// we can control this value to have terrain cast a shadow on the water surface
463451
sunReflection *= waterTexture.r;
464-
refractedPixels.xyz += sunReflection;
452+
water += sunReflection;
465453

466454
// Lerp in the wave crests
467-
refractedPixels.xyz = lerp(refractedPixels.xyz, waveCrestColor, (1 - waterTexture.a) * (1 - waterTexture.b) * waveCrest);
455+
water = lerp(water, waveCrestColor, (1 - waterTexture.a) * (1 - waterTexture.b) * waveCrest);
468456

469457
// return the pixels masked out by the water mask
470-
float4 returnPixels = refractedPixels;
458+
float4 returnPixels;
459+
returnPixels.rgb = water;
471460
returnPixels.a = 1 - mask;
472461
return returnPixels;
473462
}

0 commit comments

Comments
 (0)