Advertisement
Guest User

Untitled

a guest
Feb 21st, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. //Standard shader for shading things.
  2.  
  3. float4x4 World;
  4. float4x4 View;
  5. float4x4 Projection;
  6.  
  7. float Alpha;
  8.  
  9. float4 AmbientColor;
  10. float AmbientIntensity;
  11.  
  12. float4 DiffuseColor;
  13. float DiffuseIntensity;
  14. float3 DiffuseLightDirection;
  15.  
  16. float4 SpecularColor;
  17. float SpecularIntensity;
  18. float Shinniness;
  19. float3 CameraPosition;
  20. float3 CameraDirection;
  21.  
  22. int AtlasWidth = 25;
  23. int SquareSize = 32;
  24. float TexturePercent = 0.04; //percent of the entire texture taken up by 1 pixel
  25.  
  26. //float4 RimColor;
  27. //float RimIntensity;
  28.  
  29. float FogBegin;
  30. float FogEnd;
  31.  
  32. // Texturing variables
  33. Texture ModelTexture; // Our texture which will be mapped onto our object
  34.  
  35. // Pass coordinates to our texture sampler to get a color for a certain pixel
  36. sampler TextureSampler = sampler_state {
  37. texture = <ModelTexture> ;
  38. magfilter = LINEAR;
  39. minfilter = LINEAR;
  40. mipfilter= LINEAR;
  41. AddressU = WRAP;
  42. AddressV = WRAP;};
  43.  
  44. //-----------------------------------------------------------------------------
  45. // Inputs and Outputs
  46. //-----------------------------------------------------------------------------
  47.  
  48. struct VSInputTx
  49. {
  50. float4 Position : POSITION0;
  51. float3 Normal : NORMAL0;
  52. short2 BlockAndDecalID : TEXCOORD0;
  53. };
  54.  
  55. struct VSInputVc
  56. {
  57. float4 Position : POSITION0;
  58. float3 Normal : NORMAL0;
  59. float4 Color : COLOR0;
  60. };
  61.  
  62. struct VSOutputTx
  63. {
  64. float4 Position : POSITION0;
  65. float3 Normal : TEXCOORD0;
  66. float3 CameraView : TEXCOORD1;
  67. short2 TexCoords : TEXCOORD2;
  68. short2 DecalCoords : TEXCOORD3;
  69. float FogAmt : TEXCOORD4;
  70. };
  71.  
  72. struct VSOutputVc
  73. {
  74. float4 Position : POSITION0;
  75. float3 Normal : TEXCOORD0;
  76. float4 Color : COLOR0;
  77. float3 CameraView : TEXCOORD1;
  78. float FogAmt : TEXCOORD2;
  79. };
  80.  
  81. //-----------------------------------------------------------------------------
  82. // Vertex shaders
  83. //-----------------------------------------------------------------------------
  84.  
  85. VSOutputTx VSBasicTx( VSInputTx input )
  86. {
  87. VSOutputTx output;
  88.  
  89. float4 worldPosition = mul( input.Position, World );
  90. float4 viewPosition = mul( worldPosition, View );
  91. output.Position = mul( viewPosition, Projection );
  92.  
  93. output.Normal = mul( input.Normal, World );
  94. output.CameraView = normalize( CameraPosition - worldPosition );
  95.  
  96. output.FogAmt = 1 - saturate((distance(worldPosition,CameraPosition)-FogBegin)/(FogEnd-FogBegin)); //This sets the fog amount (since it needs position data)
  97.  
  98. // Convert texture coordinates to short2 from blockID
  99. // When they transfer to the pixel shader, they will be interpolated
  100. // per pixel.
  101. output.TexCoords = short2((short)(((input.BlockAndDecalID.x) % (AtlasWidth)) * TexturePercent), (short)(((input.BlockAndDecalID.x) / (AtlasWidth)) * TexturePercent));
  102. output.DecalCoords = short2((short)(((input.BlockAndDecalID.y) % (AtlasWidth)) * TexturePercent), (short)(((input.BlockAndDecalID.y) / (AtlasWidth)) * TexturePercent));
  103.  
  104. return output;
  105. }
  106.  
  107. VSOutputVc VSBasicVc( VSInputVc input )
  108. {
  109. VSOutputVc output;
  110.  
  111. float4 worldPosition = mul( input.Position, World );
  112. float4 viewPosition = mul( worldPosition, View );
  113. output.Position = mul( viewPosition, Projection );
  114.  
  115. output.Normal = mul( input.Normal, World );
  116. output.CameraView = normalize( CameraPosition - worldPosition );
  117.  
  118. output.FogAmt = 1 - saturate((distance(worldPosition,CameraPosition)-FogBegin)/(FogEnd-FogBegin));
  119.  
  120. // Just pass the texture coordinates to the vertex shader output.
  121. // When they transfer to the pixel shader, they will be interpolated
  122. // per pixel.
  123. output.Color = input.Color;
  124.  
  125. return output;
  126. }
  127.  
  128. //-----------------------------------------------------------------------------
  129. // Pixel shaders
  130. //-----------------------------------------------------------------------------
  131.  
  132. float4 PSBasicTx( VSOutputTx input ) : COLOR0
  133. {
  134. // Sample our texture at the specified texture coordinates to get the texture color
  135. float4 texColor = tex2D( TextureSampler, input.TexCoords );
  136. float4 decColor = tex2D( TextureSampler, input.DecalCoords );
  137. float t_a = decColor.a + texColor.a*(1 -decColor.a) ;
  138. texColor.a = 0;
  139. decColor += texColor*(1 - decColor.a)/t_a;
  140. texColor = float4(decColor.r, decColor.g, decColor.b, t_a);
  141.  
  142. float3 lightdir = normalize( DiffuseLightDirection );
  143. float3 norm = normalize( input.Normal );
  144. float3 halfAngle = normalize( lightdir + input.CameraView );
  145. float specular = pow( saturate( dot( norm, halfAngle ) ), Shinniness ) * SpecularColor * SpecularIntensity;
  146.  
  147. // Calculate the rim lighting component by subtract the dot product of the normal and camera direction
  148. // from 1, then taking that to the 2nd power. This will give pixels at more perpendicular angles to the camera
  149. // a glow effect. Multiply that by rim color and intensity to adjust the color and brightness.
  150. // - If you take the dot product to a higher power, the glow will be thinner and closer to the edges.
  151. // - If you take the dot product to a lower power, the glow will be more spread out away from the edges.
  152. //float4 rim = pow( 1 - dot( norm, CameraDirection ), 1.5 ) * RimColor * RimIntensity;
  153.  
  154. float4 diffuse = dot( lightdir, input.Normal ) * DiffuseIntensity * DiffuseColor; //Diffuse light
  155. float4 ambient = AmbientIntensity * AmbientColor; //Calculate ambient light
  156.  
  157. float4 returnColor = texColor * (diffuse + ambient + specular); //Set the return color.
  158. returnColor.a = t_a * Alpha; //Manually set the alpha value.
  159.  
  160. returnColor*=input.FogAmt; //Multiply the color by fog amount to get fog.
  161.  
  162. return returnColor;
  163. }
  164.  
  165. float4 PSBasicVc( VSOutputVc input ) : COLOR0
  166. {
  167. // Sample our texture at the specified texture coordinates to get the texture color
  168. float4 texColor = input.Color;
  169. float t_a = texColor.a;
  170.  
  171. float3 lightdir = normalize( DiffuseLightDirection );
  172. float3 norm = normalize( input.Normal );
  173. float3 halfAngle = normalize( lightdir + input.CameraView );
  174. float specular = pow( saturate( dot( norm, halfAngle ) ), Shinniness ) * SpecularColor * SpecularIntensity;
  175.  
  176. // Calculate the rim lighting component by subtract the dot product of the normal and camera direction
  177. // from 1, then taking that to the 2nd power. This will give pixels at more perpendicular angles to the camera
  178. // a glow effect. Multiply that by rim color and intensity to adjust the color and brightness.
  179. // - If you take the dot product to a higher power, the glow will be thinner and closer to the edges.
  180. // - If you take the dot product to a lower power, the glow will be more spread out away from the edges.
  181. //float4 rim = pow( 1 - dot( norm, CameraDirection ), 1.5 ) * RimColor * RimIntensity;
  182.  
  183. float4 diffuse = dot( lightdir, input.Normal ) * DiffuseIntensity * DiffuseColor;
  184. float4 ambient = AmbientIntensity * AmbientColor;
  185.  
  186. float4 returnColor = texColor * (diffuse + ambient + specular);
  187. returnColor.a = t_a * Alpha;
  188.  
  189. returnColor*=input.FogAmt;
  190.  
  191. return returnColor;
  192. }
  193.  
  194. //int ShaderIndex = 0;
  195.  
  196. VertexShader VSArray[2] =
  197. {
  198. compile vs_1_1 VSBasicTx(),
  199. compile vs_1_1 VSBasicVc()
  200. };
  201.  
  202. PixelShader PSArray[2] =
  203. {
  204. compile ps_2_0 PSBasicTx(),
  205. compile ps_2_0 PSBasicVc()
  206. };
  207.  
  208. Technique BasicEffect
  209. {
  210. Pass TexturePass
  211. {
  212. VertexShader = (VSArray[0]);
  213. PixelShader = (PSArray[0]);
  214. }
  215.  
  216. Pass VertexColorPass
  217. {
  218. VertexShader = (VSArray[1]);
  219. PixelShader = (PSArray[1]);
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement