Problems getting the scale factor

Software: Away3D 4.x

enguer, Newbie
Posted: 27 August 2012 09:26 AM   Total Posts: 6

Hi,
I have some problems getting the scale factor formula to work…
I’m using the lens perspective with fieldOfView = 20.

I need to adjust the camera z position in order to display all my object3d on the screen. This object’s height may vary.

Each time i modify my object, i adjust the camera position.

The distance formula works pretty well :

public function setCameraConfig(w : Number, h : Number, ypos : Number) : void {
var sw : Number = w / _view.width;
var 
sh : Number = h / _view.height;
// detection du mode
var mode : String = (sw > sh) ? "paysage" : "portrait";

var 
z : Number = (((mode == "portrait") ? h : _view.height * sw) / 2) / Math.tan((PerspectiveLens(_view.camera.lens).fieldOfView / (180 / Math.PI)) / 2);
return 
z;
} 


I need to display a plane on front of this object, and this plane need to have the exact height of my view, and a width in pixels.

When i use this formula which seems to be correct:

public function scaleByFOV(FOV : Number, distance : Number, viewHeight : Number) : Number {
   
var fl : Number = viewHeight / (2 * Math.tan(FOV * (180 / Math.PI)));
   
// scale = fLength / (fLength   distanceFromViewer);
   
return -1 / (fl / (fl   distance));
  
} 

the scale isn’t true. my plane is not at the right dimensions, and this dimensions vary when i move the camera .

I’ve searched a lot on this forum, but all the solutions returns the same result.

I’m using the last Away3d 4.0 gold one.

Have you some advice for me ?

 

   

enguer, Newbie
Posted: 27 August 2012 08:26 PM   Total Posts: 6   [ # 1 ]

I reply to myself.
I’ve found the right way to precisely get scale factor with the perspective lens.

public function scaleByVector(d : Number) : Number {
   
var v0 : Vector3D = _View.camera.unproject(-1, 0, 0);
   var 
v1 : Vector3D = _View.camera.unproject(1, 0, 0);
   
v0.scaleBy(d / (v0.z - _View.camera.z));
   
v1.scaleBy(d / (v1.z - _View.camera.z));
   var 
dist : Number = Vector3D.distance(v0, v1);
   var 
scale : Number = dist / _View.width;
   return 
scale;
  
} 

the method unproject return a Vector3D with 20 unit of magnitude. You only need to scale it to your length and calculate the distance.

And a way to use it:

var scale:Number = scaleByVector((_View.camera.z+100));
   
_Cube.z = -100;
   
_Cube.x = (512)*scale;
   
_Cube.x -= (75)*scale;
   
_Cube.scaleX =_Cube.scaleY = _Cube.scaleZ = 1;
   
_Cube.scale(scale); 
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X