Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

"3D"-Quad to "2D"-Quad(GUI)

Discussion in 'Immediate Mode GUI (IMGUI)' started by Themp, Aug 20, 2014.

  1. Themp

    Themp

    Joined:
    Nov 8, 2011
    Posts:
    96
    Hello,

    Straight to the case:

    I need my quad in the world rendered as a GUI Texture,

    In my own engine (C++ and DirectX 9) I can change the Matrix and FVF to match one using GUI FVF's and matrices

    How do I do this in unity as I can not really reach the WorldMatrix from any component as far as I can tell.

    I need this because all other methods of drawing textures to screen involve using Texture2D.SetPixel (and GUITexture) which is way to CPU-intensive for what I need. And GUITexture doesn't have a material I can access.

    What I'm trying to accomplish is rendering a sprite sheet onto a quad, changing the material's Scale and Offset so you see 1 part of the sprite (for example just the character) and draw that to the screen.

    I've got the whole sprite sheet animating working and stuff, Just need the quad drawn on the screen instead of "In the world".

    My whole sprite code:
    Code (csharp):
    1.  
    2. public class SpriteAnimation : MonoBehaviour
    3. {
    4.     public int xRows = 8;
    5.     public int yRows = 8;
    6.     public float animationSpeed = 0.25f;
    7.  
    8.     private float time = 0;
    9.     private Material sprite;
    10.     private Transform t;
    11.     private int maxX;
    12.     private int maxY;
    13.     private int currentX;
    14.     private int currentY;
    15.     void Start ()
    16.     {
    17.         t = transform;
    18.         sprite = t.gameObject.renderer.materials[0];
    19.     }
    20.     void Update ()
    21.     {
    22.         time+=Time.deltaTime;
    23.         if(time > animationSpeed)
    24.         {
    25.             time = 0;
    26.             float xVal = 1.0f/xRows;
    27.             float yVal = 1.0f/yRows;
    28.  
    29.             currentX++;
    30.             if(currentX > maxX)
    31.             {
    32.                 currentX = 0;
    33.                 currentY++;
    34.                 if(currentY > maxY)
    35.                 {
    36.                     currentY = 0;
    37.                 }
    38.             }
    39.             sprite.mainTextureScale = new Vector2(xVal,yVal);
    40.             sprite.mainTextureOffset = new Vector2(xVal*currentX,1.0f-(yVal+yVal*currentY));
    41.             //Graphics.Blit(sprite.GetTexture("_MainTex"),sprite);
    42.         }
    43.     }
    44.     void OnPostRender()
    45.     {  
    46.         GL.Begin(GL.QUADS);
    47.         GL.PushMatrix();
    48.         GL.LoadProjectionMatrix(GUI.matrix);
    49.         sprite.SetPass(0);
    50.  
    51.         GL.Color(new Color(1,1,1,0.5f));
    52.         GL.Vertex3(0,0,0);
    53.         GL.Vertex3(1,0,0);
    54.         GL.Vertex3(0,1,0);
    55.         GL.Vertex3(1,1,0);
    56.         GL.PopMatrix();
    57.         GL.End();
    58.     }
    59.     public void SetMaxAnim(uint x, uint y)
    60.     {
    61.         if(x > xRows)
    62.         {
    63.             x = (uint)xRows;
    64.         }
    65.         if(y > yRows)
    66.         {
    67.             y = (uint)yRows;
    68.         }
    69.         maxX =(int)x-1;
    70.         maxY =(int)y-1;
    71.     }
    72.     public void SetCurrentAnim(uint x, uint y)
    73.     {
    74.         if(x > xRows)
    75.         {
    76.             x = (uint)xRows;
    77.         }
    78.         if(y > yRows)
    79.         {
    80.             y = (uint)yRows;
    81.         }
    82.         currentX = (int)x-1;
    83.         currentY = (int)y-1;
    84.     }
    85. }
    86.  
     
    Last edited: Aug 20, 2014
  2. Themp

    Themp

    Joined:
    Nov 8, 2011
    Posts:
    96
    My second attempt at research brought me a bit closer to my goal. Seems we can access some lower level functions in Unity with the GL class (OpenGL I presume) though my code didn't work.
    Code (csharp):
    1.  
    2.  
    3. Material sprite;
    4. void OnPostRender()
    5.     {  
    6.         GL.Begin(GL.QUADS);
    7.         GL.PushMatrix();
    8.         GL.LoadProjectionMatrix(GUI.matrix);
    9.         sprite.SetPass(0);
    10.  
    11.         GL.Color(new Color(1,1,1,0.5f));
    12.         GL.Vertex3(0,0,0);
    13.         GL.Vertex3(1,0,0);
    14.         GL.Vertex3(0,1,0);
    15.         GL.Vertex3(1,1,0);
    16.         GL.PopMatrix();
    17.         GL.End();
    18.     }
    19.  
    Can anyone help me here?
     
    Last edited: Aug 20, 2014
  3. Themp

    Themp

    Joined:
    Nov 8, 2011
    Posts:
    96
    Ok fixed it, Found another function called GUI.DrawTextureWithTexCoords

    fixed code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpriteAnimation : MonoBehaviour
    6. {
    7.     public int xRows = 8;
    8.     public int yRows = 8;
    9.     public Vector2 posLU;
    10.     public Vector2 posRD; // in percentages of screen
    11.     public float animationSpeed = 0.25f;
    12.     public Texture tex;
    13.     public bool correctAspect = true;
    14.  
    15.     private float time = 0;
    16.     private Material sprite;
    17.     private Transform t;
    18.     private int maxX;
    19.     private int minX;
    20.     private int minY;
    21.     private int maxY;
    22.     private int currentX;
    23.     private int currentY;
    24.     private Vector2 tSize;
    25.     private Vector2 sSize;
    26.     void Start ()
    27.     {
    28.         t = transform;
    29.         sprite = t.gameObject.renderer.materials[0];
    30.         tSize = new Vector2(tex.width,tex.height);
    31.         sSize = new Vector2(tSize.x/xRows,tSize.y/yRows);
    32.         if(correctAspect)
    33.         {
    34.             float aspectRatio = Screen.width/Screen.height;
    35.             posRD.x /= aspectRatio;
    36.         }
    37.         currentX = minX;
    38.         currentY = minY;
    39.     }
    40.     void Update ()
    41.     {
    42.         time+=Time.deltaTime;
    43.         if(time > animationSpeed)
    44.         {
    45.             time = 0;
    46.             float xVal = 1.0f/xRows;
    47.             float yVal = 1.0f/yRows;
    48.  
    49.             currentX++;
    50.             if(currentX > maxX)
    51.             {
    52.                 currentX = minX;
    53.                 currentY++;
    54.                 if(currentY > maxY)
    55.                 {
    56.                     currentY = minY;
    57.                 }
    58.             }
    59.         }
    60.     }
    61.     public void OnGUI()
    62.     {
    63.         if(tex != null)
    64.         {
    65.             float xVal = 1.0f/xRows;
    66.             float yVal = 1.0f/yRows;
    67.  
    68.             GUI.DrawTextureWithTexCoords(new Rect(Screen.width*posLU.x/100,Screen.height*posLU.y/100,Screen.width*posRD.x/100,Screen.height*posRD.y/100),tex,new Rect(xVal*currentX,1.0f-(yVal+yVal*currentY),xVal,yVal));
    69.         }
    70.         else
    71.         {
    72.             Debug.LogError("SpriteAnimation: Texture was not assigned, Objectname: " + transform.name);
    73.         }
    74.     }
    75.     public void SetMaxAnim(uint minx, uint miny, uint maxx, uint maxy)
    76.     {
    77.         if(maxx > xRows)
    78.         {
    79.             maxx = (uint)xRows;
    80.         }
    81.         if(maxy > yRows)
    82.         {
    83.             maxy = (uint)yRows;
    84.         }
    85.         maxX =(int)maxx-1;
    86.         maxY =(int)maxy-1;
    87.         minX = (int)minx;
    88.         minY = (int)miny;
    89.     }
    90.     public void SetCurrentAnim(uint x, uint y)
    91.     {
    92.         if(x > xRows)
    93.         {
    94.             x = (uint)xRows;
    95.         }
    96.         if(y > yRows)
    97.         {
    98.             y = (uint)yRows;
    99.         }
    100.         currentX = (int)x-1;
    101.         currentY = (int)y-1;
    102.     }
    103. }
    104.  
    Also searched like hell for the last argument on the drawtexture, nobody said it was a WIDTH and HEIGHT, I thought it was a rectangle but it's not. it's: X ( 0 - 1 ) Y ( 0 - 1), width, height