首页 > 其他分享 >WPF GeneralTransform3DTo2D TransformToAncestor

WPF GeneralTransform3DTo2D TransformToAncestor

时间:2024-12-29 18:21:38浏览次数:6  
标签:Windows System transform TransformToAncestor GeneralTransform3DTo2D new using WP

//xaml
<Window x:Class="WpfApp113.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp113"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="myGrid">
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_000"/>
            </TextBlock.RenderTransform>
            (-1,-1,-1)
        </TextBlock>
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_001"/>
            </TextBlock.RenderTransform>
            (-1,-1,1)
        </TextBlock>
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_010"/>
            </TextBlock.RenderTransform>
            (-1,-1,-1)
        </TextBlock>
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_011"/>
            </TextBlock.RenderTransform>
            (-1,1,1)
        </TextBlock>
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_100"/>
            </TextBlock.RenderTransform>
            (1,-1,-1)
        </TextBlock>
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_101"/>
            </TextBlock.RenderTransform>
            (1,-1,1)
        </TextBlock>
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_110"/>
            </TextBlock.RenderTransform>
            (1,1,-1)
        </TextBlock>
        <TextBlock Panel.ZIndex="1"
                   IsHitTestVisible="False">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="t_111"/>
            </TextBlock.RenderTransform>
            (1,1,1)
        </TextBlock>
        <Viewport3D Panel.ZIndex="0">
            <Viewport3D.Camera>
                <PerspectiveCamera Position="3,3,4"
                                   LookDirection="-1,-1,-1"
                                   FieldOfView="60"/>
            </Viewport3D.Camera>
            <Viewport3D.Children>
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <DirectionalLight Direction="-0.3,-0.4,-0.5"/>
                    </ModelVisual3D.Content>
                </ModelVisual3D>
                <ModelUIElement3D x:Name="Cube">
                    <ModelUIElement3D.Transform>
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D x:Name="rotationY"
                                                     Axis="0,1,0"
                                                     Angle="0"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </ModelUIElement3D.Transform>
                    <ModelUIElement3D.Model>
                        <GeometryModel3D x:Name="ob_cube">
                            <GeometryModel3D.Material>
                                <DiffuseMaterial>
                                    <DiffuseMaterial.Brush>
                                        <SolidColorBrush Color="Orange"
                                                         x:Name="cubeBrush"/>
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D x:Name="me_cube2"
                                                Positions="1,1,-1 1,-1,-1 -1,-1,-1 -1,1,-1 1,1,1 -1,1,1 -1,-1,1
1,-1,1 1,1,-1 1,1,1 1,-1,1 1,-1,-1 1,-1,-1 1,-1,1
-1,-1,1 -1,-1,-1 -1,-1,-1 -1,-1,1 -1,1,1 -1,1,-1 1,1,1
1,1,-1 -1,1,-1 -1,1,1"
                                                TriangleIndices="0 1 2 0 2 3 4 5 6 4 6 7 8 9 10 8 10 11 12 13 14
12 14 15 16 17 18 16 18 19 20 21 22 20 22 23"
                                                TextureCoordinates="0,1 0,0 1,0 1,1 1,1 -0,1 0,-0 1,0 1,1 -0,1
0,-0 1,0 1,0 1,1 -0,1 0,-0 -0,0 1,-0 1,1 0,1
1,-0 1,1 0,1 -0,0"/>
                            </GeometryModel3D.Geometry>
                        </GeometryModel3D>
                    </ModelUIElement3D.Model>
                </ModelUIElement3D>
            </Viewport3D.Children>
        </Viewport3D>
    </Grid>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="rotationY"
                                     Storyboard.TargetProperty="Angle"
                                     From="0"
                                     To="360"
                                     Duration="0:0:12"
                                     RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp113
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }

        static TimeSpan lastRenderTime = new TimeSpan();
        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (lastRenderTime == ((RenderingEventArgs)e).RenderingTime)
            {
                return;
            }
            lastRenderTime = ((RenderingEventArgs)e).RenderingTime;

            GeneralTransform3DTo2D transform = Cube.TransformToAncestor(myGrid);

            Point p = transform.Transform(new Point3D(-1, -1, -1));
            t_000.X = p.X;
            t_000.Y = p.Y;


            p = transform.Transform(new Point3D(-1, -1, 1));
            t_001.X = p.X;
            t_001.Y = p.Y;

            p = transform.Transform(new Point3D(-1, 1, -1));
            t_010.X = p.X;
            t_010.Y = p.Y;

            p = transform.Transform(new Point3D(-1, 1, 1));
            t_011.X = p.X;
            t_011.Y = p.Y;

            p = transform.Transform(new Point3D(1, -1, -1));
            t_100.X = p.X;
            t_100.Y = p.Y;

            p = transform.Transform(new Point3D(1, -1, 1));
            t_101.X = p.X;
            t_101.Y = p.Y;

            p = transform.Transform(new Point3D(1, 1, -1));
            t_110.X = p.X;
            t_110.Y = p.Y;

            p = transform.Transform(new Point3D(1, 1, 1));
            t_111.X = p.X;
            t_111.Y = p.Y;
        }
    }
}

 

 

 

 

 

 

 

 

 

 

标签:Windows,System,transform,TransformToAncestor,GeneralTransform3DTo2D,new,using,WP
From: https://www.cnblogs.com/Fred1987/p/18639339

相关文章

  • WPF ModelVisual3D Viewport2DVisual3D
    <Windowx:Class="WpfApp110.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF Viewport3D ModelVisual3D ModelUIElement3D GeometryModel3D
    //xaml<Windowx:Class="WpfApp109.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF Model3DGroup
    <Windowx:Class="WpfApp108.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF ViewPort3D ModelVisual3D GeometryModel3D MeshGeometry3D Positions Normals
    <Windowx:Class="WpfApp107.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • wpf DataGrid 内容绑定动态资源方法
    先讲故事:系统需要区分语言环境,有中文环境与英文环境。然后有一列用户角色列,用户分为管理员(admin)与普通用户(users)。假设管理员用户角色ID为1,普通用户为2.用户角色编码字段名:UseRoleCode。当数据加到到DataGrid 之后,这是切换语言,需要根据当前的语言显示中文或者英文,再不......
  • WPF Viewport3D Model3DGroup GeometryModel3D ModelVisual3D PerspectiveCamera Dir
     Vector3DCollectionPoint3DCollectionPointCollectionInt32CollectionLinearGradientBrushDiffuseMaterial  RotateTransform3DAxisAngleRotation3D usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threadi......
  • WPF DrawingImage DrawingGroup Geometry in 2D acted as 3D
    <Windowx:Class="WpfApp102.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • 【WPF】INotifyCollectionChanged 、INotifyPropertyChanged 以及实现了二者的Observa
    INotifyCollectionChanged在WPF中,INotifyCollectionChanged是一个接口,用于实现集合(如列表)变化时通知UI更新的机制。这个接口特别常用于数据绑定中,尤其是当集合的内容发生变化时,WPF能够自动更新绑定的UI组件。1.接口概述INotifyCollectionChanged是.NET中System.Collectio......
  • WPF ComboBox multiselect via ControlTemplate of ComboxItem
    <Windowx:Class="WpfApp99.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF DatagridRow style with ControlTemplate
    <Windowx:Class="WpfApp100.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......