Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Decapitary · Jul 16, 2017 at 02:17 PM · rotationshaderrotation axismatrixvertex shader

Rotate a vertex about "u" Axis

I made an axis and I want to rotate my vertex about it.

   float3 axis = point2 - point1;
   float magnitude = sqrt(pow(axis.x,2)+pow(axis.y,2)+pow(axis.z,2));
   float3 u = axis / magnitude;


from wikipedia :

Rotation matrix from axis and angle

so I did this:

 float coss = _CosTime[1] * 0.1;
 float sinn = _SinTime[1] * 0.1;
 float3x3 tm = float3x3(float3(coss+pow(u.x,2) * (1 - coss),u.x*u.y*(1-coss)-u.z*sinn, u.x*u.z*(1-coss)+u.y*(1-sinn)),float3(u.y*u.x*(1-coss)-u.z*sinn,coss+pow(u.y,2) * (1 - coss),u.y*u.z*(1-coss)-u.x*sinn),float3(u.z*u.x*(1-coss)-u.y*sinn,u.z*u.y*(1-coss)-u.x*sinn,coss+pow(u.z,2) * (1 - coss)));       
  o.pos = float4(mul(v.vertex.xyz, tm),1);



this makes the object disapear...What am I doing wrong?

 void geom(triangle v2f input[3], inout TriangleStream<v2f> OutputStream)
     {
         v2f test = (v2f)0;
            float coss = cos(_Time[1] * _Amount);
            float sinn = sin(_Time[1] * _Amount);
         float3 axis = input[1].worldPosition.xyz - input[0].worldPosition.xyz;
         float magnitude = sqrt(pow(axis.x,2)+pow(axis.y,2)+pow(axis.z,2));
         float3 u = axis / magnitude;
         float3x3 tm = float3x3(normalize(float3(coss+pow(u.x,2) * (1 - coss),u.x*u.y*(1-coss)-u.z*sinn, u.x*u.z*(1-coss)+u.y*(1-sinn))),normalize(float3(u.y*u.x*(1-coss)-u.z*sinn,coss+pow(u.y,2) * (1 - coss),u.y*u.z*(1-coss)-u.x*sinn)),normalize(float3(u.z*u.x*(1-coss)-u.y*sinn,u.z*u.y*(1-coss)-u.x*sinn,coss+pow(u.z,2) * (1 - coss))));
         float3 normal = normalize(cross(input[1].worldPosition.xyz - input[0].worldPosition.xyz, input[2].worldPosition.xyz - input[0].worldPosition.xyz));
         for(int i = 0; i < 3; i++)
         {
             test.normal = normal;

             test.pos = input[i].pos;
             if(i == 2){
                 test.pos = float4(mul(input[i].worldPosition,tm),1);
             }
             test.uv = input[i].uv;
             OutputStream.Append(test);
         }
     }

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Jul 16, 2017 at 10:56 AM

What you created there is just a rotation matrix. The rotation happens always around the origin. From your code it's not clear in what space your "v.vertex.xyz" is. If it's a position in worldspace you will rotate around the world origin. If it's a position in local space you will rotate around the local origin.

If you want to rotate around a particular point you have to offset your position, do the rotation and translate it back.

Ps: do not use "pow" for simple powers. Instead of pow(u.x,2) you should use u.x*u.x.

edit

There are several things wrong in your shader. First of all you adapted the rotation matrix wrong. You had several +/- errors as well as a "(1-sin)" instead of just "sin".

Next problem is _CosTime and _SinTime return the cosine / sine of the time. You can't "scale" those. They need to be in the range -1 to 1, otherwise the whole rotation makes no sense. If you want to be able to scale the time you can't use the precalculated values but have to calculate the sin and cos yourself. i.e. (sin(_Time[1] * _Amount)).

Next problem is that the geometry shader takes the output of the vertex shader. In the example "vertex" is already transformed into clipspace (2d screen space in range -1 to 1 on each axis). You may want to keep the output of the vertex shader in local space and perform the local to clipspace transform in the geometry shader.

Here i modifed the example to rotate each triangle around the triangle center point around the triangle normal vector. The rotation is carried out in local space.

         v2f vert (appdata v)
         {
             v2f o;
             o.vertex = v.vertex;
             o.uv = TRANSFORM_TEX(v.uv, _MainTex);
             o.normal = v.normal;
             o.worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz;
             return o;
         }
 
         [maxvertexcount(3)]
         void geom(triangle v2f input[3], inout TriangleStream<v2f> OutputStream)
         {
             v2f test = (v2f)0;
             float3 normal = normalize(cross(input[1].vertex.xyz - input[0].vertex.xyz, input[2].vertex.xyz - input[0].vertex.xyz));
             float3 center = (input[0].vertex + input[1].vertex + input[2].vertex)/3;
             float3 u = normal;
             float c = _CosTime[2];
             float s = _SinTime[2];
             float cr = 1 - c;
             float sr = 1 - s;
             float3x3 tm = float3x3(
                 float3(c+u.x*u.x * cr,     u.x*u.y*cr-u.z*s,  u.x*u.z*cr+u.y*s),
                 float3(u.y*u.x*cr+u.z*s,   c+u.y*u.y * cr,    u.y*u.z*cr-u.x*s),
                 float3(u.z*u.x*cr-u.y*s,   u.z*u.y*cr+u.x*s,  c+u.z * u.z * cr)
             );
             for(int i = 0; i < 3; i++)
             {
                 test.normal = mul(unity_ObjectToWorld,normal);
                 float3 pos = input[i].vertex - center;
                 test.vertex = UnityObjectToClipPos(float4(mul(tm, pos) + center,1));
                 test.uv = input[i].uv;
                 OutputStream.Append(test);
             }
         }
Comment
Add comment · Show 6 · 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 Decapitary · Jul 16, 2017 at 11:06 AM 0
Share

when I do this o.worldPosition = float4(mul(v.worldPosition.xyz,tm),1); nothing happens and o.pos= float4(mul(v.worldPosition.xyz,tm),1); makes vertex rotate in a very weired way...

avatar image Bunny83 Decapitary · Jul 16, 2017 at 12:05 PM 0
Share

You have only provided tiny snippets of shader code. Also you haven't actually said what you want to achieve. ("rotating around an axis" is way too unspecific):

  • What kind of shader do you program? Is this in a surface shader or a traditional vertex shader?

  • Where does point1 and point2 comes from? In which space are they defined? (local or world space?).

  • $$anonymous$$atrix multiplication in Unity is usually left-hand i.e. ( mul(tm, v.worldPosition.xyz) ) though it depends on your matrix layout. I haven't checked yours.

avatar image Decapitary · Jul 16, 2017 at 12:37 PM 0
Share

its a geometry shader. two points are two vertex of a triangle and I want to rotate the other vertex around the axis they make.see this

avatar image Decapitary · Jul 16, 2017 at 01:36 PM 0
Share

I added the code.rest is exactly from the post

avatar image Bunny83 Decapitary · Jul 16, 2017 at 01:58 PM 0
Share

I've edited my answer and included an example. If you want to rotate each triangle around one of it's edges you can do:

         void geom(triangle v2f input[3], inout TriangleStream<v2f> OutputStream)
         {
             v2f test = (v2f)0;
             float3 normal = normalize(cross(input[1].vertex.xyz - input[0].vertex.xyz, input[2].vertex.xyz - input[0].vertex.xyz));
             float3 center = input[0].vertex;
             float3 u = normalize(input[1].vertex - input[0].vertex);
             float c = _CosTime[2];
             float s = _SinTime[2];
             float cr = 1 - c;
             float sr = 1 - s;
             float3x3 tm = float3x3(
                 float3(c+u.x*u.x * cr,     u.x*u.y*cr-u.z*s,  u.x*u.z*cr+u.y*s),
                 float3(u.y*u.x*cr+u.z*s,   c+u.y*u.y * cr,    u.y*u.z*cr-u.x*s),
                 float3(u.z*u.x*cr-u.y*s,   u.z*u.y*cr+u.x*s,  c+u.z * u.z * cr)
             );
             normal = mul(tm, normal);
             for(int i = 0; i < 3; i++)
             {
                 test.normal = mul(unity_ObjectToWorld,normal);
                 float3 pos = input[i].vertex - center;
                 test.vertex = UnityObjectToClipPos(float4(mul(tm, pos) + center,1));
                 test.uv = input[i].uv;
                 OutputStream.Append(test);
             }
         }

This will rotate around the first vertex and uses the axis from the first to the second vertex. It also rotates the local normal vector in order to get proper lighting. However the example didn't have any lighting besides a hardcoded directional light.

avatar image Decapitary · Jul 16, 2017 at 03:01 PM 0
Share

thanks,i just learned about shaders yesterday. lighting gonna be there soon.

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

118 People are following this question.

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

Related Questions

Getting the forward position of an object in a vertex shader 0 Answers

Stop Billboard Texture Rotation in Triplanar Shader 0 Answers

How to rotate a triangle created in a geometry shader around its center 1 Answer

Efficient way to keep a fixed rotation for sprite? 0 Answers

Locking rotation makes object spin 0 Answers


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