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: Implement Matrix3D with 2D support only #18888

Open
wants to merge 5 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
6 changes: 4 additions & 2 deletions core/src/avm2/globals/flash/display/display_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::avm2::activation::Activation;
use crate::avm2::error::{illegal_operation_error, make_error_2007, make_error_2008};
use crate::avm2::filters::FilterAvm2Ext;
use crate::avm2::globals::flash::geom::transform::color_transform_from_transform_object;
use crate::avm2::globals::flash::geom::transform::has_matrix3d_from_transform_object;
use crate::avm2::globals::flash::geom::transform::matrix_from_transform_object;
use crate::avm2::globals::slots::flash_display_shader as shader_slots;
use crate::avm2::globals::slots::flash_geom_point as point_slots;
Expand Down Expand Up @@ -755,14 +756,15 @@ pub fn set_transform<'gc>(
) -> Result<Value<'gc>, Error<'gc>> {
let transform = args.get_object(activation, 0, "transform")?;

// FIXME - consider 3D matrix and pixel bounds
// FIXME - consider pixel bounds
let matrix = matrix_from_transform_object(transform);

let has_matrix3d = has_matrix3d_from_transform_object(transform);
let color_transform = color_transform_from_transform_object(transform);

let dobj = this.as_display_object().unwrap();
let mut write = dobj.base_mut(activation.context.gc_context);
write.set_matrix(matrix);
write.set_has_matrix3d_stub(has_matrix3d);
write.set_color_transform(color_transform);
drop(write);
if let Some(parent) = dobj.parent() {
Expand Down
3 changes: 0 additions & 3 deletions core/src/avm2/globals/flash/geom/Transform.as
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ package flash.geom {
[Ruffle(InternalSlot)]
private var displayObject:DisplayObject;

private var _matrix3D:Matrix3D = null;
private var _perspectiveProjection:PerspectiveProjection = null;

function Transform(object:DisplayObject) {
this.displayObject = object;
}
Expand Down
104 changes: 87 additions & 17 deletions core/src/avm2/globals/flash/geom/transform.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::avm2::globals::slots::flash_geom_color_transform as ct_slots;
use crate::avm2::globals::slots::flash_geom_matrix as matrix_slots;
use crate::avm2::globals::slots::flash_geom_transform as transform_slots;
use crate::avm2::object::VectorObject;
use crate::avm2::parameters::ParametersExt;
use crate::avm2::vector::VectorStorage;
use crate::avm2::{Activation, Error, Object, TObject, Value};
use crate::display_object::TDisplayObject;
use crate::prelude::{DisplayObject, Matrix, Twips};
use crate::{avm2_stub_getter, avm2_stub_setter};
use ruffle_render::matrix3d::Matrix3D;
use ruffle_render::quality::StageQuality;
use swf::{ColorTransform, Fixed8, Rectangle};

Expand Down Expand Up @@ -48,26 +51,33 @@ pub fn get_matrix<'gc>(
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let matrix = matrix_from_transform_object(this);
matrix_to_object(matrix, activation)
if get_display_object(this).base().has_matrix3d_stub() {
Ok(Value::Null)
} else {
let matrix = matrix_from_transform_object(this);
matrix_to_object(matrix, activation)
}
}

pub fn set_matrix<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
// TODO: Despite what the docs say, FP accepts a null matrix here, and returns
// null when trying to get the matrix- but the DO's actual transform matrix will
// remain its previous non-null value.
let matrix = object_to_matrix(args.get_object(activation, 0, "value")?, activation)?;
let dobj = get_display_object(this);
let Some(obj) = args.try_get_object(activation, 0) else {
dobj.base_mut(activation.gc()).set_has_matrix3d_stub(true);
return Ok(Value::Undefined);
};

let matrix = object_to_matrix(obj, activation)?;
dobj.set_matrix(activation.context.gc_context, matrix);
if let Some(parent) = dobj.parent() {
// Self-transform changes are automatically handled,
// we only want to inform ancestors to avoid unnecessary invalidations for tx/ty
parent.invalidate_cached_bitmap(activation.context.gc_context);
}
dobj.base_mut(activation.gc()).set_has_matrix3d_stub(false);
Ok(Value::Undefined)
}

Expand Down Expand Up @@ -109,6 +119,12 @@ pub fn get_concatenated_matrix<'gc>(
}
}

pub fn has_matrix3d_from_transform_object(transform_object: Object<'_>) -> bool {
get_display_object(transform_object)
.base()
.has_matrix3d_stub()
}

pub fn matrix_from_transform_object(transform_object: Object<'_>) -> Matrix {
*get_display_object(transform_object).base().matrix()
}
Expand Down Expand Up @@ -181,6 +197,47 @@ pub fn color_transform_to_object<'gc>(
Ok(object.into())
}

fn matrix3d_to_object<'gc>(
matrix: Matrix3D,
activation: &mut Activation<'_, 'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
let number = activation.avm2().class_defs().number;
let mut raw_data_storage = VectorStorage::new(16, true, Some(number), activation);
for (i, data) in matrix.raw_data.iter().enumerate() {
raw_data_storage.set(i, Value::Number(*data), activation)?;
}
let vector = VectorObject::from_vector(raw_data_storage, activation)?.into();
let object = activation
.avm2()
.classes()
.matrix3d
.construct(activation, &[vector])?;
Ok(object.into())
}

fn object_to_matrix3d<'gc>(
object: Object<'gc>,
activation: &mut Activation<'_, 'gc>,
) -> Result<Matrix3D, Error<'gc>> {
let raw_data = object
.get_public_property("rawData", activation)?
.as_object()
.expect("rawData cannot be null");
let raw_data = raw_data
.as_vector_storage()
.expect("rawData is not a Vector");
let raw_data: Vec<f64> = (0..16)
.map(|i| -> Result<f64, Error<'gc>> {
raw_data.get(i, activation)?.coerce_to_number(activation)
})
.collect::<Result<Vec<f64>, _>>()?;
let raw_data = raw_data
.as_slice()
.try_into()
.expect("rawData size must be 16");
Ok(Matrix3D { raw_data })
}

pub fn matrix_to_object<'gc>(
matrix: Matrix,
activation: &mut Activation<'_, 'gc>,
Expand Down Expand Up @@ -261,16 +318,16 @@ pub fn get_matrix_3d<'gc>(
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
// FIXME: This Matrix3D is generated from the 2D Matrix.
// It does not work when the matrix contains any transformation in 3D.
// Support native Matrix3D.
avm2_stub_getter!(activation, "flash.geom.Transform", "matrix3D");

let display_object = get_display_object(this);
if display_object.base().has_matrix3d_stub() {
let object = activation
.avm2()
.classes()
.matrix3d
.construct(activation, &[])?;
Ok(object.into())
let matrix = *get_display_object(this).base().matrix();
let matrix3d = Matrix3D::from(matrix);
matrix3d_to_object(matrix3d, activation)
} else {
Ok(Value::Null)
}
Expand All @@ -281,16 +338,29 @@ pub fn set_matrix_3d<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
// FIXME: This sets 2D Matrix generated from the given Matrix3D, ignoring 3D parameters.
// Support native Matrix3D.
avm2_stub_setter!(activation, "flash.geom.Transform", "matrix3D");

let set = args
.get(0)
.map(|arg| arg.as_object().is_some())
.unwrap_or_default();
let display_object = get_display_object(this);
let Some(obj) = args.try_get_object(activation, 0) else {
display_object
.base_mut(activation.gc())
.set_has_matrix3d_stub(false);
return Ok(Value::Undefined);
};

let matrix3d = object_to_matrix3d(obj, activation)?;
let matrix = Matrix::from(matrix3d);
display_object.set_matrix(activation.context.gc_context, matrix);
if let Some(parent) = display_object.parent() {
// Self-transform changes are automatically handled,
// we only want to inform ancestors to avoid unnecessary invalidations for tx/ty
parent.invalidate_cached_bitmap(activation.context.gc_context);
}
display_object
.base_mut(activation.gc())
.set_has_matrix3d_stub(set);
.set_has_matrix3d_stub(true);
Ok(Value::Undefined)
}

Expand Down
1 change: 1 addition & 0 deletions render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod error;
pub mod filters;
pub mod lines;
pub mod matrix;
pub mod matrix3d;
pub mod pixel_bender;
// The `renderdoc` crate doesn't compile on apple platforms
#[cfg(all(feature = "renderdoc", not(target_vendor = "apple")))]
Expand Down
50 changes: 50 additions & 0 deletions render/src/matrix3d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::matrix::Matrix;
use swf::Twips;

/// The transformation matrix for 3D used by Flash display objects.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Matrix3D {
/// 4x4 matrix elements.
pub raw_data: [f64; 16],
}

impl From<Matrix> for Matrix3D {
fn from(matrix: Matrix) -> Self {
Self {
raw_data: [
// 1st column
matrix.a.into(),
matrix.b.into(),
0.0,
0.0,
// 2nd column
matrix.c.into(),
matrix.d.into(),
0.0,
0.0,
// 3rd column
0.0,
0.0,
1.0,
0.0,
// 4th column
matrix.tx.to_pixels(),
matrix.ty.to_pixels(),
0.0,
1.0,
],
}
}
}
impl From<Matrix3D> for Matrix {
fn from(matrix: Matrix3D) -> Self {
Self {
a: matrix.raw_data[0] as f32,
b: matrix.raw_data[1] as f32,
c: matrix.raw_data[4] as f32,
d: matrix.raw_data[5] as f32,
tx: Twips::from_pixels(matrix.raw_data[12]),
ty: Twips::from_pixels(matrix.raw_data[13]),
}
}
}
Loading