1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

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. _Daniel_

    _Daniel_

    Joined:
    Feb 28, 2007
    Posts:
    2,341
    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.
     
  6. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    70
    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.