我在WPF中定义了一个非常简单的3D空间,它定义了一个3D矩形,我希望能够操作MeshGeometry3D上的每一个Point3D对象(在“Positions”中)属性,但我遇到了麻烦用它......
这是我的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只是一个用可视化画笔包裹的图像。
这是我的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);
}
}
但在视觉屏幕上没有任何反应......
我想特别提到的一件事是,我不想将变换应用于整个对象,一次只能应用一个点。
但如果这是唯一的方法,那么我猜我应该调查它
感谢您提供任何帮助。
标记
答案 0 :(得分:2)
好的,我知道了,这是因为geometry3D.Positions返回Point3D对象的集合,这些对象是结构,所以当你引用它们时,你不会改变你认为正在改变的那个......
所以更像这样的循环就是诀窍:
for (var i = 0; i < positions.Count; i++)
{
Point3D position = positions[i];
position.Z += 5;
positions[i] = position;
}
答案 1 :(得分:0)
这将以更好的方式解决问题。
TranslateTransform3D mytranslate = new TranslateTransform3D();
mytranslate.OffsetX = 0;
mytranslate.OffsetY = 0;
mytranslate.OffsetZ = 5;
model1.Transform = mytranslate;