Search Unity

Graphics.DrawTexture Rotation

Discussion in 'Scripting' started by SkippyV, Jan 3, 2014.

  1. SkippyV

    SkippyV

    Joined:
    Dec 13, 2012
    Posts:
    34
    I'm dynamically drawing textures onto a rendertexture but I need to perform a rotation when drawing.

    I've been scouring the forums to understand this but no luck yet.

    Here's a bit of code that I use to draw on my rendertexture. My problem is that I can't figure out a way yet to draw at varying angles. I'd like to draw with a rotation of x degrees around the center of the texture.

    Code (csharp):
    1. RenderTexture.active = myRenderTexture;
    2.        
    3. #if UNITY_EDITOR
    4. myRenderTexture.DiscardContents();
    5. #endif
    6.        
    7. GL.PushMatrix();
    8. GL.LoadPixelMatrix(0, textureSize.x, textureSize.y, 0);
    9.  
    10. // HOW TO ROTATE AS NEEDED
    11.  
    12. Graphics.DrawTexture(screenRect, myTexture, myMaterial);
    13.  
    14. GL.PopMatrix ();           
    15.        
    16. RenderTexture.active = null;
    I've played with modifying the shader to see if I can perform rotations in the vertex shader with no luck. I'm wondering if I can apply the rotation in GL, but I'm very GL challenged (and shader challenged for that matter).

    Any suggestions? TIA.
     
  2. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Have you tried something like this?

    Code (csharp):
    1.  
    2. private Vector2 pivotPoint;
    3. private float fRot = 0.0f;
    4. ...
    5. Matrix4x4 origMatrix = GUI.matrix;
    6. pivotPoint = new Vector2( position.width / 2, position.height / 2 );
    7. //GUIUtility.ScaleAroundPivot( scale, pivotPoint );
    8. GUIUtility.RotateAroundPivot( fRot, pivotPoint );
    9.        
    10. if (GUI.Button(new Rect(position.width / 2 - 20, position.height / 2 - 20, 50, 50), "Big!"))
    11. {
    12.     //scale += new Vector2(0.5F, 0.5F);
    13.     fRot += 22.5f;
    14. }
    15.        
    16. GUI.matrix = origMatrix;
    17.  
     
    Last edited: Jan 3, 2014
  3. SkippyV

    SkippyV

    Joined:
    Dec 13, 2012
    Posts:
    34
    Thanks IzzySoft. I tried a few different calls using RotateAroundPivot without any luck; I assume the matrix only affects GUI operations and not the Graphics operations.

    Wondering if there is a more preferred method for quickly performing dynamic modifications to rendertextures.
     
  4. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    I dont know. Me <== n00b.

    Anyways... if it says RenderTexture.. i Run away.
    NO MOOLA to Spare! :(
     
  5. SkippyV

    SkippyV

    Joined:
    Dec 13, 2012
    Posts:
    34
    I hear ya, thanks IzzySoft.

    *bump*

    Anyone got suggestions on how to do this?