home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 18759 / ROE.7z / QSSHADER_QSSkin_1.h < prev    next >
Encoding:
C/C++ Source or Header  |  2020-09-17  |  13.4 KB  |  505 lines

  1. #ifndef QSSHADER_QSSkin_H
  2. #define QSSHADER_QSSkin_H
  3. /*
  4. *   shader definition for skin
  5. */
  6. #include "DX12Defines.fxh"
  7. #include "QSConstant.fxh"
  8. #include "FunctionLib.fxh"
  9. #include "QSLighting.fxh"
  10. #include "QSShadow.fxh"
  11. #include "QSSkinning.fxh"
  12. #include "QSVertexCalc.fxh"
  13. #include "QSScreenSpaceDepth.fxh"
  14. #include "QSPhysicalLighting.fxh"
  15. #include "QSShaderAtmosphere.fxh"
  16. #include "PostFx.fxh"
  17. #include "DepthUtil.fxh"
  18. #include "QSMaterialDetail.fxh"
  19. #include "QSMaterialCommon.fxh"
  20. #include "QSShaderIndirectLighting.fxh"
  21. #include "QSMaterialScreenDoor.fxh"
  22.  
  23. float SkinGlossFactor;    // Not gloss now, for adjusting the saturation of spec. map.
  24.  
  25. float4 SpecularColor;    //xyz -- spec, w -- gloss
  26. float3 SkinColor;
  27.  
  28. //textures
  29. sampler2D NormalSampler;
  30. sampler2D SpecularMap;
  31.  
  32. sampler2D DiffuseMaskMap0; 
  33. sampler2D DiffuseMaskMap1;
  34. sampler2D DiffuseMaskMap2;
  35.  
  36.  
  37. float4 DiffuseMaskUvScaleBias0; //xy bias, zw scale
  38. float4 DiffuseMaskUvScaleBias1;
  39. float4 DiffuseMaskUvScaleBias2;
  40.  
  41. float3 DiffuseMaskColor0;
  42. float3 DiffuseMaskColor1;
  43. float3 DiffuseMaskColor2;
  44. #if NORMAL_DETAIL
  45. sampler2D NormalDetailMap;
  46. float DetailMapIntensity;
  47. #endif
  48. #if NORMAL_WRINKLE
  49. sampler2D NormalWrinkleMap;
  50. float WrinkleMapIntensity;
  51. #endif
  52. #if NORMAL_EXPRESSION
  53. sampler2D NormalExpressionMap;
  54. sampler2D NormalExpressionMapSrc;
  55. float4 NormalExpressionMapIntensity;//x for expression intensity , y for blend weight of two expressions
  56.                                     //z for src normal intensity , w for current normal intentsity
  57. float4 NormalExpressionSizeBias;//xy for offset , z for scale , w for slotIndex
  58. float4 NormalExpressionSizeBiasSrc;
  59. float  NormalExpressionIntensityScale;
  60. static const float4 cNormalExpressionOffsetBias[5] =
  61. {
  62.     {1.0f,1.0f,0.0f,0.0f},
  63.     {0.5f,0.5f,0.0f,0.0f},
  64.     {0.5f,0.5f,0.5f,0.0f},
  65.     {0.5f,0.5f,0.0f,0.5f},
  66.     {0.5f,0.5f,0.5f,0.5f}
  67. };
  68. #endif
  69. //float4 NormalMaskUvScaleBias;
  70.  
  71. float4 BaseDiffuseColor;
  72. #include "PostFxColor.fxh"
  73.  
  74. float3 CustomSatAndLum(float3 color, float3 colorAdjuster )
  75. {
  76.     float3 result;
  77.  
  78.     result = RgbToHsl(color);
  79.     result.g *= colorAdjuster.r;
  80.     result.b *= colorAdjuster.g;
  81.     return HslToRgb(result);
  82.  
  83. }
  84.  
  85. float3 CustomColor(float3 color, float3 colorAdjuster )
  86. {
  87.     float3 result;
  88.  
  89.     result = RgbToHsl(color);
  90.  
  91.     if( colorAdjuster.r != 0.0f )
  92.     {
  93.         result.r = colorAdjuster.r;
  94.     }
  95.     result.g *= colorAdjuster.g;
  96.     result.b *= colorAdjuster.b;
  97.     return HslToRgb(result);
  98.  
  99. }
  100.  
  101. float3 CustomSaturation( float3 rgb, float satFactor )
  102. {
  103.     float3 hsl;
  104.     hsl = RgbToHsl(rgb);
  105.     hsl.b *= satFactor;
  106.     return HslToRgb(hsl);
  107. }
  108.  
  109. //-------------------------------------------------------------------------
  110. //lighting ps and vs
  111. //-------------------------------------------------------------------------
  112. #if LIGHTING
  113. VsOut SkinLightingVS(VsIn param)
  114. {
  115.     VsOut result;
  116.  
  117.     //variable
  118.     float3 pos, normal, tangent, binormal;    
  119.  
  120.     //collect variable data
  121. #if VERTEX_COMPRESSION
  122.     //pos
  123.     pos = param.pos.xyz * param.pos.w;
  124.  
  125. #if QTANGENTS
  126.     float4 qtangents = DecompressCharToFloat(param.qtangents);
  127.     DecodeQTangents(qtangents, normal, tangent, binormal);
  128. #else
  129.     //normal
  130. #if HAS_NORMAL
  131.     normal = DecompressCharToFloat(param.normal);        
  132. #else
  133.     normal = 0.0f;
  134. #endif
  135.  
  136. #if NORMALMAP
  137.     float4 tempTangent = DecompressCharToFloat( param.tangent );
  138.     tangent = tempTangent.xyz;
  139.     binormal = DecompressCharToFloat( param.binormal );
  140. #if !HAS_NORMAL
  141.     normal =cross(tangent.xyz, binormal.xyz);
  142.     normal = normalize( normal );
  143.     if( tempTangent.w == -1.0f )
  144.     {
  145.         normal *= -1.0f;
  146.     }
  147. #endif
  148. #endif
  149. #endif // QTANGENTS
  150. #else
  151.     pos = param.pos.xyz;
  152.     normal = param.normal.xyz;
  153. #if NORMALMAP
  154.     tangent = param.tangent.xyz;
  155.     binormal = param.binormal.xyz;   
  156. #endif//NORMALMAP
  157. #endif
  158.  
  159.     result.pos = 0;
  160.     
  161. #if WRITE_VELOCITY    
  162.     result.curPos = 0;
  163.     result.prePos = 0;
  164. #if SKINNING
  165.     VertexTransformCalc(pos, param.blendWeight, param.blendIndices, result.pos, result.curPos, result.prePos, normal, tangent, binormal);
  166. #else
  167.     VertexTransformCalc(pos, result.pos, result.curPos, result.prePos, normal, tangent, binormal);
  168. #endif
  169.  
  170. #else //WRITE_VELOCITY
  171. #if SKINNING
  172.     VertexTransformCalc(pos, param.blendWeight, param.blendIndices, result.pos, normal, tangent, binormal);
  173. #else
  174.     VertexTransformCalc(pos, result.pos, normal, tangent, binormal);
  175. #endif
  176.     
  177. #endif //WRITE_VELOCITY
  178.  
  179.     result.normal = normal;
  180.  
  181. #if NORMALMAP
  182.     result.binormal = binormal;
  183.     result.tangent = tangent;    
  184. #endif
  185.  
  186.     //other property setup-------------------------------------------------
  187. #if SECOND_UV
  188.     result.uv0 = float4(param.uv0, param.uv1);      
  189. #else
  190.     result.uv0 = param.uv0;      
  191. #endif
  192.  
  193. #if FORWARD_LIGHTING
  194.     result.projPos = result.pos;
  195. #endif    
  196.     
  197.     return result;
  198. }
  199. #define alphaThreshold 0.05f
  200. #define LipsAlphaThreshold 0.4f
  201. #include "QSDeferredLighting.fxh"
  202. PsOut SkinLightingPS(VsOut param, float4 vPos:VPOS)
  203. {
  204.     PsOut result;    
  205.  
  206.     float2 uv, offsetUV, tattooUV;
  207.     float2 detailUv;
  208.     float tattooMask;
  209. #if SECOND_UV
  210.     detailUv = param.uv0.zw;
  211. #else
  212.     detailUv = param.uv0.xy;
  213. #endif
  214.  
  215.     uv = param.uv0.xy;
  216.     ScreenDoor(uv);
  217.  
  218.     float4 diffuseColor = tex2D(BaseSampler, MipMapLinear, uv);
  219.  
  220. #if EYEBROW
  221.     offsetUV = GetOffsetUV(uv, EyebrowSizeBias);
  222.     if( false == IsUVOutOfRange( offsetUV ) )
  223.     {
  224.         float4 eyebrowColor = tex2D(EyeBrowMap, MipMapLinear, offsetUV);
  225.         diffuseColor.rgb  = lerp(diffuseColor.rgb, eyebrowColor.rgb, eyebrowColor.a);
  226.     }
  227. #endif
  228.  
  229. #if BEARD
  230.     offsetUV = GetOffsetUV(uv, BeardSizeBias);
  231.     if( false == IsUVOutOfRange( offsetUV ) )
  232.     {
  233.         float4 beardColor = tex2D(BeardMap, MipMapLinear, offsetUV);
  234.  
  235.         if( beardColor.a > 0.0f )
  236.         {
  237.             diffuseColor.rgb  = lerp(diffuseColor.rgb, beardColor.rgb, beardColor.a);
  238.         }
  239.     }
  240. #endif
  241.  
  242. #if TATTOO 
  243.     tattooUV = GetOffsetUV(uv, TattooSizeBias);
  244.     if( false == IsUVOutOfRange( tattooUV ) )
  245.     {
  246.         float4 tattooColor = tex2D(TattooMap, MipMapLinear, tattooUV);
  247.  
  248.         if( tattooColor.a > 0.0f )
  249.         {
  250.             diffuseColor.rgb  = lerp(diffuseColor.rgb, diffuseColor.rgb*tattooColor.rgb*2, tattooColor.a);
  251.             tattooMask = tattooColor.a;
  252.         }
  253.     }
  254. #endif
  255.  
  256. #if EYESHADOW
  257.     offsetUV = GetOffsetUV(uv, EyeShadowSizeBias);
  258.     if( false == IsUVOutOfRange( offsetUV ) )
  259.     {
  260.         float4 eyeShadowColor = tex2D(EyeShadowMap, MipMapLinear, offsetUV);
  261.         if( eyeShadowColor.a > 0.0f )
  262.         {
  263.             eyeShadowColor.rgb = CustomSatAndLum(eyeShadowColor.rgb, EyeShadowColor);
  264.             diffuseColor.rgb  = lerp(diffuseColor.rgb, diffuseColor.rgb*eyeShadowColor.rgb*2, eyeShadowColor.a);
  265.         }
  266.     }
  267. #endif
  268.  
  269. #if EYELINER
  270.     offsetUV = GetOffsetUV(uv, EyelinerSizeBias );
  271.     if( false == IsUVOutOfRange( offsetUV ) )
  272.     {
  273.         float4 eyelinerColor = tex2D(EyelinerMap, MipMapLinear, offsetUV);
  274.         if( eyelinerColor.a > 0.0f )
  275.         {
  276.             diffuseColor.rgb  = lerp(diffuseColor.rgb, diffuseColor.rgb*eyelinerColor.rgb*2, eyelinerColor.a);
  277.         }
  278.     }
  279. #endif
  280.  
  281.     
  282.     diffuseColor.rgb = CustomColor(diffuseColor.rgb, SkinColor);
  283.  
  284.     //alpha test
  285. #if ALPHATEST
  286.     AlphaTest(diffuseColor.a);
  287. #endif
  288.  
  289.     float3 specColor = tex2D(SpecularMap, MipMapLinear, uv).xyz;
  290.  
  291.     specColor = CustomSaturation(specColor, SkinGlossFactor);
  292.  
  293.     float gloss;
  294.  
  295. #if NORMALMAP        
  296.     float3 normalTexSpace;    
  297.     ReadNormalGloss(NormalSampler, uv, normalTexSpace, gloss);
  298.     ComposeDetail(detailUv, normalTexSpace, diffuseColor.xyz, specColor, gloss);    
  299.  
  300. #if NORMAL_DETAIL
  301.     float detailGloss;
  302.     float3 normalDetail = 0;
  303.     ReadNormalGloss(NormalDetailMap, uv, normalDetail, detailGloss);
  304.     //normalDetail = normalDetail*2.0f-1.0f;
  305.     normalTexSpace.xy +=  normalDetail.xy*DetailMapIntensity;
  306.     
  307.     //normalTexSpace.xy = float2(1.0,0.0);
  308. #endif
  309.  
  310. #if NORMAL_WRINKLE
  311.     float wrinkleGloss;
  312.     float3 normalWrink = 0;
  313.     ReadNormalGloss(NormalWrinkleMap , uv , normalWrink , wrinkleGloss);
  314.     normalTexSpace.xy += normalWrink.xy *WrinkleMapIntensity;
  315. #endif
  316.  
  317. #if NORMAL_EXPRESSION
  318.     float expressionGloss;
  319.     float3 normalExpression = 0;
  320.     float3 normalExpressionSrc = 0;
  321.     float2 normalExpressionUVOffset = uv * NormalExpressionSizeBias.z + NormalExpressionSizeBias.xy;
  322.     float4 normalExpressionPackUVParam = cNormalExpressionOffsetBias[NormalExpressionSizeBias.w];
  323.     float2 normalExpressionUVOffsetSrc = uv * NormalExpressionSizeBiasSrc.z + NormalExpressionSizeBiasSrc.xy;
  324.     float4 normalExpressionPackUVParamSrc = cNormalExpressionOffsetBias[NormalExpressionSizeBiasSrc.w];
  325.  
  326.     float uvOutRangeWeight = getUVInRangeWeight(normalExpressionUVOffset);
  327.     ReadNormalGloss(NormalExpressionMap,normalExpressionUVOffset * normalExpressionPackUVParam.xy + normalExpressionPackUVParam.zw,normalExpression,expressionGloss);
  328.     normalExpression *= uvOutRangeWeight;
  329.  
  330.     uvOutRangeWeight = getUVInRangeWeight(normalExpressionUVOffsetSrc);
  331.     ReadNormalGloss(NormalExpressionMapSrc,normalExpressionUVOffsetSrc * normalExpressionPackUVParamSrc.xy + normalExpressionPackUVParamSrc.zw,normalExpressionSrc,expressionGloss);
  332.     normalExpressionSrc *= uvOutRangeWeight;
  333.  
  334.     normalExpression = lerp(normalExpressionSrc * NormalExpressionMapIntensity.z,normalExpression * NormalExpressionMapIntensity.w ,NormalExpressionMapIntensity.y);
  335.     normalTexSpace.xy += normalExpression.xy  * NormalExpressionMapIntensity.x * NormalExpressionIntensityScale;
  336.  
  337.     
  338. #endif
  339.  
  340.     normalTexSpace = normalize(normalTexSpace);
  341.  
  342. #if LIPS
  343.     offsetUV = GetOffsetUV(uv, LipsSizeBias);
  344.     if( false == IsUVOutOfRange( offsetUV ) )
  345.     {
  346.         float lipMask = tex2D(LipsMap, MipMapLinear, offsetUV).a;
  347.         if( lipMask > LipsAlphaThreshold )
  348.         {
  349.             gloss *= LipGlossFactor;
  350.         }
  351.  
  352.     }
  353. #endif
  354.  
  355. #if TATTOO && TATTOO_NORM
  356.     if( false == IsUVOutOfRange( tattooUV ) )
  357.     {
  358.         if(tattooMask > alphaThreshold )
  359.         {
  360.             ReadNormalGloss(TattooNormalMap, tattooUV, normalTexSpace, gloss);
  361.         }
  362.     }
  363. #endif
  364.  
  365.     float3 normalWorldSpace = mul(normalTexSpace, float3x3(param.tangent, param.binormal, param.normal)); 
  366.  
  367.     normalWorldSpace = normalize(normalWorldSpace);
  368. #else//NORMALMAP
  369.     float3 normalWorldSpace = normalize(param.normal);    
  370.     gloss = SpecularColor.w;
  371.  
  372.     ComposeDetail(detailUv, diffuseColor.xyz, specColor, gloss);
  373. #endif//NORMALMAP        
  374.  
  375.     //pack gbuffer
  376. #if FORWARD_LIGHTING
  377.     //direct lighting
  378.     float2 screenUv = (vPos.xy+0.5f)*BufferResolution.zw;
  379.  
  380.     float3 finalLighting,diffuseLighting, specLighting, adjustDiffuse;
  381.     float4 projPos = param.projPos /param.projPos.w;    
  382.     float3 toCamera = GetPartialWorldPosFromScreenPos(projPos);
  383.     float3 viewDir = normalize(toCamera);    
  384.     diffuseColor.rgb = pow(diffuseColor.rgb,2.2);
  385.     specColor.rgb = pow(specColor.rgb,2.2);
  386.     
  387.  
  388.     float xBias;
  389.     float2 smUv;        
  390.     float4 shadow =  CascadedShadowMap(float4(screenUv.xy,projPos.z,1), xBias, smUv);
  391.  
  392.     float ndl, ndh;
  393.     QSDirectLighting(
  394.         //output
  395.         finalLighting, diffuseLighting, specLighting, adjustDiffuse, ndl, ndh, 
  396.         //light property
  397.         SunLightDir, SunColor.xyz,
  398.         //material property
  399.         normalWorldSpace, viewDir.xyz, diffuseColor.xyz, gloss, specColor.xyz, SunColor.w, shadow);    
  400.  
  401. #if ENHANCE_INDIRECT_LIGHTING
  402.     float3 sndLightResult, sndDiffuseLighting, sndSpecLighting, sndAdjustDiffuse;    
  403.     float sndNdl, sndNdh;
  404.     QSDirectLighting(
  405.         //output
  406.         sndLightResult, sndDiffuseLighting, sndSpecLighting, sndAdjustDiffuse, sndNdl, sndNdh,
  407.         //light property
  408.         SecondDirLightDir.xyz, SecondDirLightColor.xyz,
  409.         //material property
  410.         normalWorldSpace, viewDir.xyz, diffuseColor, gloss, specColor*SecondDirLightDir.w, 1.0f, 1.0f);    
  411.  
  412.     finalLighting += sndLightResult;
  413. #endif
  414.  
  415.     float3 envDiffuse;
  416.  
  417.     QSAmbientLighting(
  418.         envDiffuse, 
  419.         SkyLightColor.xyz, GroundLightColor.xyz, 
  420.         normalWorldSpace, adjustDiffuse, SpecularIndirectLightingEnhance);
  421.  
  422.     finalLighting += envDiffuse;
  423.  
  424. #if ENHANCE_INDIRECT_LIGHTING
  425.     {
  426.         float envGloss = gloss*gloss*2.0f;
  427.         float3 envMapColor = SampleEnvMapLightColor(viewDir.xyz, normalWorldSpace, envGloss);    
  428.         float3 envMapPhyLighting = PhyLightingImageBasedSpecular(normalWorldSpace, viewDir, envGloss, specColor, EnvMapProbePos.w, envMapColor);     
  429.  
  430.         finalLighting += envMapPhyLighting;    
  431.     }
  432. #endif    
  433.  
  434.     result.color0 = float4(finalLighting.xyz,TransFactor);
  435.  
  436. #if FOG
  437.     ApplyFogWithZFade(result.color0, toCamera, screenUv);
  438. #endif
  439.  
  440. #else //FORWARD_LIGHTING
  441.  
  442.     PackGBufferNormalFit(result.color0, result.color1, result.color2, diffuseColor, normalWorldSpace, specColor, gloss);
  443.  
  444. #if WRITE_VELOCITY
  445.     // SKin should always be movable, so, no static case.
  446.     float2 velocity = ((param.curPos.xy/param.curPos.w) - (param.prePos.xy/param.prePos.w));
  447.     result.velocity = float4(EncodeVelocityToTexture(velocity), 0, 1);     // w=1 means need full Subsurface scattering effect.
  448. #endif
  449.     
  450. #endif //FORWARD_LIGHTING    
  451.     return result;
  452. }
  453. #endif//LIGHTING 
  454.  
  455. //-------------------------------------------------------------------------
  456. //depth(shadow) ps and vs
  457. //-------------------------------------------------------------------------
  458. #if DEPTH
  459. VsOut SkinDepthVS(VsIn param)
  460. {
  461.     VsOut result;
  462.  
  463.     float3 pos;
  464. #if VERTEX_COMPRESSION
  465.     pos = param.pos.xyz*param.pos.w;
  466. #else
  467.     pos = param.pos.xyz;
  468. #endif
  469.  
  470. #if SKINNING
  471.     VertexTransformCalc(pos, param.blendWeight, param.blendIndices, result.pos);
  472. #else
  473.     VertexTransformCalc(pos, result.pos);
  474. #endif
  475.  
  476. #if ALPHATEST
  477.     result.uv0 = param.uv0; 
  478. #endif//ALPHATEST
  479.  
  480. #if DEPTH_COLOR
  481.     result.homoDepth = float2(result.pos.z,result.pos.w);        
  482. #endif
  483.  
  484.     return result;
  485. }
  486.  
  487. PsOut SkinDepthPS(VsOut param)
  488. {
  489.     PsOut result;
  490.     result.color0 = 0.0f;
  491.  
  492. #if DEPTH_COLOR    
  493.     DepthEncodeToColor(param.homoDepth.x/param.homoDepth.y, result.color0);
  494. #endif
  495.  
  496. #if ALPHATEST
  497.     AlphaTest(tex2D(BaseSampler, MipMapLinear, param.uv0).a);
  498. #endif
  499.  
  500.     return result;
  501. }
  502. #endif//DEPTH
  503.  
  504. #endif // QSSHADER_QSSkin_H
  505.