About Monkey 2 › Forums › Monkey 2 Programming Help › Image tiling issue
Tagged: image texture tiling
This topic contains 7 replies, has 5 voices, and was last updated by 
 peterigz
 2 years, 3 months ago.
- 
		AuthorPosts
 - 
		
			
				
December 21, 2016 at 12:51 am #5944
Having an issue where the edge of tiles seem to be wrapping round and displaying a slither of the tile adjacent in the sprite atlas. See attached image what I mean and the tile map in question. Is there a texture flag I’m missing to prevent that? The strange thing is I’m loading the sprite atlas and creating a separate image for each tile but it seems to be bleeding the edge of the tile next to it.
Here’s the code I use to load each tile, I’ve checked all the coordinates it uses to grab each frame are correct…
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125Class tlShapePrivateField width:IntField height:IntField url:StringField frames:IntField smallindex:IntField largeindex:IntField maxradius:FloatField name:StringPublicField image:Image[]Property Url:String()Return urlSetter (value:String)url = valueEndProperty Name:String()Return nameSetter (value:String)name = valueEndMethod GetImage:Image(frame:int)Return image[frame]EndProperty Width:Int()Return widthSetter (value:Int)width = valueEndProperty Height:Int()Return heightSetter (value:Int)height = valueEndProperty MaxRadius:Float()Return maxradiusSetter (value:Float)maxradius = valueEndProperty LargeIndex:Int()Return largeindexSetter (value:Int)largeindex = valueEndProperty Frames:Int()Return framesSetter (value:Int)frames = valueEndFunction LoadFrames:Image[] (path:String, numFrames:Int, cellWidth:Int, cellHeight:Int, padded:Bool = False)Local material:=Image.Load( path, null )If Not material Return New Image[0]If cellWidth * cellHeight * numFrames > material.Width * material.Height Return New Image[0]Local frames:= New Image[numFrames]If cellHeight = material.HeightLocal x:=0local width:=cellWidthIf paddedx += 1width -= 2End ifFor Local i:=0 Until numFrameslocal rect:= New Recti(i * cellWidth + x, 0, i * cellWidth + x + width, cellHeight)frames[i] = New Image(material, rect)NextElseLocal x:= 0, width:= cellWidth, y:= 0, height:= cellHeightLocal columns:= material.Width / widthIf paddedx += 1y += 1width -= 2height -= 2End IfFor Local i:= 0 Until numFramesLocal fx:Int = i Mod columns * cellWidthLocal fy:Int = Int(i / columns) * cellHeightlocal rect:= New Recti(fx + x, fy + y, fx + x + width, fy + y + height)frames[i] = New Image(material, rect)NextEnd IfReturn framesEndEndFunction LoadShape:tlShape(url:String, width:Int = 0, height:Int = 0, frames:Int = 1, padding:int = false)Local shape:tlShape = New tlShapeIf frames = 1shape.image = New Image[1]shape.image[0] = Image.Load(url)Elseshape.image = tlShape.LoadFrames(url, frames, width, height, padding)EndIfshape.Width = widthshape.Height = heightshape.Frames = framesshape.Url = urlReturn shapeEndThanks
Attachments:
December 21, 2016 at 3:23 am #5947December 21, 2016 at 3:37 am #5949Spoke too soon. It displays fine when displaying to even coordinates but it fails when displaying to odd coordinates:
Attachments:
December 21, 2016 at 8:39 am #5953sadly missing a Main function so couldn’t test on Linux…
December 21, 2016 at 8:51 pm #5978If you are using filtering, tiles should generally have a 1 pixel border around them as minor math errors in the GPU can mean incorrect texels are occasionally sampled.
More info here:
December 22, 2016 at 12:20 am #5980I see thanks. I tried filter nearest and that almost fixes it, but it glitches when y is 0.5. x doesn’t glitch at all. Not sure if that’s correct behaviour? Be nice if nearest was a solution, otherwise I can just try implementing the border technique as outlined in the link.
I’ve attached a working example if anyone wants to test.
Attachments:
December 22, 2016 at 6:42 am #5982If you are using filtering, tiles should generally have a 1 pixel border around them as minor math errors in the GPU can mean incorrect texels are occasionally sampled.
Rubbish! thats just guff to protect you poor texture coordinates Mark, and you know it! In essence the texture coordinates should always compensate a tiny bit. adding a 1 pixels border is using a hammer to do the same.
@peterigz – the app works fine here on macOS Sierra
December 23, 2016 at 12:33 am #5995I checked back in my Blitzmax version of this code where it works fine but actually Blitzmax loadanimation created each image separately which basically eliminates the problem. So I adjusted my load animation to create separate images instead (which is what I thought was happening anyway until I ran into this issue) and it works perfectly. Looking around the web it does seem like a common issue people face with various hacks to fix. Incidentally this comes close to fixing where you adjust the UpdateTexCoords in the Texture Class like so:
Monkey123456Method UpdateTexCoords()_texCoords.min.x=Float(_rect.min.x+0.5)/_textures[0].Width_texCoords.min.y=Float(_rect.min.y+0.5)/_textures[0].Height_texCoords.max.x=Float(_rect.max.x-0.5)/_textures[0].Width_texCoords.max.y=Float(_rect.max.y-0.5)/_textures[0].HeightEndBut it wasn’t quite perfect, the tiles just didn’t quite line up perfectly but the bleeding was eliminated. Either way I’m happy with separate images for tiles which i notice is what Playniax does as well.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.



