Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Tilemap-Shader not working

$
0
0

@EnemyArea wrote:

Hi ppl!
I hope you can help me. I tried to use a shader to render my tilemap, but I have a strange issue with it.
Here is the source: https://www.dropbox.com/s/d2lxro98vecazt7/TileEngineShaderTest.zip?dl=0

I tried t implement it on the shader from here: http://www.connorhollis.com/fast-tilemap-shader/

If I try to render my map, the texture gets distorted and somestimes it takes the wrong tile.

I dont get whats wrong here....
Shader:

float4x4 World;
float4x4 View;
float4x4 Projection;

Texture2D TilesetTexture;
sampler2D tilesetTextureSampler = sampler_state
{
    Texture = <TilesetTexture>;
    Filter = POINT;
    AddressU = CLAMP;
    AddressV = CLAMP;
};

Texture2D MapTexture;
sampler2D mapTextureSampler = sampler_state
{
    Texture = <MapTexture>;
    Filter = POINT;
    AddressU = CLAMP;
    AddressV = CLAMP;
};

struct VertexShaderInput
{
    float2 TexCoord : TEXCOORD0;
    float4 Position : SV_Position0;
    float4 Color : COLOR0;
};

struct VertexShaderOutput
{
    float2 TexCoord : TEXCOORD0;
    float4 Position : SV_Position0;
    float4 Color : COLOR0;
};

struct PixelShaderOutput
{
    float4 Color : COLOR0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);

    output.Position = mul(viewPosition, Projection);
    output.TexCoord = input.TexCoord;
    output.Color = input.Color;

    return output;
}

// Tilemap Dimension (in tiles)
int MapWidthInTiles;
int MapHeightInTiles;

// Max Tileset-Tiles per row
int TilesetSizeInTiles = 8;

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    float4 pixel = tex2D(mapTextureSampler, input.TexCoord);
    int index = 0; // ((pixel.g * 255) * TilesetSizeInTiles) + (pixel.r * 255);
    int xpos = index % TilesetSizeInTiles;
    int ypos = index / TilesetSizeInTiles;
    float2 uv = float2(xpos, ypos) / TilesetSizeInTiles;

    float xoffset = frac(input.TexCoord.x * MapWidthInTiles) / TilesetSizeInTiles;
    float yoffset = frac(input.TexCoord.y * MapHeightInTiles) / TilesetSizeInTiles;
    uv += float2(xoffset, yoffset);

    return tex2D(tilesetTextureSampler, uv);
}

technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_4_0_level_9_1 VertexShaderFunction();
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles