最基本的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="" 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(, , ),
};
viewport3D.Camera = perspectiveCamera; Viewport2DVisual3D viewport2DVisual3D = new Viewport2DVisual3D();
RotateTransform3D rotateTransform3D = new RotateTransform3D();
AxisAngleRotation3D axisAngleRotation3D = new AxisAngleRotation3D()
{
Angle = ,
Axis = new Vector3D(, , ),
};
rotateTransform3D.Rotation = axisAngleRotation3D;
viewport2DVisual3D.Transform = rotateTransform3D; MeshGeometry3D meshGeometry3D = new MeshGeometry3D()
{
Positions = new Point3DCollection(new Point3D[] { new Point3D(-, , ), new Point3D(-, -, ), new Point3D(, -, ), new Point3D(, , ) }),
TextureCoordinates = new PointCollection { new Point(, ), new Point(, ), new Point(, ), new Point(, ) },
TriangleIndices = new Int32Collection(new int[] { , , , , , }),
};
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(, , ),
Direction = new Vector3D(0, 0, -1),
};
modelVisual3D.Content = directionalLight; viewport3D.Children.Add(viewport2DVisual3D);
viewport3D.Children.Add(modelVisual3D); Grid组件.Children.Add(viewport3D);
}

引伸一下,按照1:1大小显示原始组件,写入原始宽度和高度。显示出来的是原始UserControl的大小

private void Init_Draw(FrameworkElement element,double width,double height)
{
double Mesh_Width = width / ;
double Mesh_Height = height / ; Viewport3D viewport3D = new Viewport3D();
viewport3D.Width = width * ; PerspectiveCamera perspectiveCamera = new PerspectiveCamera();
double fieldOfViewInRadians = perspectiveCamera.FieldOfView * (Math.PI / 180.0);
var z = (0.5 * viewport3D.Width) / Math.Tan(0.5 * fieldOfViewInRadians);
perspectiveCamera.Position = new Point3D(, , z); perspectiveCamera.LookDirection = new Vector3D(, , -);
viewport3D.Camera = perspectiveCamera; Viewport2DVisual3D viewport2DVisual3D = new Viewport2DVisual3D();
RotateTransform3D rotateTransform3D = new RotateTransform3D();
axisAngleRotation3D = new AxisAngleRotation3D()
{
Angle = ,
Axis = new Vector3D(, , ),
};
rotateTransform3D.Rotation = axisAngleRotation3D;
viewport2DVisual3D.Transform = rotateTransform3D; MeshGeometry3D meshGeometry3D = new MeshGeometry3D()
{
Positions = new Point3DCollection(new Point3D[] {
new Point3D(-Mesh_Width, Mesh_Height, ),
new Point3D(-Mesh_Width, -Mesh_Height, ),
new Point3D(Mesh_Width, -Mesh_Height, ),
new Point3D(Mesh_Width, Mesh_Height, )
}
),
TextureCoordinates = new PointCollection { new Point(, ), new Point(, ), new Point(, ), new Point(, ) },
TriangleIndices = new Int32Collection(new int[] { , , , , , }),
};
viewport2DVisual3D.Geometry = meshGeometry3D; DiffuseMaterial diffuseMaterial = new DiffuseMaterial();
Viewport2DVisual3D.SetIsVisualHostMaterial(diffuseMaterial, true);
viewport2DVisual3D.Material = diffuseMaterial; viewport2DVisual3D.Visual = element;
element.MouseDown += Element_MouseDown; ModelVisual3D modelVisual3D = new ModelVisual3D();
DirectionalLight directionalLight = new DirectionalLight()
{
Color = Color.FromRgb(, , ),
Direction = new Vector3D(, , -),
};
modelVisual3D.Content = directionalLight; viewport3D.Children.Add(viewport2DVisual3D);
viewport3D.Children.Add(modelVisual3D); MainGrid.Children.Add(viewport3D);
}
05-11 21:57