Problem with image maps(solved)
by Christopher Perkins · in Torque X 2D · 04/24/2009 (12:59 pm) · 8 replies
Ok, I'm having a small problem with an image map. It's 480x288, filled with 48x48 tiles. Some are used for animation, while some are just for a tilemap, all are just stage sprites.
I've got everything set up in the level builder and everything looks how I like it, but when I compile the game and run it, it seems that the are off. Everything is placed right in the tilemap, but sometimes the image placed in game is -1 x, -1y from where it should be(I cant tell if all tiles are doing this as all wouldn't be noticeable). In the level editor, I do not have this problem, only in the compiled game.
Is there anyway to fix this? Or should I just go to smaller image maps only for animation purposes. I'd much rather keep my levels sprites in one or a few image maps, but if it's not possible to not have this done, Ill do whatever workaround is needed.
I've got everything set up in the level builder and everything looks how I like it, but when I compile the game and run it, it seems that the are off. Everything is placed right in the tilemap, but sometimes the image placed in game is -1 x, -1y from where it should be(I cant tell if all tiles are doing this as all wouldn't be noticeable). In the level editor, I do not have this problem, only in the compiled game.
Is there anyway to fix this? Or should I just go to smaller image maps only for animation purposes. I'd much rather keep my levels sprites in one or a few image maps, but if it's not possible to not have this done, Ill do whatever workaround is needed.
#2
I made a quick 128x128 image map where each tile was 16x16. So, thats an 8x8 grid of tiles. Anywho, after putting it the level editor, no sign of the problem, but running the compiled game, voila, it's still there. Its particularly annoying when the tile next to it doesnt have a black border and you see a one pixel line of green or white next to the black of your correct tile.
The tiles topleft corners are on 0,0, 16,0, etc..., so I know that they are aligned properly, but the image tends to take a line from the left and/or the top and add it to it>.<
04/24/2009 (2:13 pm)
I know its not anywhere close to too big. Anywho, I decided to try a power of two to see if that worked.I made a quick 128x128 image map where each tile was 16x16. So, thats an 8x8 grid of tiles. Anywho, after putting it the level editor, no sign of the problem, but running the compiled game, voila, it's still there. Its particularly annoying when the tile next to it doesnt have a black border and you see a one pixel line of green or white next to the black of your correct tile.
The tiles topleft corners are on 0,0, 16,0, etc..., so I know that they are aligned properly, but the image tends to take a line from the left and/or the top and add it to it>.<
#3
I added this property to T2DSceneObject as a modified version of Position:
You'll need to change the 8 in this to whatever your unit length in pixels is and update the Render function in the following functions to change
to
in T2DAnimatedSprite, T2DScroller, T2DAnimatedSprite, T2DStaticSprite, T2DTileLayer, and T2DCollisionComponent.
All this will do is change where quads render and won't disrupt any important internal values. Hope this helps!
04/25/2009 (8:39 am)
I had a similar problem and eventually discovered that this occurs when quads are rendered at non-integer coordinates. There's probably a better fix, but the workaround I used seemed to work fine for my purposes. I added this property to T2DSceneObject as a modified version of Position:
public virtual Vector2 IntegerPosition
{
get
{
Vector2 intpos = _position;
intpos.X *= 8;
intpos.Y *= 8;
intpos.X = (float)Math.Round(intpos.X, 0);
intpos.Y = (float)Math.Round(intpos.Y, 0);
intpos.X /= 8;
intpos.Y /= 8;
return intpos;
}
set
{
//should never be accessed
_position = value;
_postTick.pos = _position;
IsSpatialDirty = true;
UpdateSpatialData();
}
}You'll need to change the 8 in this to whatever your unit length in pixels is and update the Render function in the following functions to change
Matrix TranslationMatrix = Matrix.CreateTranslation(new Vector3(Position.X, Position.Y, LayerDepth));
to
Matrix TranslationMatrix = Matrix.CreateTranslation(new Vector3(IntegerPosition.X, IntegerPosition.Y, LayerDepth));
in T2DAnimatedSprite, T2DScroller, T2DAnimatedSprite, T2DStaticSprite, T2DTileLayer, and T2DCollisionComponent.
All this will do is change where quads render and won't disrupt any important internal values. Hope this helps!
#4
Creating a custom shader and material fixed the problem. Albeit, I wasn't working on this when I figured this out, I needed something to look pixelated and had to change the material to a point magfilter to get the effect and found this out as a side effect. Just giving an update.
06/01/2009 (10:40 pm)
Ok, well, I actually found out the true cause of my problem. It wasn't anything torque was doing, it was something the shader was doing. Interpolation was the cause of my dismay, since it takes in account the whole texture instead of just the section I was drawing to the screen.Creating a custom shader and material fixed the problem. Albeit, I wasn't working on this when I figured this out, I needed something to look pixelated and had to change the material to a point magfilter to get the effect and found this out as a side effect. Just giving an update.
#5
07/24/2009 (8:02 am)
I'm now having trouble again with this issue. Could you share your shader solution? I've changed the filters in the SimpleEffect shader to POINT but that still doesn't solve the issue of blending the edges of tiles.
#6
07/31/2009 (6:37 am)
bump
#7
Again its very simple and sets all the filters to point. I then run it through a custom material I call PixelMaterial:
Again...really simple, but it works. Just need to set your image maps material to this. It's annoying with the level editor as itll change it back to Simple Material every time you save it, but it fixes what you need.
07/31/2009 (3:35 pm)
Heres my shader(it's really really simple>.<) for the point filter:float4x4 worldViewProjection;
texture baseTexture;
sampler2D baseTextureSampler = sampler_state
{
Texture = <baseTexture>;
MinFilter = point;
MagFilter = point;
MipFilter = point;
};
struct vertexshader_Input
{
float4 position : POSITION;
float4 color : COLOR;
float2 texCoord : TEXCOORD0;
};
struct vertexshader_Output
{
float4 position : POSITION;
float4 color : COLOR0;
float2 texCoord : TEXCOORD0;
};
vertexshader_Output vertexshader_main(vertexshader_Input input)
{
vertexshader_Output output;
output.position = mul(input.position, worldViewProjection);
output.texCoord = input.texCoord;
output.color = input.color;
return output;
}
float4 pixelshader_main(vertexshader_Output input) : COLOR
{
//get the color
float4 color = tex2D(baseTextureSampler, input.texCoord);
return color;
}
technique PixelTechnique
{
pass P0
{
VertexShader = compile vs_1_1 vertexshader_main();
PixelShader = compile ps_1_1 pixelshader_main();
}
}Again its very simple and sets all the filters to point. I then run it through a custom material I call PixelMaterial:
public class PixelMaterial : SimpleMaterial
{
public PixelMaterial()
{
EffectFilename = "data/effects/pixelEffect";
}
protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
{
base._SetupEffect(srs, materialData);
return "PixelTechnique";
}
}Again...really simple, but it works. Just need to set your image maps material to this. It's annoying with the level editor as itll change it back to Simple Material every time you save it, but it fixes what you need.
#8
08/01/2009 (8:15 pm)
That nailed it, thanks!
Torque Owner Sean Monahan
If there are incredibly large, and this will depend on your gfx card, like say 2048x2048 or larger it could be that the image map needs to be smaller. I did some reading about images sizes for XNA awhile ago and I believe 2048x2048 is the max supported (reliably anyway) though it's not recommended.
It may also help if your image maps have base 2 dimensions (e.g., 64x64, 128x128, 256x256, and so on). I don't really stick to this rule, but I've not had any of the kinds of problems you are describing.