gpt4 book ai didi

c# - 如何在 WPF 中进行 3D 转换?

转载 作者:行者123 更新时间:2023-12-03 02:30:25 24 4
gpt4 key购买 nike

在 flash10 I can轻松地围绕 3D 空间中的中心旋转图像。

我的 flash10 示例位于 [ http://jsc.sourceforge.net/examples/web/MatrixStuffExample/Application.htm alt text
(来源:sourceforge.net)

我想将此示例移植到 WPF。

是否有示例如何在 C# 中的 WPF 中进行此类 3D 转换而不使用 XAML?

在 silverlight 3 中,您将使用 Matrix3DProjection。您将如何在 WPF 中实现?

最佳答案

我设置了一个基本的薄立方体并将其旋转,以便您可以通过 C# 了解变换的工作原理。

在 xaml 中设置视口(viewport)

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>

<Viewport3D Name="mainViewport" ClipToBounds="True" HorizontalAlignment="Stretch" Height="300">
<Viewport3D.Camera>
<PerspectiveCamera
LookDirection="0,0,-20"
UpDirection="0,1,0"
Position="0,0,50"
/>
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup x:Name="group3d">

<SpotLight Position="10,10,10" x:Name="mySpotLight" Color="Yellow" InnerConeAngle="15" OuterConeAngle="100" Range="100" />
</Model3DGroup>
</ModelVisual3D.Content>

</ModelVisual3D>
</Viewport3D>

</StackPanel>
</Window>

这是后面的代码,转换是最后一个方法...希望这有帮助...如果您想要更多信息,请给我留言!

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Init();
}


private Timer _timer;
private ModelVisual3D _model = new ModelVisual3D();
private double _angle = 0;

public void Init()
{
_model = GetCube(GetSurfaceMaterial(Colors.Red), new Point3D(10, 10, 1), new Point3D(-10,-10,-1));
mainViewport.Children.Add(_model);
_timer = new Timer(10);
_timer.Elapsed += TimerElapsed;
_timer.Enabled = true;
}

void TimerElapsed(object sender, ElapsedEventArgs e)
{
Dispatcher.Invoke(DispatcherPriority.Normal, new Action<double>(Transform), 0.5d);
}

public MaterialGroup GetSurfaceMaterial(Color colour)
{
var materialGroup = new MaterialGroup();
var emmMat = new EmissiveMaterial(new SolidColorBrush(colour));
materialGroup.Children.Add(emmMat);
materialGroup.Children.Add(new DiffuseMaterial(new SolidColorBrush(colour)));
var specMat = new SpecularMaterial(new SolidColorBrush(Colors.White), 30);
materialGroup.Children.Add(specMat);
return materialGroup;
}

public ModelVisual3D GetCube(MaterialGroup materialGroup, Point3D nearPoint, Point3D farPoint)
{
var cube = new Model3DGroup();
var p0 = new Point3D(farPoint.X, farPoint.Y, farPoint.Z);
var p1 = new Point3D(nearPoint.X, farPoint.Y, farPoint.Z);
var p2 = new Point3D(nearPoint.X, farPoint.Y, nearPoint.Z);
var p3 = new Point3D(farPoint.X, farPoint.Y, nearPoint.Z);
var p4 = new Point3D(farPoint.X, nearPoint.Y, farPoint.Z);
var p5 = new Point3D(nearPoint.X, nearPoint.Y, farPoint.Z);
var p6 = new Point3D(nearPoint.X, nearPoint.Y, nearPoint.Z);
var p7 = new Point3D(farPoint.X, nearPoint.Y, nearPoint.Z);
//front side triangles
cube.Children.Add(CreateTriangleModel(materialGroup, p3, p2, p6));
cube.Children.Add(CreateTriangleModel(materialGroup, p3, p6, p7));
//right side triangles
cube.Children.Add(CreateTriangleModel(materialGroup, p2, p1, p5));
cube.Children.Add(CreateTriangleModel(materialGroup, p2, p5, p6));
//back side triangles
cube.Children.Add(CreateTriangleModel(materialGroup, p1, p0, p4));
cube.Children.Add(CreateTriangleModel(materialGroup, p1, p4, p5));
//left side triangles
cube.Children.Add(CreateTriangleModel(materialGroup, p0, p3, p7));
cube.Children.Add(CreateTriangleModel(materialGroup, p0, p7, p4));
//top side triangles
cube.Children.Add(CreateTriangleModel(materialGroup, p7, p6, p5));
cube.Children.Add(CreateTriangleModel(materialGroup, p7, p5, p4));
//bottom side triangles
cube.Children.Add(CreateTriangleModel(materialGroup, p2, p3, p0));
cube.Children.Add(CreateTriangleModel(materialGroup, p2, p0, p1));
var model = new ModelVisual3D();
model.Content = cube;
return model;
}

private Model3DGroup CreateTriangleModel(Material material, Point3D p0, Point3D p1, Point3D p2)
{
var mesh = new MeshGeometry3D();
mesh.Positions.Add(p0);
mesh.Positions.Add(p1);
mesh.Positions.Add(p2);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
var normal = CalculateNormal(p0, p1, p2);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);

var model = new GeometryModel3D(mesh, material);

var group = new Model3DGroup();
group.Children.Add(model);
return group;
}

private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)
{
var v0 = new Vector3D(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
var v1 = new Vector3D(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);
return Vector3D.CrossProduct(v0, v1);
}

void Transform(double adjustBy)
{
_angle += adjustBy;


var rotateTransform3D = new RotateTransform3D {CenterX = 0, CenterZ = 0};
var axisAngleRotation3D = new AxisAngleRotation3D {Axis = new Vector3D(0, 1, 0), Angle = _angle};
rotateTransform3D.Rotation = axisAngleRotation3D;
var myTransform3DGroup = new Transform3DGroup();
myTransform3DGroup.Children.Add(rotateTransform3D);
_model.Transform = myTransform3DGroup;

}


}

关于c# - 如何在 WPF 中进行 3D 转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1270822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com