Question

I'm reading Shaders for Game Programming and Artists. In Chapter 13 "Building Materials from Scratch", the author introduced some render techniques to simulate complex materials such as marble or wood by using Perlin noise. But I'm puzzled by the wood rendering. To simulate the wood, we need a function gives a circular value along a specific plane so that we can create the rings in the woods. This is what the author said, "take the dot product of two axes along a plane, creating the circular value on that plane"

Circle = dot(noisetxr.xy, noisetxr.xy);

noisetxr is a float3, it's a texture coordinate to sample the noise texture, I can't understand why the dot product will gives a circular value
Here is the complete code(pixel shader in hlsl):

float persistance;
float4 wood_color;  //a predefined value
sampler Texture0;   // noise texture
float4 ps_main(float3 txr: TEXCOORD0) : COLOR 
{
   // Determine two set of coordinates, one for the noise
   // and one for the wood rings
   float3 noisetxr = txr;
   txr = txr/8;

   // Combine 3 octaves of noise together.
   float final_noise = 0;
   for(int i=0;i<2;i++)
      final_noise += ((1.0/pow(persistance,i))*
        ((tex3D(Texture0, txr*pow(2,i))*2)-1));

   // The wood is defined by a set of concentric rings in the XY
   // plane. Those rings are pertubated by the computed noise.
   final_noise = abs(final_noise);
   float grain = cos(dot(noisetxr.xy,noisetxr.xy) + final_noise*4);//what is this ??
   return wood_color - pow(grain,8)/2; //raising the cosine to higher power
}

I know that raising the cosine function to higher power will create sharper rings, but what does the dot product mean ? Why it can create a circle value ?

Was it helpful?

Solution

A dot-product of a vector with itself simply results in the squared length of the vector. So for each point in the xy-plane, dot(noisetxr.xy,noisetxr.xy) return the squared distance of the point to the origin. Now you're applying a cosinus-function on this distance, which means for all points on the plane, which have the same distance to the origin, it creates the same output value => a circle of equal values around the origin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top