最基本的3D代码对照
xaml代码
<Viewport3D> <Viewport3D.Camera> <PerspectiveCamera Position="0, 0, 4"/> </Viewport3D.Camera> <!-- Button on 3D --> <Viewport2DVisual3D> <!-- Give the plane a slight rotation --> <Viewport2DVisual3D.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Angle="40" Axis="0, 1, 0" /> </RotateTransform3D.Rotation> </RotateTransform3D> </Viewport2DVisual3D.Transform> <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D --> <Viewport2DVisual3D.Geometry> <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0" TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 1 2 0 2 3"/> </Viewport2DVisual3D.Geometry> <Viewport2DVisual3D.Material> <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/> </Viewport2DVisual3D.Material> <Button>Hello, 3D</Button> </Viewport2DVisual3D> <!-- Lights --> <ModelVisual3D> <ModelVisual3D.Content> <DirectionalLight Color="#FFFFFFFF" Direction="0,0,-1"/> </ModelVisual3D.Content> </ModelVisual3D> </Viewport3D>
cs后台对照写法
private void Init_Draw() { Viewport3D viewport3D = new Viewport3D(); PerspectiveCamera perspectiveCamera = new PerspectiveCamera() { Position = new Point3D(0, 0, 4), }; viewport3D.Camera = perspectiveCamera; Viewport2DVisual3D viewport2DVisual3D = new Viewport2DVisual3D(); RotateTransform3D rotateTransform3D = new RotateTransform3D(); AxisAngleRotation3D axisAngleRotation3D = new AxisAngleRotation3D() { Angle = 40, Axis = new Vector3D(0, 1, 0), }; rotateTransform3D.Rotation = axisAngleRotation3D; viewport2DVisual3D.Transform = rotateTransform3D; MeshGeometry3D meshGeometry3D = new MeshGeometry3D() { Positions = new Point3DCollection(new Point3D[] { new Point3D(-1, 1, 0), new Point3D(-1, -1, 0), new Point3D(1, -1, 0), new Point3D(1, 1, 0) }), TextureCoordinates = new PointCollection { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 0) }, TriangleIndices = new Int32Collection(new int[] { 0, 1, 2, 0, 2, 3 }), }; viewport2DVisual3D.Geometry = meshGeometry3D; DiffuseMaterial diffuseMaterial = new DiffuseMaterial(); Viewport2DVisual3D.SetIsVisualHostMaterial(diffuseMaterial, true); viewport2DVisual3D.Material = diffuseMaterial; Button button = new Button() { Content = "Hello,3D",
Direction = new Vector3D(0, 0, -1), }; viewport2DVisual3D.Visual = button; ModelVisual3D modelVisual3D = new ModelVisual3D(); DirectionalLight directionalLight = new DirectionalLight() { Color = Color.FromRgb(255, 255, 255),
Direction = new Vector3D(0, 0, -1), }; modelVisual3D.Content = directionalLight; viewport3D.Children.Add(viewport2DVisual3D); viewport3D.Children.Add(modelVisual3D); Grid组件.Children.Add(viewport3D); }