Hi
I wrote an osl-shader and this is a simple 2d scratch generator.
When I assign it even to an aiUtility shader, the render time is so unusual. What may cause this?!!!
Solved! Go to Solution.
Solved by madsd. Go to Solution.
depends on your OSL shader. OSL is slower a pre compiled C++ shader. also depends on your code, how optimized it is. a wrong line can drop rendertime drastically.
like for all renderers, the more procedural noises etc... more slower the rendering.
Ty for your response @Heribert Raab
I'm new to osl shader programming and it is possible that I'm doing something wrong. But it happens even if I'm using osl shaders wrote by pro programmers. I think it's because of osl shader nature.
Is there any kind of rasterization method to output an osl shader in a specific resolution map inside Maya shading network?!!!
I think it will resolve any heavy post-process of osl shaders.
// Based on Candy.osl, by Zap Andersson
// Modified: 2020-03-18
shader GMScratch
(
point UVW = point(u, v, 0),
float Scale = 1.0,
float Radius = 0.5 [[ float min=0.0, float max=2.0 ]],
float Width = 0.005 [[ float min=0.01, float max=1.0 ]],
float Hardness = 0.5 [[ float min=0.00, float max=1.0 ]],
float Rotate = 0.0,
float RotateJitter = 0.0,
float Distort = 0.0,
float DistortScale = 1.0,
output color OutColor = 0,
)
{
point pnt = UVW / Scale;
// Standard method for randomization:
// Go through a 3x3x3 grid that we offset
for (float x = -3; x <= 3; x++)
{
for (float y = -3; y <= 3; y++)
{
for(float z = -3; z <= 3; z++)
{
// Point that sources all our randomization
// We run these through cell noises to get
// random values
// MAXX-44914 fix: Adds the 0.001 offset and floor()
point rndpoint = floor(pnt) + point(x, y, z) + 0.001;
// Compute a center
point dotcenter = rndpoint + noise("cell", rndpoint, 1);
vector PointDiff = pnt - dotcenter;
float RotRand = noise("snoise", dotcenter );
vector distort = noise("snoise", pnt / DistortScale ) * Distort;
PointDiff += distort;
PointDiff = rotate (PointDiff, ((Rotate*2)+(RotateJitter*RotRand)), point(0, 0, 0), point(0, 0, 1));
PointDiff /= vector (1.0, Width*0.16+0.016, 1.0);
float f = 0.0;
f = 1 - smoothstep(Radius*clamp(Hardness, 0.25, 0.997), Radius/clamp(Hardness, 0.25, 0.997), length(PointDiff));
if (f > 0.0 )
{
OutColor += f ;
if (!1)
return;
}
}
}
}
}
shader GMScratch ( point UVW = point(u, v, 0), float Scale = 1.0, float Radius = 0.5 [[ float min=0.0, float max=2.0 ]], float Width = 0.005 [[ float min=0.01, float max=1.0 ]], float Hardness = 0.5 [[ float min=0.00, float max=1.0 ]], float Rotate = 0.0, float RotateJitter = 0.0, float Distort = 0.0, float DistortScale = 1.0, output color OutColor = 0, ) { point pnt = UVW / Scale; // Standard method for randomization: // Go through a 3x3x3 grid that we offset for (float x = -1; x <= 1; x++) { for (float y = -1; y <= 1; y++) { for(float z = -1; z <= 1; z++) { // Point that sources all our randomization // We run these through cell noises to get // random values // MAXX-44914 fix: Adds the 0.001 offset and floor() point rndpoint = floor(pnt) + point(x, y, z) ; //+ 0.001; // Compute a center point dotcenter = rndpoint + noise("cell", rndpoint, 1); vector PointDiff = pnt - dotcenter; float RotRand = noise("snoise", dotcenter ); vector distort = noise("snoise", pnt / DistortScale ) * Distort; PointDiff += distort; PointDiff = rotate (PointDiff, ((Rotate*2)+(RotateJitter*RotRand)), point(0, 0, 0), point(0, 0, 1)); PointDiff /= vector (1.0, Width*0.16+0.016, 1.0); float f = 0.0; f = 1 - smoothstep(Radius*clamp(Hardness, 0.25, 0.997), Radius/clamp(Hardness, 0.25, 0.997), length(PointDiff)); if (f > 0.0 ) { OutColor += f ; if (!1) return; } } } } }
This is 5-10 times faster on my machine, reduce the grid size, commented out the offset as well. but doesnt matter.
Also, I would look into generating an external hash float instead of using the internal perlin. But that is just me.
Thank you for checking the shader. If I decrease the grid size it causes clipping of the shape of scratch. But it had a huge impact on render time. Thank you again.
I will check for a solution, but since we are here is there any other way to prevent that clipping effect?
Since I'm new to osl shader programming and I just started a few days ago, I'm just wondering why there is no rand() function? And I'm searching for a go0d method to do the same.
float fract ( float x ){ return x - floor(x); } vector fract ( vector x ){ return x - floor(x); } float random (vector st) { return fract(sin(dot(st, vector(12.8,78.2,0)))* 48.5); }
You can also extend to vec2 and vec3 and vec4 if you want.
Note you want to have fairly small decimals to work with to respect the sliding occuring with float precision drift.
It depends on the radius. But 2x2x2 is enough in most cases.
I made a variable to change it in various scenarios.
You can also remove the volume and use u and v, reduce more calls.
Since you only probe u and v in position, you dont need an evaluation over z over anything, anywhere.
I removed z position evaluation and that made a huge difference.
I'll polish the code more further. I'm thinking about code it in python and also I have no idea how to do it. I will figure it out tomorrow.
Thank you again for your guidance.