我需要在C#中沿着一个轴旋转一个3D网格对象。
你能展示一下如何实现吗?
将所有顶点乘以旋转矩阵
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position="-40,40,40" LookDirection="40,-40,-40 "
UpDirection="0,0,1" />
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup>
<DirectionalLight Color="White" Direction="-1,-1,-3" />
<GeometryModel3D>
<Model3DGroup.Transform>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<!-- here you do the rotation -->
<AxisAngleRotation3D x:Name="rotation" Axis="0 0 1" Angle="45" />
</RotateTransform3D.Rotation>
</RotateTransform3D>
</Model3DGroup.Transform>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,0,0 10,0,0 10,10,0 0,10,0 0,0,10
10,0,10 10,10,10 0,10,10"
TriangleIndices="0 1 3 1 2 3 0 4 3 4 7 3 4 6 7 4 5 6
0 4 1 1 4 5 1 2 6 6 5 1 2 3 7 7 6 2"/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Red"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
this.rotation.Angle = 90;
我遇到过类似的问题。我不确定您想要旋转什么,但假设您想要转换一个MeshGeometry3D对象。以下是我的解决方案。
public void RotateMesh(MeshGeometry3D mesh, Vector3D axis, double angle)
{
var transform = new RotateTransform3D();
transform.Rotation = new AxisAngleRotation3D(axis, angle);
for (int i = 0; i < mesh.Positions.Count; ++i)
mesh.Positions[i] = transform.Transform(mesh.Positions[i]);
}
让我们加入更多的细节。
给定旋转角度和旋转轴的规格,我们可以通过几个步骤完成旋转。
代码是读者的练习,因为这对我来说是一种大脑训练(计算机图形学是很久以前的事情 :d)。也许当我准备好时,我会发布更多内容。
我相信它是这样的:
R(theta) = T^-1 . Rx^-1(alpha) . Ry^-1(Beta) . Rz(theta) . Ry(beta) . Rx(alpha) . T其中:
大脑训练编辑
哦,我们可以简化它(甚至不使用四元数)
(来源:gamedev.net)
而 (x,y,z) 是旋转轴上的单位向量,θ 是旋转角度。
如果我可以相信 Google。证明留给读者作为练习,但我认为就我所见,它是正确的(来源:Graphics Gems(Glassner,Academic Press,1990年)。)