Ember

Basic Shaders

Feb 8th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. /* Basic vertex shader */
  2. void LimitedVS(
  3. float4 position : POSITION,
  4. float3 normal : NORMAL,
  5. // outputs
  6. out float4 oPosition : POSITION,
  7. out float3 oNormal : TEXCOORD0,
  8. out float3 oLightVec : TEXCOORD1,
  9. out float3 oHalfVec : TEXCOORD2,
  10. out float3 oViewVec : TEXCOORD3,
  11. // parameters
  12. uniform float4 lightPosition,
  13. uniform float3 eyePosition,
  14. uniform float4x4 wvpMat)
  15. {
  16. // calculate output position
  17. oPosition = mul(wvpMat, position);
  18.  
  19. // pass the main uvs and normals straight through unchanged
  20. oNormal = mul((float3x3)wMat, normal);
  21.  
  22. // get object space light direction
  23. float3 lightVec = normalize(lightPosition.xyz - (worldPosition.xyz * lightPosition.w));
  24.  
  25. // calculate half-angle
  26. float3 eyeVec = normalize(eyePosition - worldPosition.xyz);
  27.  
  28. // set output registers
  29. oHalfVec = eyeVec + lightVec;
  30. oLightVec = lightVec;
  31. oViewVec = eyeVec;
  32. }
  33.  
  34. /* Basic fragment program */
  35. float4 LimitedPS(
  36. float3 normal : TEXCOORD0,
  37. float3 lightVec : TEXCOORD1,
  38. float3 halfVec : TEXCOORD2,
  39. float3 viewVec : TEXCOORD3,
  40. // parameters
  41. uniform float3 lightColour,
  42. uniform float3 surfaceColour,
  43. uniform float specularHardness,
  44. uniform float specularIntensity) : COLOR
  45. {
  46. // retrieve vectors and normalize
  47. float3 N = normalize(normal);
  48. float3 L = normalize(lightVec);
  49. float3 H = normalize(halfVec);
  50. float3 V = normalize(viewVec);
  51.  
  52. // compute the diffuse factor
  53. float3 diffuseFactor = surfaceColour * saturate(dot(N, L)) * lightColour;
  54.  
  55. // compute the specular factor
  56. float3 specFactor = pow(saturate(dot(N, H)), specularHardness) * specularIntensity;
  57.  
  58. // simple phong shading
  59. float3 final = diffuseFactor + float3(specularFactor);
  60.  
  61. // calculate and return final colour with alpha
  62. return float4(final, 1);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment