WPF 和 3D 如何更改 3D 空间中的单个位置点? [英] WPF and 3D how do you change a single position point in 3D space?

查看:29
本文介绍了WPF 和 3D 如何更改 3D 空间中的单个位置点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 中定义了一个非常简单的 3D 空间,它定义了一个 3D 矩形,我希望能够操作 MeshGeometry3D 上的每个 Point3D 对象(在位置"中)属性,但是我遇到了麻烦有了它...

I have a VERY simple 3D space defined in WPF, which defines a 3D rectangle, I was hoping to be able to manipulate each one of the Point3D objects (in the "Positions") property on the MeshGeometry3D, but Im having trouble with it...

这是我的 XAML:

<Grid>
    <Viewport3D Name="ViewPort" 
                Focusable="true" 
                ClipToBounds="true" 
                Width="{Binding Width, ElementName=canvas, Mode=Default}" 
                Height="{Binding Height, ElementName=canvas, Mode=Default}">
        <Viewport3D.Camera>
      <PerspectiveCamera Position="0,0,5"/>
     </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <PointLight Color="White" Position="0,0,0">
                        <PointLight.Transform>
                            <Transform3DGroup>
                                <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
                                <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1"/>
                                <RotateTransform3D d:EulerAngles="0,0,0">
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D Angle="0" Axis="0,1,0"/>
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                                <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="2"/>
                            </Transform3DGroup>
                        </PointLight.Transform>
                    </PointLight>

                    <GeometryModel3D x:Name="model1" Material="{DynamicResource test1}">
                        <GeometryModel3D.Transform>
                            <Transform3DGroup>
                                <RotateTransform3D>
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D Axis="1,0,0" Angle="5" />
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                                <RotateTransform3D>
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D Axis="0,1,0" Angle="-5" />
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                            </Transform3DGroup>
                        </GeometryModel3D.Transform>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D
           Positions=" -0.5,  0.5, 0.0
                                            -0.5, -0.5, 0.0
                                             1.5, -0.5, 0.0
                                             1.5,  0.5, 0.0"
           TextureCoordinates="0,0 0,1 1,1 1,0"
           TriangleIndices="0 1 2 2 3 0" />
                        </GeometryModel3D.Geometry>
                    </GeometryModel3D>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
    <Canvas Background="Transparent" Grid.Column="0" Grid.Row="0" x:Name="canvas" s:Contacts.ContactDown="canvas_ContactDown"  Width="{Binding Width, ElementName=window, Mode=Default}" Height="{Binding Height, ElementName=window, Mode=Default}">
    </Canvas>
</Grid>

其中 test1 只是一个包裹在可视画笔中的图像.

where test1 is just an image wrapped in a visual brush.

这是我的 C#:

private void canvas_ContactDown(object sender, ContactEventArgs e)
    {
        //Point contactPosition = e.GetPosition(this);
        //var rayMeshResult = (RayMeshGeometry3DHitTestResult)VisualTreeHelper.HitTest(ViewPort, e.GetPosition(ViewPort));
        translatedX -= 0.25;
        translatedY -= 0.25;
        model1.Transform = new TranslateTransform3D(translatedX, translatedY, 0.0);
        //model1.Transform.Transform(new Point3D(translatedX, translatedY, 0.0));

        Console.WriteLine("Changed");
        //Apply Z index changes here...
        var geometry3D = model1.Geometry as MeshGeometry3D;
        Point3DCollection positions = geometry3D.Positions;

        foreach (var position in positions)
        {
            position.Offset(0,0,-15);
        }
    }

但是在视觉屏幕上什么也没有发生...

But nothing happens in the visual screen...

我想特别提到的一件事是,我不想对整个对象应用转换,一次只应用一个点.

One thing I want to specifically mention is that I do not want to apply a transformation to the entire object, just one point at a time.

但如果那是唯一的方法,那么我想我必须调查一下

But if thats the only way then I guess Ill have to look into it

感谢您提供的任何帮助.

Thanks for any help you can give.

标记

推荐答案

好的,我明白了,这是因为 geometry3D.Positions 返回 Point3D 对象的集合,它们是结构体,所以当你引用它们时,你并没有改变一个你认为你正在改变的......

Ok I got it, it was because geometry3D.Positions returns a collection of Point3D objects, which are structs, so when you reference them, you are not changing the one that you think you are changing...

所以更像这样的循环可以解决问题:

So a loop more like this does the trick:

        for (var i = 0; i < positions.Count; i++)
        {
            Point3D position = positions[i];
            position.Z += 5;
            positions[i] = position;
        }

这篇关于WPF 和 3D 如何更改 3D 空间中的单个位置点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆