// <SnippetMatrix3DProjectionSample_code> private void ApplyProjection(Object sender, PointerRoutedEventArgs e) { // Translate the image along the negative Z-axis such that it occupies 50% of the // vertical field of view. double fovY = Math.PI / 2.0; double translationZ = -BeachImage.ActualHeight / Math.Tan(fovY / 2.0); double theta = 20.0 * Math.PI / 180.0; // You can create a 3D effect by creating a number of simple // tranformation Matrix3D matrixes and then multiply them together. Matrix3D centerImageAtOrigin = TranslationTransform( -BeachImage.ActualWidth / 2.0, -BeachImage.ActualHeight / 2.0, 0); Matrix3D invertYAxis = CreateScaleTransform(1.0, -1.0, 1.0); Matrix3D rotateAboutY = RotateYTransform(theta); Matrix3D translateAwayFromCamera = TranslationTransform(0, 0, translationZ); Matrix3D perspective = PerspectiveTransformFovRH(fovY, LayoutRoot.ActualWidth / LayoutRoot.ActualHeight, // aspect ratio 1.0, // near plane 1000.0); // far plane Matrix3D viewport = ViewportTransform(LayoutRoot.ActualWidth, LayoutRoot.ActualHeight); Matrix3D m = Matrix3DHelper.Multiply(centerImageAtOrigin, invertYAxis); m = Matrix3D.Multiply(m, rotateAboutY); m = Matrix3D.Multiply(m, translateAwayFromCamera); m = Matrix3D.Multiply(m, perspective); m = Matrix3D.Multiply(m, viewport); Matrix3DProjection m3dProjection = new Matrix3DProjection(); m3dProjection.ProjectionMatrix = m; BeachImage.Projection = m3dProjection; }
private void UpdateOpenedBookTransform(float progress) { //scale var openedProgress = 1 - progress; var openedScale = MathUtility.Lerp(SelectedBookScale3D.X, 1, openedProgress); var scaleMatrix = Matrix3D.CreateScale(openedScale); //rotation var openedBookQuaternion = Matrix3DHelper.Slerp(SelectedBookRotation3D, openedBookTargetQuaternion, openedProgress, true); var rotationM3D = Matrix3D.CreateFromQuaternion(openedBookQuaternion); //translation var openedX = MathUtility.Lerp(SelectedBookTranslation3D.X, OpenedBookRect.X + (float)Book.CoverWidth, openedProgress); var openedY = MathUtility.Lerp(SelectedBookTranslation3D.Y, OpenedBookRect.Y, openedProgress); var translationMatrix = Matrix3D.CreateTranslation(openedX, openedY, 0); OpenedBook.TransformMatrix3D = rotationM3D * scaleMatrix * translationMatrix; }
public void UpdateBookPlaneTransforms(double coverWidth = 0, double coverHeight = 0, double spineWidth = 0) { if (coverWidth == 0) { coverWidth = CoverWidth; } if (coverHeight == 0) { coverHeight = CoverHeight; } if (spineWidth == 0) { spineWidth = SpineWidth; } float x, y, rotX, rotY; Point3D rotCenter; Quaternion rotQuartY; Matrix3D rotMatrixX, rotMatrixY, translationMatrix; //front cover x = (float)spineWidth; rotY = (float)(OpenedProgress * HalfPi); rotMatrixY = Matrix3D.CreateRotationY(rotY); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, 0, 0)); FrontCover3D.TransformMatrix3D = rotMatrixY * translationMatrix; //spine Spine3D.TransformMatrix3D = Matrix3D.Identity; //back cover x = (float)-coverWidth; rotY = (float)(-OpenedProgress * HalfPi); rotQuartY = Quaternion.CreateFromAxisAngle(yAxis, rotY); rotCenter = new Point3D((float)coverWidth, 0, 0); rotMatrixY = Matrix3DHelper.CreateRotationMatrix(ref rotQuartY, ref rotCenter); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, 0, 0)); BackCover3D.TransformMatrix3D = rotMatrixY * translationMatrix; //top back cover x = (float)-coverWidth; y = (float)coverHeight; rotX = -HalfPi; rotY = (float)(-OpenedProgress * HalfPi); rotCenter = new Point3D((float)coverWidth, 0, 0); rotQuartY = Quaternion.CreateFromAxisAngle(yAxis, rotY); rotMatrixX = Matrix3D.CreateRotationX(rotX); rotMatrixY = Matrix3DHelper.CreateRotationMatrix(ref rotQuartY, ref rotCenter); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, y, 0)); TopBackCoverPages3D.TransformMatrix3D = rotMatrixX * rotMatrixY * translationMatrix; //top spine pages x = 0f; y = (float)(coverHeight - 0.1); rotX = -HalfPi; rotMatrixX = Matrix3D.CreateRotationX(rotX); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, y, 0)); TopSpinePages3D.TransformMatrix3D = rotMatrixX * translationMatrix; //top front cover pages x = (float)spineWidth; y = (float)coverHeight; rotX = -HalfPi; rotY = (float)(OpenedProgress * HalfPi); rotMatrixX = Matrix3D.CreateRotationX(rotX); rotMatrixY = Matrix3D.CreateRotationY(rotY); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, y, 0)); TopFrontCoverPages3D.TransformMatrix3D = rotMatrixX * rotMatrixY * translationMatrix; //inner page right x = (float)(spineWidth * 0.5d); rotY = Pi - (float)(OpenedProgress * HalfPi); rotMatrixY = Matrix3D.CreateRotationY(rotY); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, 0, 0)); InnerPageRight3D.TransformMatrix3D = rotMatrixY * translationMatrix; if (TransformsUpdated != null) { TransformsUpdated(this); } }