Bladeren bron

Removed alloc from calcScreenRatio. Inlined camera axis getter methods to avoid alloc. Added inlined version of the camera project method.

TothBenoit 1 jaar geleden
bovenliggende
commit
9a99c24209
2 gewijzigde bestanden met toevoegingen van 24 en 11 verwijderingen
  1. 22 9
      h3d/Camera.hx
  2. 2 2
      h3d/scene/Mesh.hx

+ 22 - 9
h3d/Camera.hx

@@ -157,10 +157,9 @@ class Camera {
 	/**
 		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();
 
+	inline public function getForward() : h3d.Vector {
+		var forward = new h3d.Vector();
 		if ( directions == null ) {
 			directions = new h3d.Matrix();
 			directions._44 = 0;
@@ -178,9 +177,9 @@ class Camera {
 	/**
 		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();
+
+	inline public function getRight() : h3d.Vector {
+		var right = new h3d.Vector();
 		if ( directions == null ) {
 			directions = new h3d.Matrix();
 			directions._44 = 0;
@@ -198,9 +197,9 @@ class Camera {
 	/**
 		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();
+
+	inline public function getUp() : h3d.Vector {
+		var up = new h3d.Vector(); 
 		if ( directions == null ) {
 			directions = new h3d.Matrix();
 			directions._44 = 0;
@@ -442,6 +441,20 @@ class Camera {
 	/**
 		Project a 3D point into the 2D screen. Make sure to update() the camera if it's been moved before using that.
 	**/
+
+	inline public function projectInline( x : Float, y : Float, z : Float, screenWidth : Float, screenHeight : Float, snapToPixel = true ) {
+		var p = new h3d.Vector();
+		p.set(x, y, z);
+		p.project(m);
+		p.x = (p.x + 1) * 0.5 * screenWidth;
+		p.y = (-p.y + 1) * 0.5 * screenHeight;
+		if( snapToPixel ) {
+			p.x = Math.round(p.x);
+			p.y = Math.round(p.y);
+		}
+		return p;
+	}
+
 	public function project( x : Float, y : Float, z : Float, screenWidth : Float, screenHeight : Float, snapToPixel = true, ?p: h3d.Vector) {
 		if(p == null)
 			p = new h3d.Vector();

+ 2 - 2
h3d/scene/Mesh.hx

@@ -100,8 +100,8 @@ class Mesh extends Object {
 		var worldTopLeft = worldCenter + cameraTopLeft * worldRadius;
 		var worldBottomRight = worldCenter - cameraTopLeft * worldRadius;
 
-		var screenTopLeft = ctx.camera.project( worldTopLeft.x, worldTopLeft.y, worldTopLeft.z, 1.0, 1.0, false );
-		var screenBottomRight = ctx.camera.project( worldBottomRight.x, worldBottomRight.y, worldBottomRight.z, 1.0, 1.0, false );
+		var screenTopLeft = ctx.camera.projectInline( worldTopLeft.x, worldTopLeft.y, worldTopLeft.z, 1.0, 1.0, false );
+		var screenBottomRight = ctx.camera.projectInline( worldBottomRight.x, worldBottomRight.y, worldBottomRight.z, 1.0, 1.0, false );
 
 		var screenArea = hxd.Math.max( screenBottomRight.x - screenTopLeft.x, screenBottomRight.y - screenTopLeft.y );