Search Unity

rotations on a GUI texture

Discussion in 'Immediate Mode GUI (IMGUI)' started by ratamorph, Dec 17, 2007.

  1. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I'm trying to make an analog clock for my project's GUI, this clock needs to display the current time with its hands so I need to perform rotations on the Z axis of the hands in order to do this. I tried to rotate the GUI texture along the Z axis and nothing happens so I'm assuming rotations aren't allowed on GUI textures, any ideas?
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    you can do this with the new GUI system in Unity2 - assign a rotation matrix to GUI.Matrix and draw your clock hands with GUI.Box
     
  3. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    cool, I'll give that I try thanks!!!
     
  4. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    ok, I tried it, it does allow me to rotate GUI textures, but is there a way to set the pivit point? it's hard to control the rotation for thicker objects when the rotation occurs on the top left exactly, or is there a trick to it?
     
  5. JohnGalt

    JohnGalt

    Joined:
    Nov 27, 2007
    Posts:
    85
    You need to move your texture to its new center, rotate, then move back to its original position.

    Here I post a complete example taking into account the grouping system:

    Code (csharp):
    1.  
    2.      var orgX : int = Screen.width - SpeedometerBackground.width - 50;
    3.     var orgY : int = Screen.height - SpeedometerBackground.height - 50;
    4.     GUI.BeginGroup (Rect (orgX, orgY, SpeedometerBackground.width, SpeedometerBackground.height));
    5.     GUI.Label (Rect (0, 0, SpeedometerBackground.width,SpeedometerBackground.height), SpeedometerBackground);
    6.    
    7.     var tOffset:Matrix4x4 = Matrix4x4.identity;
    8.     tOffset.SetColumn(3, Vector4(-Needle.width/2.0 - orgX, -Needle.height/2.0 - orgY, 0.0, 1.0));
    9.     var tRotation:Matrix4x4 = Matrix4x4.TRS(Vector3(0,0,0), Quaternion.AngleAxis(needleAngle, new Vector3(0, 0, 1)), Vector3(1,1,1));
    10.  
    11.     var tPostOffset:Matrix4x4 = Matrix4x4.identity;
    12.     tPostOffset.SetColumn(3, Vector4(Needle.width/2.0 + orgX, Needle.height/2.0 + orgY, 0.0, 1.0));
    13.    
    14.     var tempMatrix:Matrix4x4 = GUI.matrix;
    15.     GUI.matrix = tPostOffset * tRotation * tOffset;
    16.     GUI.Label (Rect (0,0, Needle.width, Needle.height), Needle);
    17.     GUI.matrix = tempMatrix;
    18.    
    19.     GUI.EndGroup();
    20.  
    21.  
    A suggestion to whoever may be reading: It would be much more intuitive if GUI.matrix was expressed in group coordinates, that's it, relative to the current group. Right now it's referred to screen space, and you lose the power of groups.
     
  6. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    I know. I would love it to work within groups, but GPUs have problems clipping to arbitrary shapes. If GUI.matrix worked within groups, you could do such things as:

    * begin a group
    * rotate
    * begin another group

    Now you could have an 8-sided clipping "rectangle". Since that couldn't be supported across-the-board, it ended up with this solution
     
  7. JohnGalt

    JohnGalt

    Joined:
    Nov 27, 2007
    Posts:
    85
    aaah, I see... maybe rendering the groups to intermediate render targets that do the clipping for u?

    I suposse that generating the coords in the CPU is not an option...

    anyway... I don't see a big deal as is, maybe it's not as elegant or as intuitive as it could, but I don't see it as very important, as least for me :)
     
  8. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    That was sorta my opinion as well - when you know this will take many days to get right (Not all cards actually support rendertextures) - and have been working like mad for several months, it's like... oh well.. I can live without it :)