Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Forrest_Gimp · Jul 04, 2012 at 03:25 PM · shadertextureuvmatrixtransforms

Animating UV transforms in Shader

I am looking for a way to make a little propeller rotate. The propeller is a texture placed on a mesh using an ordinary UV-map. I know, I could just create a movie-texture which is animated itself, but I was wondering if it is possible to do it without going through the process of making a movie first. I have recently made my own moving water-texture using a JS-script that animates the offset-placement. So I see no reason why a rotation should not be possible just as well.

Possible starting-point: I have read that it is possible to do matrix-calculations within a shader, so I basicly need to know if I can implement a rotation-transform (either in the shader or as an external script) and then send that translation (e.g. as matrix) to the texture.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Piflik · Jul 04, 2012 at 03:30 PM 0
Share

Can't you just rotate the mesh the propeller texture is on?

avatar image Forrest_Gimp · Jul 05, 2012 at 06:09 AM 0
Share

I could, but for two reasons it's not an option: 1. It's actually not a single mesh, it is part of a more complex mesh with multiple materials (O$$anonymous$$, I could separate it) but 2. It is a cylinder with just ten sides, so rotating it will show the low poly-count even more. Anyway, this is more the basic question whether or not such texture-manipulation is basicly possible and how to do it. $$anonymous$$essing with the placement rather than using movietextures could improve performance, I think.

avatar image save · Jul 05, 2012 at 08:02 AM 0
Share

You wouldn't use a movie texture, that would be far too expensive (perhaps you meant moving texture). For best visual and performance you would rotate the texture within the plane. I would have two textures in the material, one with a blurred set of the propeller which fades in when speed increases using the matrix transformation below. That way it will look good if you pause the game for instance.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ninjafish · Jul 10, 2012 at 06:54 PM

Not sure if this is what you are looking for. But here is a script that will take a texture and rotate it about a specified position on the texture. Just drag the script on the gameobject. Then attach a texture to the script, adjust the speed and set the position. If you want the texture to rotate from its center set x = 0.5 and y = 0.5.

//here is the script below

 var rotateSpeed = 30;
 var texture : Texture;
 var rotationCenter = Vector2.zero;
 
 function Start() {
     // Create a new material with a shader
     // that rotates the texture. Texture rotation
     // is performed with a _Rotation matrix.
     var m : Material = new Material (
         "Shader \"Rotating Texture\" {" +
         "Properties { _MainTex (\"Base\", 2D) = \"white\" {} }" +
         "SubShader {" +
         "    Pass {" +
         "        Material { Diffuse (1,1,1,0) Ambient (1,1,1,0) }" +
         "        Lighting On" +
         "        SetTexture [_MainTex] {" +
         "            matrix [_Rotation]" +
         "            combine texture * primary double, texture" +
         "        }" +
         "    }" +
         "}" +
         "}"
     );
     m.mainTexture = texture;
     renderer.material = m;
 }
 
 function Update() {
     // Construct a rotation matrix and set it for the shader
     var rot = Quaternion.Euler (0, 0, Time.time * rotateSpeed);
     var m = Matrix4x4.TRS ( Vector3.zero, rot, Vector3(1,1,1) );
     var t = Matrix4x4.TRS (-rotationCenter, Quaternion.identity, Vector3(1,1,1));
     var t_inverse = Matrix4x4.TRS (rotationCenter, Quaternion.identity, Vector3(1,1,1));
     renderer.material.SetMatrix ("_Rotation", t_inverse*m*t);
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gulfam131 · Dec 22, 2014 at 07:16 AM 0
Share

Thanks, its very helping us.

avatar image
0

Answer by Forrest_Gimp · Jul 05, 2012 at 11:00 AM

The script in istself seems logical, but I think _Matrix is not actually influencing anything. I got it to work creating a custom shader:

 Shader "Custom/rotationShader" {
  Properties 
  {
  _MainTex ("Base (RGB)", 2D) = "white" {}
  }
  SubShader {
  Tags { "RenderType"="Opaque" }
  
  Pass {
          Material { Diffuse (1,1,1,0) Ambient (1,1,1,0) }
  Lighting On
  
  SetTexture [_MainTex] 
  {
  matrix [_Rotation]
  combine texture * primary double, texture
  }
  }
  } 
  FallBack "Diffuse"
 }

Then I changed renderer.material.SetMatrix ("_Matrix", matrix); to renderer.material.SetMatrix ("_Rotation", matrix); and it started turning!

There is one problem though: the pivot of the turning-motion is 0/0 (should have expected that...). To make it turn around the texture's center I would have to change the first parameter of Matrix4x4.TRS to the maps center. Has anybody got an idea how to automati this, so it will automaticly update this position depending on the texturemaps size?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image save · Jul 05, 2012 at 11:17 AM 0
Share

Oh sorry yes, you would need a shader for this as you presumed. Will get back to you on the offset part, I don't have a solution at hand but we'll figure something out!

avatar image Forrest_Gimp · Jul 05, 2012 at 11:51 AM 0
Share

Great! Thanx for the help so far! I tried to work on it further, and have tried to initially set the center parameter of $$anonymous$$atrix4x4.TRS to something other than 0,0,0 by hand. So far I had no success.

avatar image
0

Answer by Forrest_Gimp · Jul 06, 2012 at 06:58 AM

What I thought was the center parameter is just the translation vector. Should have read the documentation more carefully ^^. But inspired by my 'discovery' I tried this:

 var speed : float = 1.0;
 var rot : float;
 
 function Update () 
 {
     rot += speed*Time.deltaTime;
     if (rot>360) rot=0;
 
  displaceMatrix(); // move texture, so that 0/0 matches the center of the image
     RotateMatrix(); // turn texture
     replaceMatrix(); // reset to normal placement
 }
 
 function displaceMatrix () {
     var move = Vector3(-0.5,-0.5,0);
  
     var matrix : Matrix4x4 = Matrix4x4.TRS(move, Quaternion.Euler (0, 0, 0), Vector3.one);
 
     renderer.material.SetMatrix ("_Rotation", matrix);
     print("move 1 complete!");
 }
 
 function RotateMatrix () {
 
     var rotate = Quaternion.Euler (0, 0, rot);
  
     var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler (0, 0, rot), Vector3.one);
 
     renderer.material.SetMatrix ("_Rotation", matrix);
     print("turn complete!");
 }
 
 function replaceMatrix () {
     var move = Vector3(0,0,0); 
     var matrix : Matrix4x4 = Matrix4x4.TRS(move, Quaternion.Euler (0, 0, 0), Vector3.one);
 
    renderer.material.SetMatrix ("_Rotation", matrix);
     print("move 2 complete!");
 }

As you can see, my logic was "move texture so its center is at 0/0, then turn it, then move it back to original location". It should work, but it doesn't. In the property inspector I see the angle growing, but the turning seems to be undone by replaceMatrix(). Probably to do it I would have to use an additional matrix which only handles the placement. Unfortunately I have no idea how to implement this into the shader.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Forrest_Gimp · Jul 11, 2012 at 11:28 AM

That is indeed exactly what I was looking for. Shame on me for not remembering matrices can simply be multiplied to combine the transitions. At least I was on the right track basicly.

Thanks for the help!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Uv's not set in unity according to maya 0 Answers

Accessing a second texture's offset in a modified sprite shader 0 Answers

Shader, 2 Textures on one UV Map 1 Answer

Why are the UV's in my texture array shader always zero? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges