Displacement Shader Writing CSCD 472 Slide 1 4/5/10
Displacement Shader Variables CSCD 472 Slide 2 4/5/10
Displacement Shader Variables Of these variables only P (the point being shaded) and N (the non-geometric normal) can be written. Displacement vs. Bump Displacement shaders - the point is displaced along the normal and then a new normal is created. Bump shaders – the point displacement is not saved but a new normal is calculated based on the point displacement. CSCD 472 Slide 3 4/5/10
Displacement Shader Variables Displacement: displacement d_simplenoise_displ(float Km = 0.03; float noiFreq = 10;) { float noi; noi = noise(transform("shader", P*noiFreq)); P+= normalize(N) * noi * Km; N = calculatenormal(P); } Bump: displacement d_simplenoise_bump(float Km = 0.03; float noiFreq = 10;) { float noi; noi = noise(transform("shader", P*noiFreq)); N = calculatenormal(P+ normalize(N) * noi * Km); } CSCD 472 Slide 4 4/5/10
Bump vs. Displacement Object with no Displacement shaders: CSCD 472 Slide 5 4/5/10
No Displacement RIB Display "rock.tiff" "tiff" "rgba" Format 640 480 1 PixelSamples 3 3 PixelFilter "catmull-rom" 3 3 ShadingRate 1.0 Projection "perspective" "fov" 40 WorldBegin LightSource "ambientlight" 0 "intensity" [ 0.2 ] "lightcolor" [ 1 1 1 ] LightSource "distantlight" 1 "intensity" [ 1.0 ] "lightcolor" [ 1 1 1 ] "from" [ -1 1 -1 ] "to" [ 0 0 0 ] LightSource "distantlight" 2 "intensity" [ 0.75 ] "lightcolor" [ 1 1 1 ] "from" [ 1 1 -2 ] "to" [ 0 0 0 ] LightSource "distantlight" 3 "intensity" [ 0.5 ] "lightcolor" [ 1 1 1 ] "from" [ 1 -1 -1 ] "to" [ 0 0 0 ] #Displacement "d_simplenoise_bump" "Km" [0.5] "noiFreq" [5] Surface "rock" "float ringdensity" [10] "float spread" [3] "color darkcolor" [0.3 0.6 0.3] Color [.6 .4 .4] Translate 0 0 4 Rotate -90 1 0 0 Rotate 90 0 1 0 Scale .85 .85 1.2 Sphere 1 -1 1 360 WorldEnd CSCD 472 Slide 6 4/5/10
No Displacement- rock.sl surface rock( float Ka=1, Kd=.6, Ks=0.4, roughness=0.2, ringdensity=4, swirl=0.25, swirlfreq=1, spread=1; point c0=point "shader" (0,0,0); color specularcolor=1, darkcolor=0.7) { point C0, newP; normal Nf; vector V, PP; float dd, alpha; color Cwood; Nf = faceforward( normalize(N), I); V = -normalize(I); C0 = transform("shader", c0); newP = transform("shader", P); PP = newP - C0; dd = sqrt(abs(PP.PP)); alpha = mod(ringdensity*dd, 1); alpha = pow(alpha, spread); alpha = 1 - alpha; Cwood = mix(darkcolor, Cs, alpha); Oi = Os; Ci = Oi * (Cwood * (Ka*ambient() + Kd*diffuse(Nf) ) + Ks * specularcolor * specular(Nf, V, roughness)); } CSCD 472 Slide 7 4/5/10
rock.sl - Discussion PP is the vector from P to the origin of shader coords so dd is the distance from P to the origin of shader coords. alpha is the fractional part of dd*ringdensity mod function: mod(a,b) returns a value from 0 to b with the fractional part of a. taking alpha to the spread power makes it smaller, then invert alpha and use it to mix Cs and darkcolor. Basically rock.sl produces a sort of wood grain texture. CSCD 472 Slide 8 4/5/10
Bump vs. Displacement Add the Displacement shader d_simplenoise_bump.sl Outline is smooth because only normals are changed – no displacement. CSCD 472 Slide 9 4/5/10
Bump vs. Displacement Replace d_simplenoise_bump.sl with d_simplenoise_displ.sl Outline is altered because now true displacements are taken. CSCD 472 Slide 10 4/5/10
Bump vs. Displacement CSCD 472 Slide 11 4/5/10
Cracks with large displacements Occur when a displacement extends beyond the bounding box of the current render bucket. CSCD 472 Slide 12 4/5/10
Cracks Fix Attribute "displacementbound" "coordinatesystem" ["shader"] "sphere" [0.5] Defines a new bounding area for use by the shading engine If new area is too large – slow rendering If too small cracks reappear. CSCD 472 Slide 13 4/5/10
Displacing Polygons After points are displaced normals must be recalculated. In a polygon mesh model the important normals are the averaged “vertex” normals and they can not be re-established easily. The function calculatenormal() calculates purely geometric normals (Ng) or polygon normals so following recalculation a smooth surface often becomes faceted -as shown in the next slide. CSCD 472 Slide 14 4/5/10
Loss of Smoothness in Polygon Meshes After Displacement No Displacement displacement d_simple() { P+= P * normalize(N)*.01; N = calculatenormal(P); } CSCD 472 Slide 15 4/5/10
Correctly calculating Normals Fixing the situation: Take the difference between the shading or vertex normal (N) and the geometric normal (Ng) and store it. After recalculating the new geometric normal add the saved difference to form the new shading normal N. displacement d_simple_fix() { varying normal Ndiff; Ndiff= normalize(N) - normalize(Ng); P+= P * normalize(N) *.01; N = normalize(calculatenormal(P)) + Ndiff; } CSCD 472 Slide 16 4/5/10
Correctly Calculating Normals Original and uncorrected Normals Corrected Normals CSCD 472 Slide 17 4/5/10
Fixing Loss of Smoothness in Polygon Meshes No Displacement After Displacement After Displacement with Normal fix. CSCD 472 Slide 18 4/5/10
Recommend
More recommend