Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. //NormalMapping.fx
  2. //FirstName: Igor
  3. //LastName: Vermeulen
  4. //Class 2DAE_GD05
  5.  
  6.  
  7. //GLOBAL VARIABLES
  8. //****************
  9. float4x4 gMatrixWorldViewProj : WORLDVIEWPROJECTION;
  10. float4x4 gMatrixWorld: WORLD;
  11.  
  12. float4 gColorAmbient: COLOR = float4(1.0,1.0,1.0,1.0);
  13.  
  14. float gAmbientIntensity<
  15. string UIName = "Ambient Intensity";
  16. string UIWidget = "Slider";
  17. float UIMin = 0.0;
  18. float UIMax = 1.0;
  19. float UIStep = 0.01;
  20. > = 0.0f;
  21.  
  22. //Diffuse Map Variables
  23. Texture2D gTextureDiffuse<
  24. string UIName = "Diffuse Texture";
  25. string ResourceName = "CobbleStone Textures/CobbleStone_DiffuseMap.dds";
  26. string ResourceType = "2D";
  27. >;
  28.  
  29. //Light
  30. float3 gLightDirection: DIRECTION<
  31. string UIName = "Light Direction";
  32. string Object = "TargetLight";
  33. > = float3 (-0.57 , -0.57, 0.57);
  34.  
  35. float4 gColorDiffuse : COLOR = float4(1.0, 1.0, 1.0, 1.0);
  36.  
  37. //Normal map Variables
  38. Texture2D m_normalMapTexture <
  39. string UIName = "Normal Map";
  40. string ResourceName = "CobbleStone Textures/CobbleStone_NormalMap.dds";
  41. string ResourceType = "2D";
  42. >;
  43. SamplerState m_NormalMapSampler
  44. {
  45. Filter = MIN_MAG_MIP_POINT;
  46. AddressU = WRAP;
  47. AddressV = WRAP;
  48. };
  49. //STATES
  50. //******
  51. RasterizerState DisableCulling { CullMode = NONE; };
  52.  
  53. SamplerState gDiffuseSamplerState
  54. {
  55. Filter = MIN_MAG_MIP_LINEAR;
  56. AddressU = WRAP;
  57. AddressV = WRAP;
  58. };
  59.  
  60. struct VS_INPUT
  61. {
  62. float3 Position: POSITION;
  63. float3 Normal: NORMAL;
  64. float2 TexCoord: TEXCOORD0;
  65. float3 tangent : TANGENT;
  66. };
  67.  
  68. struct VS_OUTPUT
  69. {
  70. float4 Position: SV_POSITION;
  71. float3 Normal: NORMAL;
  72. float2 TexCoord: TEXCOORD0;
  73. float3 tangent : TANGENT;
  74. };
  75.  
  76. //MAIN VERTEX SHADER
  77. //******************
  78. VS_OUTPUT MainVS(VS_INPUT input)
  79. {
  80. VS_OUTPUT output = (VS_OUTPUT)0;
  81. output.Position = mul(
  82. float4(input.Position,1.0),
  83. gMatrixWorldViewProj );
  84.  
  85. output.Normal = normalize(mul(
  86. input.Normal,
  87. (float3x3)gMatrixWorld));
  88.  
  89. output.TexCoord = input.TexCoord;
  90.  
  91. output.tangent = normalize(mul(input.tangent,
  92. (float3x3)gMatrixWorld));
  93. return output;
  94. }
  95.  
  96.  
  97. //MAIN PIXEL SHADER
  98. //*****************
  99. float4 MainPS(VS_OUTPUT input) : SV_TARGET {
  100. float3 normal = normalize(input.Normal);
  101. float3 tangent = normalize(input.tangent);
  102. //
  103. float3 binormal = normalize(cross(tangent,normal));
  104. //
  105. float3x3 localAxis = {tangent,
  106. binormal,
  107. normal};
  108. //
  109. float3 sampledNormal = m_normalMapTexture.Sample(gDiffuseSamplerState, input.TexCoord);
  110. sampledNormal.x = (sampledNormal.x *2) -1;
  111. sampledNormal.y = (sampledNormal.y *2) -1;
  112. sampledNormal.z = (sampledNormal.z *2) -1;
  113.  
  114. float3 newNormal = mul(sampledNormal,localAxis);
  115.  
  116. //
  117. float4 finalColor = gTextureDiffuse.Sample(
  118. gDiffuseSamplerState, input.TexCoord);
  119.  
  120. float diffuseIntensity = dot( newNormal, -gLightDirection);
  121. diffuseIntensity = saturate(diffuseIntensity);
  122. finalColor *= diffuseIntensity;
  123. finalColor += gColorAmbient * gAmbientIntensity;
  124. return finalColor;
  125. }
  126.  
  127.  
  128. //TECHNIQUES
  129. //**********
  130. technique10 DefaultTechnique {
  131. pass p0 {
  132. SetRasterizerState(DisableCulling);
  133. SetVertexShader(CompileShader(vs_4_0, MainVS()));
  134. SetPixelShader(CompileShader(ps_4_0, MainPS()));
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement