One of the arguments is the actual player distance to the ground directly below him, which is unknown and could be anything from 'zero' to 'infinity,' but always positive since the player is always above the ground even when touching it.
The other argument is the distance from the ground that I want the shadow to be completely faded. Anything less than this, the shadow will be partially visible to some degree.
With these arguments, the function will return something between 'zero' and 'max_player_distance,' which is mapped to the [0, 1] region by dividing it by the biggest of the known values, the maximum player distance. This yields 'result.'
Since the shadow should be more transparent the higher the player is, the ideal alpha value is the unit complement of 'result,' instead of 'result' itself ( alpha = 1.0 - result ).
The alpha value is '1.0' when the player is on the ground (fully visible), and 'zero' when he's at the maximum distance or greater (fully transparent).
- - - - -
Another way would be to replace the Min( a, b ) function with a Clamp( val, a, b ) function, which clamps 'val' between the 'a' and 'b' range: Clamp( playerDistance, 1, 80 )
These are common numerical functions that should come with whatever engine or library that you're using, but in any case this is their source:
Function Min:Float( a:float, b:float ) 'Or the ternary operation: (a < b) : a ? b'
If a < b Return a
Return b
End Function
Function Clamp:Float( val:float, a:float, b:float ) 'Range parameter "a" is assumed to be smaller than range parameter "b."'
If val <= a Return a
If val >= b Return b
Return val
End Function