My first through was to use a camera and a render texture to generate a depth buffer. However, I wasnt able to get the generated texture into any script to get the size.
My second throught was to use raycasts but that seemed a bit performance heavy.
Raycasts seem like it would be fine. You only need to shoot a Raycast at each of the corners, however your problem is going to be trying to figure out the size of it from the Raycasts.
I haven’t messed with it too much, but if you’re using a camera and a render texture, you can filter the camera to only catch specific objects, so you could render a texture, and count the colored pixels on it.
I might have misrepresented my problem. I dont want to only capture simple objects like this cube, but also quite complex objects.
I already got the depths buffer part working. My problem ist to get the texture somewhere where I can calculate the size an use it in other stuff. I found people saying I can use ReadPixel but Im unable to find this method.
Your problem is likely to be that it will not be performant enough for any given size of image. Getting pixels and counting them in C# is super-slow. You might get away with it if you additionally render to a very small surface, like 32x24 or something like that, and use that as a proxy for the areal coverage of your main image.
To do image processing in realtime you might want to consider something like OpenCV, which is even distributed through a third-party integration on the Asset Store, or you can integrate it yourself from the sources:
Depending on what you actually want to do, converting your coordinates to screen coordinates and calculating the surface area of the resulting polygon is more performant and easier than reading pixels. Also depending on the real, underlying goal, you may get away doing this for the bounding box.
I see why I dont have the ReadPixel method. the RenderTexture inherits from Texture while the ReadPixel method is only available in Texture2D.
But I cant find a way to converting from Texture to Texture2D. Casting it yielded no results. using Graphics.CopyTexture() always throw a error because I cant get the color format right.
I was able to get some results using texture.LoadRawTextureData(rTexture.GetNativeTexturePtr(), rTexture.width * rTexture.height * 32) however the values seem to be very off. Is there anything I might be missing?
Also I might have guessed that I need some Library like OpenCV. However I didnt know that there is a unity package for it. Do you know if it has any advantages over using the Nuget Package?
As of my final goal (as I might be suffering from the XY-Problem). I want to make a game where you can build your own vehicles. I want to make it somewhat realistic. I want to use the “size” to create a more realistic drag.