Rotate texture within group

I want to rotate an image in a cropped region but when I try to rotate the image within the group, the whole group rotates, not just the texture.

I put code that is basically what is shown below into my OnGUI handler (rotation is a global float and myImg is a global Texture2D)…

GUI.BeginGroup(Rect(0,0,100,100));
    var saveMatrix: Matrix4x4 = GUI.matrix;
    rotation += 0.5; 
    GUIUtility.RotateAroundPivot(rotation, new Vector2(myImg.width/2,myImg.height/2));
    GUI.DrawTexture(Rect(0,0,myImg.width,myImg.height),myImg);
    GUI.matrix = mat;
GUI.EndGroup();

I’m expecting the group Rect to be fixed and the texture to rotate around its midpoint. Instead, the group’s rect and everything in it is rotating.

How do I code this so that the image rotates and is then cropped within the group’s stationary Rect?

You don’t appear to be restoring the value of saveMatrix before the GUI.EndGroup call (or is this just a copy/paste error?)

Thanks for responding, andeeee. This is a cut-and-paste error. The line that reads GUI.matrix = mat should be GUI.matrix = saveMatrix. I just verified the corrected code and the whole group still rotates instead of just the texture.

I’d still like to understand what I’m doing wrong here so I’ve attached a file with a simple example. I want to view the top/center portion of a spinning wheel. Instead, the whole group rotates with the wheel.

What is the correct way to rotate the wheel while the group remains stationary?

326762–11521–$rotationtest_175.unitypackage (135 KB)

Sorry it’s taken a while to figure this out - the dev team were stumped by it too!

It turns out that the problem is not with the GUI matrix. If you add an extra control to the group, it doesn’t get rotated. The issue is actually that the texture doesn’t get masked by the group when it rotates. This bug seems to have been fixed according to the database. As a workaround in the meantime, you may be able to add the wheel texture to a scene object and rotate it. Then, have a camera pointing at the wheel and set the camera’s normalized viewport rect property so that the view is displayed at the appropriate screen position. Alternatively, you might find it easier to use a render texture if you have Unity Pro.

Thanks for explaining that to me, andeeee. I am able to accomplish what I want with a separate camera viewing the rotating wheel in 3D space. That seems kind of cumbersome, though.

I do have Unity Pro so I think I’ll give render textures a try. If I get stuck, I’ll be back!

And I’ll be there, waiting for you…

:wink:

Hi!

So is it possible to rotate GUI.Label with image in a Group so it gets clipped correctly?

A text label appears to be OK, but one with a texture suffers from the same problem as GUI.DrawTexture.

Hi, I’m having this same issue with rotated textures being drawn inside a GUI Group not clipping to the group’s bounds. I see it was marked as resolved over two years ago, but this doesn’t seem to be the case.
I am trying to draw icons on a map, one of these icons represents the player, and it needs to be rotated to match where the player is facing.

Per suggestion, here is a simplified example of how I’m drawing the icons:

 void ChildOnGUI (){
...
	//Start GUI Group
	GUI.BeginGroup(maskedMapTexture.cachedBounds);	

	//draw location icons
	for(int i=0; i<_drawList.Count; i++){
		//If angle is not 0, rotate matrix around center of icon before drawing
		Matrix4x4 rollbackMatrix = GUI.matrix;
		if(drawData.angle != 0){
			Vector2 pivotPoint = new Vector2(drawData.x + halfIconSize, drawData.y + halfIconSize);
				
			//Rotate GUI around pivot point
			GUIUtility.RotateAroundPivot(drawData.angle, pivotPoint);
		}
				
		//Draw
		GUI.DrawTexture(new Rect(drawData.x, drawData.y, iconPixelSize, iconPixelSize), drawData.texture);
		
		//Rollback matrix
		GUI.matrix = rollbackMatrix;
	}

	//End GUI Group
	GUI.EndGroup();
...
}

Here is my actual code:

 //Menu Draw Method (called from Monobehaviour OnGUI method)
override protected void ChildOnGUI (){
	base.ChildOnGUI ();

	//Toggle map mode button
	mapModeButton.Draw(gameObject);

	//Directions label
	directionsLabel.Draw(gameObject);

	//Draw map background
	GUI.Box(maskedMapTexture.cachedBounds, "", "Map Background");

	//Draw map
	maskedMapTexture.Draw(gameObject);

	//Start GUI Group
	GUI.BeginGroup(maskedMapTexture.cachedBounds);	

	//draw location icons
	if(drawLocationIcons){
		for(int i=0; i<_drawList.Count; i++){
			_DrawIcon(_drawList[i]);
		}
	}	

	//End GUI Group
	GUI.EndGroup();

	//Draw overlay texture
	GUI.Box(maskedMapTexture.cachedBounds, "", "Map Overlay");
}
//Icon draw method
protected void _DrawIcon(DrawData drawData){		
	//Calculate x position
	float halfIconSize = iconPixelSize / 2;

	//Draw icon
	if(drawData.texture){
		//Calc x draw origin
		float x = _zoom * maskedMapTexture.cachedBounds_w * (drawData.normCoords.x - maskedMapTexture.texCoords.x) - halfIconSize;
		//Calc y draw origin
		float y = _zoom * maskedMapTexture.cachedBounds_h * (drawData.normCoords.y - (1 - maskedMapTexture.texCoords.height - maskedMapTexture.texCoords.y))
				- halfIconSize;
			
		//If angle is not 0, rotate matrix around center of icon before drawing
		Matrix4x4 rollbackMatrix = GUI.matrix;
		if(drawData.angle != 0){
			Vector2 pivotPoint = new Vector2(x + halfIconSize, y + halfIconSize);
				
			//Rotate GUI around pivot point
			GUIUtility.RotateAroundPivot(drawData.angle, pivotPoint);
		}
				
		//Draw
		GUI.DrawTexture(new Rect(x, y, iconPixelSize, iconPixelSize), drawData.texture);
		
		//Rollback matrix
		GUI.matrix = rollbackMatrix;
	}
}

My map and all of my non-rotated icons draw perfectly, clipping at the bounding edges of the GUI group. Its only the rotated icon that is having issue. Any help would be appreciated.

You should really try to extract the simplest example - a few liner - then you’ll probably get the answer.

I’ve added a simpler example per your suggestion. However, I’ve left the original context for people to see “I’m doing it exactly like this,” as opposed to “I’m doing it something like this.” Sometimes things get lost in the translation from complex to simple. The actual problem does not seem to be my code, as far as I can tell. The issue seems to be with Unity’s GUI group not clipping rotated textures properly.

Wouldn’t know, but I had problems with rotated buttons when in rotated group (buttons just go wild). The rotation thing isn’t perfect with Unity GUI…

Unfortunately there are not many fixes by UT, so this stuff gets propagated to each and every new release :frowning:

You can say that again.
I’ve got a few ideas for workarounds; I’ll try them out this evening (after 5 PM EST) and post the results.

No techniques I’ve tried seem to aleviate this issue. Someone’s idea of using GUI.Window looked like a possible workaround, but I couldn’t get that working. Using GUI.DrawTextureWithTexCoords has the same clipping issue. I have textures drawing with a masking shader, but that technique isn’t that practical or useful in this case. From what I’ve researched, using RenderTextures may not work on all graphics cards; so that isn’t a viable solution. I’ve scoured the unity forums and have found no solution for this, only confirmation that it exists.

I’m just about all out of ideas here… anyone from Unity wanna come save the day? :smile:

EDIT: btw I’m using version 4.0.0f7

The threads and data is excellent and informative as well.