Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rendering text to a RenderTexture... how?

Discussion in 'Scripting' started by markt1964, Nov 29, 2011.

Thread Status:
Not open for further replies.
  1. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    A normal google search seems to indicate that it is possible with Unity Pro, and I've found a couple of pages that describe, mostly in passing, how it would be accomplished, but I cannot find any actual code examples of actually how it it can be done. Any assistance or help would be greatly appreciated.

    Thanks in advance.
    Mark
     
    Last edited: Nov 29, 2011
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  3. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    Thank you, although if I was unclear before, I am looking for some actual code samples so that I can see how to do it in code, not just within the editor.

    Thanks again
    Mark
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    never tried to code it, but I could imagine it would something like this.

    please note: this is complete speculation based on the way outlined in the previous link (plus info from script ref: http://unity3d.com/support/documentation/ScriptReference/RenderTexture.RenderTexture.html , so it may be wrong ;-))

    Code (csharp):
    1.  
    2. public Camera sourceCamera;
    3. public RenderTexture renderTexture;
    4.  
    5. void Start()
    6. {
    7. if(sourceCamera)
    8. {
    9.   renderTexture = new RenderTexture(128,128,24, RenderTextureFormat.Default);
    10.   sourceCamera.TargetTexture = renderTexture;
    11.  
    12.   renderTexture.Create();
    13.  
    14. }
    15.  
    16. }
    17.  
    18.  
     
  5. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    That's what I've noticed when I did a google search for it... lots of people having the general idea, but nobody actually demonstrating how to do it. :)

    Again, however, I am looking for an example that actually draws some text onto a RenderTexture, which can then be utilized elsewhere in code as any other texture might be (either applied as a material onto an object or what have you).

    While the code you've shown illustrates how to set up a camera's RenderTexture, it doesn't illustrate how text could be drawn into that texture, which is what I'm actually trying to accomplish.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    A rendertexture is simply what a camera sees, rendered into a texture. You don't need any code; just put a GUIText or 3DText object in front of the camera that's generating the rendertexture.

    --Eric
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    lol I completely glossed over the fact you were trying to embed text... my bad... haha.. just thought you were trying to get it going through code.

    think I need another coffee
     
  8. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    This is what I had originally, just some code quickly hammered together to see if I can a texture with some text in it. It does not work. AT ALL.

    It crashes right after I try to create a new camera... apparently, I cannot allocate my own Camera with the new operator. There is another camera that I am already using and do not wish to mess with, so I'm not sure what all I need to do to set it up. Also, in the following code, what do I have to do to make sure that the guitext I am making is seen by the camera? Finally, of course, will the resulting texture be transparent on the pixels that aren't covered by the font, or are they going to be black and I will have to do some additional processing on the texture to make them transparent?
    Code (csharp):
    1.  
    2.     Texture2D output = new Texture2D(200,200);
    3.     RenderTexture renderTexture = new RenderTexture(200,200,24);
    4.     Camera myCamera = new Camera();
    5.     myCamera.orthographicSize = renderTexture.height/2;
    6.     myCamera.orthographic = true;
    7.     myCamera.targetTexture = renderTexture;
    8.     GUIText guiText = new GUIText();
    9.     guiText.text = "Hello\nWorld!";
    10.     guiText.anchor = TextAnchor.LowerLeft;
    11.     guiText.alignment = TextAlignment.Left;
    12.     guiText.lineSpacing = 1;
    13.     guiText.pixelOffset = new Vector2(50,50);
    14.     guiText.font = (Font)Resources.Load("Fonts/ARIALUNI_3");
    15.     guiText.fontSize = 5;
    16.     myCamera.Render();     
    17.     RenderTexture.active = renderTexture;      
    18.     output.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    19.     output.Apply();
    20.     RenderTexture.active = null;
    21.  
    Again... any assistance would be appreciated.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Correct...only GameObjects can be created using the new operator. A Camera is a component and is added to a GameObject using AddComponent (or as one of the default components when creating a GameObject).

    --Eric
     
  10. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    What GameObject am I to add my camera to? How do I make sure that the guitext I create will render with it?
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Whatever one you want. Or, as I said, you can make a new one using the new operator.

    --Eric
     
  12. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    (sigh) still doesn't work.

    What I have now is:
    Code (csharp):
    1.  
    2.     Texture2D output = new Texture2D(200,200);
    3.     RenderTexture renderTexture = new RenderTexture(200,200,24);
    4.     RenderTexture.active = renderTexture;      
    5.     GameObject tempObject = new GameObject("Temporary");
    6.     Camera myCamera = tempObject.AddComponent<Camera>();
    7.     myCamera.orthographic = true;
    8.     myCamera.orthographicSize = 100;
    9.     myCamera.targetTexture = renderTexture;
    10.     GUIText guiText = tempObject.AddComponent<GUIText>();
    11.     guiText.text = "Hello\nWorld!";
    12.     guiText.anchor = TextAnchor.LowerLeft;
    13.     guiText.alignment = TextAlignment.Left;
    14.     guiText.lineSpacing = 1;
    15.     guiText.pixelOffset = new Vector2(50,50);
    16.     guiText.font = (Font)Resources.Load("Fonts/ARIALUNI_3");
    17.     myCamera.Render();
    18.     output.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    19.     output.Apply();
    20.     RenderTexture.active = null;
    21.         Material newMaterial = new Material (TEXT_SHADER);
    22.         newMaterial.mainTexture = output;
    23.  
    Although it doesn't crash now, it doesn't draw anything to the output texture either.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Is there any particular reason it has to be done with code? It would be easier if you set everything up in the editor.

    --Eric
     
  14. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    The above example is only a very basic test that I can quickly run, so that I can learn how to actually implement it... once I know how to get it working, I am going to be getting text to put into many different textures at runtime, and I will not be able to set it up inside the editor.
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Yes, but that only requires doing guiText.text = "blah"; is there a reason why anything else needs to be in code?

    --Eric
     
  16. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    Because source code is svn merge-friendly, while the binary data files that Unity seems to use are not.
     
  17. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    Just a one-shot bump to the top to see if anybody else has any further suggestions.

    Thanks in advance again for any assistance... it would be hugely appreciated.
     
  18. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I think there is still confusion about your requirement. The question Eric is asking is why you can't set up and position the camera and objects in the editor and then use code to change the text being drawn in the RenderTexture.
     
  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Well, he answered that it's because of SVN. My opinion is that intentionally making things harder because of the version control you're using seems kind of self-defeating, but it's not my project.

    --Eric
     
  20. markt1964

    markt1964

    Joined:
    Aug 30, 2011
    Posts:
    83
    Of course, I am perfectly aware that setting it all up in code is liable to be significantly more work than using the editor anyways... if it were easy, there would likely be far more information available on how to actually accomplish it, which I probably would have found yesterday during my rather extensive google search yesterday morning.

    I stated my requirement in the beginning that I intend to set this all up in code. The reason why is related to the limitations of text-based version control, which is what I am required to use. This reason, however, is wholly immaterial to how it can actually be accomplished.

    If this is simply not possible, I can, I suppose, live with that. (I may not like it, of course... because it means that Unity may simply not be suitable for projects that depend heavily on revision control systems like svn).
     
    Last edited: Nov 30, 2011
  21. GilesDMiddleton

    GilesDMiddleton

    Joined:
    Aug 12, 2015
    Posts:
    91
    I think you are missing a canvas? I made the same mistake today, adding a canvas that rendered to this camera worked a treat.

    I suggest you first construct your items using the editor, then work out how to script them - it's much easier.
    You may even decide that you can keep your scene objects, and just enable them for tiny moment just to call render, then disable them again. That's what I've done for a solar-system selection screen - I have another mini scene within my scene at -50000,-50000,-50000 which I just setup with objects, enable, render to texture, disable. It includes a canvas so I can place text onto it. That texture can then be placed on a panel or in a HUD. Although If baked into a single rendertexture which isn't on an active camera, none of the GUI elements will respond/work, so if you add buttons, you'd have to do that on another gui surface.

    You can however, just create an arbitrary gui surface and put it into world coordinates, and make it layer ontop of a plane/cube that Is a console. There are many ways of approaching this. And now RenderTexture is supported in the Free mode - even noobs like us can do cool things.
     
  22. BlaideFedorowytsch

    BlaideFedorowytsch

    Joined:
    Nov 28, 2022
    Posts:
    2
    I know this is a real old forum thread but

    Code (CSharp):
    1. Graphics.blit( (Texture source, RenderTexture destination );
    This would copy a texture to a rendertexture, so if you write the text into a texture, pixel by pixel,.. you could then copy that to the rendertexture.
     
  23. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
Thread Status:
Not open for further replies.