- Home /
In game paint brush with spreading. LoadRawTextureData() issues.
Hi everybody. This is my first question here ! My english is poor and forgive me for my typos/misspelling.
First of all, take a look on the result i want :
This is close to the final result, and the shader part is not merged yet, but that's a good visual to show what i want. The problem is : my CPU is totally overloaded. It looks smooth, but it's not when i have a great amount of points.
I'm going explain how i proceeded. It's a 2D game with sprites.
When the game start, i create a texture from scratch, same size of the sprite it's on.
Then when Input.MouseButtonDown, i cast a Ray and if it hits the sprite, i start a function which create a cricle.
When a circle is drawn, the function will draw the same circle next frame with radius +1 until it reaches the bounds of the sprite.
What am i using?
I create my texture like that :
texture = new Texture2D(spriteWidth, spriteHeight, TextureFormat.Alpha8, false);
filling = new byte[spriteWidth * spriteHeight];
for(int i=0; i<filling.Length ; i++){
filling[i] = 0x00;
}
// Load data into the texture and upload it to the GPU.
texture.LoadRawTextureData(filling);
texture.Apply();
// Assign texture to renderer's material.
targetRenderer.material.mainTexture = texture;
I'm using a byte array to stock alpha (alpha8 texture) to minimize useless data.
But i read something that said actually, references are created in my array so it uses 4bytes to create a reference (same like pointers in c++, doesnt it?) to read the 1byte i've created? It sounds stupid. Is this true?
On a second hand, i've got spikes in the profiler when i apply my changes :
I've got an algotithm to get the pixels on texture to change inspired from this where i use C# part
It works well except the part where i apply changes on my texture. I'm doing like this :
public void RefreshAndApplyTexture()
{
texture.LoadRawTextureData(filling);
texture.Apply();
targetRenderer.material.mainTexture = texture;
}
It looks like LoadRawTextureData() burning my CPU everytime it's called because it had to refresh my whole array of pixels (around 1 million entries for 1k texture). I call it every frame i have to draw a cicle, for each point which draw a circle (with 10 origin points, it will be called 10 times). I don't know how to deal with this and i assume that i've reached c# limits... Or i'm just using the wrong way to do it.
Can you tell me how you would process for something like that?
This is my profiler :
And by the way, i think my 1 million entry array makes my garbage collector mad, using around 6ms every spike...
Thank you for reading this and i hope you will be able to help me.