Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if OPENGL
- #define SV_POSITION POSITION
- #define VS_SHADERMODEL vs_3_0
- #define PS_SHADERMODEL ps_3_0
- #else
- #define VS_SHADERMODEL vs_4_0_level_9_3
- #define PS_SHADERMODEL ps_4_0_level_9_3
- #endif
- matrix WorldViewProjection;
- // Matrix
- float4x4 View;
- float4x4 Projection;
- float4x4 LightViewProj;
- // Light
- float4 AmbientColor;
- float AmbientIntensity;
- float4 DiffuseColor;
- float DiffuseIntensity;
- float4 SpecularColor;
- int SpecularIntensity;
- float3 LightDirection;
- float3 EyePosition;
- // Flags
- bool UseTextureColor;
- float4 TextureColor;
- bool UseSpecular;
- bool UseSolidColor;
- float4 SolidColor;
- bool UseShadowMap;
- float DepthBias;
- bool UseTransparency;
- float Transparency;
- // Color Map
- bool UseColorMap;
- texture2D ColorMap;
- sampler2D ColorMapSampler = sampler_state
- {
- Texture = <ColorMap>;
- MinFilter = linear;
- MagFilter = linear;
- MipFilter = linear;
- };
- // Normal Map
- bool UseNormalMap;
- texture2D NormalMap;
- sampler2D NormalMapSampler = sampler_state
- {
- Texture = <NormalMap>;
- MinFilter = linear;
- MagFilter = linear;
- MipFilter = linear;
- };
- // Shadow Map
- texture2D ShadowMap;
- sampler2D ShadowMapSampler = sampler_state
- {
- Texture = <ShadowMap>;
- minfilter = linear;
- magfilter = linear;
- mipfilter = linear;
- addressu = clamp;
- addressv = clamp;
- };
- float2 ShadowMapSize;
- static const float2 PoissonDisk[16] = {
- { -0.5843019, -0.7709476, },
- { -0.2078192, -0.2685965, },
- { -0.7936803, -0.2392976, },
- { -0.09280941, -0.6844395, },
- { -0.4505372, 0.1911876, },
- { 0.02774003, 0.2767662, },
- { 0.2266135, -0.2961758, },
- { 0.3591927, -0.7994291, },
- { 0.8307029, -0.473116, },
- { 0.6333103, 0.2970356, },
- { 0.9171839, -0.07370353, },
- { -0.2672351, 0.5755621, },
- { 0.1362572, 0.6735747, },
- { -0.8297537, 0.5041837, },
- { 0.566824, 0.7320002, },
- { -0.1563496, 0.9875766 }
- };
- struct InstancingVSinput
- {
- float4 Position : POSITION0;
- float2 TexCoord : TEXCOORD0;
- float3 Normal : NORMAL0;
- float3 Binormal : BINORMAL0;
- float3 Tangent : TANGENT0;
- };
- struct InstancingVSoutput
- {
- float4 Position : POSITION0;
- float2 TexCoord : TEXCOORD0;
- float3 View : TEXCOORD3;
- float4 WorldPos : TEXCOORD4;
- float4 HighlightColor : TEXCOORD5;
- float3x3 WorldToTangentSpace : TEXCOORD6;
- };
- InstancingVSoutput InstancingVS(InstancingVSinput input, float4 HighlightColor : TEXCOORD1, float4x4 World : TEXCOORD2)
- {
- InstancingVSoutput output;
- // Modify the vertex position with the Matrix received in the instance
- float4 worldPosition = mul(input.Position, transpose(World));
- float4 viewPosition = mul(worldPosition, View);
- // Final position with projection
- output.Position = mul(viewPosition, Projection);
- output.TexCoord = input.TexCoord;
- // Output to Tangeant space in world coordinates
- output.WorldToTangentSpace[0] = normalize(mul(input.Tangent, transpose(World)));
- output.WorldToTangentSpace[1] = normalize(mul(input.Binormal, transpose(World)));
- output.WorldToTangentSpace[2] = normalize(mul(input.Normal, transpose(World)));
- // Precalculation for specular lighting
- output.View = normalize(float4(EyePosition, 1.0) - worldPosition);
- // Transfer the instance parameters to the pixel shader
- output.WorldPos = mul(input.Position, transpose(World));
- output.HighlightColor = HighlightColor;
- return output;
- }
- float4 InstancingPS(InstancingVSoutput input) : COLOR0
- {
- if (UseSolidColor)
- return SolidColor;
- // The base color for the object
- float4 color;
- color = input.HighlightColor;
- // Get the normal map color for this pixel
- float3 normalMap;
- if (UseNormalMap)
- normalMap = 2.0 *(tex2D(NormalMapSampler, input.TexCoord)) - 1.0;
- else
- normalMap = float3 (0, 0, 1);
- normalMap = normalize(mul(normalMap, input.WorldToTangentSpace));
- float4 normal = float4(normalMap, 1.0);
- // Diffuse and specular
- float4 diffuse = saturate(dot(-LightDirection, normal));
- float4 reflect = normalize(2 * diffuse*normal - float4(LightDirection, 1.0));
- float4 specular = float4(0, 0, 0, 0);
- if (UseSpecular)
- specular = pow(saturate(dot(reflect, input.View)), SpecularIntensity);
- // The final (non shadowed) color
- float4 final =
- color * AmbientColor * AmbientIntensity +
- color * DiffuseIntensity * DiffuseColor * diffuse +
- color * SpecularColor * specular;
- if (UseShadowMap)
- {
- // Find the position of this pixel in light space
- float4 lightingPosition = mul(input.WorldPos, LightViewProj);
- // Find the position in the shadow map for this pixel
- float2 ShadowTexCoord = 0.5 * lightingPosition.xy /
- lightingPosition.w + float2(0.5, 0.5);
- ShadowTexCoord.y = 1.0f - ShadowTexCoord.y;
- // Calculate the current pixel depth
- // The bias is used to prevent folating point errors that occur when
- // the pixel of the occluder is being drawn
- float ourdepth = (lightingPosition.z / lightingPosition.w) - DepthBias;
- // Do 16 samples of the shadow map, using a poisson disk
- float shadowSample = 16;
- for (int i = 0; i < 15; i++)
- {
- float2 shadowlookup;
- shadowlookup.x = ShadowTexCoord.x + PoissonDisk[i].x * 0.002 * 30 / ShadowMapSize.x;
- shadowlookup.y = ShadowTexCoord.y + PoissonDisk[i].y * 0.002 * 30 / ShadowMapSize.y;
- float shadowdepth = tex2D(ShadowMapSampler, shadowlookup).r;
- if (shadowdepth != 1 && shadowdepth < ourdepth)
- shadowSample -= 0.5;
- }
- shadowSample *= 1 / 16.0;
- final *= shadowSample;
- }
- // Set transparency for temporary coins
- final.a = 1;
- final *= input.HighlightColor.a;
- return final;
- }
- technique Instancing
- {
- pass P0
- {
- VertexShader = compile VS_SHADERMODEL InstancingVS();
- PixelShader = compile PS_SHADERMODEL InstancingPS();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement