Advertisement
Guest User

shader_normalmap_fragment

a guest
Jan 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1.  
  2. /*
  3. varying vec2 v_vTexcoord;//Texture coordinates
  4. varying vec4 v_vColour;//Vertex color
  5. varying vec3 v_vPosition;//Vertex position
  6. varying vec3 v_vNormal;//Vertex normals
  7. varying float fog;//Fog amount#
  8. */
  9. struct PixelShaderInput{
  10. float4 Position : SV_POSITION;
  11. float3 Normal : NORMAL;
  12. float4 Colour : COLOR0;
  13. float2 Texcoord : TEXCOORD0;
  14. float Fog : FOG;
  15. };
  16.  
  17. /*
  18. #define LN 8 //Number of lights
  19. #define DL 8 //Number of directional lights
  20. */
  21. static const int LN = 8;
  22. static const int DL = 8;
  23.  
  24. /*
  25. uniform sampler2D normalmap; //normalmap
  26. */
  27. Texture2D normal_map : register(t1);
  28. SamplerState normal_map_sampler : register(s1);
  29.  
  30. uniform float3 ambience;
  31. uniform float light[LN*4];//x,y,z, and range for point lights
  32. uniform float lcolor[LN*3];//r,g,b for point lights
  33. uniform float dlight[DL*4];//x,y,z, and range for directional lights
  34. uniform float dcolor[DL*3];//r,g,b for directional lights
  35. uniform float3 fogcolor;
  36.  
  37. float3 lighting(float3 pos, float3 col, float3 norm, float3 lpos,float lrange, float3 lcol)
  38. {
  39. float N = normalize(lpos-pos);//Light vector
  40. float L = max(1.0-length(pos-lpos)/lrange,0.0);//Light range
  41.  
  42. return (col * lcol * L * pow(max(dot(norm,N),0.0),4.0));//Brightness calculation
  43. }
  44.  
  45. float3 dlighting(float3 col, float3 norm, float3 lpos, float3 lcol)
  46. {
  47. float3 N = normalize(lpos);//Light vector
  48. return (col * lcol * pow(max(dot(norm,N),0.0),4.0));//Brightness calculation
  49. }
  50.  
  51. float4 main(PixelShaderInput INPUT) : SV_TARGET {
  52.  
  53. float4 Col = INPUT.Colour * tex2D(gm_BaseTexture, INPUT.Texcoord);//Pixel color
  54.  
  55.  
  56. float3 normalvector = normalize(normal_map.Sample(normal_map_sampler,INPUT.Texcoord).rgb*2.0-1.0);
  57.  
  58.  
  59. // derivations of the fragment position
  60. float3 pos_dx = ddx(INPUT.Position);
  61. float3 pos_dy = ddy(INPUT.Position);
  62. // derivations of the texture coordinate
  63. float2 texC_dx = ddx(INPUT.Texcoord);
  64. float2 texC_dy = ddy(INPUT.Texcoord);
  65. // tangent vector and binormal vector
  66. float3 tangent = texC_dy.y * pos_dx - texC_dx.y * pos_dy;
  67. float3 bitangent = texC_dx.x * pos_dy - texC_dy.x * pos_dx;
  68. tangent = cross( cross( INPUT.Normal, tangent ), tangent ); // orthonormalization of the tangent vector
  69. bitangent = cross( bitangent, cross( bitangent, INPUT.Normal ) ); // orthonormalization of the binormal vectors to the normal vector
  70. bitangent = cross( cross( tangent, bitangent ), tangent ); // orthonormalization of the binormal vectors to the tangent vector
  71. float3x3 tbn = float3x3( normalize(tangent), normalize(bitangent), INPUT.Normal);
  72.  
  73. normalvector = mul(normalvector,tbn);
  74.  
  75. float3 total = float3(0.0,0.0,0.0);
  76. for(int i = 0;i < LN;i++)//Loop for all point lights
  77. {
  78. total += lighting(INPUT.Position, Col.rgb,normalvector,float3(light[i*4],light[i*4+1],light[i*4+2]),light[i*4+3],
  79. abs(float3(lcolor[i*3],lcolor[i*3+1],lcolor[i*3+2])));
  80. }
  81. for(int i = 0;i < DL;i++)//Loop for all directional lights
  82. {
  83. total += dlighting(Col.rgb,normalvector,float3(dlight[i*3],dlight[i*3+1],dlight[i*3+2]),
  84. abs(float3(dcolor[i*3],dcolor[i*3+1],dcolor[i*3+2])));
  85. }
  86. return float4(lerp(total+Col.rgb*ambience,fogcolor,clamp(INPUT.Fog,0.0,1.0)),Col.a);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement