-
-
Save ArieLeo/b44c2dcfc890506cd9b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/QuaternionTest" { | |
Properties { | |
_AX("axis", Vector) = (0,0,0,0) | |
_TH("theta", Float) = 0 | |
} | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
#define PI 3.1416 | |
float4 _AX; | |
float _TH; | |
struct v2f { | |
float4 pos : SV_POSITION; | |
fixed4 color : COLOR; | |
}; | |
// Quaternion multiplication. | |
// http://mathworld.wolfram.com/Quaternion.html | |
float4 qmul(float4 q1, float4 q2) | |
{ | |
return float4( | |
q2.xyz * q1.w + q1.xyz * q2.w + cross(q1.xyz, q2.xyz), | |
q1.w * q2.w - dot(q1.xyz, q2.xyz) | |
); | |
} | |
// Rotate a vector with a rotation quaternion. | |
// http://mathworld.wolfram.com/Quaternion.html | |
float3 rotate_vector(float3 v, float4 r) | |
{ | |
float4 r_c = r * float4(-1, -1, -1, 1); | |
return qmul(r, qmul(float4(v, 0), r_c)).xyz; | |
} | |
float3 rotate(float3 v, float3 axis, float theta){ | |
axis = normalize(axis); | |
float s,c; | |
sincos(theta,s,c); | |
float4 | |
q = float4(axis.x*s,axis.y*s,axis.z*s,c); | |
return rotate_vector(v,q); | |
} | |
v2f vert (appdata_full v) | |
{ | |
float3 axis = normalize(_AX.xyz); | |
float theta = PI * _TH; | |
v.vertex.xyz = rotate(v.vertex.xyz, axis, theta); | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.color = v.color; | |
return o; | |
} | |
half4 frag (v2f i) : COLOR | |
{ | |
return i.color; | |
} | |
ENDCG | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma fragmentoption ARB_precision_hint_fastest | |
ENDCG | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/QuaternionTest2" { | |
Properties { | |
_AX("axis", Vector) = (0,0,0,0) | |
_TH("theta", Float) = 0 | |
_Q("quaternion", Vector) = (0,0,0,1) | |
} | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
#define PI 3.1416 | |
float4 _AX,_Q; | |
float _TH; | |
struct v2f { | |
float4 pos : SV_POSITION; | |
fixed4 color : COLOR; | |
}; | |
// Quaternion multiplication. | |
// http://mathworld.wolfram.com/Quaternion.html | |
float4 qmul(float4 q1, float4 q2) | |
{ | |
return float4( | |
q2.xyz * q1.w + q1.xyz * q2.w + cross(q1.xyz, q2.xyz), | |
q1.w * q2.w - dot(q1.xyz, q2.xyz) | |
); | |
} | |
// Rotate a vector with a rotation quaternion. | |
// http://mathworld.wolfram.com/Quaternion.html | |
float3 rotate_vector(float3 v, float4 r) | |
{ | |
float4 r_c = r * float4(-1, -1, -1, 1); | |
return qmul(r, qmul(float4(v, 0), r_c)).xyz; | |
} | |
float3 rotate(float3 v, float3 axis, float theta){ | |
axis = normalize(axis); | |
float s,c; | |
sincos(theta,s,c); | |
float4 | |
q = float4(axis.x*s,axis.y*s,axis.z*s,c); | |
return rotate_vector(v,q); | |
} | |
v2f vert (appdata_full v) | |
{ | |
float3 axis = normalize(_AX.xyz); | |
float theta = PI * _TH; | |
v.vertex.xyz = rotate_vector(v.vertex.xyz, _Q); | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.color = v.color; | |
return o; | |
} | |
half4 frag (v2f i) : COLOR | |
{ | |
return i.color; | |
} | |
ENDCG | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma fragmentoption ARB_precision_hint_fastest | |
ENDCG | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/QuaternionTest3" { | |
Properties { | |
_LA ("look at", Vector) = (0,0,0,1) | |
_T ("t",Float) = 0 | |
} | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
#define PI 3.1416 | |
float4 _LA; | |
float _T; | |
struct v2f { | |
float4 pos : SV_POSITION; | |
fixed4 color : COLOR; | |
}; | |
// Quaternion multiplication. | |
// http://mathworld.wolfram.com/Quaternion.html | |
float4 qmul(float4 q1, float4 q2) | |
{ | |
return float4( | |
q2.xyz * q1.w + q1.xyz * q2.w + cross(q1.xyz, q2.xyz), | |
q1.w * q2.w - dot(q1.xyz, q2.xyz) | |
); | |
} | |
// Rotate a vector with a rotation quaternion. | |
// http://mathworld.wolfram.com/Quaternion.html | |
float3 rotate_vector(float3 v, float4 r) | |
{ | |
float4 r_c = r * float4(-1, -1, -1, 1); | |
return qmul(r, qmul(float4(v, 0), r_c)).xyz; | |
} | |
float3 rotate(float3 v, float3 axis, float theta){ | |
axis = normalize(axis); | |
float s,c; | |
sincos(theta,s,c); | |
float4 | |
q = float4(axis.x*s,axis.y*s,axis.z*s,c); | |
return rotate_vector(v,q); | |
} | |
v2f vert (appdata_full v) | |
{ | |
float3 | |
v1 = float3(0,0,1), | |
v2 = normalize(_LA.xyz), | |
cr = cross(v1,v2); | |
float4 q = float4( cr,1+dot(v1,v2) ); | |
q = normalize(lerp(float4(0,0,0,1),q,saturate(_T))); | |
v.vertex.xyz = rotate_vector(v.vertex.xyz, q).xyz; | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.color = v.color; | |
return o; | |
} | |
half4 frag (v2f i) : COLOR | |
{ | |
return i.color; | |
} | |
ENDCG | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma fragmentoption ARB_precision_hint_fastest | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment