in SamplesCommon/SamplesCommon/PerspectivePanel.cs [117:152]
private void UpdatePerspectiveMatrix()
{
if (!m_setUpExpressions)
{
Vector3 perspectiveOrigin = new Vector3(PerspectiveOriginPercent * m_rootVisual.Size, 0);
Matrix4x4 transform =
Matrix4x4.CreateTranslation(-perspectiveOrigin) *
new Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -1 / (float)PerspectiveDepth, 0, 0, 0, 1) *
Matrix4x4.CreateTranslation(perspectiveOrigin);
m_rootVisual.TransformMatrix = transform;
}
else if (m_matrixExpression == null)
{
m_matrixExpression = m_compositor.CreateExpressionAnimation();
m_matrixExpression.Properties.InsertVector3("LayoutSize", new Vector3(m_rootVisual.Size, 0));
// Expressions don't have an easy way to convert vector2 to vector3. But having this intermediate expression makes the below expression cleaner anyway.
var perspectiveOriginExpression = m_compositor.CreateExpressionAnimation(
"Vector3(publicProps.PerspectiveOriginPercent.x, publicProps.PerspectiveOriginPercent.y, 0) * props.LayoutSize");
perspectiveOriginExpression.SetReferenceParameter("publicProps", m_rootVisual.Properties);
perspectiveOriginExpression.SetReferenceParameter("props", m_matrixExpression.Properties);
m_matrixExpression.Properties.InsertVector3("PerspectiveOrigin", Vector3.Zero);
m_matrixExpression.Properties.StartAnimation("PerspectiveOrigin", perspectiveOriginExpression);
m_matrixExpression.Expression =
"Matrix4x4.CreateFromTranslation(-props.PerspectiveOrigin) * " +
"Matrix4x4(1,0,0,0, 0,1,0,0, 0,0,1,-1/publicProps.PerspectiveDepth, 0,0,0,1) * " +
"Matrix4x4.CreateFromTranslation( props.PerspectiveOrigin)";
m_matrixExpression.SetReferenceParameter("publicProps", m_rootVisual.Properties);
m_matrixExpression.SetReferenceParameter("props", m_matrixExpression.Properties);
m_rootVisual.StartAnimation("TransformMatrix", m_matrixExpression);
}
}