Pixel Shader Displacement and Lighting

by sdfsafasas   Last Updated August 14, 2019 17:13 PM

I'm doing some displacement mapping on a grid.

enter image description here

Ambient light works well as seen in the picture but I cannot get the directional light working. When adding directional light, grid is yellow in color.

Here is the pixel shader

float4 PS(DomainOut pin) : SV_Target
{

  float4 diffuseAlbedo = gDiffuseMap.Sample(gsamLinearWrap, pin.Tex) * gDiffuseAlbedo;

  pin.NormalW = normalize(pin.NormalW);

  float4 normalMapSample = gNormalMap.Sample(gsamLinearWrap,pin.Tex);
  float3 bumpedNormalW = NormalSampleToWorldSpace(normalMapSample.rgb, pin.NormalW, pin.TangentW);

  // Uncomment to turn off normal mapping.
  //bumpedNormalW = pin.NormalW;

  // Dynamically look up the texture in the array.
  diffuseAlbedo *= gDiffuseMap.Sample(gsamLinearWrap, pin.Tex);

  // Vector from point being lit to eye. 
  float3 toEyeW = normalize(gEyePosW - pin.PosL);
  float distToEye = length(toEyeW);

  toEyeW /= distToEye;

  float4 ambient = gAmbientLight * diffuseAlbedo;

  const float shininess = 1.0f - gRoughness * normalMapSample.a;
  Material mat = { diffuseAlbedo, gFresnelR0, shininess };
  float3 shadowFactor = 1.0f;
  float4 directLight = ComputeLighting(gLights, mat, pin.PosL,
      bumpedNormalW, toEyeW, shadowFactor);

  float4 litColor = ambient + directLight;


  litColor.a = diffuseAlbedo.a;

  return litColor;
}
Tags : hlsl


Related Questions



Convert DX9 TO DX11 HLSL

Updated July 19, 2017 18:13 PM


Making a grid wave

Updated May 10, 2016 08:05 AM

Easiest way of adding screen space reflections?

Updated October 23, 2016 09:05 AM