Advertisement
Guest User

Pixel Shader

a guest
Aug 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. struct OutputVertex
  2. {
  3.     float4 xyzw : SV_POSITION;
  4.     float4 rgba : OCOLOR;
  5.     float2 uv : UV;
  6.     float3 nrm : NORMAL;
  7.     float4 world : WPOS;
  8.     float3 loc : LOCAL;
  9. };
  10.  
  11. struct Light
  12. {
  13.     float3 dir;
  14.     float x;
  15.     float4 ambient;
  16.     float4 diffuse;
  17.     float3 pos;
  18.     float range;
  19.     float3 att;
  20.     float y;
  21.     float3 dir2;
  22.     float cone;
  23. };
  24.  
  25. cbuffer cbperframe : register(b)
  26. {
  27.     Light light;
  28. };
  29.  
  30. Texture2D env : register(t0);
  31. SamplerState envFilter : register(s0);
  32.  
  33. float4 main(OutputVertex inputP) : SV_TARGET
  34. {
  35.  
  36.     // Directional
  37.     inputP.nrm = normalize(inputP.nrm);
  38.     float3 dir = normalize(light.dir);
  39.     float4 inp = env.Sample(envFilter, inputP.uv);
  40.     float3 final1 = inp * light.ambient;
  41.     final1 += saturate(dot(-dir, inputP.nrm) * light.diffuse * inp);
  42.     float4 dire = float4(final1, inp.a);
  43.  
  44.     // Point
  45.     float3 lightToPixel = light.pos - inputP.world.xyz;
  46.     float d = length(lightToPixel);
  47.     float3 ambi = inp * light.ambient;
  48.  
  49.     float4 pnt = float4(0.0f, 0.0f, 0.0f, 0.0f);
  50.     if (d < light.range)
  51.     {
  52.         float3 final2 = float3(0.0f, 0.0f, 0.0f);
  53.         lightToPixel /= d;
  54.         float howMuch = dot(lightToPixel, inputP.nrm);
  55.  
  56.         if (howMuch > 0.0f)
  57.         {
  58.             final2 += (howMuch * inp.xyz * float3(1.0f, 0.0f, 0.0f));
  59.             float rangeAtt = 1.0f - saturate(d / light.range);
  60.             final2 *= rangeAtt;
  61.         }
  62.  
  63.         final2 = saturate(final2);
  64.         pnt = float4(final2, inp.a);
  65.     }
  66.  
  67.     // Output
  68.     float4 combined = dire + pnt + float4(ambi, 0.0f);
  69.     float4 real = saturate(combined);
  70.     return real;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement