-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixelLighting_ps.hlsl
76 lines (54 loc) · 4.04 KB
/
PixelLighting_ps.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//--------------------------------------------------------------------------------------
// Per-Pixel Lighting Pixel Shader
//--------------------------------------------------------------------------------------
// Pixel shader receives position and normal from the vertex shader and uses them to calculate
// lighting per pixel. Also samples a samples a diffuse + specular texture map and combines with light colour.
#include "Common.hlsli" // Shaders can also use include files - note the extension
//--------------------------------------------------------------------------------------
// Textures (texture maps)
//--------------------------------------------------------------------------------------
// Here we allow the shader access to a texture that has been loaded from the C++ side and stored in GPU memory.
// Note that textures are often called maps (because texture mapping describes wrapping a texture round a mesh).
// Get used to people using the word "texture" and "map" interchangably.
Texture2D DiffuseSpecularMap : register(t0); // Textures here can contain a diffuse map (main colour) in their rgb channels and a specular map (shininess) in the a channel
SamplerState TexSampler : register(s0); // A sampler is a filter for a texture like bilinear, trilinear or anisotropic - this is the sampler used for the texture above
//--------------------------------------------------------------------------------------
// Shader code
//--------------------------------------------------------------------------------------
// Pixel shader entry point - each shader has a "main" function
// This shader just samples a diffuse texture map
float4 main(LightingPixelShaderInput input) : SV_Target
{
// Normal might have been scaled by model scaling or interpolation so renormalise
input.worldNormal = normalize(input.worldNormal);
///////////////////////
// Calculate lighting
// Direction from pixel to camera
float3 cameraDirection = normalize(gCameraPosition - input.worldPosition);
//// Light 1 ////
// Direction and distance from pixel to light
float3 light1Direction = normalize(gLight1Position - input.worldPosition);
float3 light1Dist = length(gLight1Position - input.worldPosition);
// Equations from lighting lecture
float3 diffuseLight1 = gLight1Colour * max(dot(input.worldNormal, light1Direction), 0) / light1Dist;
float3 halfway = normalize(light1Direction + cameraDirection);
float3 specularLight1 = diffuseLight1 * pow(max(dot(input.worldNormal, halfway), 0), gSpecularPower); // Multiplying by diffuseLight instead of light colour - my own personal preference
//// Light 2 ////
float3 light2Direction = normalize(gLight2Position - input.worldPosition);
float3 light2Dist = length(gLight2Position - input.worldPosition);
float3 diffuseLight2 = gLight2Colour * max(dot(input.worldNormal, light2Direction), 0) / light2Dist;
halfway = normalize(light2Direction + cameraDirection);
float3 specularLight2 = diffuseLight2 * pow(max(dot(input.worldNormal, halfway), 0), gSpecularPower);
// Sum the effect of the lights - add the ambient at this stage rather than for each light (or we will get too much ambient)
float3 diffuseLight = gAmbientColour + diffuseLight1 + diffuseLight2;
float3 specularLight = specularLight1 + specularLight2;
////////////////////
// Combine lighting and textures
// Sample diffuse material and specular material colour for this pixel from a texture using a given sampler that you set up in the C++ code
float4 textureColour = DiffuseSpecularMap.Sample(TexSampler, input.uv);
float3 diffuseMaterialColour = textureColour.rgb; // Diffuse material colour in texture RGB (base colour of model)
float specularMaterialColour = textureColour.a; // Specular material colour in texture A (shininess of the surface)
// Combine lighting with texture colours
float3 finalColour = diffuseLight * diffuseMaterialColour + specularLight * specularMaterialColour;
return float4(finalColour, 1.0f); // Always use 1.0f for output alpha - no alpha blending in this lab
}