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
0
Question by Han-Wen · Jun 04, 2020 at 03:36 PM · shaderquaternionrotate

Rotate Gameobject by vertex shader with quaternion

Hi, I am trying to rotate a cube by vertex shader with quaternion, the cube position is (0,0,1), and rotate axis is (1,0,0), I had tried the quaternion rotate method, but things do not work like I expected, it scale up and down the gameobject from the (0,0,0) point, what did I do wrong, this is driving me crazy since I can not figure out why. Please help! Here is my whole shader code :

 Shader "Unlit/NewTestTrackShader"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
       
         _OffsetX("Offset X",float) = 0
         _OffsetY("Offset Y",float) = 0
 
     }
 
     SubShader
     {
         Tags { "RenderType"="Opaque" "Queue"="Transparent-1"}
        
         LOD 100
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
             Cull Off
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
                 float4 posWorld : TEXCOORD1;
             };
 
             float4 RotateionAxisToQuaternion(float3 axis,half angle){
                 half half_angle = radians(angle/2);
                 float4 q = (axis.x * sin(half_angle),
                             axis.y * sin(half_angle),
                             axis.z * sin(half_angle),
                             cos(half_angle));
                 return q;
             }
             /////////
             float3 RotateVertexposition(float3 vertexPosition,float3 axis,float angle){
                 float4 q = RotateionAxisToQuaternion(axis,angle);
                 float3 v = vertexPosition;
                 return v + 2 * cross(q.xyz,cross(q.xyz,v) + q.w * v);
             }
 
             ////   Equal Above Function
             float4 quat_mult(float4 q1, float4 q2){ 
                 float4 qr;
                 qr.x = (q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y);
                 qr.y = (q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x);
                 qr.z = (q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w);
                 qr.w = (q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z);
                 return qr;
             }
 
             float4 quat_conj(float4 q){ 
                 return float4(-q.x, -q.y, -q.z, q.w); 
             }
 
             float3 rotate_vertex_position_detail(float3 position, float3 axis, float angle){ 
                 float4 qr = RotateionAxisToQuaternion(axis, angle);
                 float4 qr_conj = quat_conj(qr);
                 float4 q_pos = float4(position.x, position.y, position.z, 0);
                 float4 q_p = quat_mult(qr, q_pos);
                 float4 qp_q = quat_mult(q_p, qr_conj);
                 return float3(qp_q.x, qp_q.y, qp_q.z);
             }
             ////////////////////
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float_OffsetX,_OffsetY,;
 
             v2f vert (appdata v)
             {
                 v2f o;
 
                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
 
                 float3 axis_X = (1,0,0);
                 float3 axis_Y = (0,1,0);
                 float3 axis_Z = (0,0,1);
 
                 o.posWorld.xyz = rotate_vertex_position_detail(o.posWorld.xyz, axis_X, _OffsetX);
 
                 v.vertex = mul(unity_WorldToObject, o.posWorld);
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
 
                 return o;
             }
 
             fixed4 frag (v2f i) : SV_Target
             {
                 fixed4 col = tex2D(_MainTex, i.uv);
                 return half4(col);
             }
             ENDCG
         }
     }
 }
 


and here is what it look like : alt text

screen-shot-2020-06-03-at-24105-pm.png (114.3 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

201 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 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

How do I rotate on World Axes with Quaternion.Slerp? 2 Answers

Snapping Quaternion Rotations in Script 3 Answers

Rotation Jumping values (0 to 180) 1 Answer

Rotate object to where aiming 1 Answer

How to make a model to seems like it is rotating very fast with out rotating it fast 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