Scales Shader

Hi!

After reading Gottfried Hofmann’s Introduction to OSL in Blender Cycles, I am trying to translate the scales shader found here. Unfortunately not being a programmer, my understanding of the math in that shader is rather limited.

You can find the original scales.c source code for that shader here, for comparison.

Here is what I have done so far:

#include “stdosl.h”
shader scales(
color color1 = color(0.5, 0.5, 0.5),
color colo2 = color(0.3, 0.3, 0.3),
float ColNoiseSiz = 0,
float ColNoiseDep = 0,
float EdgeNoiseSiz = 0,
float EdgeNoiseDep = 0,
float NorNoiseSiz = 0,
float NorNoiseDep = 0,
point TextureVector = P,
output color Texture = 0,
)
{
float ct=0,er=0, nx=0, ny=0,x,y;

y=5001-TextureVector[0];   
y=y-(int)y;

x=5001-TextureVector[1];
x=x-(int)x;

if (EdgeNoiseDep !=0) {
er=hnoise(EdgeNoiseSiz,TextureVector[0], TextureVector[1], TextureVector[2])-0.5;
er*=EdgeNoiseDep;
}

if (ColNoiseDep !=0) {
    ct=hnoise(ColNoiseSiz, TextureVector[0], TextureVector[2], TextureVector[1])-0.5;
    ct*=ColNoiseDep;
}

if (NorNoiseDep!=0){
    nx=hnoise(NorNoiseSiz, TextureVector[0], TextureVector[1], TextureVector[2])-0.5;
    nx*=NorNoiseDep;    
    nx=hnoise(NorNoiseSiz, TextureVector[1], TextureVector[0], TextureVector[2])-0.5;
    nx*=NorNoiseDep;            
}


if ( ((x-0.5)*(x-0.5)+(y+er)*(y+er) )< 0.25 ) { 
    
    result[1]=(0.5-y)*color1+(0.5+y)*color2 +ct;
    
    result[2]=(4*(x-0.5)*(x-0.5)+nx);
    result[3]=y+ny;

}

else if ( ( ( x*x   + (y-0.5+er)*(y-0.5+er) ) < 0.25)  ||
        ( ( (x-1)*(x-1) + (y-0.5+er)*(y-0.5+er) ) < 0.25) ) { 
    result[1]=(1-y)*color1+(y+ct)*color2 +ct;

    if (x>0.5) x=1-x;
    result[2]=(4*x*x)+nx;
    result[3]=(y-0.5)+ny;
}

else  {
    result[1]=(1.5-y)*color1+(y-0.5)*color2 + ct;
    result[2]=(4*(x-0.5)*(x-0.5)+nx);
    result[3]=(y-1)+ny;
}

result[4]=1.0;
result[5]=0;

return 3;   

}

Basically I just substituted the variables StRed, StGrn, StBlu, FinRed, FinGr and FinBlu by two colors (color1 and color2) as well the cr, cg and cb (red, green and blue I believe) variables by just one variable: ct (color total).

But after trying several times to translate the rest without success, I gave up.

Can anyone brighter than me offer some help? :eyebrowlift:

Thanks in advance! :eyebrowlift:

Great job.
Can you help me to port this plugin http://wiki.blender.org/index.php/Extensions:2.4/Plugins/Texture/Water to osl?

Don’t give up. Here is a slightly tidied version of your code which compiles. I haven’t really looked at the original source code properly but you need to figure out that each result[x] is a separate output.

#include "stdosl.h"
shader scales(
color color1 = color(0.5, 0.5, 0.5),
color color2 = color(0.3, 0.3, 0.3),
float Scale = 10,
float ColNoiseSiz = 0,
float ColNoiseDep = 0,
float EdgeNoiseSiz = 0,
float EdgeNoiseDep = 0,
float NorNoiseSiz = 0,
float NorNoiseDep = 0,
point TextureVector = P,
output color Color = 0,
output float Texture2 = 0,
output float Texture3 = 0
)
{

float ct=0, er=0, nx=0, ny=0, x, y, res2, res3, res4, res5;
color res1;

y=5001-TextureVector[0];
y=mod(Scale*y,1.0);

x=5001-TextureVector[1];
x=mod(Scale*x,1.0);

if (EdgeNoiseDep !=0) {
er=noise(TextureVector,EdgeNoiseSiz)-0.5;
er*=EdgeNoiseDep;
}

if (ColNoiseDep !=0) {
ct=noise(TextureVector[0],ColNoiseSiz)-0.5;
ct*=ColNoiseDep;
}

if (NorNoiseDep!=0){
nx=noise(TextureVector[0],NorNoiseSiz)-0.5;
nx*=NorNoiseDep;
nx=noise(TextureVector[0],NorNoiseSiz)-0.5;
nx*=NorNoiseDep;
}


if ( ((x-0.5)*(x-0.5)+(y+er)*(y+er) )< 0.25 ) {

res1=(0.5-y)*color1+(0.5+y)*color2 +ct;

res2=(4*(x-0.5)*(x-0.5)+nx);
res3=y+ny;

}

else if ( ( ( x*x + (y-0.5+er)*(y-0.5+er) ) < 0.25) ||
( ( (x-1)*(x-1) + (y-0.5+er)*(y-0.5+er) ) < 0.25) ) {
res1=(1-y)*color1+(y+ct)*color2 +ct;

if (x>0.5) x=1-x;
res2=(4*x*x)+nx;
res3=(y-0.5)+ny;
}

else {
res1=(1.5-y)*color1+(y-0.5)*color2 + ct;
res2=(4*(x-0.5)*(x-0.5)+nx);
res3=(y-1)+ny;
}

res4=1.0;
res5=0;

Color = res1;
Texture2 = res2;
Texture3 = res3;
}


Brothermechanic: you misread my post. I was looking for help, the code I showed does not work as is. :frowning:

Thank for your help Charlie! I am a bit tired to analyse your code thoroughly right now, but will do it tomorrow.

I noticed the result[x] were different outputs, the problem was I did not know what the hell should I do with them.

I will study your code, and come back with doubts, if I have them.

Once again, thank you very much! :smiley: