F# WPF - 在 Viewport3D 中显示简单对象

发布于 2024-10-30 15:08:32 字数 279 浏览 0 评论 0原文

我试图用很少的时间和很少的 F# 初步知识来在 F# 中生成 3D 图形(从我之前的问题可以明显看出)。我正在研究托马斯·佩特里克(Tomas Petricek)的分形示例,但我无法真正理解它。我成功地在 XAML 中定义了一个带有 Viewport3D 对象的窗口,从 F# 初始化并显示它。但就在 F# 中创建 3D 对象并显示它们而言,我迷失在分形生成、坐标转换和其他计算的海洋中。 有人可以提供一个简单的示例,在 F# 中创建一个非常简单的对象(单个立方体,或只是一个三角形)并将其显示在 WPF 窗口中吗?这将是巨大的帮助。 谢谢。

I'm trying to get into generating 3D graphics in F# (as was apparent from my previous question) with very little time and very little initial knowledge of F#. I'm studying Tomas Petricek's fractal example, but I can't really make heads or tails of it. I've managed to define a window with a Viewport3D object in XAML, initialize and display it from F#. But as far as creating 3d objects in F# and displaying them goes, I'm lost in a sea of fractal generation, coordinate translation, and other calculations.
Could somebody provide a simple example, of creating one really simple object in F# (a single cube, or just a triangle) and display it in the WPF window? That would be huge help.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏闹i 2024-11-06 15:08:32

这是一个简单的例子,用两个三角形组成一个正方形:

#if INTERACTIVE
    #r "PresentationCore"
    #r "PresentationFramework"
    #r "WindowsBase"
    #r "System.Xaml"
#endif


open System.Windows
open System.Windows.Controls
open System.Windows.Media
open System.Windows.Media.Media3D

let grp = Model3DGroup()
let geo = MeshGeometry3D()

// Point collection
for x,y,z in [0.5, 0.0, 0.0; 
              1.0, 0.0, 0.0;
              0.5, 0.5, 0.0;
              1.0, 0.5, 0.0] do
    geo.Positions.Add(Point3D(x,y,z))

// First triangle
for i in [0;1;2] do geo.TriangleIndices.Add(i)

// Second triangle - order matters for deciding front vs. back
for i in [2;1;3] do geo.TriangleIndices.Add(i)

// Create a model with the mesh and a front and back material
let model = 
    GeometryModel3D(
        Geometry = geo, 
        Material = DiffuseMaterial(Brushes.Black), 
        BackMaterial = DiffuseMaterial(Brushes.Red))

grp.Children.Add(model)

// add light so back color is visible
grp.Children.Add(AmbientLight())

// set up a continuous rotation around the y-axis
let rotation = AxisAngleRotation3D(Axis = Vector3D(0.,1.,0.))
let anim = 
    Animation.DoubleAnimation(0.0, 360., Duration(System.TimeSpan.FromSeconds 2.), 
        RepeatBehavior = Animation.RepeatBehavior.Forever)
rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, anim)

// apply the rotation to the geometry
grp.Transform <- RotateTransform3D(rotation)

// create a camera pointing at the triangle
let cam = PerspectiveCamera(Point3D(0.,0.,2.), Vector3D(0., 0., -1.), Vector3D(0., 1., 0.), 60.)

// set the viewport up with the camera and geometry
let vprt = Viewport3D(Camera = cam)
vprt.Children.Add(ModelVisual3D(Content = grp))

// add the viewport to a window
let wnd = Window(Content = vprt, Title = "3D", Visibility = Visibility.Visible)

Here's a simple example with two triangles making a single square:

#if INTERACTIVE
    #r "PresentationCore"
    #r "PresentationFramework"
    #r "WindowsBase"
    #r "System.Xaml"
#endif


open System.Windows
open System.Windows.Controls
open System.Windows.Media
open System.Windows.Media.Media3D

let grp = Model3DGroup()
let geo = MeshGeometry3D()

// Point collection
for x,y,z in [0.5, 0.0, 0.0; 
              1.0, 0.0, 0.0;
              0.5, 0.5, 0.0;
              1.0, 0.5, 0.0] do
    geo.Positions.Add(Point3D(x,y,z))

// First triangle
for i in [0;1;2] do geo.TriangleIndices.Add(i)

// Second triangle - order matters for deciding front vs. back
for i in [2;1;3] do geo.TriangleIndices.Add(i)

// Create a model with the mesh and a front and back material
let model = 
    GeometryModel3D(
        Geometry = geo, 
        Material = DiffuseMaterial(Brushes.Black), 
        BackMaterial = DiffuseMaterial(Brushes.Red))

grp.Children.Add(model)

// add light so back color is visible
grp.Children.Add(AmbientLight())

// set up a continuous rotation around the y-axis
let rotation = AxisAngleRotation3D(Axis = Vector3D(0.,1.,0.))
let anim = 
    Animation.DoubleAnimation(0.0, 360., Duration(System.TimeSpan.FromSeconds 2.), 
        RepeatBehavior = Animation.RepeatBehavior.Forever)
rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, anim)

// apply the rotation to the geometry
grp.Transform <- RotateTransform3D(rotation)

// create a camera pointing at the triangle
let cam = PerspectiveCamera(Point3D(0.,0.,2.), Vector3D(0., 0., -1.), Vector3D(0., 1., 0.), 60.)

// set the viewport up with the camera and geometry
let vprt = Viewport3D(Camera = cam)
vprt.Children.Add(ModelVisual3D(Content = grp))

// add the viewport to a window
let wnd = Window(Content = vprt, Title = "3D", Visibility = Visibility.Visible)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文