surface shader writing
play

Surface Shader Writing CSCD 471 Slide 1 4/5/10 Plotting Functions - PowerPoint PPT Presentation

Surface Shader Writing CSCD 471 Slide 1 4/5/10 Plotting Functions in Shaders This is a trick but can be useful and very helpful in understanding how shaders work. Remember that each call to the shader happens at one specific X and Y value.


  1. Surface Shader Writing CSCD 471 Slide 1 4/5/10

  2. Plotting Functions in Shaders This is a trick but can be useful and very helpful in understanding how shaders work. Remember that each call to the shader happens at one specific X and Y value. The following shader will plot the Y values of the smoothstep function at each X position: CSCD 471 Slide 2 4/5/10

  3. testplot.sl surface testplot(float freq=1.0) { float delta=0.03; //consider just the x component of P point PP = transform("shader", P); float stepval1 = smoothstep(-0.75, -0.5, xcomp(PP)); float l1 = stepval1-2*delta*(stepval1); float u1 = stepval1+2*delta*(1-stepval1); float ss1 = smoothstep(l1-delta, l1, t); float ss2 = smoothstep(u1-delta, u1, t); float f1 = ss1-ss2; f1 = biasFunc(f1, 0.7); color blue = color(0,0,1); color black = color (0,0,0); color C1 = mix(black, blue, f1); Ci = C1; } CSCD 471 Slide 3 4/5/10

  4. Evaluate the Function to be Graphed point PP = transform("shader", P); float stepval1 = smoothstep(-0.75, -0.5, xcomp(PP)); The transform is unnecessary in most cases since object coordinates are shader coordinates Smoothstep: float step ( float min, value ) float smoothstep ( float min, max, value ) step returns 0 if value is less than min; otherwise it returns smoothstep returns 0 if value is less than min, 1 if value is greater than or equal to max, and performs a smooth Hermite interpolation between 0 and 1 in the interval min to max. This call evaluates the smoothstep function for graphing – stepval1 is the Y value of this smoothstep function for the X component of PP. CSCD 471 Slide 4 4/5/10

  5. Bracketing Y values float l1 = stepval1-2*delta*(stepval1); float u1 = stepval1+2*delta*(1-stepval1); These calls set u1 and l1 to be values slightly above and below the Y value (stepval1) u1 and l1 essentially provide a small range of values that bracket stepval1. CSCD 471 Slide 5 4/5/10

  6. Decide if t is in the correct range float ss1 = smoothstep(l1-delta, l1, t); float ss2 = smoothstep(u1-delta, u1, t); float f1 = ss1-ss2; f1 = biasFunc(f1, 0.7); These calls use the surface parameter t (used in slicing) which represents the Y value in the final image. t varies from -1 to 1. They produce one smoothstep that is 0 when t is less than (l1-delta) or (u1-delta), is 1 when t is greater than l1 or u1 and a smooth curve between 0 and 1 for t values between the limits. Subtracting produces a “pulse” function that determines the color to be used to draw this point. CSCD 471 Slide 6 4/5/10

  7. Choosing Color - mix float biasFunc(float t; float a;) float ss1 = smoothstep(l1-delta, l1, t); { float ss2 = smoothstep(u1-delta, u1, t); return pow(t, -(log (a)/log(2))); float f1 = ss1-ss2; } f1 = biasFunc(f1, 0.7); color C1 = mix(black, blue, f1); Ci = C1; biasFunc changes f1 by pow(f1, -(log (0.7)/log(2))) f1 is used as a mixing factor in mix() color mix( color color0, color color1, float value) { return (1-value) * color0 + (value) * color2; } So if value is 0 return color0 and if value is 1 return color2 – otherwise colors are blended. CSCD 471 Slide 7 4/5/10

  8. Possible Color Values u1 u1- △ l1 l1- △ t ss1 ss2 f1 color mix(black, blue, f1) t < (l1- △ ) 0 0 0 1-f1*black+ f1*blue = black (l1- △ ) ≤ t ≤ l1 [0,1] 0 [0,1] 1-f1*black+ f1*blue = black/blue l1 ≤ t ≤ (u1- △ ) 1 0 1 1-f1*black+ f1*blue = blue (u1- △ ) ≤ t ≤ u1 1 [0,1] [0,1] 1-f1*black+ f1*blue = black/blue t > u1 1 1 0 1-f1*black+ f1*blue = black CSCD 471 Slide 8 4/5/10

  9. testplot.rib Display "testplot.tiff" "tiff" "rgba" WorldBegin Surface "testplot" "float freq" [5.0] Polygon "P" [-1 -0.9 30 1 -0.9 30 1 1.0 30 -1 1.0 30] WorldEnd CSCD 471 Slide 9 4/5/10

  10. testplot Output testplot output CSCD 471 Slide 10 4/5/10

  11. Other Function Graphs smoothstep(-0.75, -0.5, xcomp(PP)) smoothstep(0.25, 0.5, xcomp(PP)) pulse (-0.5, 0.5, 0.25, xcomp(PP)) smoothstep((-0.5-0.25), -0.5, xcomp(PP))- smoothstep((0.5-0.25), 0.5, xcomp(PP)) CSCD 471 Slide 11 4/5/10

  12. Other Function Graphs float tt = (t/2.0)+0.5; float ss = (t/2.0)+0.5; plot2=ss; plot1=biasFunc(ss, 0.7); CSCD 471 Slide 12 4/5/10

  13. Plotting Noise #define snoise(p) (2 * (float noise(p)) - 1) #define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz), (a), (x)) - \ smoothstep((b) - (fuzz), (b), (x))) float biasFunc(float t; float a;) { return pow(t, -(log (a)/log(2))); } surface snoiseplot(float freq=5.0) { float delta=0.03; point PP = transform("shader", P); float noiseval = snoise(freq * xcomp(PP)); float usnoiseval = noiseval/2.0+0.5; //unsigning noiseval color noisecol = color(usnoiseval, usnoiseval, usnoiseval); float l = noiseval-2*delta*(noiseval); float u = noiseval+2*delta*(1-noiseval); float f = pulse(l, u, delta, t); color wh = color(1,1,1); f = biasFunc(f, 0.25); Ci = mix(noisecol, wh, f); } CSCD 471 Slide 13 4/5/10

  14. Plotting Noise- RIB File Display "snoiseplot.ortho.tiff" "tiff" "rgba" // default orthographic projection WorldBegin Surface "snoiseplot" "float freq" [5.0] Polygon "P" [-1 -0.9 30 1 -0.9 30 1 0.9 30 -1 0.9 30] WorldEnd CSCD 471 Slide 14 4/5/10

  15. Varying Frequency Noise freq = 5.0 CSCD 471 Slide 15 4/5/10

  16. Varying Frequency Noise freq = 10.0 CSCD 471 Slide 16 4/5/10

  17. Varying Frequency Noise freq = 15.0 CSCD 471 Slide 17 4/5/10

  18. Turbulence Turbulence (or Fractional Brownian Motion) is composited or layered noise at different frequences and amplitudes. A common way to create turbulence is to start producing noise with low frequencies and high amplitudes and at each level: increase frequency of noise decrease amplitude of noise At each level layer or composite new values (at increased frequency and decreased amplitude) with values from previous levels into the final turbulence value. CSCD 471 Slide 18 4/5/10

  19. Fractional Brownian Motion - non-filtered float nonfilt_fBM (point p; uniform float octaves, lacunarity, gain, ampl0, freq0) { varying point pp = p; varying float sum = 0; uniform float i; for (i = 0; i < octaves; i += 1) { float f = pow(lacunarity, i); float a = pow(gain, -i); sum += a*ampl0 *snoise (freq0*f*pp); } return sum; } surface turbtest( string sspace="shader"; float freq0=0.5, ampl0=1, octaves = 4, filterwidth =0.3, lacunarity=2, gain=2, contrast=0.5) { float t =0, i; point PP= transform(sspace, P); t = nonfilt_fBM(PP, octaves, lacunarity, gain, ampl0, freq0); t = pow(t, contrast); Oi= Os; Ci = Oi*Cs*color(t,t,t); } CSCD 471 Slide 19 4/5/10

  20. Turbulence Definitions l acunarity - the base by which frequency changes from layer to layer. Each layer the power of lacunarity increases by 1. gain (or persistence) - the base by which amplitude decreases from layer to layer. Each layer the power of gain decreases by 1 starting at 0. octaves – the number of layers to be summed. for (i = 0; i < octaves; i += 1) { float f = pow(lacunarity, i); float a = pow(gain, -i); sum += a*ampl0 *snoise (freq0*f*pp); } CSCD 471 Slide 20 4/5/10

  21. Fractional Brownian Motion vs. Perlin Turbulence The only change from Fractional Brownian Motion is the use of abs on the signed noise. sum += a*ampl0 *snoise (freq0*f*pp); <-- fBM sum += a*ampl0 *abs(snoise (freq0*f*pp)); <-- Turbulence (Perlin) Turbulence (Perlin) Fractional Brownian Motion both images – gain=2 lacunarity=2 octaves=4 freq0=0.2 contrast=1.0 CSCD 471 Slide 21 4/5/10

  22. Filtered Turbulence Since we are sampling a function at a point the possibility exists for aliasing if sampling rate is below the Nyquist Limit This is made even more likely since noise frequency can get very high as we composite additional layers The frequency value f increases exponentially (f = pow(lacunarity, i); ) as we iterate between levels. Filtering allows us to retrieve a "sampled" value that is an areal average of values thus smoothing out any aliasing while allowing contributions by the lower frequency levels to the composite color. CSCD 471 Slide 22 4/5/10

  23. Filtered Turbulence #include "noises.h" float filt_turbulence (point p; uniform float octaves, lacunarity, gain, ampl0, freq0) { extern float du, dv; /* Needed for filterwidth macro */ varying point pp = p; varying float sum = 0, fw =filterwidthp(p); uniform float i; for (i = 0; i < octaves; i += 1) { float f = pow(lacunarity, i); float a = pow(gain, -i); float n = filteredsnoise (freq0*f*pp, fw); sum += a*ampl0 * filteredabs (n, fw); fw *= lacunarity; } return sum; } From Advanced RenderMan . CSCD 471 Slide 23 4/5/10

  24. Non-filtered vs. filtered Turbulence Non-Filtered Turbulence Filtered Turbulence both images – gain=2 lacunarity=2 octaves=4 freq0=0.2 contrast=1.0 CSCD 471 Slide 24 4/5/10

  25. Effects of Filtering at Different Frequencies freq = 0.2 freq = 0.1 CSCD 471 freq = 0.5 freq = 1.0 Slide 25 4/5/10

  26. Effects of Differing Number of Octaves Octaves = 4 Octaves = 3 Octaves = 2 Octaves = 1 CSCD 471 Slide 26 4/5/10

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend