sebban

PixelShader

Apr 17th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Texture2D diffTexture : register(t0);
  2. Texture2D ambientTexture : register(t1);
  3. Texture2D specularTexture : register(t2);
  4. Texture2D normalTexture: register(t3);
  5. Texture2D worldPosTexture: register(t4);
  6. SamplerState pointSampler : register(s0);
  7.  
  8. cbuffer LightConstantBuffer
  9. {
  10.     matrix worldMatrix;
  11.     matrix viewMatrix;
  12.     matrix projectionMatrix;
  13.     matrix lightViewMatrix;
  14.     matrix lightProjectionMatrix;
  15.  
  16.     float4 camPos;
  17.     float4 lightPos;
  18. };
  19.  
  20. struct PSInput
  21. {
  22.     float4 position : SV_POSITION;
  23.     float2 tex : TEXCOORD0;
  24. };
  25.  
  26. float4 main(PSInput input) : SV_TARGET
  27. {
  28.     float4 diffColor;
  29.     float4 ambientColor;
  30.     float4 specColor;
  31.     float4 normal;
  32.     float4 worldPos;
  33.     float4 outputColor;
  34.     float2 textCoords = float2(input.position.x / 800, input.position.y / 600);
  35.  
  36.     float lightIntensity = 0.0f;
  37.  
  38.     //Sample the diffuse texture from deferred render
  39.     diffColor = diffTexture.Sample(pointSampler, textCoords);
  40.  
  41.     //Sample the ambient texture from deferred render
  42.     ambientColor = ambientTexture.Sample(pointSampler, textCoords);
  43.  
  44.     //Sample the specual texture from deferred render
  45.     specColor = specularTexture.Sample(pointSampler, textCoords);
  46.  
  47.     //Sample the normal texture from deferred render
  48.     normal = normalTexture.Sample(pointSampler, textCoords);
  49.    
  50.     //Sample the texture with positions in world space from deferred render
  51.     worldPos = worldPosTexture.Sample(pointSampler, textCoords);
  52.  
  53.     //Create the normalized vector from position to light source
  54.     float3 outVec = normalize(lightPos.xyz - (worldPos).xyz);
  55.  
  56.     //Create the normalized reflection vector
  57.     float3 refVec = normalize(reflect(-outVec, normal));
  58.  
  59.     //Creathe the normalized vector from position to camera
  60.     float3 viewDir = normalize(camPos - worldPos).xyz;
  61.  
  62.     float specIntesity = saturate(dot(refVec, viewDir));
  63.     float shineFactor = 5.0f;
  64.     float lightSpecular = 0.65f;
  65.  
  66.     //Calculate the specular part
  67.     float4 specular = float4(specColor.rgb * lightSpecular * max(pow(specIntesity, shineFactor), 0.0f), 1.0f);
  68.  
  69.     lightIntensity = dot(normal, outVec);
  70.     if (lightIntensity < 0) {
  71.         lightIntensity = 0;
  72.     }
  73.  
  74.     //Combine everything for the output color
  75.     outputColor = saturate(((diffColor.rgba + specular.rgba) * lightIntensity * 0.8f) + ((ambientColor.rgba) * 0.2f));
  76.  
  77.     //return float4(input.tex.x, input.tex.y, 0.0f, 1.0f);
  78.     //return float4(input.position.x / 800, input.position.y / 600, 0.0f, 1.0f);
  79.     return outputColor;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment