Page 1 of 1

Small Request: Update OSL code for Flakes texture

Posted: Tue Jun 23, 2020 5:28 pm
by leehenshall
When the osl flakes were originally compiled for Octane OSL a user called "funk" edited the original code from Chaos Group to include a projection pin so that you can triplanar the flakes texture over complex/non UV'd objects.

Here is the original post:

viewtopic.php?f=33&t=63905&hilit=osl+flakes&start=10

Here is the code from that post:

Code: Select all

shader
flakes
(
    float flake_scale = 10.0 [[ string description = "Smaller values zoom into the flake map, larger values zoom out" ]],
    float flake_size = 0.5 [[ string description = "Relative size of the flakes" ]],
    float flake_size_variance = 0.0 [[ string description = "0.0 makes all flakes the same size, 1.0 assigns random size between 0 and the given flake size" ]],
    float flake_normal_orientation = 0.0 [[ string description = "Blend between the flake normals (0.0) and the surface normal (1.0)" ]],
    point proj = point(u, v, 0) [[string label = "Projection"]],
    output color result = 1.0
)
{
    float safe_flake_size_variance = clamp(flake_size_variance, 0.1, 1.0);
    vector cellCenters[9] = {
        vector( 0.5,  0.5,  0.0),
        vector( 1.5,  0.5,  0.0),
        vector( 1.5,  1.5,  0.0),
        vector( 0.5,  1.5,  0.0),
        vector(-0.5,  1.5,  0.0),
        vector(-0.5,  0.5,  0.0),
        vector(-0.5, -0.5,  0.0),
        vector( 0.5, -0.5,  0.0),
        vector( 1.5, -0.5,  0.0)
    };
     
    //point position = vector(u, v, 0.0);
    point position = flake_scale * proj;

    point base = floor(position);
     
    point nearestCell = point(0.0, 0.0, 1.0);
    int nearestCellIndex = -1;
    for(int cellIndex = 0; cellIndex < 9; ++cellIndex)   {
        point cellCenter = base + cellCenters[cellIndex];
         
        vector centerOffset = cellnoise(cellCenter) * 2.0 - 1.0;
        centerOffset[2] *= safe_flake_size_variance;
        centerOffset = normalize(centerOffset);
         
        cellCenter += 0.5 * centerOffset;
        float cellDistance = distance(position, cellCenter);
        if(cellDistance < flake_size && cellCenter[2] < nearestCell[2]) {
            nearestCell = cellCenter;
            nearestCellIndex = cellIndex;
        }
    }
     
    result = color(0.5, 0.5, 1.0);
     
    if (nearestCellIndex != -1) {
        vector randomNormal = cellnoise(base + cellCenters[nearestCellIndex] + vector(0.0, 0.0, 1.5));
        randomNormal = 2.0 * randomNormal - 1.0;
        randomNormal = faceforward(randomNormal, I, randomNormal);
        randomNormal = normalize(mix(randomNormal, vector(0.0, 0.0, 1.0), flake_normal_orientation));
         
        result = color(0.5*randomNormal[0]+0.5, 0.5*randomNormal[1]+0.5, randomNormal[2]);
    }
}
I really like that you have provided easy access to community OSL scripts in your node editor. It is really handy.

Please can you update the quick access pre-set in the Octane Node Editor to this more advanced version of OSL flakes?
OSL-Flakes.jpg

Re: Small Request: Update OSL code for Flakes texture

Posted: Tue Jun 23, 2020 5:41 pm
by aoktar
You can organize it by yourself. Isn't a good idea to do it?

Re: Small Request: Update OSL code for Flakes texture

Posted: Tue Jun 23, 2020 6:26 pm
by leehenshall
I've just been pasting the updated OSL shader code into the OSL node to overwrite the old osl flakes code that comes with the plugin.

I didn't know it was an option to edit these node editor pre-sets. How do I do that?

Regardless, wouldn't you rather include the best version of this OSL script? It's objectively better, it doesn't change any of the core features other than append the ability for it talk to octane projections.