Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Vertex
- {
- float4 pos : POSITION;
- float2 tex : TEXTURE;
- float3 norm : NORMAL;
- };
- struct PixelShaderArgs
- {
- float4 pos : SV_POSITION;
- float2 col : TEXTURE;
- float3 norm : NORMAL;
- float3 worldPos : POSITION;
- };
- struct PointLightShaderArgs
- {
- float3 position;
- float radius;
- float intensity;
- float3 padding;
- float4 ambient;
- float4 color;
- };
- Texture2D ShaderTexture : register(t0);
- SamplerState Sampler : register(s0);
- float4x4 localMatrix : register(b0);
- cbuffer ShaderDataBuffer : register(b1)
- {
- float2 TextureResolution;
- };
- cbuffer cbPerFrame : register(b3)
- {
- PointLightShaderArgs light[8];
- };
- cbuffer WorldPositionBuffer : register(b4)
- {
- float4x4 World;
- };
- PixelShaderArgs VertexShaderMain(Vertex vertex)
- {
- PixelShaderArgs output;
- output.pos = mul(vertex.pos, localMatrix);
- output.col = vertex.tex;
- output.norm = mul(vertex.norm, World);
- output.worldPos = mul(vertex.pos, World);
- return output;
- }
- int2 convertUVToPixel(float u, float v)
- {
- int width = TextureResolution.x;
- int height = TextureResolution.y;
- int xCoordinate = floor(u * width);
- int yCoordinate = floor(v * height);
- return int2(xCoordinate % width, yCoordinate % height);
- }
- float Falloff(float distance, float radius)
- {
- return clamp(1.0f - (distance / radius), 0.0, 1.0);
- }
- #define ATTENUATION_CONSTANT 1.0f // 0% Constant
- #define ATTENUATION_LINEAR 0.0f // 100% Linear
- #define ATTENUATION_QUADRATIC 0.0f // 100% Quadratic
- float4 PixelShaderMain(PixelShaderArgs pixelShaderArgs) : SV_Target
- {
- float u = pixelShaderArgs.col.x;
- float v = pixelShaderArgs.col.y;
- // Lighting
- float3 fragColor = float3(0.0f, 0.0f, 0.0f);
- float4 diffuse = ShaderTexture.Load(int3(convertUVToPixel(u, v), 0));
- pixelShaderArgs.norm = normalize(pixelShaderArgs.norm);
- float3 ambient = diffuse.rgb * float3(0.3f, 0.3f, 0.3f);
- for (int i = 0; i < 8; i++)
- {
- float3 lightToPixelVec = light[i].position - pixelShaderArgs.worldPos;
- float distance = length(lightToPixelVec);
- float luminosity = dot(lightToPixelVec / distance, pixelShaderArgs.norm);
- if (luminosity > 0.0f)
- {
- // Do lighting attenuation
- fragColor += luminosity * diffuse * light[i].color * Falloff(distance, light[i].radius) * light[i].intensity;
- fragColor /= ATTENUATION_CONSTANT + (ATTENUATION_LINEAR * distance) + (ATTENUATION_QUADRATIC * (distance * distance));
- }
- }
- fragColor = saturate(fragColor + ambient);
- /*
- Gamma Correction
- */
- //float gamma = 2.2f;
- //fragColor = pow(fragColor, 1.0f / gamma);
- return float4(fragColor, diffuse.a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement