Draw directly on a texture

I’m working on a task that draws a graph on a texture based on the data from the scene. What I’ve done is to use a separate camera for the graph. Then set the camera.targetTexture to a renderTexture. I used GL to draw the graph. However, I wonder if there is some way to draw directly into a texture without the camera twist. In other words, can I set a texture to be the render target in some way?

Well you could look at using SetPixels on the Texture2D to write to it’s pixels:

Thanks, MADmarine. I need draw lines on the texture. The data I have from scene is separate points. I can calculate the position (on the texture coordinate) of a point, but I don’t know how to draw a line on Texture2D directly. Before I used GL.LINES to draw lines with a separate camera by providing start and end points of lines.

as said with set pixels you can draw lines directly to the texture

How? If you can provide a simple example, it will be helpful.

Search for Bresenham algorithms for lines etc and then use this to decide which pixels you need to paint.

you can’t draw a texture into another one or just call ‘drawline’ and done unless you implemented a drawline

There’s a script on the wiki, which seems to be down at the moment, so here it is:

static function Line (tex : Texture2D, x0 : int, y0 : int, x1 : int, y1 : int, col : Color) {
    var dy = y1-y0;
    var dx = x1-x0;
    
    if (dy < 0) {dy = -dy; var stepy = -1;}
    else {stepy = 1;}
    if (dx < 0) {dx = -dx; var stepx = -1;}
    else {stepx = 1;}
    dy <<= 1;
    dx <<= 1;
    
    tex.SetPixel(x0, y0, col);
    if (dx > dy) {
        var fraction = dy - (dx >> 1);
        while (x0 != x1) {
            if (fraction >= 0) {
                y0 += stepy;
                fraction -= dx;
            }
            x0 += stepx;
            fraction += dy;
            tex.SetPixel(x0, y0, col);
        }
    }
    else {
        fraction = dx - (dy >> 1);
        while (y0 != y1) {
            if (fraction >= 0) {
                x0 += stepx;
                fraction -= dy;
            }
            y0 += stepy;
            fraction += dx;
            tex.SetPixel(x0, y0, col);
        }
    }
}

–Eric

Thank you for all your help. I really appreciate it. After I integrated the code (Eric provided) into my program. It works. When I dragged one parameter to increase/decrease more data, the curve on graph is dynamically changed.