- Home /
DrawTexture on RenderTexture without Camera
Hello, what I am trying to do is the following: I want to draw an array of Bullets onto a RenderTexture. This Texture is then used for a Button.
This is so the user can choose between three different patterns of Bullets.
Displaying the texture on the Buttons is no Problem. However Drawing onto the Texture gives really weird results.
Here's my Code for Drawing the positions onto the texture:
preview [patIdx] = new RenderTexture (256, 256, 0);
preview [patIdx].enableRandomWrite = true;
preview [patIdx].Create ();
RenderTexture.active = preview [patIdx];
float halfsize = bulletPreview.width / 2;
for (int i = 0; i < shots; i++) {
float x = (positions [i, 0] + 0f) * 2f / 6f;//+3*256/6
float y = (positions [i, 1] + 0f) * 2f / 8f;//+4*256/8
Graphics.DrawTexture (new Rect (x - halfsize,
y - halfsize,
halfsize * 2,
halfsize * 2),
bulletPreview, null);
}
RenderTexture.active = null;
halfsize is half of bulletPreview's width/height. This gives me an empty Texture. If I change the Rect to
Graphics.DrawTexture (new Rect (-1f, -1f, 2f, 2f), bulletPreview, null);
bulletPreview gets drawn over the entire texture, so apparently the coordinate space for rect goes from -1 to 1 even though DrawTexture's Documentation states otherwise. This would be no problem if I could just scale my coordinates into [-1,1] but it gets rounded/floored to the nearest integer, so i can only chose onto which quarters of my RenderTexture I can draw.
If this explanation is unclear, please let me know and I'll try to show examples.
Answer by bastischo · Jun 06, 2016 at 12:04 PM
Since I have found no Solution I'll post my workaround.
I created a Camera for every RenderTexture and placed them far away from the actual game. my game is in x/y-range[-3..3,-4..4] and the cameras are at x=100/130/160. then i assigned the RenderTextures as target Texture for these cameras and added the sprites in front of them as GameObjects with only Transform and SpriteRenderer Components
Your answer
Follow this Question
Related Questions
How to get the rendered texture from a camera, save for later display on another camera? 1 Answer
Black RenderTexture when used with GUI.DrawTexture 0 Answers
Render only certain levels of UI 0 Answers
Graphics.DrawTexture vs GUI.DrawTexture vs Graphics.Blit 0 Answers
New GUI - Text not rendering in front of geometry if using a Mask 1 Answer