- Home /
Fastest rendering of 2D world with lots of changes to screen every frame?
If you have a texture, let's say 1024 * 1024, and your constantly changing pixels (getpixel setpixel apply) because on almost every update there are changes to draw - what's the most optimal technique to render that efficiently?
I looked into something involving render texture, and in not sure how that applies in this situation because that seems more for rendering a cameras view? What I've been trying is just having a quad with a camera, both setup to be full screen, but find the performance too sluggish for mobile use, which I have little optimization elsewhere, but bottleneck seems to be in apply() of textures rapidly... Who has suggestions? Must be better ways to handle this stuff? Any experts on 2D pixel level changes rapidly updating in unity? :)
There is no fast way of changing a texture entity as you describe it. Every change causes the entire texture to be loaded from the graphics card back into RA$$anonymous$$ and then back again. This is a very expensive process.
It would be better if you described what you want to accomplish, because most of the time, standard rendering techniques are much much faster.
Well basically I am doing dynamic terrain, that is able to be broken up and added to, so pixels can be removed or added to the ground, as well as a layer of "dynamic" pixels which can move around such as the player and projectiles. These work, but just bog down my old dual core notebook a bit, and thats when I do several setpixel() and at the end of that an apply() during updates. Basically I could do things like keep all the information in color arrays ins$$anonymous$$d of actual textures, if there were only some other way to get the color/alpha info directly drawn to screen (on mobile as well) that I can edit on the fly (a LOT) and have changes happen very quickly to keep the action going (think cortex command or liero)
I can only think of one other way to handle it - and its ugly and bad I know it - which would be lots of 1x1 quads, each with renderer color set to match an array of my colors - but my bet is that would be more costly than just two "texture2d"s and alot of apply and such. Who knows maybe that might perform better since there would be no loading unloading of texture info, but would mean that 1024 x 1024 = 1,048,576 quads with 100+ changing each frame from "removing" and "adding" pixels all over...
So ins$$anonymous$$d of a camera looking at a moving game world, your camera is looking at a texture, which is changing like your game would?
well yes, setup as "pixel-perfect" camera looking at 1024x1024 texture (for testing purposes i am using the square, later will be a pot rect)
Here is what it is, just a heavily re-coded version of this project, which I posted a few months back:
http://forum.unity3d.com/threads/198919-Destructible-Pixel-Terrain-Open-Source
Which was a port of a Java/Processing project, and as of right now I have been doing a ton of work on it without updating anything in that thread, so it is way way out of date - but gives you an idea whats I am trying to do here.... hope that helps explain my setup
A way you could avoid any CPU work would be to use a render texture and then evaluate changes using a shader (like a post-effect)
Answer by MD_Reptile · Nov 30, 2014 at 02:30 AM
A proper answer for this problem is to use smaller textures tiled to represent the overall texture, and only update (Apply() after setpixels) any tiles which have been changed. I ended up writing my own editor script which does this slicing and re-assigning automatically for me - saving me tons of effort and time, then I had to write methods that consider the "cells" of each section of the overall image and grabs pixels from there, and sets pixels from there. Good luck anybody trying to take this on!
PS Do not even think about trying to using little 1x1 quads as your representation of your world - works horribly.
Answer by unimechanic · Feb 21, 2014 at 09:17 PM
This question became a discussion, consider using Unity Forums which is a great place to discuss ideas. Adding this answer to remove it from the Unanswered view and give more relevance to other questions requiring attention. Thanks for your comprehension, Unity Support.
Answer by luniac · Nov 30, 2014 at 02:53 AM
It sounds like you want tilemaps like terraria or something... i believe 2Dtoolkit has a tilemap part to their plugin and it's pretty fleshed out and optimized. Maybe that's what you're looking for.
Well perhaps similar, but at per pixel destruction detail. I have solved this problem but thanks for your answer.