Hello Guest

Author Topic: Non-rectangular sprite clipping  (Read 5738 times)

daviwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Non-rectangular sprite clipping
« on: August 21, 2013, 04:46:36 am »
Hey guys,

I'm working on a tiled isometric game with 2D Toolkit and wanted to figure out if there was an easy way to do sprite clipping in a non-rectangular way.  More specifically, I'd like to clip the sprite in the shape of a hexagon so that I can control the height of a vertically long isometric tile by clipping the bottom part off at the desired height.  See the attached image for an example of a sprite with an example clipping geometry (red lines denote the section I want to target).

Seems like I could just derive from tk2dBaseSprite and then define my own geometry for the 6 vertices and their U/V coordinates, but I wanted to make sure there wasn't another (easier) way to do this before I went down this path.  If I do go down this route, is there an optimal vertex ordering that I should use?  (I know for rects you usually order them as 2 triangles but not sure how that would work for a hexagon).

Let me know if you need any further info.  Thanks for taking the time to read this post!

David

profanicus

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 167
    • View Profile
Re: Non-rectangular sprite clipping
« Reply #1 on: August 21, 2013, 05:26:27 am »
Tk2d already supports custom geometry for sprites, look for Rendermesh: Custom. There you can make your hex-shaped polygon, or whatever shape you like. Is that the kind of thing you meant? I have used the same thing myself when I was mucking about with isometric tilemaps.

daviwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Non-rectangular sprite clipping
« Reply #2 on: August 21, 2013, 06:12:00 am »
Awesome, I hadn't seen that before!  I just created a custom render mesh for this sprite, will experiment with this to see what I can do with it :)

Thanks profanicus!

daviwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Non-rectangular sprite clipping
« Reply #3 on: August 21, 2013, 03:50:34 pm »
Hey Dinesh,

Based on a cursory investigation, it looks like this custom clipping polygon is just stored in the sprite definition's positions array.  Is there a good place to hook into tk2dSprite so that I could change (or replace) the pre-existing positions/uvs lists for a given sprite instance in the scene?  Ideally I'd like to be able to take a sprite with a normal rectangular clip region and modify it in code to have this polygonal clip region so that my artist doesn't have to set that region up for every tile he brings in to the sprite collection.

Thanks!

David

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Non-rectangular sprite clipping
« Reply #4 on: August 21, 2013, 05:06:14 pm »
While you could do this at runtime.... couldn't you simply get your artist to multi select them in the sprite collection editor and edit all of them in one go? Or add a button to do it in the editor?

It'll be way more efficient as the mesh data won't be invalidated as soon as its created.

daviwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Non-rectangular sprite clipping
« Reply #5 on: August 21, 2013, 05:35:32 pm »
Right, we could at least set up the geometry at edit time in the editor (Side note: are you suggesting there's actually a way to insert new UI into the sprite collection editor?  That would be handy  :)), but we also need to be able to modify the mesh points at runtime to affect the perceived height of the tile.  Basically, I need to be able to move the bottom 3 points of the hexagon (and their u/v coords) up and down based on the desired tile height.  It seems like I could override the Build or UpdateGeometry methods in tk2dSprite to achieve this, but I wanted to make sure there wasn't a simpler integration point before I did that.

Also worth mentioning that I don't intend to be doing these adjustments every frame, just when the tile height itself needs to be changed (we may or may not do this with smooth interpolation at runtime, trying to figure out what is possible).

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Non-rectangular sprite clipping
« Reply #6 on: August 21, 2013, 05:45:15 pm »
About changing the mesh at runtime, yeah there is one thing you could do...
Hook into the SpriteChanged event on the sprite. This is called as soon as any relevant sprite properties change.

But in your case, I think creating a new specialization of the tk2dSprite class might be the best way to do it.