Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #if OPENGL
  3.     #define SV_POSITION POSITION
  4.     #define VS_SHADERMODEL vs_3_0
  5.     #define PS_SHADERMODEL ps_3_0
  6. #else
  7.     #define VS_SHADERMODEL vs_4_0_level_9_3
  8.     #define PS_SHADERMODEL ps_4_0_level_9_3
  9. #endif
  10.  
  11. matrix WorldViewProjection;
  12.  
  13. // Matrix
  14. float4x4 View;
  15. float4x4 Projection;
  16. float4x4 LightViewProj;
  17.  
  18. // Light
  19. float4 AmbientColor;
  20. float AmbientIntensity;
  21.  
  22. float4 DiffuseColor;
  23. float DiffuseIntensity;
  24.  
  25. float4 SpecularColor;
  26. int SpecularIntensity;
  27.  
  28. float3 LightDirection;
  29. float3 EyePosition;
  30.  
  31.  
  32. // Flags
  33. bool UseTextureColor;
  34. float4 TextureColor;
  35.  
  36. bool UseSpecular;
  37.  
  38. bool UseSolidColor;
  39. float4 SolidColor;
  40.  
  41. bool UseShadowMap;
  42. float DepthBias;
  43.  
  44. bool UseTransparency;
  45. float Transparency;
  46.  
  47. // Color Map
  48. bool UseColorMap;
  49. texture2D ColorMap;
  50. sampler2D ColorMapSampler = sampler_state
  51. {
  52.     Texture = <ColorMap>;
  53.     MinFilter = linear;
  54.     MagFilter = linear;
  55.     MipFilter = linear;
  56. };
  57.  
  58. // Normal Map
  59. bool UseNormalMap;
  60. texture2D NormalMap;
  61. sampler2D NormalMapSampler = sampler_state
  62. {
  63.     Texture = <NormalMap>;
  64.     MinFilter = linear;
  65.     MagFilter = linear;
  66.     MipFilter = linear;
  67. };
  68.  
  69. // Shadow Map
  70. texture2D ShadowMap;
  71. sampler2D ShadowMapSampler = sampler_state
  72. {
  73.     Texture = <ShadowMap>;
  74.     minfilter = linear;
  75.     magfilter = linear;
  76.     mipfilter = linear;
  77.  
  78.     addressu = clamp;
  79.     addressv = clamp;
  80. };
  81. float2 ShadowMapSize;
  82.  
  83. static const float2 PoissonDisk[16] = {
  84.     { -0.5843019,  -0.7709476, },
  85.     { -0.2078192,  -0.2685965, },
  86.     { -0.7936803,  -0.2392976, },
  87.     { -0.09280941, -0.6844395, },
  88.     { -0.4505372,   0.1911876, },
  89.     { 0.02774003,  0.2767662, },
  90.     { 0.2266135,  -0.2961758, },
  91.     { 0.3591927,  -0.7994291, },
  92.     { 0.8307029,  -0.473116, },
  93.     { 0.6333103,   0.2970356, },
  94.     { 0.9171839,  -0.07370353, },
  95.     { -0.2672351,   0.5755621, },
  96.     { 0.1362572,   0.6735747, },
  97.     { -0.8297537,   0.5041837, },
  98.     { 0.566824,    0.7320002, },
  99.     { -0.1563496,   0.9875766 }
  100. };
  101.  
  102. struct InstancingVSinput
  103. {
  104.     float4 Position : POSITION0;
  105.     float2 TexCoord : TEXCOORD0;
  106.     float3 Normal : NORMAL0;
  107.     float3 Binormal : BINORMAL0;
  108.     float3 Tangent : TANGENT0;
  109. };
  110.  
  111. struct InstancingVSoutput
  112. {
  113.     float4 Position : POSITION0;
  114.     float2 TexCoord : TEXCOORD0;
  115.     float3 View : TEXCOORD3;
  116.     float4 WorldPos : TEXCOORD4;
  117.     float4 HighlightColor : TEXCOORD5;
  118.     float3x3 WorldToTangentSpace : TEXCOORD6;
  119. };
  120.  
  121. InstancingVSoutput InstancingVS(InstancingVSinput input, float4 HighlightColor : TEXCOORD1, float4x4 World : TEXCOORD2)
  122. {
  123.     InstancingVSoutput output;
  124.  
  125.     // Modify the vertex position with the Matrix received in the instance
  126.     float4 worldPosition = mul(input.Position, transpose(World));
  127.     float4 viewPosition = mul(worldPosition, View);
  128.  
  129.     // Final position with projection
  130.     output.Position = mul(viewPosition, Projection);
  131.     output.TexCoord = input.TexCoord;
  132.  
  133.     // Output to Tangeant space in world coordinates
  134.     output.WorldToTangentSpace[0] = normalize(mul(input.Tangent, transpose(World)));
  135.     output.WorldToTangentSpace[1] = normalize(mul(input.Binormal, transpose(World)));
  136.     output.WorldToTangentSpace[2] = normalize(mul(input.Normal, transpose(World)));
  137.  
  138.     // Precalculation for specular lighting
  139.     output.View = normalize(float4(EyePosition, 1.0) - worldPosition);
  140.  
  141.     // Transfer the instance parameters to the pixel shader
  142.     output.WorldPos = mul(input.Position, transpose(World));
  143.     output.HighlightColor = HighlightColor;
  144.  
  145.     return output;
  146. }
  147.  
  148. float4 InstancingPS(InstancingVSoutput input) : COLOR0
  149. {
  150.     if (UseSolidColor)
  151.         return SolidColor;
  152.  
  153.     // The base color for the object
  154.     float4 color;
  155.     color = input.HighlightColor;
  156.  
  157.     // Get the normal map color for this pixel
  158.     float3 normalMap;
  159.     if (UseNormalMap)
  160.         normalMap = 2.0 *(tex2D(NormalMapSampler, input.TexCoord)) - 1.0;
  161.     else
  162.         normalMap = float3 (0, 0, 1);
  163.  
  164.     normalMap = normalize(mul(normalMap, input.WorldToTangentSpace));
  165.     float4 normal = float4(normalMap, 1.0);
  166.  
  167.     // Diffuse and specular
  168.     float4 diffuse = saturate(dot(-LightDirection, normal));
  169.     float4 reflect = normalize(2 * diffuse*normal - float4(LightDirection, 1.0));
  170.  
  171.     float4 specular = float4(0, 0, 0, 0);
  172.     if (UseSpecular)
  173.         specular = pow(saturate(dot(reflect, input.View)), SpecularIntensity);
  174.  
  175.     // The final (non shadowed) color
  176.     float4 final =
  177.         color * AmbientColor * AmbientIntensity +
  178.         color * DiffuseIntensity * DiffuseColor * diffuse +
  179.         color * SpecularColor * specular;
  180.  
  181.     if (UseShadowMap)
  182.     {
  183.         // Find the position of this pixel in light space
  184.         float4 lightingPosition = mul(input.WorldPos, LightViewProj);
  185.  
  186.         // Find the position in the shadow map for this pixel
  187.         float2 ShadowTexCoord = 0.5 * lightingPosition.xy /
  188.             lightingPosition.w + float2(0.5, 0.5);
  189.         ShadowTexCoord.y = 1.0f - ShadowTexCoord.y;
  190.  
  191.         // Calculate the current pixel depth
  192.         // The bias is used to prevent folating point errors that occur when
  193.         // the pixel of the occluder is being drawn
  194.         float ourdepth = (lightingPosition.z / lightingPosition.w) - DepthBias;
  195.  
  196.         // Do 16 samples of the shadow map, using a poisson disk
  197.         float shadowSample = 16;
  198.         for (int i = 0; i < 15; i++)
  199.         {
  200.             float2 shadowlookup;
  201.             shadowlookup.x = ShadowTexCoord.x + PoissonDisk[i].x * 0.002 * 30 / ShadowMapSize.x;
  202.             shadowlookup.y = ShadowTexCoord.y + PoissonDisk[i].y * 0.002 * 30 / ShadowMapSize.y;
  203.             float shadowdepth = tex2D(ShadowMapSampler, shadowlookup).r;
  204.             if (shadowdepth != 1 && shadowdepth < ourdepth)
  205.                 shadowSample -= 0.5;
  206.         }
  207.  
  208.         shadowSample *= 1 / 16.0;
  209.  
  210.         final *= shadowSample;
  211.  
  212.     }
  213.  
  214.  
  215.     // Set transparency for temporary coins
  216.     final.a = 1;
  217.     final *= input.HighlightColor.a;
  218.     return final;
  219.  
  220. }
  221.  
  222. technique Instancing
  223. {
  224.     pass P0
  225.     {
  226.         VertexShader = compile VS_SHADERMODEL InstancingVS();
  227.         PixelShader = compile PS_SHADERMODEL InstancingPS();
  228.     }
  229. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement