RSL

IntroductionThere are many ways that domelike protrusions can be generated by a displacement shader. This tutorial looks at a couple of procedural techniques that are intended to be a useful basis for a shader that generates water droplets of the type shown shown in figure 1, 2 and 3. 
Figure 1 Figure 2 
Figure 3 
This tutorial does not consider displacement mapping techniques nor
does it address issues of how water droplets might be rendered using a
surface shader.
FormulaFigure 4 shows the relationship between a line (chord) that cuts a circle. The cord divides the circle into two parts. The lower portion, labelled S for sagitta measures how much the circle "protrudes" beyond the cord. The height of the gray (rightangled triangle) can be calculated from the theorem of Pythagorus ie. sqrt(R * R  L * L)
Therefore, the sagitta is equal to the radius of the circle less the height of the gray triangle ie. S = R  sqrt(R * R  L * L) 
This formula forms the basis of another formula called the sphereometer formula that derives its name from the instrument that opticians use when they measure the depth of spherical mirrors.
The problem with the sphereometer formula is that it only provides a measure of the sagitta  just a single number. A displacement shader, on the other hand, will require a sequence of numbers (figure 5) that measure the depth of the circle at various distances from the central axis of the "dome". Rather than attempting to adapt the formula, a few shortcuts can be taken that that will enable a rounded dome to be produced even though, strickly speaking, it will have a slightly noncircular profile. 
Squash & Stretch SemiCircles
If we confine ourselves to a semicircle of radius "R", then calculating distance "d",
figure 6, for a particular value of "r" again can be done using the theorum
of Pythagorus (note the subtration) ie.
hump = sqrt(R * R  d * d)
If we do not wish to produce semicircular dome we can apply a scaling factor
(squash/stretch) each time we calculate "d". For our first attempt at applying
this to a displacement shader it will be assumed a dome, centered
in texture space ie. ' 
Listing 1

Listing 2

Listing 3

© 2002 Malcolm Kesson. All rights reserved.