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

DrawTexture to draw part of a texture?

Discussion in 'Immediate Mode GUI (IMGUI)' started by SWGraham, Mar 27, 2008.

  1. SWGraham

    SWGraham

    Joined:
    Mar 10, 2008
    Posts:
    49
    I want to do something like the radar in Counterstrike: a great big texture of the whole map, and then only draw part of it in a GUI element. I don't think DrawTexture can do it; is there something I'm missing? I want to draw to x,y of the screen with w,h but from u,v on the texture.
     
  2. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    Use a second camera that points 90 degrees down and follows the character around as a render texture. Then use its render mask to make it only render what you want. Use this render texture on the gui object.
     
  3. SWGraham

    SWGraham

    Joined:
    Mar 10, 2008
    Posts:
    49
    But I don't want the actual world, I want to be showing part of a large stylized map image like CS. So I'll look into having my map be a RenderTexture and look up how to use the "render mask" like you say to draw part of it. Unless there is an easier way someone thinks of.
     
  4. SWGraham

    SWGraham

    Joined:
    Mar 10, 2008
    Posts:
    49
    Heh, if I put the map texture somewhere in the world, I could point a camera at it like you suggest. Put the map in a new Layer that only this camera can see. That's actually twisted-clever.

    But dood, a whole camera and render target for each texture that I want to scroll? Picture an odometer with 6 number strips that need to have what they draw be scrolled. There is really no uv control for drawing a texture in the GUI?
     
  5. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    If you have Pro, you could do this with the GL class. I don't know about layering that custom drawing with the GUI system, though.
     
  6. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    I guess you could somehow make a shader which only renders part of a texture based on width, height and an origin.
     
  7. SWGraham

    SWGraham

    Joined:
    Mar 10, 2008
    Posts:
    49
    Graphics.DrawTexture seems to be what I want. The page on GL told me to go there, so thanks for that tip. However, this function has no comments on it, and nobody has ever mentioned it in the forums.

    http://unity3d.com/support/documentation/ScriptReference/Graphics.DrawTexture.html

    Code (csharp):
    1. static function DrawTexture (screenRect : Rect, texture : Texture, sourceRect : Rect, leftBorder : int, rightBorder : int, topBorder : int, bottomBorder : int, mat : Material = null) : void
    I'm so happy that I found a function with SourceRect, but I'm having a hard time stabbing numbers in to these arguments blindly trying to figure out what they do.
     
  8. SWGraham

    SWGraham

    Joined:
    Mar 10, 2008
    Posts:
    49
    Okay, got it now. Thanks for the tip, Joachim. The border numbers are red herrings, and the SourceRect is in percents. So here is a texture slowly being scrolled upwards.

    m_mapSize is desired output size. m_map is the texture. m_scrollPercent is incremented slowly from 0->1 in Update.

    Code (csharp):
    1.     Graphics.DrawTexture(new Rect(0, 0, m_mapSize.x, m_mapSize.y), m_map, new Rect(0, m_scrollPercent, m_mapSize.x/m_map.width, m_mapSize.y/m_map.height), 0, 0, 0, 0);
    2.  
     
  9. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    You can also use GUILayout.BeginScrollView and put your entire map (enemy positions and all) into that view.

    It will automatically clip them as they reach the side of the view square too.

    You just need to style the scroll bars so that they do not display.
     
  10. SWGraham

    SWGraham

    Joined:
    Mar 10, 2008
    Posts:
    49
    Aw, heck. Looks like this function got deprecated in the last Unity release. SourceRect is ignored now. No matter what I put in, the texture gets squooshed in to the target. I'll file a bug, but just wanted to make sure anybody finding this thread in a search doesn't go crazy trying to make this work.
     
  11. SWGraham

    SWGraham

    Joined:
    Mar 10, 2008
    Posts:
    49
    Oh, and this doesn't work as a fallback because textures are not clipped like Labels are. m_mapScreenSize here does nothing. The whole texture always draws.

    Code (csharp):
    1.  
    2.     GUI.BeginScrollView( new Rect( m_mapPosition.x, m_mapPosition.y, m_mapScreenSize.x, m_mapScreenSize.y ),
    3.         m_mapTopLeft,
    4.         new Rect( 0, 0, m_map.width, m_map.height )
    5.         );
    6.     {
    7.         GUI.DrawTexture( new Rect( 0, 0, m_map.width, m_map.height ), m_map );
    8.     }
    9.     GUI.EndScrollView();
     
  12. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    If you need clipping try surrounding it with a GUI.BeginGroup.
     
  13. Ekta-Mehta-D

    Ekta-Mehta-D

    Joined:
    Feb 25, 2013
    Posts:
    24
    hey anyone got the solution ?