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?