I'm unable to find my TerrainTypeSampler when I try to call it from my code! Why?
struct VertexToPixel
{
float4 Position : POSITION;
float4 Color : COLOR0;
float LightingFactor : TEXCOORD0;
float2 TextureCoords: TEXCOORD1;
//texture2D Texture : TEXTURE; //TODO: Figure out how to change the texture used for the current vertex.
};
struct PixelToFrame
{
float4 Color : COLOR0;
};
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float3 xLightDirection;
float xAmbient;
bool xEnableLighting;
bool xShowNormals;
//------- Texture Samplers --------
texture2D xTexture;
SamplerState TextureSampler
{
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
texture2D TerrainType;
SamplerState TerrainTypeSampler
{
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
texture2D Grass;
SamplerState GrassSampler
{
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
texture2D Rock;
SamplerState RockSampler
{
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
texture2D Sand;
SamplerState SandSampler
{
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
texture2D Snow;
SamplerState SnowSampler
{
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
texture2D Water;
SamplerState WaterSampler
{
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
//------- Technique: Textured --------
VertexToPixel TexturedVS(float4 inPos : POSITION, float3 inNormal : NORMAL, float2 inTexCoords : TEXCOORD0)
{
VertexToPixel Output = (VertexToPixel)0;
float4x4 preViewProjection = mul(xView, xProjection);
float4x4 preWorldViewProjection = mul(xWorld, preViewProjection);
Output.Position = mul(inPos, preWorldViewProjection);
Output.TextureCoords = inTexCoords;
float3 Normal = normalize(mul(normalize(inNormal), xWorld));
Output.LightingFactor = 1;
if (xEnableLighting)
Output.LightingFactor = saturate(dot(Normal, -xLightDirection));
return Output;
}
PixelToFrame TexturedPS(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
float4 Color = TerrainType.Sample(TerrainTypeSampler, PSIn.TextureCoords);
float4 GrassClr = float4(0, 255, 0, 255);
float4 RockClr = float4(255, 0, 0, 255);
float4 SandClr = float4(255, 255, 0, 255);
float4 SnowClr = float4(255, 255, 255, 255);
float4 WaterClr = float4(12, 0, 255, 255);
float4 Diff = Color - GrassClr;
if (!any(Diff))
Output.Color = tex2D(GrassSampler, PSIn.TextureCoords);
Diff = Color - RockClr;
if (!any(Diff))
Output.Color = tex2D(RockSampler, PSIn.TextureCoords);
Diff = Color - SandClr;
if(!any(Diff))
Output.Color = tex2D(SandSampler, PSIn.TextureCoords);
Diff = Color - SnowClr;
if(!any(Diff))
Output.Color = tex2D(SnowSampler, PSIn.TextureCoords);
Diff = Color - WaterClr;
if(!any(Diff))
Output.Color = tex2D(WaterSampler, PSIn.TextureCoords);
Output.Color = tex2D(TextureSampler, PSIn.TextureCoords);
Output.Color.rgb *= saturate(PSIn.LightingFactor + xAmbient);
return Output;
}
technique Textured_2_0
{
pass Pass0
{
VertexShader = compile vs_4_0_level_9_1 TexturedVS();
PixelShader = compile ps_4_0_level_9_1 TexturedPS();
}
}
technique Textured
{
pass Pass0
{
VertexShader = compile vs_4_0_level_9_1 TexturedVS();
PixelShader = compile ps_4_0_level_9_1 TexturedPS();
}
}
When I'm calling this, TerrainTypeSampler returns null!
public void Draw()
{
Matrix WorldMatrix = Matrix.CreateTranslation(-m_TerrainWidth / 2.0f, 0, m_TerrainHeight / 2.0f);
m_Effect.CurrentTechnique = m_Effect.Techniques["Textured"];
m_Effect.Parameters["TerrainTypeSampler"].SetValue(m_TerrainType);
m_Effect.Parameters["GrassSampler"].SetValue(m_Grass);
m_Effect.Parameters["RockSampler"].SetValue(m_Rock);
m_Effect.Parameters["SandSampler"].SetValue(m_Sand);
m_Effect.Parameters["SnowSampler"].SetValue(m_Snow);
m_Effect.Parameters["WaterSampler"].SetValue(m_Water);
//m_Effect.Parameters["TextureSampler"].SetValue(m_Grass);
m_Effect.Parameters["xView"].SetValue(m_CController.View/*m_ViewMatrix*/);
m_Effect.Parameters["xProjection"].SetValue(m_CController.Projection/*m_ProjectionMatrix*/);
m_Effect.Parameters["xWorld"].SetValue(WorldMatrix/*Matrix.Identity*/);
RasterizerState RS = new RasterizerState();
RS.CullMode = CullMode.None;
RS.FillMode = FillMode.WireFrame;
m_Device.RasterizerState = RS;
m_Device.Clear(Color.CornflowerBlue);
foreach (EffectPass Pass in m_Effect.CurrentTechnique.Passes)
{
Pass.Apply();
m_Device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, m_Vertices, 0, m_Vertices.Length,
m_Indices, 0, m_Indices.Length / 3, VertexPositionNormalTexture.VertexDeclaration);
}
}
Please help!