Skip to content

Instantly share code, notes, and snippets.

@ifournight
Last active August 29, 2015 14:12
Show Gist options
  • Save ifournight/53784c744045cce2756f to your computer and use it in GitHub Desktop.
Save ifournight/53784c744045cce2756f to your computer and use it in GitHub Desktop.
import flash.geom.Matrix3D;
import flash.geom.Orientation3D;
import flash.geom.Vector3D;
public class Matrix3DUnils {
private static var _raw : Vector.<Number> = new Vector.<Number>(16, true);
private static var _toRad : Number = 0.0174532925199433;
private static var _toAng : Vector3D = 57.2957795130823;
private static var _vector : Vector3D = new Vector3D();
private static var _right : Vector3D = new Vector3D();
private static var _up : Vector3D = new Vector3D();
private static var _dir : Vector3D = new Vector3D();
private static var _scale : Vector3D = new Vector3D();
private static var _pos : Vector3D = new Vector3D();
private static var _x : Number;
private static var _y : Number;
private static var _z : Number;
// 4x4矩阵里面第一列就是右方方向
public static function getRight(m : Matrix3D, out : Vector3D = null) : Vector3D {
if (out == null) {
out = new Vector3D();
}
m.copyColumnTo(0, out);
return out;
}
public static function getUp(m : Matrix3D, out : Vector3D = null) : Vector3D {
if (out == null) {
out = new Vector3D();
}
m.copyColumnTo(1, out); // 第二列是上方方向
return out
}
public static function getDir(m : Matrix3D, out : Vector3D = null) : Vector3D {
if (out == null) {
out = new Vector3D();
}
m.copyColumnTo(2, out); // 第三列是前方方向
return out
}
public static function setOrientation(m : Matrix3D, dir : Vector3D, at : Vector3D = null, up : Vector3D = null, smooth : Number = 1) : void
{
getScale(m, _scale);
if (up == null) {
if ((dir.x == 0) && (Math.abs(dir.y) == 1) && (dir.z == 0)) {
up = Vector3D.Z_AXIS;
} else {
up = Vector3D.Y_AXIS;
}
}
dir.normalize();
var rVec : Vector3D = up.crossProduct(dir);
rVec.normalize();
var uVec : Vector3D = dir.crossProduct(rVec);
rVec.scaleBy(_scale.x);
uVec.scaleBy(_scale.y);
dir.scaleBy(_scale.z);
setVectors(m, rVec, uVec, dir);
}
public static function setVectors(m : Matrix3D, right: Vector3D, up : Vector3D, dir : Vector3D) : void {
right.w = 0;
up.w = 0;
dir.w = 0;
m.copyColumnFrom(0, right);
m.copyColumnFrom(1, up);
m.copyColumnFrom(2, dir);
}
public static function lookAt(m : Matrix3D, x : Number, y : Number, z : Number, up : Vector3D = null, smooth : Number = 1) : void {
m.copyColumnTo(3, _pos);
_vector.x = (x - _pos.x);
_vector.y = (y - _pos.y);
_vector.z = (z - _pos.z);
setOrientation(m, _vector, up, smooth);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment