Search code examples
algorithmmathtexturesprocedural-generation

Clarity in Procedural Texture Algorithms?


In the Big Picture Section of this page here a table is given for comparing different combinations of 3 different functions. Let the function in the left be y = f(x) then what about the functions Average, Difference, Weighted Sum, 4% Threshold ? I need the mathematical equation in terms of y


Solution

  • Everything is explained on that page :

    Here are some simple, boring functions which, when repeatedly combined with smaller and smaller versions of themselves, create very interesting patterns. The table below shows you the basic source pattern (left), and combinations of that pattern with smaller versions of itself using various combination methods.

    Average (1/n) - This is simply the average of all of the scales being used, 'n' is the total number of scales. So if there are 6 scales, each scale contributes about 16% (1/6th) of the final value.

    Difference - This uses the difference between the color values of each scale as the final texture color.

    Weighted Sum (1/2^n) - The weighted sum is very similar to the average, except the larger scales have more weight. As 'n' increases, the contribution of that scale is lessened. The smallest scales (highest value of n) have the least effect. This method is the most common and typically the most visually pleasing.

    4% Threshold - This is a version of the Weighted Sum where anything below 48% gray is turned black, and anything above 52% gray is turned white.

    Let us take the Average and checker function. You are averaging a number of repeating different images, 6 in their example, but 3 in the following example :

    3 frequencies of checkerboard average

    So each pixel of the output image is the average value of the pixel values from the other images. You can have as many of these images as you want, and they are always built the same way : the image at level n is made of 4 tiles which are the image at level n-1 scaled to a fourth of its size. Then from all these pictures you apply one of the above functions to get only one.

    Is it clearer now ? It is, however, generally hard to give a function f that defines each image. However, the "compounding" functions are defined even though there are n inputs (xs) for 1 output (y = f(x1, x2, ....xn)) in pseudocode and math :

    • Average (1/n) - For n levels, final_pixel[x][y] = sum for i from 1 to n of image_i[x][y]/n

      math formula of average

    • Difference - For n levels, final_pixel[x][y] = sum for i from 2 to n of to n of image_i[x][y] - image_i-1[x][y] -- Not entirely sure about this one.

      math formula of sum of differences

    • Weighted Sum (1/2^n) - For n levels, final_pixel[x][y] = sum for i from 1 to n of image_i[x][y]/(2**n)

      math formula of wieghted sum

    • 4% Threshold - For n levels,

    value = sum for i from 1 to n of image_i[x][y]/(2**n)
    if value/max_value > .52 then final_pixel[x][y]=white 
    else if value/max_value < .48 then final_pixel[x][y]=black;
    else final_pixel[x][y]=value
    

    Where 2**n is 2 to the power of n.