OSL Shader code knowledge?

Forums: OSL Shader code knowledge?
Newtek Lightwave 3D (exporter developed by holocube, Integrated Plugin developed by juanjgon)

Moderator: juanjgon

OSL Shader code knowledge?

Postby TheMightySpud » Tue Aug 04, 2020 10:12 am

TheMightySpud Tue Aug 04, 2020 10:12 am
Does anyone have any knowledge of OSL Shader coding?

I've found a free script to add Interiors to windows which looks really good (replicates the effects from the old vRoom plugin).

Problem is the guy didn't have access to LW when he wrote it and kinda guessed about the mapping (Was written back in 2017).

I've tried poking around the code, but have no idea how LW deals with it's mapping in terms of OSL.

Would be extremely grateful if anyone would be able to take a look at it and maybe fix it? (I think other people may be quite grateful too)

I've included the shader script and 'template' image along with an IPR render of how it's currently (not) working.

(would also be very handy to change the 'string' input to a texture image :) if that's possible)

Thanks
TheMightySpud

OSL_IPR_image.jpg




Code: Select all
#include <stdosl.h>

shader jiWindowBox_LWOctane(
   string input_tex = "t:/installs/genericLivingRoom03.exr",
        int textureFlip = 0 ,
        int textureFlop = 0 ,
        float roomDepth = 1 [[ float min = 0.1, float max = 100 ]],
        float widthOverscan = 0 [[ float min = 0.0, float max = 0.9 ]],
        float heightOverscan = 0 [[ float min = 0.0, float max = 0.9 ]],   
        int enableMidground = 0,
        float midgroundDepth = 0.5 [[ float min = 0.05, float max = 99 ]], 
        float midgroundOffsetX = 0,
        float midgroundOffsetY = 0, 
        int enableCurtains = 0,
        output color result = 0.0
)
{
    //user controls remapping
    float roomDepthMult = clamp(roomDepth,0.1,100);
    float heightOverscanMult = 1 - clamp(heightOverscan,0,0.9);
    float widthOverscanMult = 1 - clamp(widthOverscan,0,0.9);
    float midgroundDepthMult = clamp(midgroundDepth,0.05,roomDepthMult-0.01);
    float midgroundOffY = midgroundOffsetY * (textureFlip*2-1) * 0.1;
    float midgroundOffX = midgroundOffsetX * (textureFlop*2-1) * 0.1;
   
   
    //global variables & remapping
    vector objI = transform("object", -I);
    objI = vector(-objI[0],-objI[1],-objI[2]) * color(widthOverscanMult, heightOverscanMult, 1);      //reorder to match UV for Y up axis
    color objPOrig = (color(u,v,0.5) * 2 - 1) * 0.5 + 0.5;                     //for curtains
    color objP = (color(u,v,0.5) * 2 - 1) * color(widthOverscanMult, heightOverscanMult, 1)  * 0.5 + 0.5;    //UV seems to be the better approach
   
   
    //bases for width/height/depth
    vector sections = step(0, objI);
    color baseDepth = (objP-sections)/(-objI * roomDepthMult);
    color mgDepth = (objP-sections)/(-objI * midgroundDepthMult);   
    color baseBack = (objP-sections)/(-objI);
    color baseWidth = baseDepth * roomDepthMult;
   
   
   
    //depth and width ramps
    color baseDepthX = (baseDepth[1]*objI+objP + 1);
    color baseDepthY = (baseDepth[0]*objI+objP + 1);
    color baseWidthX = (baseWidth[1]*objI+objP + 1);
    color baseWidthY = (baseWidth[0]*objI+objP + 1);

    float horizU = baseDepthY[2] - 0.5;
    float vertU = baseWidthX[0] - 1;
    float horizV = baseWidthY[1] - 1;
    float vertV = baseDepthX[2] - 0.5;
   
   
    //convert ramps to UV/ST... WIP - not very efficient
    float sideWallsMask = step(0,horizU) * step(0,1-max(horizV, 1-horizV));
    color sideWallsUV = color(horizU, horizV, 0) / 3;
    color rWallUV = (sideWallsUV + color(2.0/3.0, 1.0/3.0, 0)) * sideWallsMask * sections[0];
    color lWallUV = (sideWallsUV + color(0.0, 1.0/3.0, 0)) * sideWallsMask * (1-sections[0]);
    lWallUV[0] = (1.0/3.0 - lWallUV[0]) * sideWallsMask * (1-sections[0]);     
   
    float FloorCeilMask = step(0,vertV) * step(0,1-max(vertU, 1-vertU));
    color FloorCeilUV = color(vertU, vertV, 0) / 3;
    color ceilUV = (FloorCeilUV + color(1.0/3.0, 2.0/3.0, 0)) * FloorCeilMask * sections[1];
    color floorUV = (FloorCeilUV + color(1.0/3.0, 0, 0)) * FloorCeilMask * (1-sections[1]);
    floorUV[1] = (1.0/3.0 - floorUV[1]) * FloorCeilMask * (1-sections[1]);
   
    color backWallUV = ((baseBack[2]*objI + (objP/2)/(roomDepthMult)) * (roomDepthMult*2) / 3 + color(1.0/3.0, 1.0/3.0, 0) ) * (1 - max(step(0,horizU), step(0,vertV)));

   
    color midgroundUV = (1.0/3.0 - (baseBack[2]*objI + (objP)/(midgroundDepthMult*2)) * (midgroundDepthMult*2) / 3);
    float midgroundMask = step( 0, midgroundUV[1] * 3 * (1-midgroundUV[1]*3) ) * step( 0, midgroundUV[0] * (1.0/3.0-midgroundUV[0]) );
    midgroundUV = (color(midgroundOffX, midgroundOffY, 0) + midgroundUV);
    midgroundUV[1] = (1-midgroundUV[1]) - 2.0/3.0; 
   
    color curtainsUV = objPOrig * color(1.0/3.0, 1.0/3.0, 1);
    curtainsUV[0] = 1.0/3.0 - curtainsUV[0];
    curtainsUV[1] = 2.0/3.0 + curtainsUV[1]; //VRay specific


   
    color finalUV = ceilUV + floorUV + rWallUV + lWallUV + backWallUV;
   
   
   
   
    //flipping ctrl
    if (textureFlop < 1){   //VRay specific
        midgroundUV[0] = 1.0/3.0 - midgroundUV[0];
        curtainsUV[0] = 1.0/3.0 - curtainsUV[0];
    }else
        finalUV[0] = 1-finalUV[0];
    if (textureFlip > 0){
        finalUV[1] = 1-finalUV[1];
        midgroundUV[1] = 1.0/3.0 - midgroundUV[1]; // VRay specific
        curtainsUV[1] = 1 - curtainsUV[1] + 2.0/3.0; // VRay specific
    }
   

    color roomRGB = texture(input_tex, finalUV[0], finalUV[1]);
       
    color finalRGB;
   
   
   
   
    //midground switch
    if (enableMidground > 0){
        float midgroundA;
        color midgroundRGB = texture(input_tex, midgroundUV[0], midgroundUV[1], "alpha", midgroundA);
        finalRGB = mix(roomRGB,midgroundRGB,midgroundA * midgroundMask); //VRay specific
    }
    else{
        finalRGB = roomRGB;
    }
   
   
   
   
    //curtains switch
    if (enableCurtains > 0){
        float curtainsA;
        color curtainsRGB = texture(input_tex, curtainsUV[0], curtainsUV[1], "alpha", curtainsA);
        finalRGB = mix(finalRGB,curtainsRGB,curtainsA);
    }


   

    result = finalRGB;

}
Attachments
jiWindowBox_template.zip
(82.21 KiB) Downloaded 171 times
TheMightySpud
Licensed Customer
Licensed Customer
 
Posts: 70
Joined: Thu Jun 06, 2013 11:19 am

Re: OSL Shader code knowledge?

Postby frankmci » Tue Aug 04, 2020 6:06 pm

frankmci Tue Aug 04, 2020 6:06 pm
I'm coming from C4D, so this may not work at all for you in LW. It's been over a year since I used this, so I'm not really sure what I may have tweaked, if anything, but here's the version of the script that works for me. If you look back a year or two, there was a decent amount of discussion about this script here in the Octane forums.

Code: Select all
#include <stdosl.h>

shader jiWindowBox_LWOctane(
       string input_tex = "t:/installs/genericLivingRoom03.exr",
        int textureFlip = 0 ,
        int textureFlop = 0 ,
        float roomDepth = 1 [[ float min = 0.1, float max = 100 ]],
        float widthOverscan = 0 [[ float min = 0.0, float max = 0.9 ]],
        float heightOverscan = 0 [[ float min = 0.0, float max = 0.9 ]],
        int enableMidground = 0,
        float midgroundDepth = 0.5 [[ float min = 0.05, float max = 99 ]],
        float midgroundOffsetX = 0,
        float midgroundOffsetY = 0,
        int enableCurtains = 0,
        output color result = 0.0
)
{
    //user controls remapping
    float roomDepthMult = clamp(roomDepth,0.1,100);
    float heightOverscanMult = 1 - clamp(heightOverscan,0,0.9);
    float widthOverscanMult = 1 - clamp(widthOverscan,0,0.9);
    float midgroundDepthMult = clamp(midgroundDepth,0.05,roomDepthMult-0.01);
    float midgroundOffY = midgroundOffsetY * (textureFlip*2-1) * 0.1;
    float midgroundOffX = midgroundOffsetX * (textureFlop*2-1) * 0.1;


    //global variables & remapping
    vector objI = transform("object", -I);
    objI = vector(-objI[0],-objI[1],-objI[2]) * color(widthOverscanMult, heightOverscanMult, 1);      //reorder to match UV for Y up axis
    color objPOrig = (color(u,v,0.5) * 2 - 1) * 0.5 + 0.5;                     //for curtains
    color objP = (color(u,v,0.5) * 2 - 1) * color(widthOverscanMult, heightOverscanMult, 1)  * 0.5 + 0.5;    //UV seems to be the better approach


    //bases for width/height/depth
    vector sections = step(0, objI);
    color baseDepth = (objP-sections)/(-objI * roomDepthMult);
    color mgDepth = (objP-sections)/(-objI * midgroundDepthMult);
    color baseBack = (objP-sections)/(-objI);
    color baseWidth = baseDepth * roomDepthMult;



    //depth and width ramps
    color baseDepthX = (baseDepth[1]*objI+objP + 1);
    color baseDepthY = (baseDepth[0]*objI+objP + 1);
    color baseWidthX = (baseWidth[1]*objI+objP + 1);
    color baseWidthY = (baseWidth[0]*objI+objP + 1);

    float horizU = baseDepthY[2] - 0.5;
    float vertU = baseWidthX[0] - 1;
    float horizV = baseWidthY[1] - 1;
    float vertV = baseDepthX[2] - 0.5;


    //convert ramps to UV/ST... WIP - not very efficient
    float sideWallsMask = step(0,horizU) * step(0,1-max(horizV, 1-horizV));
    color sideWallsUV = color(horizU, horizV, 0) / 3;
    color rWallUV = (sideWallsUV + color(2.0/3.0, 1.0/3.0, 0)) * sideWallsMask * sections[0];
    color lWallUV = (sideWallsUV + color(0.0, 1.0/3.0, 0)) * sideWallsMask * (1-sections[0]);
    lWallUV[0] = (1.0/3.0 - lWallUV[0]) * sideWallsMask * (1-sections[0]);

    float FloorCeilMask = step(0,vertV) * step(0,1-max(vertU, 1-vertU));
    color FloorCeilUV = color(vertU, vertV, 0) / 3;
    color ceilUV = (FloorCeilUV + color(1.0/3.0, 2.0/3.0, 0)) * FloorCeilMask * sections[1];
    color floorUV = (FloorCeilUV + color(1.0/3.0, 0, 0)) * FloorCeilMask * (1-sections[1]);
    floorUV[1] = (1.0/3.0 - floorUV[1]) * FloorCeilMask * (1-sections[1]);

    color backWallUV = ((baseBack[2]*objI + (objP/2)/(roomDepthMult)) * (roomDepthMult*2) / 3 + color(1.0/3.0, 1.0/3.0, 0) ) * (1 - max(step(0,horizU), step(0,vertV)));


    color midgroundUV = (1.0/3.0 - (baseBack[2]*objI + (objP)/(midgroundDepthMult*2)) * (midgroundDepthMult*2) / 3);
    float midgroundMask = step( 0, midgroundUV[1] * 3 * (1-midgroundUV[1]*3) ) * step( 0, midgroundUV[0] * (1.0/3.0-midgroundUV[0]) );
    midgroundUV = (color(midgroundOffX, midgroundOffY, 0) + midgroundUV);
    midgroundUV[1] = (1-midgroundUV[1]) - 2.0/3.0;

    color curtainsUV = objPOrig * color(1.0/3.0, 1.0/3.0, 1);
    curtainsUV[0] = 1.0/3.0 - curtainsUV[0];
    curtainsUV[1] = 2.0/3.0 + curtainsUV[1]; //VRay specific



    color finalUV = ceilUV + floorUV + rWallUV + lWallUV + backWallUV;




    //flipping ctrl
    if (textureFlop < 1){   //VRay specific
        midgroundUV[0] = 1.0/3.0 - midgroundUV[0];
        curtainsUV[0] = 1.0/3.0 - curtainsUV[0];
    }else
        finalUV[0] = 1-finalUV[0];
    if (textureFlip > 0){
        finalUV[1] = 1-finalUV[1];
        midgroundUV[1] = 1.0/3.0 - midgroundUV[1]; // VRay specific
        curtainsUV[1] = 1 - curtainsUV[1] + 2.0/3.0; // VRay specific
    }


    color roomRGB = texture(input_tex, finalUV[0], finalUV[1]);

    color finalRGB;




    //midground switch
    if (enableMidground > 0){
        float midgroundA;
        color midgroundRGB = texture(input_tex, midgroundUV[0], midgroundUV[1], "alpha", midgroundA);
        finalRGB = mix(roomRGB,midgroundRGB,midgroundA * midgroundMask); //VRay specific
    }
    else{
        finalRGB = roomRGB;
    }




    //curtains switch
    if (enableCurtains > 0){
        float curtainsA;
        color curtainsRGB = texture(input_tex, curtainsUV[0], curtainsUV[1], "alpha", curtainsA);
        finalRGB = mix(finalRGB,curtainsRGB,curtainsA);
    }




    result = finalRGB;

}
Attachments
Screen Shot 2020-08-04 at 2.08.45 PM.png
Windowbox OSL C4D
Technical Director - C4D, Maya, AE, - Washington DC
frankmci
Licensed Customer
Licensed Customer
 
Posts: 842
Joined: Fri May 26, 2017 2:00 pm

Re: OSL Shader code knowledge?

Postby TheMightySpud » Wed Aug 05, 2020 7:37 am

TheMightySpud Wed Aug 05, 2020 7:37 am
Very cool reply thank you.

I will take a look a little later today.

In the meantime, I guess the most useful thing would be an idea of where in the code the different 'tiles' of the texture are positioned/rotated etc. as that seems to be where the script falls down inside of LW (guessing because LW deals with those things differently).

I've tried dissecting the code, but coming up blank. :-/

TheMightySpud
TheMightySpud
Licensed Customer
Licensed Customer
 
Posts: 70
Joined: Thu Jun 06, 2013 11:19 am

Re: OSL Shader code knowledge?

Postby TheMightySpud » Wed Aug 05, 2020 9:38 am

TheMightySpud Wed Aug 05, 2020 9:38 am
Okay, so this is really weird.....

I found this tutorial digging around the older posts about OSL.

https://vimeo.com/303298454

Weird thing is, it uses the exact script that I posted and it works fine, so I am very very confused.

Has something changed in the way the OSL node reads/parses things?

TheMightySpud
TheMightySpud
Licensed Customer
Licensed Customer
 
Posts: 70
Joined: Thu Jun 06, 2013 11:19 am

Re: OSL Shader code knowledge?

Postby TheMightySpud » Wed Aug 05, 2020 10:16 am

TheMightySpud Wed Aug 05, 2020 10:16 am
Even more bizarre-ness going on with OSL.

The room shader now works. Was curious if the rotation of the plane had any bearing on the results. So so in modeller, I rotated it 180 degrees, all went even more screwy.....rotated it another 180 degrees and it now it works fine. wtf? lol.

Now trying to get the tiled image shader to work.....currently not doing a danged thing. lol.

TheMightySpud
TheMightySpud
Licensed Customer
Licensed Customer
 
Posts: 70
Joined: Thu Jun 06, 2013 11:19 am

Re: OSL Shader code knowledge?

Postby frankmci » Wed Aug 05, 2020 4:15 pm

frankmci Wed Aug 05, 2020 4:15 pm
TheMightySpud wrote:Even more bizarre-ness going on with OSL.

The room shader now works. Was curious if the rotation of the plane had any bearing on the results. So so in modeller, I rotated it 180 degrees, all went even more screwy.....rotated it another 180 degrees and it now it works fine. wtf? lol.

Now trying to get the tiled image shader to work.....currently not doing a danged thing. lol.


Oh, right, I forgot about that weirdness. Yes, the orientation of the geometry matters in a strange way, based on global coordinates. I could rotate an object and have the material stick properly, but the initial texture projection coordinates of the desired face needed to be on the Neg Z. I'm afraid I never dug into it enough to figure it out, since I found a work-around (create all windows facing -Z, then rotate into proper position) and had a deadline to meet.

Try applying it to a simple cube to see some real wackiness. If it's like in C4D, it will be clear that it only works properly on the -Z face. I'm guessing that to work properly, this script needs a seperate Image Texture node input, and then that node needs a Projection node with, "OSL Projection," or "OSL Delayed."
Technical Director - C4D, Maya, AE, - Washington DC
frankmci
Licensed Customer
Licensed Customer
 
Posts: 842
Joined: Fri May 26, 2017 2:00 pm

Re: OSL Shader code knowledge?

Postby TheMightySpud » Wed Aug 05, 2020 5:08 pm

TheMightySpud Wed Aug 05, 2020 5:08 pm
Very baffled about the projection direction tbh, as it's the same as it originally was, just with a 360 rotation. So very very stupid. lol.
TheMightySpud
Licensed Customer
Licensed Customer
 
Posts: 70
Joined: Thu Jun 06, 2013 11:19 am

Re: OSL Shader code knowledge?

Postby baltort » Mon Aug 10, 2020 5:07 pm

baltort Mon Aug 10, 2020 5:07 pm
I hacked the vRoom C4D OSL to work in Lightwave a couple of years ago. Does this do what you need?:

viewtopic.php?f=36&t=66988

I think there's an OSL camera to bake rooms in that thread as well, but I'm struggling to remember how it works...

Shout if you need anything,

James.
baltort
Licensed Customer
Licensed Customer
 
Posts: 72
Joined: Mon Jul 07, 2014 4:00 pm

Return to Lightwave 3D


Who is online

Users browsing this forum: No registered users and 3 guests

Tue Jun 04, 2024 12:57 am [ UTC ]