How to efficiently count the number of pixels which can be seen from a certain point of view in MonoGame if those pixels have a certain color

那年仲夏 提交于 2021-02-11 12:07:20

问题


I have a project on monogame platform. The purpose of the project is to make the calculation of the viewfactor of geometry put into the platform using the ortographic method. In a basic level, I put a basic cube and camera across from the cube. Here as I look into the cube through the camera, I am required to count the number of pixels of an object seen from a perspective by the ortographic method. I already have a solution but it is very slow. In my solution, I count the number of pixels with a certain color and then divide that number to the total number of pixels on the screen. I have heard of a technique that involves using OcclusionQuery. But I guess I have to do some shader programming in order to use that technique, of which I do not have a clue. Can you guys do some suggestions if there is another technique that is easy to implement and faster than what I recently do or explain how that OcclusionQuery works.Here for example I count the total number of grey pixels then divide it to total screen area

here you can find my code written below;

    private void CalculateViewFactor(Color[] data)
    {
        int objectPixelCount = 0;
        var color = new Color();
        color.R = data[0].R;
        color.G = data[0].G;
        color.B = data[0].B;
        foreach (Color item in data)
            if (item.R != color.R && item.G != color.G && item.B != color.B)
                objectPixelCount++;

        Console.WriteLine(objectPixelCount);
        Console.WriteLine(data.Length);
        Console.WriteLine( (float) objectPixelCount / data.Length);
    }

due to fact that the color of the first pixel of the screen is also color of the background, I take the RGB values of the first pixel and compare these RGB values to all the other pixels on the screen and count the number of pixels which has a different color from the first pixel. But as I know that this method is pretty slow, I want to adapt OcclusionQuery into my code. If you could help me, I would be grateful.


回答1:


This is pretty tricky to do right, and I can only suggest an "alternative", not necessarily more performant or better design-wise approach.

In case you don't really need to know exact number of drawn pixels, you can approximate it. There is a technique called Monte Carlo Integration.

Start off by creating N points on the screen with random coordinates. You check and count colors at these points. Divide the number of points with color of your object by the total number of tested points (that is N). What you get is an approximate ratio of pixels that your object occupies on the final screen. If now you multiply this ratio by the total number of pixels on the screen (that is WidthPx * HeightPx) you'll get an approximate number of pixels occupied by the object.

Advantages:

  • Select bigger N for more accurate result, select lesser N for better performance
  • Algorithm is simple, harder to screw it up

Disadvantages:

  • It's random and never deterministic (you'll get a different result every time)
  • It's approximate and never exact
  • You'll need to generate 2 * N random values (two for each of test points), generating random values is a long operation
  • I'm sure later you'll want to draw textures/shading on the screen, and then this technique won't work as you'll not be able to distinguish pixels of your object and the others. You can still manage a smaller unseen buffer, where you draw the same objects, but without any shading, and each object having same unique color, then you apply Monte Carlo algorithm on it, but of course, that'll cost computing resources.


来源:https://stackoverflow.com/questions/60135762/how-to-efficiently-count-the-number-of-pixels-which-can-be-seen-from-a-certain-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!