111,095
社区成员




using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
public class RotatingEffect
{
private AxisAngleRotation3D axisAngleRotation;
private double delay;
/// <summary>
/// 3D 旋转切换组件
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="direction">旋转方向 0-横向 1-纵向</param>
/// <param name="control_First">正面显示的组件</param>
/// <param name="control_Second">背面显示的组件</param>
/// <param name="timeDelay">动画时长</param>
/// <returns>Viewport3D 组件</returns>
public Viewport3D rotatingEffect(double width, double height, int direction, UserControl control_First, UserControl control_Second, double timeDelay)
{
delay = timeDelay;
double Mesh_Width = width / 2;
double Mesh_Height = height / 2;
Viewport3D viewport3D = new Viewport3D();
PerspectiveCamera perspectiveCamera = new PerspectiveCamera()
{
Position = new Point3D(0, 0, width * 2), // 可调整
LookDirection = new Vector3D(0, 0, -1),
NearPlaneDistance = 100 // 可调整
};
viewport3D.Camera = perspectiveCamera;
ContainerUIElement3D containerUIElement3D = new ContainerUIElement3D();
DiffuseMaterial diffuseMaterial = new DiffuseMaterial();
Viewport2DVisual3D.SetIsVisualHostMaterial(diffuseMaterial, true);
MeshGeometry3D MeshGeometry_1 = new MeshGeometry3D()
{
TriangleIndices = new Int32Collection(new int[] { 0, 1, 2, 0, 2, 3 }),
TextureCoordinates = new PointCollection(new Point[] { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 0) }),
Positions = new Point3DCollection(new Point3D[] {
new Point3D(-Mesh_Width, Mesh_Height, 0),
new Point3D(-Mesh_Width, -Mesh_Height, 0),
new Point3D(Mesh_Width, -Mesh_Height, 0),
new Point3D(Mesh_Width, Mesh_Height, 0)
})
};
Viewport2DVisual3D viewport2DVisual3D_1 = new Viewport2DVisual3D()
{
Geometry = MeshGeometry_1,
Visual = control_First,
Material = diffuseMaterial
};
PointCollection Direction;
Vector3D vector3D;
if (direction == 0)
{
Direction = new PointCollection(new Point[] { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 0) });
vector3D = new Vector3D(0, 1, 0);
}
else
{
Direction = new PointCollection(new Point[] { new Point(1, 1), new Point(1, 0), new Point(0, 0), new Point(0, 1) });
vector3D = new Vector3D(1, 0, 0);
}
MeshGeometry3D MeshGeometry_2 = new MeshGeometry3D()
{
TriangleIndices = new Int32Collection(new int[] { 0, 1, 2, 0, 2, 3 }),
TextureCoordinates = Direction,
Positions = new Point3DCollection(new Point3D[] {
new Point3D(Mesh_Width, Mesh_Height, 0),
new Point3D(Mesh_Width, -Mesh_Height, 0),
new Point3D(-Mesh_Width, -Mesh_Height, 0),
new Point3D(-Mesh_Width, Mesh_Height, 0)
})
};
Viewport2DVisual3D viewport2DVisual3D_2 = new Viewport2DVisual3D()
{
Geometry = MeshGeometry_2,
Visual = control_Second,
Material = diffuseMaterial
};
containerUIElement3D.Children.Add(viewport2DVisual3D_1);
containerUIElement3D.Children.Add(viewport2DVisual3D_2);
axisAngleRotation = new AxisAngleRotation3D()
{
Angle = 0,
Axis = vector3D
};
RotateTransform3D rotateTransform3D = new RotateTransform3D()
{
CenterX = 0.5,
CenterY = 0.5,
CenterZ = 0.5,
Rotation = axisAngleRotation
};
containerUIElement3D.Transform = rotateTransform3D;
ModelVisual3D modelVisual3D = new ModelVisual3D()
{
Content = new DirectionalLight(Colors.Transparent, new Vector3D(0, 0, -1))
};
viewport3D.Children.Add(containerUIElement3D);
viewport3D.Children.Add(modelVisual3D);
return viewport3D;
}
/// <summary>
/// 旋转角度
/// </summary>
/// <param name="degrees">角度</param>
public void Turn(double degrees)
{
DoubleAnimation da = new DoubleAnimation();
da.Duration = new Duration(TimeSpan.FromSeconds(delay));
da.To = degrees;
axisAngleRotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
}
}