Search Unity

rotate texture?

Discussion in 'Scripting' started by ivkoni, Mar 12, 2009.

  1. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Hi,

    is it possible? I found in the forum that you can reflect a texture by using
    Code (csharp):
    1. renderer.material.maiTextureScale.x =-1;
    but I would like to rotate a texture at 90 degrees.
    I could create separate instances of the texture outside of unity rotated at 90, 180, 270 and use that but I would like to know if I am not missing some simple way of rotating a texture inside unity.

    thanks
     
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    since nobody replied I would assume it's not possible. It's not a big deal anyway. I already implemented a solution that works in my case.
    thanks.
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Yes, this can be done by creating a custom matrix. You would have to create your own shader and script for it to work though. Something like this:

    Javascript to place onto the GameObject:

    Code (csharp):
    1. var offset : Vector2;
    2. var tiling : Vector2;
    3. var rot = 45.0;
    4.  
    5. function Update ()
    6. {
    7.     var matrix = Matrix4x4.TRS (offset, Quaternion.Euler (0, 0, rot), tiling);
    8.     renderer.material.SetMatrix ("_Matrix", matrix);
    9. }
    And the shader:

    Code (csharp):
    1. Shader "Test" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white"
    4.     }
    5.     SubShader {
    6.         Pass {
    7.             SetTexture [_MainTex] {
    8.                 Matrix [_Matrix]
    9.             }
    10.         }
    11.     }
    12. }
     
  4. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    Daniel is correct in that rotation of a texture would probably be done by a shader. The Animation API example project actually has an example of this and a shader you could probably modify to get the desired result.
     
  5. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Thank you very much guys for the replies. I will keep this mind.
     
    TheNDev likes this.
  6. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    Digging up an old post I know, but I am trying the same sort of thing (rotating a material) and looked up the Animation API. It's doing a similar thing to what I was trying to do in a custom shader, but unfortunately also has the same results of the material being rotated from the bottom left corner of a plane/face of cube. I need to rotate the material from the centre of the plane/cube face. Anyone know how I can do this, either a modification of above or altering a pivot point somehow?
     
  7. videep

    videep

    Joined:
    Jan 2, 2014
    Posts:
    5
  8. TBH-MITRE

    TBH-MITRE

    Joined:
    Apr 2, 2013
    Posts:
    2
    Here's a C# modification of the code above that will rotate the texture around the center:

    Code (csharp):
    1.  
    2. public class CirclePictureAnimate : MonoBehaviour {
    3.    float rotation = 0;
    4.    float scale = 10;
    5.    Vector3 offset = new Vector3(0.5f,0.5f,0);
    6.    Vector3 tiling = new Vector3(1,1,1);
    7.    Material animMat;
    8.  
    9.    // Use this for initialization
    10.    void Start () {
    11.      animMat = gameObject.renderer.materials[1];
    12.      //Debug.Log ("Material used: "+animMat.name);
    13.    }
    14.  
    15.    void Update () {
    16.      rotation += Time.deltaTime*scale;
    17.  
    18.  
    19.      Quaternion quat = Quaternion.Euler(0,0,rotation);
    20.      Matrix4x4 matrix1 = Matrix4x4.TRS (offset, Quaternion.identity, tiling);
    21.      Matrix4x4 matrix2 = Matrix4x4.TRS (Vector3.zero, quat, tiling);
    22.      Matrix4x4 matrix3 = Matrix4x4.TRS (-offset, Quaternion.identity, tiling);
    23.      animMat.SetMatrix ("_Matrix",matrix1*matrix2*matrix3);
    24.    }
    25. }
    26.  
     
  9. DougCole

    DougCole

    Joined:
    Sep 5, 2018
    Posts:
    2
    I needed to rotate a windblown sand texture. I just copied it to Pain.Net, rotated the image 90dg and then brought it back to Unity. Works like a charm.
     
    brucegigis and Zwoggeh like this.
  10. brucegigis

    brucegigis

    Joined:
    May 2, 2021
    Posts:
    1
    This works like a charm! Thanks, bud!