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

问题描述

我在WPF定义了一个非常简单的三维空间,它定义了一个3D矩形,我希望能够操纵的三维点对象中的每一个(在位置)财产上的MeshGeometry3D,但有麻烦林它...

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返回三维点对象,这是结构,所以当你引用它们,你是不改变的集合一个你认为你正在改变...

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你如何改变在三维空间中的一个位置呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:41