- Home /
Compositing a cursor over a rendertexture - GL pipeline problems?
Basically I'm trying to recreate the interactive surfaces in Doom3 using RenderTextures. So far I raycast every frame to see what the player is pointing at, and if it's using a RenderTexture, I get the camera, target texture, and texture coordinates of where on the texture the player is pointing.
The camera-find algorithm is brute force, but there will only ever be a few active cameras in a given Scene, so while alternatives are welcome this won't be too bad of a performance hit.
What I want to do next is display a HTML-esque hand cursor at the specified texture coordinates as a precursor to making and using a simple UI, but it's not working. What am I doing wrong?
Note: This attempt incorporates solutions from other unityAnswers, available [here],[1] [here],[2] [here],[3] and here.
using UnityEngine;
using System.Collections;
public class mouseScript : MonoBehaviour {
public Camera lastrendTexCam;
public GameObject lastSightedObj;
public GameObject testObj;
public Texture2D cursorTexture;
string lastScreen = null;
Ray tempray = new Ray();
RaycastHit temphit;
RenderTexture rendTex;
private int i;
float wperc;
float hperc;
// Use this for initialization
void Start () {
lastrendTexCam = new Camera();
lastSightedObj = new GameObject();
testObj = new GameObject();
temphit = new RaycastHit();
rendTex = new RenderTexture(1,1,0);
i = 0;
wperc = 0.0f;
hperc = 0.0f;
cursorTexture = new Texture2D(128,128);
cursorTexture = (Texture2D)Resources.Load("UI/hand");
}
// Update is called once per frame
void Update () {
tempray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(tempray, out temphit)){
print ("Raycasted " + temphit.transform.name);
}
if(i == 0 || temphit.transform.CompareTag("Interactive")){
testObj = temphit.transform.gameObject;
i++;
isCamera();
}
}
bool isCamera(){
rendTex = (RenderTexture) testObj.renderer.material.mainTexture;
Camera[] cameras = FindObjectsOfType(typeof(Camera)) as Camera[];
foreach(Camera c in cameras){
if (c.targetTexture == rendTex){
lastrendTexCam = c;
lastSightedObj = temphit.transform.gameObject;
lastScreen = lastSightedObj.name;
var pixelUV = temphit.textureCoord;
wperc = (pixelUV.x * rendTex.width);
hperc = (pixelUV.y * rendTex.height);
print (cursorTexture);
drawCursor ();
return true;
}
}
print ("Didn't find the RenderTexture target.");
return false;
}
void drawCursor(){
RenderTexture.active = lastrendTexCam.targetTexture;
print (RenderTexture.active);
GL.PushMatrix();
GL.LoadPixelMatrix (0, 2048, 2048, 0);
Graphics.DrawTexture (new Rect (wperc, hperc, cursorTexture.width, cursorTexture.height), cursorTexture);
GL.PopMatrix ();
RenderTexture.active = null;
}
}
[1]: http://answers.unity3d.com/questions/13512/how-do-you-programmatically-retrieve-the-rendering.html
[2]: http://answers.unity3d.com/questions/16139/when-does-rendertextureactive-myrt-work.html
[3]: http://answers.unity3d.com/questions/23523/pass-clicks-through-camera.html
Did you find any avenues for success with this? I need something like this for a project I'm working on, but rather than displaying an HT$$anonymous$$L cursor I'm ok with using the system cursor as long as it can translate coordinates effectively for a raycast selection through the secondary camera(s).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
When making a large scene/map/level, is graphic/quality compromise inevitable? 2 Answers
Best way to convert particles and sprites in a single sprite at runTime 1 Answer
Split a big level mesh in Unity 2 Answers
Help! how to convert input.GetAxis to Accelerometer 0 Answers