 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”
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;
y=y-(int)y;

x=5001-TextureVector;
x=x-(int)x;

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

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

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

if ( ((x-0.5)*(x-0.5)+(y+er)*(y+er) )&lt; 0.25 ) {

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

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

}

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

if (x&gt;0.5) x=1-x;
result=(4*x*x)+nx;
result=(y-0.5)+ny;
}

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

result=1.0;
result=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:

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"
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;
y=mod(Scale*y,1.0);

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

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

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

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

if ( ((x-0.5)*(x-0.5)+(y+er)*(y+er) )&lt; 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) ) &lt; 0.25) ||
( ( (x-1)*(x-1) + (y-0.5+er)*(y-0.5+er) ) &lt; 0.25) ) {
res1=(1-y)*color1+(y+ct)*color2 +ct;

if (x&gt;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. 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! 