Search Unity

[Solved]Texture 3D Rotation from GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by IsGreen, Jul 21, 2014.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I know GUIUtility.RotateAroundPivot. that rotate the GUI around a point.

    I want to make 3D rotations. I managed to make 3D rotations with the following code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class script : MonoBehaviour {
    5.    
    6.     public Texture2D texture;
    7.     public Vector2 center = new Vector2(100f,100f);
    8.     public Vector3 angle = Vector3.zero;
    9.     public Vector2 pivot = new Vector2(0.5f,0.5f);
    10.    
    11.     Texture2D rTexture;
    12.     Rect rect = new Rect ();
    13.    
    14.     void Update(){
    15.        
    16.         this.rTexture = rotateTexture(this.texture,this.angle,this.pivot);
    17.         rect.size = new Vector2(rTexture.width,rTexture.height);
    18.         rect.position = center - rect.size * 0.5f;
    19.        
    20.     }
    21.    
    22.     void OnGUI(){ GUI.DrawTexture(rect,rTexture,ScaleMode.StretchToFill); }
    23.    
    24.     Texture2D rotateTexture(Texture2D texture, float angle){ return rotateTexture(texture,new Vector3(0f,0f,angle),new Vector2(0.5f,0.5f)); }
    25.     Texture2D rotateTexture(Texture2D texture, Vector3 angle, Vector2 pivot){
    26.        
    27.         Quaternion Angle    = Quaternion.Euler(angle);
    28.         Vector2 e1 = Angle * new Vector2( texture.width,texture.height);
    29.         Vector2 e2 = Angle * new Vector2(-texture.width,texture.height);
    30.         Texture2D tex = new Texture2D((int)(Mathf.Max(Mathf.Abs(e1.x),Mathf.Abs(e2.x))),
    31.                                       (int)(Mathf.Max(Mathf.Abs(e1.y),Mathf.Abs(e2.y))),TextureFormat.RGBA32,false);
    32.         Color32[] pixelsTexture = texture.GetPixels32();
    33.         Color32[] pixelsTex     = tex.GetPixels32();
    34.         for(int w=0;w<pixelsTex.Length;w++) pixelsTex[w].a=0;
    35.  
    36.         Vector2 position;
    37.         int x,y;
    38.         float xPivotTex = Mathf.Clamp01(pivot.x)*tex.width;
    39.         float yPivotTex = tex.height*(1f-Mathf.Clamp01(pivot.y));
    40.         float xPivotTexture = Mathf.Clamp01(pivot.x)*texture.width;
    41.         float yPivotTexture = texture.height*(1f-Mathf.Clamp01(pivot.y));
    42.         int width  = tex.width-1;
    43.         int height = tex.height-1;
    44.        
    45.         for(int i=0;i<pixelsTexture.Length;i++){
    46.            
    47.             position = Angle * new Vector2(i%texture.width-xPivotTexture , i/texture.width-yPivotTexture);
    48.             x = (int)(position.x + xPivotTex);
    49.             y = (int)(position.y + yPivotTex);          
    50.             if(!(x < 0 || x > width || y < 0 || y > height)) pixelsTex[x + y * tex.width] = pixelsTexture[i];
    51.  
    52.         }
    53.        
    54.         tex.SetPixels32(pixelsTex);
    55.         tex.Apply();
    56.         return tex;
    57.        
    58.     }
    59.    
    60. }
    The above code rotates pixels of the original image and draw in the new texture that takes the size needed.

    Problem: With this system to increase the size of the new texture pixels are not drawn.

    I tried to reverse the process, ie, compute the reverse rotation and reverse rotate the pixels of the new texture to detect which is the closest to the original texture pixel:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Texture2DRotation3D : MonoBehaviour {
    5.  
    6.     public Texture2D texture;
    7.     public Vector2 center = new Vector2(100f,100f);
    8.     public Vector3 angle = Vector3.zero;
    9.     public Vector2 pivot = new Vector2(0.5f,0.5f);
    10.  
    11.     Texture2D rTexture;
    12.     Rect rect = new Rect ();
    13.  
    14.     void Update(){
    15.  
    16.         this.rTexture = rotateTexture(this.texture,this.angle,this.pivot);
    17.         rect.size = new Vector2(rTexture.width,rTexture.height);
    18.         rect.position = center - rect.size * 0.5f;
    19.  
    20.     }
    21.  
    22.     void OnGUI(){ GUI.DrawTexture(rect,rTexture,ScaleMode.StretchToFill); }
    23.  
    24.     Texture2D rotateTexture(Texture2D texture, float angle){ return rotateTexture(texture,new Vector3(0f,0f,angle),new Vector2(0.5f,0.5f)); }
    25.     Texture2D rotateTexture(Texture2D texture, Vector3 angle, Vector2 pivot){
    26.  
    27.         Quaternion Angle    = Quaternion.Euler(Mathf.Repeat(angle.x,89f),Mathf.Repeat(angle.y,89f),Mathf.Repeat(angle.z,360f));
    28.         Quaternion invAngle = Quaternion.Inverse(Angle);
    29.         Vector2 e1 = Angle * new Vector2( texture.width,texture.height);
    30.         Vector2 e2 = Angle * new Vector2(-texture.width,texture.height);
    31.         Texture2D tex = new Texture2D((int)(Mathf.Max(Mathf.Abs(e1.x),Mathf.Abs(e2.x))),
    32.                                       (int)(Mathf.Max(Mathf.Abs(e1.y),Mathf.Abs(e2.y))),TextureFormat.RGBA32,false);
    33.         Color32[] pixelsTexture = texture.GetPixels32();
    34.         Color32[] pixelsTex     = tex.GetPixels32();
    35.         Vector2 position,invpos;
    36.         int x,y;
    37.         float xPivotTex = Mathf.Clamp01(pivot.x)*tex.width;
    38.         float yPivotTex = tex.height*(1f-Mathf.Clamp01(pivot.y));
    39.         float xPivotTexture = Mathf.Clamp01(pivot.x)*texture.width;
    40.         float yPivotTexture = texture.height*(1f-Mathf.Clamp01(pivot.y));
    41.         int width  = texture.width-1;
    42.         int height = texture.height-1;
    43.  
    44.         for(int i=0;i<pixelsTex.Length;i++){
    45.  
    46.             position = new Vector2(i%tex.width-xPivotTex , i/tex.width-yPivotTex);
    47.             invpos = invAngle * position;
    48.            
    49.             x = (int)(invpos.x + xPivotTexture);
    50.             y = (int)(invpos.y + yPivotTexture);
    51.            
    52.             if(x < 0 || x > width || y < 0 || y > height) pixelsTex[i].a = 0;
    53.             else pixelsTex[i] = pixelsTexture[x+y*texture.width];
    54.  
    55.         }
    56.  
    57.         tex.SetPixels32(pixelsTex);
    58.         tex.Apply();
    59.         return tex;
    60.  
    61.     }
    62.  
    63. }
    It does not work because to calculate the reverse rotation is necessary to consider the Vector3 value in the Z axis

    Does anyone know how to calculate the reverse rotation and the value in the Z axis?
     
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Solved using vector equation of the plane: a(x-x0)+b(y-y0)+c(z-z0)=0

    At center point 0,0,0, we solve the equation of the z variable: z = (-ax-by)/c

    a,b,c is the normal of the plane.

    Download link.