How to add shadow effect to 2D fractals?

128 Views Asked by At

My question is simple: how do you add a shadow effect (like the one in Kalles Fraktaler 2)? I have tried distance estimation, but have failed at creating it reliably. I would like a relatively fast method, but it doesn't have to be extremely accurate.

Kalles Fraktaler render with shadow effect

1

There are 1 best solutions below

0
On BEST ANSWER

For a good description of the theory, with pseudo-code, see Arnaud Chéritat's wiki page on drawing the Mandelbrot set.

The implementation in Kalles Fraktaler 2 has two methods for calculating a 2D vector it calls (diffx, diffy) (which I will abbreviate $(x,y)$ later):

  • if derivatives have been calculated, it uses the components of the reciprocal of the directional (complex) distance estimate;

  • otherwise they're the difference in smooth iteration count between neighbouring pixels in the horizontal and vertical directions (w,h will be 1 for final colouring pass when all pixels have been evaluated).

Then the background colour is mixed with black or white according to a non-linearly scaled dot product of this vector with the light direction.

The parameters for the colouring are three real numbers SlopeAngle, SlopePower and SlopeRatio, which I will call $\theta$, $P$ and $R$ in the following, that modifies the input RGB $\mathbf{c}_i$ (calculated via other means, for example mapping iteration counts to colours, or via distance estimation) to give output RGB $\mathbf{c}_o$:

  1. $$ d = P \cdot \begin{pmatrix} x \\ y \end{pmatrix} \cdot \begin{pmatrix} \cos(\theta) \\ \sin(\theta) \end{pmatrix} $$

  2. $$ t = R \frac{\tan^{-1}(|d|)}{\pi / 2} $$

  3. if $d > 0$, $$\mathbf{c}_o = (1 - t) \mathbf{c}_i$$ if $d < 0$, $$\mathbf{c}_o = (1 - t) \mathbf{c}_i + t \begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix}$$