Maperitive: Alpenglow Effect Using A Custom Shader
The latest release comes with a new Python script called Alpenglow.py which renders a hillshading relief adding some yellowish tint to illuminated high slopes to enhance the 3D effect. It was inspired by the “See the light: How to make illuminated shaded relief in Photoshop 6.0" article on the excellent shadedrelief.com site.
You can run the script by executing
The script generates the hillshading for a fixed map area in Switzerland, but you can change this quite easily in the script. You can also change other parameters, like shading (and alpenglow) colors, elevation ranges etc.
The script also demonstrates the power of writing a custom shader. A shader transforms a small part of the digital elevation model (DEM) into a pixel color, based on the elevation, aspect and the slope of that part of the DEM. This way you can write your own hillshading effects function and then supply it to the generator. Here’s how it’s done for Alpenglow.py:
# Execute the hillshading
bitmap = ReliefUtils.generate_relief_bitmap(dem, relief_bbox, Srid.WebMercator, 1, alpenglow)
The last parameter of the generate_relief_bitmap function is the shader function, in our case called alpenglow:
# The actual shader function which is called for each pixel of the hillshading bitmap.
# It receives the elevation, the aspect (orientation) of the slope and the steepnes (slope).
def alpenglow(elevation, aspect, slope):
sin_slope = math.sin (slope)
nx = math.cos(aspect) * sin_slope
ny = math.sin(aspect) * sin_slope
nz = math.cos(slope)
direction = dotproduct(nx, ny, nz, lx, ly, lz)
direction = (direction - minDirection) / (1 - minDirection);
if direction < 0:
direction = 0
# This is how we can mix two colors together.
shade_color = shade_base_color.mix (light_color, direction)
if direction > 0.0 and elevation > min_alpenglow_elevation:
elevation_factor = min((elevation - min_alpenglow_elevation) / delta_alpenglow_elevation, 1);
alpenglow_factor = math.pow(direction, 4)
return shade_color.mix (alpenglow_color, elevation_factor * alpenglow_factor).argb;