Advertisement
Guest User

SWTOR: extract from shader code for hueing Garment materials

a guest
Sep 13th, 2013
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. // ===================================================================================
  2. // HUEING
  3. // ===================================================================================
  4. //Utility Functions
  5. float3 ExpandHSL(in float3 HSL)
  6. {
  7.     //float3 outputVal = inVal * (maxVal - minVal) + minVal;
  8.     float3 expanded = HSL;
  9.     expanded.r = (HSL.r * (.706 - .3137)) + .3137; //reexpand hue
  10.     expanded.r -= .41176; //offset hue
  11.     expanded.g = (HSL.g * .5882);
  12.     expanded.b = (HSL.b * .70588);
  13.     return expanded;
  14. }
  15.  
  16. float3 AdjustLightness(in float3 HSL, float brightness, float contrast) //Adjust the lightness of the HSL
  17. {
  18.     HSL.b = pow(HSL.b, contrast) * contrast;
  19.     HSL.b = brightness + ((1 - brightness) * HSL.b);
  20.     return HSL;
  21. }
  22.  
  23. float OffsetHue(in float hue, float hueOffset)
  24. {
  25.     float H = frac(hue + hueOffset);
  26.     return H;
  27. }
  28. float OffsetSaturation(in float saturation, float satOffset)
  29. {
  30.     float S = saturation;
  31.     S = pow(S, satOffset);
  32.     S = S * (1 - satOffset);
  33.     S = saturate(S);
  34.     return S;
  35. }
  36.  
  37. float3 OffsetHSL(in float3 HSL, float hueOffset, float satOffset)
  38. {
  39.     float H = OffsetHue(HSL.x, hueOffset);
  40.     float S = OffsetSaturation(HSL.y, satOffset);
  41.    
  42.     return float3(H, S, HSL.z);
  43. }
  44.  
  45. float3 ConvertHSLToRGB(in float3 HSL)
  46. {
  47.     const float fOneThird = 0.333333333f;
  48.     const float fTwoThirds = 0.666666666f;
  49.     float3 color;
  50.    
  51.     float temp1, temp2;
  52.     float H = HSL.r;
  53.     float S = HSL.g;
  54.     float L = HSL.b;
  55.    
  56.     //if (S == 0)
  57.     //  return float3(L, L, L);
  58.      
  59.     float LtimesS = L*S;
  60.    
  61.     if (L<0.5f)
  62.         temp2 = L + LtimesS;
  63.     else
  64.         temp2 = L + S - LtimesS;
  65.  
  66.     temp1 = 2.0f*L-temp2;
  67.    
  68.     float3 temp3 = frac(float3(H+fOneThird, H, H-fOneThird));
  69.    
  70.     float3 temp3Times6 = 6.0f * temp3;
  71.     float3 temp3Times2 = 2.0f * temp3;
  72.     float3 temp3Times1point5 = 1.5f * temp3;
  73.     float3 firstEquation = temp1+(temp2-temp1)*6.0f*temp3;
  74.     float3 secondEquation = temp1+(temp2-temp1)*(fTwoThirds-temp3)*6.0f;
  75.    
  76.     if (temp3Times6.r < 1.0f)
  77.         color.r = firstEquation.r;
  78.     else if (temp3Times2.r < 1.0f)
  79.         color.r = temp2;
  80.     else if (temp3Times1point5.r < 1.0f)
  81.         color.r = secondEquation.r;
  82.     else
  83.         color.r = temp1;
  84.        
  85.     if (temp3Times6.g < 1.0f)
  86.         color.g = firstEquation.g; 
  87.     else if (temp3Times2.g < 1.0f)
  88.         color.g = temp2;
  89.     else if (temp3Times1point5.g < 1.0f)
  90.         color.g = secondEquation.g;
  91.     else
  92.         color.g = temp1;   
  93.        
  94.     if (temp3Times6.b < 1.0f)
  95.         color.b = firstEquation.b;
  96.     else if (temp3Times2.b < 1.0f)
  97.         color.b = temp2;
  98.     else if (temp3Times1point5.b < 1.0f)
  99.         color.b = secondEquation.b;
  100.     else
  101.         color.b = temp1;
  102.    
  103.     return color;
  104. }
  105.  
  106.  
  107.  
  108. float3 ManipulateHSL(in float3 HSL, float4 palette)
  109. {
  110.     HSL = ExpandHSL(HSL);
  111.     HSL = AdjustLightness(HSL, palette.z, palette.w);
  112.     HSL = OffsetHSL(HSL.rgb, palette.x, palette.y);
  113.     return HSL;
  114. }
  115.  
  116. float ManipulateAO(in float ao, float brightness, float contrast)
  117. {
  118.     brightness += 1;
  119.     //brightness + ((1 - brightness) * HSL.b);
  120.     float ret = ao * (brightness + (1 - brightness) * ao);
  121.     return saturate(ret);
  122. }
  123.  
  124.  
  125. void HuePixel(
  126.     in float4 diffuseMapValue,
  127.     in float4 specularMapValue,
  128.     float4 paletteMaskMapValue,
  129.     float4 paletteMapValue,
  130.     out float3 fragmentDiffuseColor,
  131.     out float4 fragmentSpecularColor
  132.     )
  133. {
  134.     fragmentDiffuseColor = diffuseMapValue.rgb;
  135.     fragmentSpecularColor = specularMapValue;
  136.  
  137.     float paletteMaskSum = paletteMaskMapValue.x + paletteMaskMapValue.y;
  138.    
  139.    
  140.     // Only use the palette that applies
  141.     float4 palette;
  142.     float3 chosenSpecColor, chosenMetallicSpecColor;
  143.  
  144.     if (paletteMaskMapValue.x < paletteMaskMapValue.y)
  145.         {
  146.             palette = palette2;
  147.             chosenSpecColor = palette2Specular.rgb;
  148.             chosenMetallicSpecColor = palette2MetallicSpecular.rgb;
  149.         }
  150.         else
  151.         {
  152.             palette = palette1;
  153.             chosenSpecColor = palette1Specular.rgb;
  154.             chosenMetallicSpecColor = palette1MetallicSpecular.rgb;
  155.         }
  156.  
  157.         // Get the palette map, apply the deltas, and convert it to RGB
  158.         float3 HSL = ManipulateHSL(float3(paletteMapValue.g, paletteMapValue.b, paletteMapValue.a), palette);
  159.         float ambientOcclusion = ManipulateAO(paletteMapValue.r, palette.z, palette.w);
  160.         HSL.z *= ambientOcclusion;
  161.         float3 RGB = ConvertHSLToRGB(HSL);
  162.  
  163.         //Blend the result into the original diffuse
  164.         fragmentDiffuseColor = lerp(diffuseMapValue.rgb, RGB.rgb, paletteMaskSum);
  165.  
  166.         // Determine the hue specular color
  167.         float3 hueSpecColor;
  168.         const float3 white = float3(1,1,1);
  169.         float metallicMask = paletteMaskMapValue.b;
  170.         //1.0 uses chosenMetallicSpecColor, .5 uses white, 0.0 uses chosenSpecColor      
  171.         if( metallicMask > .5 )
  172.         {
  173.             hueSpecColor = lerp(white, chosenMetallicSpecColor, (metallicMask - 0.5) * 2);
  174.         }
  175.         else
  176.         {
  177.             hueSpecColor = lerp(chosenSpecColor, white, metallicMask * 2);
  178.         }
  179.         hueSpecColor *= specularMapValue.r;
  180.         fragmentSpecularColor.rgb = lerp(fragmentSpecularColor.rgb, hueSpecColor, paletteMaskSum);
  181.    
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement