http://msdn.microsoft.com/en-us/library/ff770576.aspx
Wednesday, February 5, 2014
Saturday, February 1, 2014
Unity에서 빌보드 설정하기
Lookat 함수를 이용하는 방법도 있지만 전통적인 방법으로 행렬을 설정하고 그 행렬로 Transform을 변경하는 방법으로 빌보드를 구현해 보았다.
using UnityEngine;
using System.Collections;
public class billboard : MonoBehaviour {
// Use this for initialization
void Start () {
}
public static void TransformFromMatrix(Matrix4x4 matrix, Transform trans) {
trans.rotation = Quaternion.Inverse(QuaternionFromMatrix(matrix));
trans.position = matrix.GetColumn(3); // uses implicit conversion from Vector4 to Vector3
}
public static Quaternion QuaternionFromMatrix(Matrix4x4 m) {
// Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
Quaternion q = new Quaternion();
q.w = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] + m[1,1] + m[2,2] ) ) / 2;
q.x = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] - m[1,1] - m[2,2] ) ) / 2;
q.y = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] + m[1,1] - m[2,2] ) ) / 2;
q.z = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] - m[1,1] + m[2,2] ) ) / 2;
q.x *= Mathf.Sign( q.x * ( m[2,1] - m[1,2] ) );
q.y *= Mathf.Sign( q.y * ( m[0,2] - m[2,0] ) );
q.z *= Mathf.Sign( q.z * ( m[1,0] - m[0,1] ) );
return q;
}
// Update is called once per frame
void Update ()
{
Vector3 origin = transform.position;
Vector3 cameraPos = Camera.main.transform.position;
Vector3 w = cameraPos - origin;
w = w.normalized;
Vector3 u = Vector3.Cross (Vector3.up, w);
u = u.normalized;
Vector3 v = Vector3.Cross (w, u);
Matrix4x4 mat = new Matrix4x4 ();
/*mat.m00 = 1;
mat.m01 = 0;
mat.m02 = 0;
mat.m03 = 0;
mat.m10 = 0;
mat.m11 = 1;
mat.m12 = 0;
mat.m13 = 0;
mat.m20 = 0;
mat.m21 = 0;
mat.m22 = 1;
mat.m23 = 0;*/
mat.m00 = u.x;
mat.m01 = u.y;
mat.m02 = u.z;
mat.m03 = 0;
mat.m10 = v.x;
mat.m11 = v.y;
mat.m12 = v.z;
mat.m13 = 0;
mat.m20 = w.x;
mat.m21 = w.y;
mat.m22 = w.z;
mat.m23 = 0;
mat.m30 = origin.x;
mat.m31 = origin.y;
mat.m32 = origin.z;
mat.m33 = 1;
TransformFromMatrix (mat, gameObject.transform);
}
}
using UnityEngine;
using System.Collections;
public class billboard : MonoBehaviour {
// Use this for initialization
void Start () {
}
public static void TransformFromMatrix(Matrix4x4 matrix, Transform trans) {
trans.rotation = Quaternion.Inverse(QuaternionFromMatrix(matrix));
trans.position = matrix.GetColumn(3); // uses implicit conversion from Vector4 to Vector3
}
public static Quaternion QuaternionFromMatrix(Matrix4x4 m) {
// Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
Quaternion q = new Quaternion();
q.w = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] + m[1,1] + m[2,2] ) ) / 2;
q.x = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] - m[1,1] - m[2,2] ) ) / 2;
q.y = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] + m[1,1] - m[2,2] ) ) / 2;
q.z = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] - m[1,1] + m[2,2] ) ) / 2;
q.x *= Mathf.Sign( q.x * ( m[2,1] - m[1,2] ) );
q.y *= Mathf.Sign( q.y * ( m[0,2] - m[2,0] ) );
q.z *= Mathf.Sign( q.z * ( m[1,0] - m[0,1] ) );
return q;
}
// Update is called once per frame
void Update ()
{
Vector3 origin = transform.position;
Vector3 cameraPos = Camera.main.transform.position;
Vector3 w = cameraPos - origin;
w = w.normalized;
Vector3 u = Vector3.Cross (Vector3.up, w);
u = u.normalized;
Vector3 v = Vector3.Cross (w, u);
Matrix4x4 mat = new Matrix4x4 ();
/*mat.m00 = 1;
mat.m01 = 0;
mat.m02 = 0;
mat.m03 = 0;
mat.m10 = 0;
mat.m11 = 1;
mat.m12 = 0;
mat.m13 = 0;
mat.m20 = 0;
mat.m21 = 0;
mat.m22 = 1;
mat.m23 = 0;*/
mat.m00 = u.x;
mat.m01 = u.y;
mat.m02 = u.z;
mat.m03 = 0;
mat.m10 = v.x;
mat.m11 = v.y;
mat.m12 = v.z;
mat.m13 = 0;
mat.m20 = w.x;
mat.m21 = w.y;
mat.m22 = w.z;
mat.m23 = 0;
mat.m30 = origin.x;
mat.m31 = origin.y;
mat.m32 = origin.z;
mat.m33 = 1;
TransformFromMatrix (mat, gameObject.transform);
}
}
Subscribe to:
Posts (Atom)
Task in UnrealEngine
https://www.youtube.com/watch?v=1lBadANnJaw
-
Unity released very good FPS example for people and I decided to analysis how they make this. Personally I wanted to show you how I analys...
-
When we use DrawDebugSphere function for debugging, it is working well but when you are trying to use it in anim node's function it will...
-
If you press a key 'L' in the jupyter notebook then you can see the line number in the editor.