Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active September 11, 2024 05:46
Show Gist options
  • Save unitycoder/c5847a82343a8e721035 to your computer and use it in GitHub Desktop.
Save unitycoder/c5847a82343a8e721035 to your computer and use it in GitHub Desktop.
UNITY_MATRIX_IT_MV[] Vectors in Shader
UNITY_MATRIX_IT_MV[0].xyz = ???
UNITY_MATRIX_IT_MV[1].xyz = Camera Up
UNITY_MATRIX_IT_MV[2].xyz = Camera Forward
float3 forward = -normalize(UNITY_MATRIX_V._m20_m21_m22);
cameraFwd = -unity_MatrixInvV._m02_m12_m22;
float3 up = normalize(UNITY_MATRIX_V._m10_m11_m12);
float3 right = normalize(UNITY_MATRIX_V._m00_m01_m02);
float3 cameraUp = unity_CameraInvProjection[1].xyz;
float3 camRight = cross(unity_CameraInvProjection[2].xyz, cameraUp);
float3 cameraForward = cross(cameraUp, camRight);
Camera right direction = UNITY_MATRIX_V[0].xyz = mul((float3x3)UNITY_MATRIX_V,float3(1,0,0));
Camera up direction = UNITY_MATRIX_V[1].xyz = mul((float3x3)UNITY_MATRIX_V,float3(0,1,0));
Camera forward direction = UNITY_MATRIX_V[2].xyz = mul((float3x3)UNITY_MATRIX_V,float3(0,0,1));
Camera position = _WorldSpaceCameraPos = mul(UNITY_MATRIX_V,float4(0,0,0,1)).xyz;
//http://forum.unity3d.com/threads/get-main-camera-up-direction.189947/#post-1297952
// camera right
float3 right = cross(cameraUp, cameraForward);
// ??
float4 objWorldPos=float4(_Object2World._m03,_Object2World._m13,_Object2World._m23, 1);
// c# - source https://github.com/miguel12345/UnityTextDrawer
private void RotateMatrix180DegreesInYAxis(ref Matrix4x4 matrix)
{
//Same as doing -> matrix *= Matrix4x4.Rotate(Quaternion.AngleAxis(180f, Vector3.up));
matrix.m00 = -matrix.m00;
matrix.m10 = -matrix.m10;
matrix.m20 = -matrix.m20;
matrix.m02 = -matrix.m02;
matrix.m12 = -matrix.m12;
matrix.m22 = -matrix.m22;
}
private void TranslateMatrixInYAxis(ref Matrix4x4 matrix, float translation)
{
//Same as doing -> matrix = matrix * Matrix4x4.Translate(new Vector3(0f,translation,0f))
matrix.m03 = matrix.m03 + matrix.m01 * translation;
matrix.m13 = matrix.m13 + matrix.m11 * translation;
matrix.m23 = matrix.m23 + matrix.m21 * translation;
}
private void TranslateMatrixInXAxis(ref Matrix4x4 matrix, float translation)
{
//Same as doing -> matrix = matrix * Matrix4x4.Translate(new Vector3(translation,0f,0f))
matrix.m03 = matrix.m03 + matrix.m00 * translation;
matrix.m13 = matrix.m13 + matrix.m10 * translation;
matrix.m23 = matrix.m23 + matrix.m20 * translation;
}
private void TranslateMatrixInXY(ref Matrix4x4 matrix, float xTranslation, float yTranslation)
{
//Same as doing -> matrix = matrix * Matrix4x4.Translate(new Vector3(xTranslation,yTranslation,0f))
matrix.m03 = matrix.m03 + matrix.m01 * yTranslation + matrix.m00 * xTranslation;
matrix.m13 = matrix.m13 + matrix.m11 * yTranslation + matrix.m10 * xTranslation;
matrix.m23 = matrix.m23 + matrix.m21 * yTranslation + matrix.m20 * xTranslation;
}
// references
// http://makegamessa.com/discussion/3368/facing-billboards-shader-in-unity
// http://www.codinglabs.net/article_world_view_projection_matrix.aspx
// http://answers.unity3d.com/questions/557922/rotate-a-mesh-on-world-y-axis-using-a-cg-vertex-sh.html
// scaling http://forum.unity3d.com/threads/scaling-in-the-vertex-shader.197815/
// billboarding http://forum.unity3d.com/threads/leaf-card-billboards-camera-facing-matrix.186667/#post-1278929
// http://forum.unity3d.com/threads/stream-out-for-geometry-shader.179377/
// https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html
// https://forum.unity.com/threads/standard-surface-shader-billboard.513060/#post-3359378
@patricknelson
Copy link

Great reference, thanks for putting this here 👍

@STARasGAMES
Copy link

Thanks!

@sunrise1028
Copy link

thx guy ,help me a lot

@tanat44
Copy link

tanat44 commented Oct 13, 2021

Awesome!

@ipud2
Copy link

ipud2 commented Dec 7, 2022

Nothing to preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment