ソースを参照

Add getRight, getRight and getUp to camera

TothBenoit 1 年間 前
コミット
1977f20e11
2 ファイル変更86 行追加3 行削除
  1. 83 0
      h3d/Camera.hx
  2. 3 3
      h3d/scene/Mesh.hx

+ 83 - 0
h3d/Camera.hx

@@ -47,6 +47,7 @@ class Camera {
 	var mcamInv : Matrix;
 	var mprojInv : Matrix;
 	var needInv : Bool;
+	var directions : Matrix;
 
 	public function new( fovY = 25., zoom = 1., screenRatio = 1.333333, zNear = 0.02, zFar = 4000., rightHanded = false ) {
 		this.fovY = fovY;
@@ -130,6 +131,87 @@ class Camera {
 		return mcamInv;
 	}
 
+	function calcDirections() {
+		var cameraForward = ( target - pos ).normalized();
+		var cameraRight = up.cross(cameraForward).normalized();
+		var cameraUp = cameraForward.cross(cameraRight);
+
+		directions._11 = cameraForward.x;
+		directions._12 = cameraForward.y;
+		directions._13 = cameraForward.z;
+
+		directions._21 = cameraRight.x;
+		directions._22 = cameraRight.y;
+		directions._23 = cameraRight.z;
+
+		directions._31 = cameraUp.x;
+		directions._32 = cameraUp.y;
+		directions._33 = cameraUp.z;
+
+		directions._44 = 1;
+	}
+
+	/**
+		Returns the forward of the camera. Cache the result until the next update().
+	**/
+	public function getForward( ?forward : h3d.Vector ) : h3d.Vector {
+		if ( forward == null)
+			forward = new h3d.Vector();
+
+		if ( directions == null ) {
+			directions = new h3d.Matrix();
+			directions._44 = 0;
+		}
+		if ( directions._44 == 0 )
+			calcDirections();
+
+		forward.x = directions._11;
+		forward.y = directions._12;
+		forward.z = directions._13;
+
+		return forward;
+	}
+
+	/**
+		Returns the right of the camera. Cache the result until the next update().
+	**/
+	public function getRight( ?right : h3d.Vector ) : h3d.Vector {
+		if ( right == null)
+			right = new h3d.Vector();
+		if ( directions == null ) {
+			directions = new h3d.Matrix();
+			directions._44 = 0;
+		}
+		if ( directions._44 == 0 )
+			calcDirections();
+
+		right.x = directions._21;
+		right.y = directions._22;
+		right.z = directions._23;
+
+		return right;
+	}
+
+	/**
+		Returns the up of the camera. Cache the result until the next update().
+	**/
+	public function getUp( ?up : h3d.Vector ) : h3d.Vector {
+		if ( up == null)
+			up = new h3d.Vector();
+		if ( directions == null ) {
+			directions = new h3d.Matrix();
+			directions._44 = 0;
+		}
+		if ( directions._44 == 0 )
+			calcDirections();
+		
+		up.x = directions._31;
+		up.y = directions._32;
+		up.z = directions._33;
+
+		return up;
+	}
+
 	/**
 		Setup camera for cubemap rendering on the given face.
 	**/
@@ -199,6 +281,7 @@ class Camera {
 		needInv = true;
 		if( mcamInv != null ) mcamInv._44 = 0;
 		if( mprojInv != null ) mprojInv._44 = 0;
+		if( directions != null ) directions._44 = 0;
 
 		frustum.loadMatrix(m);
 	}

+ 3 - 3
h3d/scene/Mesh.hx

@@ -94,9 +94,9 @@ class Mesh extends Object {
 		var worldRadius = bounds.dimension() / 3.0;
 		var worldCenter = getAbsPos().getPosition();
 
-		var cameraForward = ( ctx.camera.target - ctx.camera.pos ).normalized();
-		var cameraRight = ctx.camera.up.cross(cameraForward).normalized();
-		var cameraUp = cameraForward.cross(cameraRight);
+		var cameraForward = ctx.camera.getForward();
+		var cameraRight = ctx.camera.getRight();
+		var cameraUp = ctx.camera.getUp();
 		var cameraTopLeft = cameraUp - cameraRight;
 		var worldTopLeft = worldCenter + ( cameraTopLeft - cameraForward ) * worldRadius;
 		var worldBottomRight = worldCenter + ( cameraTopLeft.scaled(-1) - cameraForward ) * worldRadius;