Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm2: Make PerspectiveProjection.focalLength more precise #19748

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/avm2/globals/flash/geom/perspective_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn get_focal_length<'gc>(
let fov = this.get_slot(pp_slots::FOV).coerce_to_number(activation)?;

let width = get_width(activation, this);
let focal_length = (width / 2.0) / f64::tan(fov / 2.0 * DEG2RAD);
let focal_length = (width / 2.0) as f32 * f64::tan((PI - fov * DEG2RAD) / 2.0) as f32;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After many trials and observations, I found this code. It doesn't seem to be perfect yet (for edge values like 1e-10), though.

  • 1 / tan x = cot x = tan (pi/2 - x)
  • _ as f32 * f32::tan(_) is not good. This produces better result.


Ok(focal_length.into())
}
Expand Down
35 changes: 11 additions & 24 deletions tests/tests/swfs/avm2/perspective_projection/Test.as
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ package {
for (var i: int = 1; i < 180; i++) {
var pp: PerspectiveProjection = new PerspectiveProjection();
pp.fieldOfView = i;
var fl: Number = pp.focalLength;
trace("FOV to FL", i, roundN(fl, 100)); // FIXME: Large numerical errors
trace("FOV to FL", i, pp.focalLength);
}
}

private function TestFLtoFOV(): void {
for (var i: int = 1; i < 1000; i++) {
var pp: PerspectiveProjection = new PerspectiveProjection();
pp.focalLength = i;
var fl: Number = pp.fieldOfView;
trace("FL to FOV", i, roundN(fl, 100000000000));
trace("FL to FOV", i, pp.fieldOfView);
}
}

Expand Down Expand Up @@ -108,35 +106,34 @@ package {
}

private function TestToMatrix3D(): void {
var precision: Number = 100;
var pp: PerspectiveProjection = new PerspectiveProjection();

trace("// toMatrix3D(default)");
trace(roundVecN(pp.toMatrix3D().rawData, precision));
trace(pp.toMatrix3D().rawData);

trace("// toMatrix3D(FOV: 1)");
pp.fieldOfView = 1;
trace(roundVecN(pp.toMatrix3D().rawData, precision));
trace(pp.toMatrix3D().rawData);

trace("// toMatrix3D(FOV: 100)");
pp.fieldOfView = 100;
trace(roundVecN(pp.toMatrix3D().rawData, precision));
trace(pp.toMatrix3D().rawData);

trace("// toMatrix3D(FOV: 179)");
pp.fieldOfView = 179;
trace(roundVecN(pp.toMatrix3D().rawData, precision));
trace(pp.toMatrix3D().rawData);

trace("// toMatrix3D(FL: 1)");
pp.focalLength = 1;
trace(roundVecN(pp.toMatrix3D().rawData, precision));
trace(pp.toMatrix3D().rawData);

trace("// toMatrix3D(FL: 10)");
pp.focalLength = 10;
trace(roundVecN(pp.toMatrix3D().rawData, precision));
trace(pp.toMatrix3D().rawData);

trace("// toMatrix3D(FL: 10000)");
pp.focalLength = 10000;
trace(roundVecN(pp.toMatrix3D().rawData, precision));
trace(pp.toMatrix3D().rawData);
}

private function TestTransform(): void {
Expand Down Expand Up @@ -179,20 +176,10 @@ package {
private function printProps(pp: PerspectiveProjection): void {
trace(" perspectiveProjection = " + pp);
if (pp) {
trace(" perspectiveProjection.fieldOfView = " + roundN(pp.fieldOfView, 100000000000));
trace(" perspectiveProjection.focalLength = " + roundN(pp.focalLength, 100)); // FIXME: Large numerical errors
trace(" perspectiveProjection.fieldOfView = " + pp.fieldOfView);
trace(" perspectiveProjection.focalLength = " + pp.focalLength);
trace(" perspectiveProjection.projectionCenter = " + pp.projectionCenter);
}
}

private function roundN(n: Number, precision: Number): Number {
return Math.round(n * precision) / precision;
}

private function roundVecN(v: Vector.<Number>, precision: Number): Vector.<Number> {
return v.map(
function (n: Number, _, _): Number {return roundN(n, precision);}
);
}
}
}
Loading
Loading