SetPixels, SetPixels32 so slow

Hello,
I’m creating a game where you can destroy pieces of texture at each frame.
I’m on Unity 2D in free version (64bits).
So I tried using SetPixel to color my texture in transparency. After several tests I quickly realize that it is really very slow.
After several searches I ended up understanding why. Indeed with each modification, it is necessary to reupload all the texture in the Graphic Card.
By continuing my research on the forum I conclude that using SetPixels was more efficient. After still testing it’s still too slow.
In the end I tested the method SetPixels, SetPixels32, Large texture 1024x1024, Small texture 32x32, putting my texture in a power of two and also all these combined methods.
In the end on the forums we often talk about alternative RenderTexture or Shader style but no concrete example.
So, please, is there a method for doing this type of operation very quickly?
Thanks for not giving me a link to an asset because I would like to understand.

Thank you very much because its 3 days I search everywhere and I code full of unsuccessful tests…

(Sorry if my English is not perfect)

I understand your frustration. Yes, the real bottleneck is that the texture then needs to be uploaded to the GPU, so it doesn’t matter whether you use SetPixels or SetPixels32. Worse yet, this can also cause a pipeline stall because CPU may be forced to wait for the GPU and/or vice versa.

Have you looked into using shaders instead?

Thank you for your reply.
I do not know the shaders at all but for me it’s just visual not? I also need the information (GetPixel) to make my collisions.
I do not understand what the shaders are.

In any case thank you to answer me, its gives me a little hope :slight_smile:

It depends on what you’re trying to do, so I can’t say for sure whether you want to look at shaders or not. However, since you’re also looking to use GetPixel, I suppose you need something more complex than just doing it all in a shader.

For what it’s worth, you can get/set custom shader properties through the Material class (e.g. Material.SetVector, etc.)

Also for what it’s worth, I’ve done something similar where I needed to perform collision testing using a depth value from a shader. A “ray cast” was done using a camera with a FOV of 1 degrees, and a 1x1 render texture. Accessing data from this render texture only took a millisecond or two, which was an acceptable performance hit in my case.

I don’t know if you’re doing the same thing, but hopefully that’ll give you food for thought.