Advertisement
Guest User

HLSL Skinning Shader - Windows 10 FIX

a guest
Dec 28th, 2020
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.39 KB | None | 0 0
  1. float4x4 mProj;
  2. float4x4 mView;
  3. float4x4 mWorldMatrixArray;
  4.  
  5. int lightCount = 0;
  6. float4 lightAmbient[3] : LIGHTARRAYAMBIENT;
  7. float4 lightDiffuse[3] : LIGHTARRAYDIFFUSE;
  8. float4 lightDir[3] : LIGHTARRAYDIR;
  9. float4 CamPos = 0.0f;
  10.  
  11. float4 mtrAmbient : MATERIALAMBIENT;
  12. float4 mtrDiffuse : MATERIALDIFFUSE;
  13. float4 Ambient;
  14.  
  15. bool Skinning = 0;
  16. bool CanDiffuse = 0;
  17. bool CanFog = 0;
  18.  
  19. float4 Fog;
  20. float4 FogColor : FOGCOLOR;
  21.  
  22. float3x3 TexTrans;
  23. float3x3 TexTrans2;
  24.  
  25. texture t;
  26.  
  27. sampler samp =
  28. sampler_state
  29. {
  30.     Texture = <t>;
  31.     MinFilter = POINT;
  32.     MagFilter = POINT;
  33.     MipFilter = NONE;
  34.     AddressU = CLAMP;
  35.     AddressV = CLAMP;
  36. };
  37.  
  38.  
  39. sampler2D samp1 : register(s0);
  40. sampler2D samp2 : register(s1);
  41.  
  42. struct VS_INPUT
  43. {
  44.     float3 vPosition : POSITION0;
  45.     float3 BlendWeights : BLENDWEIGHT;
  46.     float4 BlendIndices : BLENDINDICES;
  47.     float3 vNormal : NORMAL0;
  48.     float2 vUV : TEXCOORD0;
  49. };
  50.  
  51. struct VS_OUTPUT
  52. {
  53.     float4 vPosition : POSITION0;
  54.     float4 Diffuse : COLOR0;
  55.     float2 vUV : TEXCOORD0;
  56.     float2 vUV2 : TEXCOORD1;
  57.     float3 vNormal : NORMAL0;
  58.     float3 vCamDir : NORMAL1;
  59.     float3 vLightDir : NORMAL2;
  60.     float vFog : FOG;
  61. };
  62.  
  63. float4x4 getMatrix(int nIdx)
  64. {
  65.     if (!Skinning)
  66.         return mWorldMatrixArray;
  67.  
  68.     float fIdx = ((float) nIdx / 256.0f);
  69.  
  70.     float4x4 m = float4x4(
  71.         tex2Dlod(samp, float4(float2(0.0, fIdx), 0.0f, 0.0f)),
  72.         tex2Dlod(samp, float4(float2(1.0f / 3.0f, fIdx), 0.0f, 0.0f)),
  73.         tex2Dlod(samp, float4(float2(2.0f / 3.0f, fIdx), 0.0f, 0.0f)),
  74.         tex2Dlod(samp, float4(float2(1.0f, fIdx), 0.0f, 0.0f)));
  75.  
  76.     return m;
  77. }
  78. float3 getMulSkinnedPos(float3 vPos, int nIdx, float fWeight,float4x4 mWord)
  79. {
  80.     if (nIdx == 0)
  81.         return (mul(float4(vPos, 1.0f), mWord) * fWeight).xyz;
  82.  
  83.     return (mul(float4(vPos, 1.0f), getMatrix(nIdx)) * fWeight).xyz;
  84. }
  85. float4 getDiffuse(float3 pos,float3 normal)
  86. {
  87.     float4 Diffuse = Ambient;
  88.      for (int i = 0; i < lightCount; i++)
  89.         {
  90.             if (CanDiffuse)
  91.             {
  92.                 if (lightDir[i].w < 0.0f)
  93.                 {
  94.                     // Point Light             
  95.                     float fDist = distance(lightDir[i].xyz, pos);
  96.                     float lMul = fDist * lightAmbient[i].x;
  97.            
  98.                     if (lMul < 1.0f)
  99.                     {
  100.                         float3 fColor = lerp(0.f, lightDiffuse[i].xyz, lMul);
  101.                         float3 normlPos = normalize(lightDir[i].xyz);
  102.                         float fIntenzity = max(-dot(normal, normlPos), 0.0f);
  103.                         float3 mulVar = (fIntenzity * mtrDiffuse.xyz) * fColor;
  104.  
  105.                         Diffuse.xyz += mulVar;
  106.                     }
  107.                 }
  108.                 else
  109.                 {
  110.                     //Direction Light
  111.            
  112.                     float fIntenzity = saturate(-dot(normal, lightDir[i].xyz));
  113.                     float4 mulVar = fIntenzity * mtrDiffuse * lightDiffuse[i];
  114.                     Diffuse += max(mulVar, mtrAmbient * lightAmbient[i]);          
  115.                 }
  116.             }
  117.             else
  118.                 Diffuse += mtrAmbient * lightAmbient[i];
  119.  
  120.         }
  121.     return saturate(float4(Diffuse.xyz, mtrDiffuse.w));
  122. }
  123. VS_OUTPUT VSkin(VS_INPUT Input)
  124. {
  125.         VS_OUTPUT Output;
  126.  
  127.         float3 Position = float3(0.0f, 0.0f, 0.0f);
  128.         float3 Normal = 0.0f;
  129.  
  130.         float4x4 mWorld = getMatrix(0);
  131.  
  132.         if (Skinning)
  133.         {
  134.             int4 aiIndices = D3DCOLORtoUBYTE4(Input.BlendIndices);
  135.             float fWeight = 1.0f;
  136.             for (int i = 0; i < 3; i++)
  137.             {
  138.                 if(Input.BlendWeights[i] > 0.0f)
  139.                 {
  140.                     Position += getMulSkinnedPos(Input.vPosition, aiIndices[i], Input.BlendWeights[i],mWorld);
  141.                     fWeight -= Input.BlendWeights[i];
  142.                 }
  143.             }
  144.  
  145.             if(fWeight)
  146.             {
  147.                 Position += getMulSkinnedPos(Input.vPosition, aiIndices[3], fWeight,mWorld);
  148.             }
  149.         }
  150.         else
  151.         {
  152.             Position = Position = mul(float4(Input.vPosition, 1.0f), mWorld);
  153.         }
  154.    
  155.         Normal = mul(Input.vNormal, (float3x3) mWorld);
  156.  
  157.         Output.vCamDir = Position - CamPos.xyz;
  158.  
  159.         Output.vPosition = mul(float4(Position, 1.0f), mView);
  160.         Output.vFog = CanFog ? Fog.w < 0.0f ? saturate((Fog.y - Output.vPosition.z) / (Fog.y - Fog.x)) : (length(Output.vPosition.xyz) * Fog.z) : 1;
  161.  
  162.         Output.vPosition = mul(Output.vPosition, mProj);
  163.      
  164.         Output.vNormal = normalize(Normal);
  165.         Output.vLightDir = lightDir[0].xyz;
  166.         Output.Diffuse = getDiffuse(Position,Output.vNormal);
  167.         Output.vUV = mul(float3(Input.vUV, 1.0f), TexTrans).xy;
  168.         Output.vUV2 = mul(float3(Input.vUV, 1.0f), TexTrans2).xy;
  169.  
  170.         return Output;
  171.     }
  172.  
  173. float4 TextureFactor;
  174. float4 PixelArg;
  175. uint nPsShader;
  176.  
  177. float4 comp(float Arg, float4 c , float4 t)
  178. {
  179.     return Arg >= 0 ? c : t;
  180. }
  181.  
  182. float4 PixelShaderFunction(VS_OUTPUT Input): COLOR0
  183. {
  184.     float4 colorTex1 = comp(PixelArg.x, tex2D(samp1, Input.vUV), TextureFactor);
  185.     float4 colorTex2 = comp(PixelArg.y, tex2D(samp2, Input.vUV2), TextureFactor);
  186.     float4 color = 0;
  187.     switch (nPsShader)
  188.     {
  189.        
  190.         case 0:
  191.         {
  192.                 color = colorTex1;
  193.         }
  194.             break;
  195.         case 1:
  196.         {
  197.                 color = (colorTex1 * colorTex2);
  198.             }
  199.             break;
  200.         case 2:
  201.         {
  202.                 color = (colorTex1 * 2.0f) * colorTex2;
  203.             }
  204.             break;
  205.         case 3:
  206.         {
  207.                 color = (colorTex1 * 4.0f) * colorTex2;
  208.             }
  209.             break;
  210.         case 4:
  211.         {
  212.                 color = (colorTex1 + colorTex2);
  213.             }
  214.             break;
  215.         case 5:
  216.         {
  217.                 color = colorTex1 + colorTex2 - 0.5f;
  218.         }
  219.         break;
  220.         case 6:
  221.         {
  222.                 color = float4((2 * (colorTex1 + colorTex2 - 0.5f)).xyz, colorTex1.w);
  223.         }
  224.          break;
  225.         case 7:
  226.         {
  227.                 color = (colorTex1 - colorTex2);
  228.             }
  229.             break;
  230.         case 8:
  231.         {
  232.                 color = (mad(-colorTex1, colorTex2, colorTex2) + colorTex2);
  233.             }
  234.             break;
  235.         case 9:
  236.         {
  237.                 float blend = 1 - Input.Diffuse.w;
  238.                 float4 colb = colorTex1 * Input.Diffuse.wwww;
  239.                 color = mad(colorTex2, blend, colb);
  240.         }
  241.         break;
  242.         case 10:
  243.         {
  244.                 float blend = 1 - colorTex2.w;
  245.                 float4 colb = colorTex1 * colorTex2.w;
  246.                 color = mad(colorTex2, blend, colb);
  247.             }
  248.             break;
  249.         case 11:
  250.         {
  251.                 float blend = 1 - TextureFactor.w;
  252.                 float4 colb = colorTex1 * TextureFactor.w;
  253.                 color = mad(colorTex2, blend, colb);
  254.             }
  255.             break;
  256.         case 12:
  257.         {
  258.                 float blend = 1 - colorTex2.w;
  259.                 color = mad(colorTex2, blend, colorTex1);
  260.             }
  261.             break;
  262.         case 13:
  263.         {
  264.                 float blend = 1 - colorTex1.w;
  265.                 float4 colb = colorTex1 * colorTex1.w;
  266.                 color = mad(colorTex2, blend, colb);
  267.             }
  268.             break;
  269.         case 14:
  270.         {
  271.                 float3 blend = mad(colorTex2, colorTex1.wwww, colorTex1);
  272.                 color = float4(blend, colorTex1.w);
  273.             }
  274.             break;
  275.         case 15:
  276.         {
  277.                 float4 blend = mad( colorTex1,colorTex2, colorTex1.wwww);
  278.                 color = float4(blend.xyz, colorTex1.w);
  279.             }
  280.             break;
  281.         case 16:
  282.         {
  283.  
  284.                 float blend = 1 - colorTex1.w;
  285.                 float3 col = mad(colorTex2, blend, colorTex1);
  286.                 color = float4(col, colorTex1.w);
  287.             }
  288.             break;
  289.         case 17:
  290.         {
  291.  
  292.                 float4 blend = 1 - colorTex1;
  293.                 float3 colr = mad(colorTex2, blend, colorTex1.wwww);
  294.                 color = float4(colr, colorTex1.w);
  295.  
  296.             }
  297.             break;
  298.         case 18:
  299.         {
  300.                 color = dot(colorTex1, colorTex2);
  301.             }
  302.             break;
  303.         case 19:
  304.         {
  305.                 color = mad(colorTex1, colorTex2, colorTex1);
  306.             }
  307.             break;
  308.         case 20:
  309.         {
  310.                 color = lerp(colorTex1, colorTex2, colorTex1);
  311.             }
  312.             break;
  313.         case 21:
  314.         {
  315.                 //Set alpha   and specular   color
  316.  
  317.                 float rcp = 1 / 257.0f;
  318.  
  319.                 float idk = mad(256.0f, colorTex2.y, colorTex2.z) * rcp;
  320.                 float alpha = mad(256.0f, colorTex2.w, colorTex2.x);
  321.                 colorTex1.w = alpha * rcp;
  322.  
  323.                 //Compute specular color
  324.  
  325.                 float3 View = normalize(Input.vCamDir) - lightDir[0].xyz;
  326.                 float3 Colr = normalize(View);
  327.                 float3 Normal = normalize(Input.vNormal);
  328.  
  329.                 float sColor = dot(Colr, Normal);
  330.                 sColor = max(sColor, 0.0f);
  331.                 sColor = pow(sColor, PixelArg.z);
  332.  
  333.                 color = mad(idk, sColor, colorTex1);
  334.  
  335.             }
  336.             break;
  337.     }
  338.  
  339.     float4 finalTex = float4(color.xyz * Input.Diffuse.xyz, min(color.w,Input.Diffuse.w));
  340.  
  341.     if (!CanFog || Input.vFog > 0.9999f)
  342.         return finalTex;
  343.  
  344.     return float4(lerp(FogColor.xyz, finalTex.xyz, Input.vFog), finalTex.w);
  345.  
  346. }
  347.  
  348. technique TSkinning
  349. {
  350.     pass p0
  351.     {
  352.         VertexShader = compile vs_3_0 VSkin();
  353.         PixelShader = compile ps_3_0 PixelShaderFunction();
  354.     }
  355. }
  356.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement