Hello Guest

Author Topic: Texture2D realtime modification  (Read 5260 times)

MariusVE

  • Newbie
  • *
  • Posts: 6
    • View Profile
Texture2D realtime modification
« on: October 14, 2013, 10:58:34 am »
Hi All,

I would like to start using 2D Toolkit but before I purchase it, I was wondering if my scenario is somehow compatible with it.

I will be working on a game where the player will be able to "dig" into the terrain. This will be a 2D game so in terms of that, I am sure 2D Toolkit is the right fit for me.

However, as I mentioned above, the nature of the game requires me to modify a Texture2D in real time as the player "digs" through it.

In order to simulate the digging effect, my idea is to set the alpha channels of the pixels where the player is digging to fully transparent. I know that SetPixel/GetPixel is really slow so my idea is to break up the main background Texture2D into multiple other Texture2Ds (kind of having a bunch of tiles that make up the whole background Texture2D) and only do SetPixel/GetPixel/Texture.Apply on the affected Texture2Ds (i.e., where the player is digging.) The game will run on mobile platforms so, indeed, performance is important.

Can 2D Toolkit help with this scenario or is it at least "compatible" with something like this ? (i.e., combine Texture2Ds with 2D Toolkit sprites, etc.)

If you guys have any other ideas how to implement this in an efficient manner, I would really appreciate if you'd tell me. :) I was thinking about a shader but I need to actually modify the Texture itself because I need to run an "edge-detection" algorithm on it in order to create colliders so that I can have stuff fall through the dug out paths....

Thank you!


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Texture2D realtime modification
« Reply #1 on: October 14, 2013, 11:17:45 am »
I suggest using the tilemap system for this, coupled with your own collision detection. It should be fairly efficient to build this out of smaller tiles - you would need combinations of tiles to simulate the digging, but that will be far more efficient than modifying the texture using Get/Set Pixels.

Alternatively, if you really still need to modify the texture you can use render textures (Unity Pro only) to modify the texture at runtime - that will be way more efficient than get/set pixels as well.

MariusVE

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Texture2D realtime modification
« Reply #2 on: October 14, 2013, 11:21:57 am »
Hi Unikronsoftware,

Thank you for your prompt reply. I do own Unity3D Pro so my question is this:

 - Would it be more efficient to use your tilemap system or should I be using Render Textures ?

Thank you!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Texture2D realtime modification
« Reply #3 on: October 14, 2013, 11:35:06 am »
It depends on what you want to do. Tilemaps are much much easier to deal with, and you can have relatively big worlds without having to code your own paging system. Using render textures on the other hand will require some form of system written to page date in and out unless your game is presented on one screen only.

I would suggest trying tilemaps first if you can get away with it - but don't use the built in collision system, roll your own here based on tileIds and such. The slowest part of the tilemap building is updating colliders, and if you can avoid rebuilding it completely using your own collision system, you can modify anything you like on there at runtime.

MariusVE

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Texture2D realtime modification
« Reply #4 on: October 14, 2013, 11:53:54 am »
Thank you so much for your help!