Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do you use Graphics.CopyTexture?

Discussion in 'General Graphics' started by FreeGameDev, Aug 27, 2016.

  1. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    I am trying to render my own font on the screen from a bitmap font. I have tried multiple ideas and every time I get blurry jagged letters. I am hoping CopyTexture might be the answer. My current script is attached to a camera and I draw right to the camera.

    Code (CSharp):
    1.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    2.  
    3.  {
    4.  
    5.   for (int i = 0; i < charData.Length; i++)
    6.  
    7.    {
    8.  
    9.     Rect r = GetNormalizedRectFromId(charData[i].id);
    10.  
    11.     Rect DestRect = new Rect(charData[i].x, charData[i].y, charData[i].width, charData[i].height);
    12.  
    13.     Graphics.DrawTexture(DestRect, charData.FontImage, r, 0, 0, 0, 0);
    14.  
    15.    }
    16.  
    17.   }
    18.  
    19.  


    Instead of using DrawTexture would CopyTexture be pixel perfect? Also how do I use this function, there is literally no info on it. Like what value do I put in for srcElement, srcMip, dstElement, and dstMip?

    Here is the syntax I want to use from the Unity Docs
    Code (CSharp):
    1. public static void CopyTexture(Texture src, int srcElement, int srcMip, int srcX, int srcY, int srcWidth, int srcHeight, Texture dst, int dstElement, int dstMip, int dstX, int dstY);
    Thanks in advance for the help. :)
     
    rAigner likes this.
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Those parameters have texture arrays and other texture types in mind. For a regular 2D texture, element will always be 0. The element can change to point at different textures within a texture array, for example. As for the mip, put 0 if your texture doesn't have mip maps enabled. If it does, you may want to copy each mip level as well. So you would copy source mip 0 to destination mip 0, source mip 1 to destination mip 1, up to the total number of mips.
     
  3. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    Thank you for that explanation. I was able to get it to work. But it is horribly slow. Also it does not seem to have any alpha.
    Here is my code. Any suggestion on simply copying a part of a texture to the screen with pixel perfect results? I just want something like the old bitblit function. Copy the pixel per pixel to the screen. Thanks again for the help. :)

    Code (CSharp):
    1.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    2.     {
    3.        // Texture2D dest = new Texture2D(Screen.width, Screen.height);
    4.         for (int i = 0; i < charData.charData.Length; i++)
    5.         {
    6.             Rect r = charData.GetNormalizedRectFromId(charData.charData[i].id);
    7.             float scale = 1f;
    8.             Rect DestRect = new Rect(charData.charData[i].x * scale, charData.charData[i].y * scale, charData.charData[i].width * scale, charData.charData[i].height * scale);
    9. //            Graphics.DrawTexture(DestRect, charData.FontImage, r, 0, 0, 0, 0, Color.red, renderMat);
    10.             Graphics.CopyTexture(charData.FontImage, 0, 0,
    11.                 charData.charData[i].x,
    12.                 charData.charData[i].y,
    13.                 charData.charData[i].width,
    14.                 charData.charData[i].height, dest, 0, 0, charData.charData[i].x, charData.charData[i].y);
    15.         }
    16.         Graphics.Blit(dest, destination,renderMat);
    17.     }