![]() |
Hynotizing OSL... |
This morning, with question on creating Spiral texture, I am doing OSL exercise by again looking at already existing code, this time from WebGL code:
http://stackoverflow.com/questions/4638317/how-to-implement-this-rotating-spiral-in-webgl
Thanks to the code example solution above, we get to see the result from few simple lines of code.
OSL Spiral
/*float logspiral(float sita, float r, float phi, float n){
sita += log(r) * phi;
return fract(sita) * n);
}
*/
shader spiral(
color ColorA = color(1,0,0),
color ColorB = color(0,1,0),
vector Vector = P,
float Rotation = 0.0,
float Thickness = 15.0,
float ModAngle = 30.0,
output color ColOut = color(0.2)
)
{
color f;
vector p = Vector;
float angle = 0.0;
float radius = length(p);
if(p[0] != 0.0 && p[1] != 0.0){
angle = degrees(atan2(p[0], p[1]));
}
float amod = mod(angle+30.0 * Rotation-120.0 * log(radius), ModAngle);
if(amod<Thickness){
f = color(0.0,0.0,0.0);
} else {
f = color(1.0,1.0,1.0);
}
//ColOut = f; // this maybe useful as mask
ColOut = mix(ColorA, ColorB, f);
}
I think this is as simple as it gets to get a Spiral texture. But probably not the only way to get Spiral.
Texture Coordinate and Mapping
Still not sure if I am doing it correctly with the mapping, but this is my solution so far.Further Milking of OSL Spiral
Almost always every basic function that creates a certain basic texture can be further tweaked and modified. Just by multiplying or adding values, it will adjust the look. This is the fun part./*
float logspiral(float sita, float r, float phi, float n){
sita += log(r) * phi;
return fract(sita) * n);
}
*/
shader spiral(
color ColorA = color(1,0,0),
color ColorB = color(0,1,0),
int Freq = 2,
vector Vector = P,
float Rotation = 0.0,
float Thickness = 15.0,
float ModAngle = 30.0,
output color ColOut = color(0.2)
)
{
color f;
vector p = Vector;
float angle = 0.0;
float radius = length(p);
if(p[0] != 0.0 && p[1] != 0.0){
angle = degrees(atan2(p[0], p[1]));
}
float amod = mod(angle * Freq * 0.25 + 10.0 * Rotation-120.0 * log(radius), ModAngle);
if(amod<Thickness){
f = color(0.0,0.0,0.0);
} else {
f = color(1.0,1.0,1.0);
}
//ColOut = f; // this maybe useful as mask
ColOut = mix(ColorA, ColorB, f);
}
Post a Comment