Or Particle collision point to UV 2: SetPixels Boogaloo
So I have my system setup where I get the UV coordinates of where particles collide, but now I’m stuck with a different problem. I want to draw a texture over a heightmap texture so that I can get the blood on the floor/walls/ceiling/grandma to stick. So far I can do this with circles using some borrowed code, but I want to draw a texture on impact instead.
Here’s the code I’m using that draws circles. Unfortunately, it isn’t super readable to me as it suffers from Single Letter Named Variables:
public void Circle (Texture2D tex, int cx, int cy, int r, Color col)
{
int x, y, px, nx, py, ny, d;
Color[] tempArray = tex.GetPixels ();
int length = tempArray.Length;
for (x = 0; x <= r; x++) {
d = (int)Mathf.Ceil (Mathf.Sqrt (r * r - x * x));
for (y = 0; y <= d; y++) {
px = cx + x;
nx = cx - x;
py = cy + y;
ny = cy - y;
if (py * tex.width + px >= 0 && py * tex.width + px < length) {
tempArray [py * tex.width + px] = col;
}
if (py * tex.width + nx >= 0 && py * tex.width + nx < length) {
tempArray [py * tex.width + nx] = col;
}
if (ny * tex.width + px >= 0 && ny * tex.width + px < length) {
tempArray [ny * tex.width + px] = col;
}
if (ny * tex.width + nx >= 0 && ny * tex.width + nx < length) {
tempArray [ny * tex.width + nx] = col;
}
}
}
tex.SetPixels(tempArray);
tex.Apply ();
}
I get it kinda but the math there doesn’t provide me with anything I can take away from to do what I want to do. I have the very bare minimum start of my own function here:
public void DrawSplatter (Texture2D heightMap, Texture2D splatterTexture, int texelX, int texelY, Color colour) {
Color[] heightmapArray = heightMap.GetPixels ();
Color[] splatterArray = splatterTexture.GetPixels ();
}
As you can see, I’m not very far in. What I think I need to do is the following:
- Find where the intersection point is and figure out if there’s any points on the splatterTexture that fall out of bounds so I can ignore that part and not paint it. I’m not sure how I’m supposed to do this because I’m not entirely sure how to find the valid intersections.
- I then need to actually draw over the heightmap texture. I’m not entirely sure how to do this either because I have no idea how to handle alpha values in this sort of situation. I suppose the best option would be to use Mathf.Lerp to blend them and use the alpha value for T?
- I also want to be able to use variable sizes for the splatterTexture so it doesn’t look so uniform. Is there a way I can scale the texture in the script so that I can accomplish this?
I know I’m asking kind of a lot here, but even being pointed in the right direction, especially for 1 and 3, would be great, thanks.