-- 作者:卷积内核
-- 发布时间:7/30/2009 4:14:00 PM
--
3. 3D对象 下面研究真正的3D对象:立方体。立方体由5个矩形组成:后面、前面、左面、右面和底面。每个矩形都由两个三角形组成,因为这是网格的核心。在WPF和3D术语中,网格用于描述建立3D形状的基本三角形。 下面是立方体中前面矩形的代码,该矩形由两个三角形组成。三角形的位置按TriangleIndies定义的逆时针设置。立方体的前面用红色的笔刷绘制,后面用灰色笔刷绘制。这两个笔刷都是SolidColorBrush类型,用Window的资源定义。 < !-- Front -- >< GeometryModel3D >< GeometryModel3D.Geometry >< MeshGeometry3DPositions="-1 -1 1, 1 -1 1, 1 1 1, 1 1 1,-1 1 1, -1 -1 1"TriangleIndices="0 1 2, 3 4 5" / >< /GeometryModel3D.Geometry >< GeometryModel3D.Material >< DiffuseMaterial Brush="{StaticResource redBrush}" / >< /GeometryModel3D.Material >< GeometryModel3D.BackMaterial >< DiffuseMaterial Brush="{StaticResource grayBrush}" / >< /GeometryModel3D.BackMaterial >< /GeometryModel3D > 其他矩形非常类似,只是在不同的位置上。下面是立方体左面的XAML代码: < !-- Left side -- >< GeometryModel3D >< GeometryModel3D.Geometry >< MeshGeometry3DPositions="-1 -1 1, -1 1 1, -1 -1 -1, -1 -1 -1, -1 1 1,-1 1 -1"TriangleIndices="0 1 2, 3 4 5" / >< /GeometryModel3D.Geometry >< GeometryModel3D.Material >< DiffuseMaterial Brush="{StaticResource redBrush}" / >< /GeometryModel3D.Material >< GeometryModel3D.BackMaterial >< DiffuseMaterial Brush="{StaticResource grayBrush}" / >< /GeometryModel3D.BackMaterial >< /GeometryModel3D > 提示: 示例代码为立方体的每个面定义了一个GeometryModel3D,仅是为了更好地理解代码。只要每个面都使用相同的材质,就可以定义一个网格,它包含立方体所有面的全部10个三角形。 所有的矩形都在Model3DGroup中组合,所以可以对立方体的所有面进行变换: < !-- the model -- >< ModelVisual3D >< ModelVisual3D.Content >< Model3DGroup > < ! - GeometryModel3D elements for every side of the box -- > < /Model3DGroup > 使用Model3DGroup的Transform属性,就可以变换这个组中的所有几何体。下面使用RotateTransform3D定义一个AxisAngleRotation3D。要在运行期间旋转立方体,Angle属性要绑定到Slider控件的值上。 < !-- Transformation of the complete model -- >< Model3DGroup.Transform >< RotateTransform3D CenterX="0" CenterY="0" CenterZ="0" >< RotateTransform3D.Rotation >< AxisAngleRotation3D x:Name="axisRotation"Axis="0, 0, 0"Angle="{Binding Path=Value,ElementName=axisAngle}" / >< /RotateTransform3D.Rotation >< /RotateTransform3D >< /Model3DGroup.Transform > 为了查看立方体,需要一个摄像机。这里使用PerspectiveCamera,因此立方体离摄像机越远,就越小。摄像机的位置的方向在运行期间设置。 < !-- Camera -- >< Viewport3D.Camera >< PerspectiveCamera x:Name="camera"Position="{Binding Path=Text,ElementName=textCameraPosition}"LookDirection="{Binding Path=Text,ElementName=textCameraDirection}" / >< /Viewport3D.Camera > 应用程序使用两个不同的光源,其中一个光源是DirectionalLight: < !-- directional light -- >< ModelVisual3D >< ModelVisual3D.Content >< DirectionalLight Color="White" x:Name="directionalLight" >< DirectionalLight.Direction >< Vector3D X="1" Y="2" Z="3" / >< /DirectionalLight.Direction >< /DirectionalLight >< /ModelVisual3D.Content >< /ModelVisual3D > 另一个光源是SpotLight。使用这个光源可以突出显示立方体的一个特定区域。SpotLight定义了属性InnerConeAngle和OuterConeAngle,以指定完全照亮的区域: < !-- spot light -- >< ModelVisual3D >< ModelVisual3D.Content >< SpotLight x:Name="spotLight"InnerConeAngle="{Binding Path=Value,ElementName=spotInnerCone}"OuterConeAngle="{Binding Path=Value,ElementName=spotOuterCone}"Color="#FFFFFF"Direction="{Binding Path=Text, ElementName=spotDirection}"Position="{Binding Path=Text, ElementName=spotPosition}"Range="{Binding Path=Value, ElementName=spotRange}" / >< /ModelVisual3D.Content >< /ModelVisual3D > 运行应用程序,就可以改变立方体的旋转角度、摄像机和灯光,如图35-27所示。 [URL=http://new.51cto.com/files/uploadimg/20081202/160818560.gif] 498)this.style.width=498;" border=0>[/URL] [URL=http://book.51cto.com/files/uploadimg/20060921/153223104.gif][/URL] 图 35-27 提示: 创建仅包含矩形或三角形的3D模型是很简单的。不应手工创建更复杂的模型,而应使用对应的工具。WPF的3D工具在[URL=http://www.codeplex/3DTools]www.codeplex/3DTools[/URL]上。
|