Advertisement
Dorex

PBR Alpha

Sep 5th, 2024 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //##############################################################################
  3. //
  4. // Simply:PBRTextureUpdater v0.2
  5. //
  6. // Since the inception of PBR Materials, builders have complained about missing
  7. // functions to easily set the Alpha and Color of PBR materials.
  8. //
  9. // These included functions are designed to make life easier for builders and scripters
  10. //
  11. // These are free scripts, designed by me, and you can use them in any way you like.
  12. // Use them in your scripts, your builds, modify them, whatever you like.
  13. //
  14. // Just don't sell them as is.
  15. //
  16. // Dorex Delicioso 2024
  17. //
  18. // YouTube: https://www.youtube.com/@SecondLifeSimplyScripting
  19. // Marketplace: https://marketplace.secondlife.com/stores/173632
  20. //
  21. //##############################################################################
  22.  
  23. //##############################################################################
  24. //##############################################################################
  25. //
  26. // Main Functions
  27. //
  28. // setPBRAlpha(alpha, face)
  29. // setPBRColor(color, face)
  30. // setPBRLinkAlpha(link, alpha, face)
  31. // setPBRLinkColor(link, color, face);
  32. //
  33. //##############################################################################
  34. //##############################################################################
  35.  
  36. setPBRAlpha(float alpha, integer face){
  37.     //
  38.     // sets the alpha value for PBR material on one or more faces
  39.     //
  40.     setPBRData(LINK_THIS , face, alpha, <0,0,0> , "Alpha");
  41. }
  42.  
  43. setPBRColor(vector color, integer face){
  44.     //
  45.     // sets the color value for PBR material on one or more faces
  46.     //
  47.     setPBRData(LINK_THIS , face, 0, color , "Color");
  48. }
  49.  
  50. setPBRLinkAlpha(integer link, float alpha, integer face){
  51.     //
  52.     // sets the alpha value for a PBR material on one or more faces on a specified link or all links
  53.     //
  54.     setPBRData(link, face, alpha, <0,0,0> , "Alpha");
  55. }
  56.  
  57. setPBRLinkColor(integer link, vector color, integer face){
  58.     //
  59.     // sets the color tint value for a PBR material on one or more faces on a specified link or all links
  60.     //
  61.     setPBRData(link, face, 0, color , "Color");
  62. }
  63.  
  64. //##############################################################################
  65. //##############################################################################
  66. //
  67. // Helper Scripts
  68. //
  69. //##############################################################################
  70. //##############################################################################
  71.  
  72. setPBRData(integer link, integer face, float alpha, vector color, string changeType){
  73.     //
  74.     // this is just a helper function, the ugly starts here
  75.     //
  76.     integer stride = 9;   // number of values returned for each face
  77.     list params;
  78.  
  79.     integer startLink = link;
  80.     integer endLink = link;
  81.  
  82.     if (startLink == LINK_SET){
  83.         startLink = !!llGetLinkNumber();
  84.         endLink = llGetObjectPrimCount(llGetKey());
  85.     }
  86.  
  87.     integer alphaMode = PRIM_GLTF_ALPHA_MODE_OPAQUE;
  88.     if (alpha < 1.0){
  89.         alphaMode = PRIM_GLTF_ALPHA_MODE_BLEND;
  90.     }
  91.  
  92.     for( ; startLink <= endLink; ++startLink){
  93.         //
  94.         // to set a PBR value without losing other data, we have read all settings first, then change only what we want
  95.         // while that's not 'always' exactly true, it's safer to assume it is.
  96.         // we read PRIM_GLTF_BASE_COLOR once per link
  97.         //
  98.         list details = llGetLinkPrimitiveParams(startLink, [PRIM_GLTF_BASE_COLOR, ALL_SIDES ]);
  99.         //
  100.         // return values for each face for PRIM_GLTF_BASE_COLOR
  101.         // texture, repeats, offsets, rotation_in_radians, color, alpha, gltf_alpha_mode, alpha_mask_cutoff, double_sided  
  102.         // texture, repeats, offsets, rotation_in_radians, color, alpha, gltf_alpha_mode, alpha_mask_cutoff, double_sided
  103.         // etc
  104.         //
  105.         integer startFace = face;
  106.         integer endFace = face + 1;
  107.  
  108.         if (startFace == ALL_SIDES ){
  109.             startFace = 0;
  110.             endFace = llGetLinkNumberOfSides(startLink);
  111.         }
  112.  
  113.         params += [PRIM_LINK_TARGET, startLink];
  114.  
  115.         for( ; startFace < endFace; ++startFace){
  116.             //
  117.             // set the start parameter and face number
  118.             //
  119.             params += [PRIM_GLTF_BASE_COLOR,  startFace];
  120.  
  121.             if (changeType == "Alpha"){
  122.                 //
  123.                 // we're changing the alpha value
  124.                 // take the first 5 values up to alpha for this face
  125.                 //
  126.                 params += llList2List(details, (startFace * stride),  (startFace * stride) + 4);
  127.                 //
  128.                 // add alpha and alphaMode for this face
  129.                 //
  130.                 params += [alpha, alphaMode];
  131.                 //
  132.                 // add the last 2 values for this face
  133.                 //
  134.                 params += llList2List(details, (startFace * stride) + 7,  (startFace * stride) + 8);            
  135.        
  136.             } else if (changeType == "Color"){
  137.                 //
  138.                 // we're chaning the color value
  139.                 // take the first 4 values up to color for this face
  140.                 //
  141.                 params += llList2List(details, (startFace * stride),  (startFace * stride) + 3);
  142.                 //
  143.                 // add the color for this face
  144.                 //
  145.                 params += [color];
  146.                 //
  147.                 // add the last 4 values for this face
  148.                 //
  149.                 params += llList2List(details, (startFace * stride) + 5,  (startFace * stride) + 8);
  150.             }          
  151.         }
  152.     }
  153.  
  154.     llSetLinkPrimitiveParamsFast(1, params);
  155.  
  156. }
  157.  
  158. default
  159. {
  160.     touch_start(integer total_number)
  161.     {
  162.         llOwnerSay((string)llGetUsedMemory());
  163.         //
  164.         // Uncomment each function call one at a time and touch the linkset to run the script
  165.         //
  166.         // for single prims
  167.         //
  168.         // {alpha 0.0 = transparent => 1.0 = opaque}, {face # or ALL_SIDES}
  169.         // setPBRAlpha(1, 0 );  
  170.         //
  171.         // {color}, {face # or ALL_SIDES}
  172.         // setPBRColor(<1, 1, 1>, 0 );  
  173.         //
  174.         // for linksets
  175.         //
  176.         // {Link # or LINK_SET} ,{alpha 0 == transparent => 1.0 = opaque}, {face number or ALL_SIDES}
  177.         // setPBRLinkAlpha(LINK_SET  , 1.1, ALL_SIDES  );
  178.         //
  179.         // {Link # or LINK_SET}, {color}, {face number or ALL_SIDES}
  180.          setPBRLinkColor(LINK_SET, <1, 0, 0> , 0);        
  181.     }
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement