Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. //====================================================
  2. // NormalMapping
  3. //====================================================
  4. // By EVOLVED
  5. // [URL]www.evolved-software.com[/URL]
  6. //====================================================
  7.  
  8. //--------------
  9. // un-tweaks
  10. //--------------
  11. matrix WorldVP:WorldViewProjection;
  12. matrix World:World;
  13. matrix ViewInv:ViewInverse;
  14. matrix WorldInv:WorldInverse;
  15.  
  16. //--------------
  17. // tweaks
  18. //--------------
  19. float3 Ambient={0.1f,0.1f,0.1f};
  20. float3 LightPosition={150.0f,150.0f,0.0f};
  21. float3 LightColor={1.0f,1.0f,1.0f};
  22. float LightRange=200.0f;
  23. float4 FogColor={0.8f,0.8f,0.8f,1.0f};
  24. float FogRange=2500.0f;
  25. float Alpha=1.0f;
  26.  
  27. //--------------
  28. // Textures
  29. //--------------
  30. texture BaseTX <string Name="";>;
  31. sampler2D Base = sampler_state
  32. {
  33. texture = <BaseTX>;
  34. };
  35. texture NormalTX <string Name="";>;
  36. sampler2D Normal = sampler_state
  37. {
  38. texture = <NormalTX>;
  39. };
  40. texture NormalizerTX <string Name = "";>;
  41. samplerCUBE Normalizer = sampler_state
  42. {
  43. Texture = <NormalizerTX>;
  44. ADDRESSU = Clamp;
  45. ADDRESSV = Clamp;
  46. };
  47.  
  48. //--------------
  49. // structs
  50. //--------------
  51. struct input
  52. {
  53. float4 Pos:POSITION;
  54. float2 UV:TEXCOORD;
  55. float3 Normal:NORMAL;
  56. float3 Tangent:TANGENT;
  57. float3 Binormal:BINORMAL;
  58. };
  59. struct output
  60. {
  61. float4 OPos:POSITION;
  62. float2 Tex:TEXCOORD0;
  63. float3 LightVec:TEXCOORD1;
  64. float3 Attenuation:TEXCOORD2;
  65. float3 ViewVec:TEXCOORD3;
  66. float Fog:FOG;
  67. };
  68.  
  69. //--------------
  70. // vertex shader
  71. //--------------
  72. output VS(input IN)
  73. {
  74. output OUT;
  75. OUT.OPos=mul(IN.Pos,WorldVP);
  76. OUT.Tex=IN.UV;
  77. float3x3 TBN={-IN.Tangent,IN.Binormal,IN.Normal};
  78. TBN=transpose(mul(TBN,World));
  79. float3 WPos=mul(IN.Pos,World);
  80. float3 LightPos=LightPosition-WPos;
  81. float3 ViewPos=ViewInv[3].xyz-WPos;
  82. OUT.LightVec=mul(LightPos,TBN);
  83. OUT.Attenuation=-(LightPos/LightRange);
  84. OUT.ViewVec=mul(ViewPos,TBN);
  85. OUT.Fog=1-saturate(dot(ViewPos/FogRange,ViewPos/FogRange));
  86. return OUT;
  87. }
  88.  
  89. //--------------
  90. // pixel shader
  91. //--------------
  92. float4 PS(output IN) : COLOR
  93. {
  94. float4 Texture=tex2D(Base,IN.Tex);
  95. float3 NormalMap=tex2D(Normal,IN.Tex)*2-1;
  96. float3 LightV=texCUBE(Normalizer,IN.LightVec)*2-1;
  97. float3 View=texCUBE(Normalizer,IN.ViewVec)*2-1;
  98. float Normal=saturate(dot(reflect(-View,NormalMap),LightV));
  99. Normal=Normal*Normal; Normal=Normal*Normal; Normal=Normal*Normal;
  100. Normal=Normal+saturate(dot(NormalMap,LightV));
  101. float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
  102. float3 Light=PixelLight*LightColor;
  103. return float4(Texture*((Normal*Light)+Ambient),Texture.w*Alpha);
  104. }
  105.  
  106. //--------------
  107. // techniques
  108. //--------------
  109. technique NormalMapping
  110. {
  111. pass p1
  112. {
  113. vertexShader = compile vs_1_1 VS();
  114. pixelShader = compile ps_1_4 PS();
  115. FOGCOLOR=(FogColor);
  116. FOGENABLE=TRUE;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement