Drawing pixels (Dynamic pixel sprites)

Hey, so i’ve been thinking about it for a while. How can i draw pixels to the screen for example with my mouse. I know i can use SetPixel() but its waaay to expensive… Shaders? Is it possible to draw pixels with shaders? Im familiar with them but i cant possibly think of a way to draw pixels using them. Any ideas? Any help is reeally appreciated!

-Thanks!:))

SetPixels works reasonably well, and can be performant. How many textures are you setting each frame that it doesn’t work for you?

One way I used before was to have the camera render a render texture - when you click the mouse you can then just draw to that render texture using openGL commands. For example, here’s how I render some debug points:

        GL.PushMatrix();
        m_LineMaterial.SetPass(0);
        GL.Begin(GL.LINES);
     
        foreach( Vector3 Point in Helpers.GetDebugPoints() )
        {
            float fZ = 10.0f;
            GL.Color( Color.green );
         
            GL.Vertex3( Point.x , -Point.y , fZ );
            GL.Vertex3( Point.x , -Point.y + 1.0f , fZ );
        }

        GL.End();
        GL.PopMatrix();

This needs to be called in the OnPostRender function of the camera you’re using. You don’t actually need to use a render texture either - that would just be more helpful if you didn’t want to redraw all the points each time.

Lets say for example i have a scene with 100x80 pixels on a single texture. If i were to draw pixels to it every single frame we are talking about a lot of drawcalls. What if i want to constantly draw 7 lines using Bresenham’s algorithm? Its expensive. Fps wise its not really what im looking for:smile:

Seems to me we’re talking about one call per frame.

You see he said SetPixels, right? SetPixel is slow if you have to call it hundreds of times, but set them all at once and it’s much faster.

I don’t think that word means what you think it means.

Do the calcs once, then call SetPixels once on the entire texture. SetPixels on a 100x80 texture should have virtually no performance inpact.

Oh Okay. But what if i want to draw lines every second lets say. Still wouldnt that be expensive?

No. Doing pretty much anything once per second is downright leisurely in game development world. Honestly both me and BoredMormon (I assume) have been answering on the assumption that whatever you’re doing, you would’ve been doing every frame.

2 Likes

This.

I’ve got RTS style games that use SetPixels to do a map overlay every frame. Sure there is a limit, but a few textures every frame won’t kill performance.

1 Like