Jump to content
  • Advertisement

MatsK

Member
  • Content Count

    167
  • Joined

  • Last visited

Community Reputation

394 Neutral

About MatsK

  • Rank
    Member

Personal Information

  • Interests
    Art
    Programming

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. 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!
  2. Thanks! How do I pass the texture to the sampler?
  3. Wait, what? Ok so how would that be in SM4.0? Here's the new code: texture DisplacementMap; /*sampler DisplacementSampler = sampler_state { Texture = <DisplacementMap>; MipFilter = Point; MinFilter = Point; MagFilter = Point; AddressU = Clamp; AddressV = Clamp; };*/ SamplerState DisplacementSampler { Texture = <DisplacementMap>; MipFilter = Point; MinFilter = Point; MagFilter = Point; AddressU = Clamp; AddressV = Clamp; }; struct VS_INPUT { float4 position : POSITION; float4 uv : TEXCOORD0; }; struct VS_OUTPUT { float4 uv : TEXCOORD0; float4 worldPos : TEXCOORD1; float4 textureWeights : TEXCOORD2; float4 position : POSITION; }; float4x4 world; float4x4 view; float4x4 proj; float maxHeight = 128; float textureSize = 512.0f; float texelSize = 1.0f / 512.0f; //size of one texel; float4 tex2Dlod_bilinear(/*sampler texSam,*/ float4 uv) { float4 height00 = tex2Dlod(DisplacementSampler, uv); float4 height10 = tex2Dlod(DisplacementSampler, uv + float4(texelSize, 0, 0, 0)); float4 height01 = tex2Dlod(DisplacementSampler, uv + float4(0, texelSize, 0, 0)); float4 height11 = tex2Dlod(DisplacementSampler, uv + float4(texelSize, texelSize, 0, 0)); float2 f = frac(uv.xy * textureSize); float4 tA = lerp(height00, height10, f.x); float4 tB = lerp(height01, height11, f.x); return lerp(tA, tB, f.y); } VS_OUTPUT Transform(VS_INPUT In) { VS_OUTPUT Out = (VS_OUTPUT)0; float4x4 viewProj = mul(view, proj); float4x4 worldViewProj = mul(world, viewProj); float height = tex2Dlod_bilinear(/*DisplacementSampler,*/ float4(In.uv.xy, 0, 0)).r; In.position.y = height * maxHeight; Out.worldPos = mul(In.position, world); Out.position = mul(In.position, worldViewProj); Out.uv = In.uv; float4 TexWeights = 0; TexWeights.x = saturate(1.0f - abs(height - 0) / 0.2f); TexWeights.y = saturate(1.0f - abs(height - 0.3) / 0.25f); TexWeights.z = saturate(1.0f - abs(height - 0.6) / 0.25f); TexWeights.w = saturate(1.0f - abs(height - 0.9) / 0.25f); float totalWeight = TexWeights.x + TexWeights.y + TexWeights.z + TexWeights.w; TexWeights /= totalWeight; Out.textureWeights = TexWeights; return Out; } technique GridDraw { pass P0 { VertexShader = compile vs_4_0 Transform(); } }
  4. I changed it, it didn't work!
  5. Shader source This compiles just fine, but when I try to load it in MonoGame, SharpDX gives me a cryptic "Invalid Parameter" error.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!