Texture coordinate problem

Hello, I’ve just got a shader based Conway’s game of life working.

I am using Render Texture on a quad to display the texture.

And now I’ve created this script to draw life pixels on the texture with custom texture brush.

using UnityEngine;

public class DrawLifeTex : MonoBehaviour {

    public RenderTexture renderTexture;

    public Texture brushTexture;
    public int size = 255;

    void Update () {

        if (Input.GetMouseButton(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if ( Physics.Raycast (ray,out hit,10000.0f)) {
                if (hit.transform == transform) {
                    RenderTexture.active = renderTexture;
                    Vector2 hitPos = hit.textureCoord;
                    hitPos.x *= renderTexture.width;
                    hitPos.y *= renderTexture.height;
                    Graphics.DrawTexture(
                        new Rect(
                            (int)hitPos.x,
                            (int)-hitPos.y,
                            size,
                            size * 0.5f
                        ),
                        brushTexture
                    );
                    RenderTexture.active = null;
                    print(hitPos);
                }
            }
        }

    }
}

The brush and drawing works, but it is way off the coordinate.

Print(hitPos);

is showing the right coordinate (64, 64) on the edge of the 64x64 texture.


What went wrong? :frowning:

Graphics.DrawTexture uses screen coordinates. It sounds like you want to draw directly on the RenderTexture itself which means you should be using Unity - Scripting API: RaycastHit.textureCoord instead.

Nevermind, I found someone else from another thread with the same problem, I modified the code to fit mine, and it now works!

Here is the new code!

using UnityEngine;

public class DrawLifeTex : MonoBehaviour {

    public RenderTexture renderTexture;

    public Texture brushTexture;
    public int size = 11;
  
    void Update ()
    {
        if (Input.GetMouseButton(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if ( Physics.Raycast (ray,out hit,10000.0f)) {
                if (hit.transform == transform) {
                    GL.PushMatrix();
                    GL.LoadPixelMatrix(0, renderTexture.width, renderTexture.height, 0);
                    RenderTexture.active = renderTexture;
                    Vector2 hitPos = hit.textureCoord;
                    hitPos.x *= renderTexture.width;
                    hitPos.y = renderTexture.height - hitPos.y * renderTexture.height;
                    Graphics.DrawTexture(
                        new Rect(
                            (int)hitPos.x - size/2,
                            (int)hitPos.y - (size * 1.35f)/2,
                            size,
                            size * 1.35f
                        ),
                        brushTexture
                    );
                    GL.PopMatrix();
                    RenderTexture.active = null;
                    print(hitPos);
                }
            }
        }
    }
}