Forum: Carrara


Subject: Iridescence Part 2

marcq opened this issue on May 23, 2003 ยท 18 posts


marcq posted Fri, 23 May 2003 at 8:54 AM

Formula first then explanation:

cg1 = 1;
cb1 = 1;
cr1 = 0;

ctr1 = 0.1;

cg2 = 0;
cb2 = 1;
cr2 = 0;

ctr2 = 0.2;

cg3 = 1;
cb3 = 1;
cr3 = 0;

ctr3 = 0.3;

cg4 = 1;
cb4 = 0;
cr4 = 0;

ctr4 = 0.4;

cg5 = 1;
cb5 = 0;
cr5 = 1;

ctr5 = 0.5;

cg6 = 0.5;
cb6 = 0;
cr6 = 1;

ctr6 = 0.6;

cg7 = 0;
cb7 = 0;
cr7 = 1;

ctr7 = 0.7;

sx = 0.5;
sy = 0.5;
sz = 0.5;

fo = 6;

slen = sqrt( sx * sx + sy * sy + sz * sz);

angle = abs ( acos( (sx * nx + sy * ny + sz * nz) / slen )) * 2 / PI;

v1 = 1 fo * abs ( angle ctr1);
cv1 = v1 > 0 ? v1 : 0;

v2 = 1 fo * abs ( angle ctr2);
cv2 = v2 > 0 ? v2 : 0;

v3 = 1 fo * abs ( angle ctr3);
cv3 = v3 > 0 ? v3 : 0;

v4 = 1 fo * abs ( angle ctr4);
cv4 = v4 > 0 ? v4 : 0;

v5 = 1 fo * abs ( angle ctr5);
cv5 = v5 > 0 ? v5 : 0;

v6 = 1 fo * abs ( angle ctr6);
cv6 = v6 > 0 ? v6 : 0;

v7 = 1 fo * abs ( angle ctr7);
cv7 = v7 > 0 ? v7 : 0;

red = cv1 * cr1 + cv2 * cr2 + cv3 * cr3 + cv4 * cr4 + cv5 * cr5 + cv6 * cr6 + cv7 * cr7;

green = cv1 * cg1 + cv2 * cg2 + cv3 * cg3 + cv4 * cg4 + cv5 * cg5 + cv6 * cg6 + cv7 * cg7;

blue = cv1 * cb1 + cv2 * cb2 + cv3 * cb3 + cv4 * cb4 + cv5 * cb5 + cv6 * cb6 + cv7 * cb7;

This is the formula I used in the highlights channel. Carrara doesn't appear to allow comments (or if it does, I don't know the comment delimiter.)

It isn't as bad as it looks. Basically, the formula uses the normal vector (nx, ny, nz) to compute the angle between the normal and reference vector you give it (sx, sy, sz.) The reference vector need not be normalized (i.e., don't worry about making the length "one").

This angle represents how far away from the reference vector a given surface is aligned. Zero means the surface is perpendicular to the reference vector, higher values mean more tilted. In this formula, angle goes from 0 to 1 but it could be tweaked to be in radians or degrees.

The angle is then used to determine how much of each of the seven reference colors should be shown at each point. This is linear with slope "fo".

cr1, cg1, and cb1 define the red, green and blue values for color one, cr2, cg2, cb2 for color two, etc. Values are from 0 to 1 again.

ctr1, ctr2, etc. determines the angular center of the color. A zero means the color is center around angle of 0 degrees, i.e., it shows when the facet is perpendicular to the reference vector. A 0.1 would mean the color shows at a slight angle. 0.4 at a greater angle, etc.

fo determines how fast a color falls off around its center. Use larger values for more tightly spaced colors (who have ctr values near each other) and smaller values for larger spacing unless you want a ringed effect.

Ideally the reference vector is related to your light source.

At the moment, the equation uses the local normal vector, not the global vector. It should probably use the global. Additionally, it would be better than rather specifying a reference vector, you gave it yaw and pitch of your light source and it computed the reference vector. I don't think this is a big deal except for the necessary tinkering of finding the two correct angles and then finding the right polarity of the reference vector.

You could theoretically make the reference vector (or light angles) use the p1 through p4 parameters which are supposed to be animatable.

There may also be errors in the above like my reference vector is backward or some such.

If anyone does to the anlge modification, please post or send to me. This shader was inspired by DOtpainter's post I came across but I was really looking for snow effects and need to get back to my winter project ;-)

To use this formula, cut and paste into the text of a formula shader in your highlights channel (alternately, use the color channel until you have it looking the way you want and then move it.) Adjust the reference vector to be as you want. Currently, this is just trial and error.

If you want different colors, get the rgb values for the colors you want (you can use carrara's color picker to find these by using the RGB slider view). Adjust the centering you want for each. Adjust the falloff 'fo' if the colors over lap to much or too little. A higher fo makes for a narrower band of color.

This formula supports 7 colors (for the rainbow colors); you can use less by deleting the other colors or setting their centers large. You can easily add more colors by cloning the text.

Colors need not be in order but it helps to keep track of them.

As a tip, the carrara formula window is tiny and hard to edit in. I used word to do my edits and cut and paste back to the window.

Keep in mind that you must hit 'parse' for the new formula to take effect and that the parser is not very good. It is unforgiving of the lack of space (put space around every variable and operator) and don't forget the semicolon at the end of the line.

Marc