Advertisement
Krythic

Voidwalker Forward Rendering v3

May 24th, 2022
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. struct Vertex
  2. {
  3.     float4 pos : POSITION;
  4.     float2 tex : TEXTURE;
  5.     float3 norm : NORMAL;
  6. };
  7.  
  8. struct PixelShaderArgs
  9. {
  10.     float4 pos : SV_POSITION;
  11.     float2 col : TEXTURE;
  12.     float3 norm : NORMAL;
  13.     float3 worldPos : POSITION;
  14. };
  15.  
  16. struct PointLightShaderArgs
  17. {
  18.     float3 position;
  19.     float radius;
  20.     float intensity;
  21.     float3 padding;
  22.     float4 ambient;
  23.     float4 color;
  24. };
  25.  
  26. Texture2D ShaderTexture : register(t0);
  27. SamplerState Sampler : register(s0);
  28.  
  29. float4x4 localMatrix : register(b0);
  30. cbuffer ShaderDataBuffer : register(b1)
  31. {
  32.     float2 TextureResolution;
  33. };
  34. cbuffer cbPerFrame : register(b3)
  35. {
  36.     PointLightShaderArgs light[8];
  37. };
  38.  
  39. cbuffer WorldPositionBuffer : register(b4)
  40. {
  41.     float4x4 World;
  42. };
  43.  
  44. PixelShaderArgs VertexShaderMain(Vertex vertex)
  45. {
  46.     PixelShaderArgs output;
  47.     output.pos = mul(vertex.pos, localMatrix);
  48.     output.col = vertex.tex;
  49.     output.norm = mul(vertex.norm, World);
  50.     output.worldPos = mul(vertex.pos, World);
  51.     return output;
  52. }
  53.  
  54. int2 convertUVToPixel(float u, float v)
  55. {
  56.     int width = TextureResolution.x;
  57.     int height = TextureResolution.y;
  58.     int xCoordinate = floor(u * width);
  59.     int yCoordinate = floor(v * height);
  60.     return int2(xCoordinate % width, yCoordinate % height);
  61. }
  62.  
  63. float Falloff(float distance, float radius)
  64. {
  65.     return clamp(1.0f - (distance / radius), 0.0, 1.0);
  66. }
  67.  
  68. #define ATTENUATION_CONSTANT 1.0f // 0% Constant
  69. #define ATTENUATION_LINEAR 0.0f // 100% Linear
  70. #define ATTENUATION_QUADRATIC 0.0f // 100% Quadratic
  71.  
  72.  
  73. float4 PixelShaderMain(PixelShaderArgs pixelShaderArgs) : SV_Target
  74. {
  75.     float u = pixelShaderArgs.col.x;
  76.     float v = pixelShaderArgs.col.y;
  77.     // Lighting
  78.     float3 fragColor = float3(0.0f, 0.0f, 0.0f);
  79.     float4 diffuse = ShaderTexture.Load(int3(convertUVToPixel(u, v), 0));
  80.     pixelShaderArgs.norm = normalize(pixelShaderArgs.norm);
  81.     float3 ambient = diffuse.rgb * float3(0.3f, 0.3f, 0.3f);
  82.     for (int i = 0; i < 8; i++)
  83.     {
  84.         float3 lightToPixelVec = light[i].position - pixelShaderArgs.worldPos;
  85.         float distance = length(lightToPixelVec);
  86.         float luminosity = dot(lightToPixelVec / distance, pixelShaderArgs.norm);
  87.         if (luminosity > 0.0f)
  88.         {
  89.             // Do lighting attenuation
  90.             fragColor += luminosity * diffuse * light[i].color * Falloff(distance, light[i].radius) * light[i].intensity;
  91.             fragColor /= ATTENUATION_CONSTANT + (ATTENUATION_LINEAR * distance) + (ATTENUATION_QUADRATIC * (distance * distance));
  92.         }
  93.     }
  94.     fragColor = saturate(fragColor + ambient);
  95.    
  96.     /*
  97.         Gamma Correction
  98.     */
  99.     //float gamma = 2.2f;
  100.     //fragColor = pow(fragColor, 1.0f / gamma);
  101.     return float4(fragColor, diffuse.a);
  102.        
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement