WPF 2D纹理的准确映射
TextureCoordinates定义了如何将一副2D纹理映射到所建立的3D网格上,TextureCoordinates为Positions集合中的每一个3D顶点提供了一个2D顶点。
映射时方向确定比较麻烦,需要3D每个面映射都为正确的方向,在baidu上找了很多,映射基本是乱的。
通过归纳测试,有了准确的参数,需要的可参考使用,不必再费劲计算。
里面注销掉的部分,可以恢复并注销调自动转动部分,为手动点击对象转动。
通过VisualBrush写入的 image可以换为UserControl等组件。
代码不长,就100多行,仅供参考,如有错误,自行修正。
MainWindow.xaml
<Window x:Class="Test3.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"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1024">
<Grid x:Name="main">
<Button Content="纵向" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="横向" HorizontalAlignment="Left" Margin="10,48,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<Button Content="纵横" HorizontalAlignment="Left" Margin="10,85.74,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
<Button Content="横纵" HorizontalAlignment="Left" Margin="10,124.5,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_3"/> </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D; namespace Test3
{
public partial class MainWindow : Window
{
private AxisAngleRotation3D axisAngleRotation3D; public MainWindow()
{
InitializeComponent();
main.MouseDown += Main_MouseDown; init_draw();
} public void init_draw()
{
Point3DCollection[] positions = new Point3DCollection[6]
{
new Point3DCollection(new Point3D[] {
new Point3D(0.5, 0.5, -0.5), new Point3D(- 0.5, 0.5, -0.5), new Point3D(- 0.5, 0.5, 0.5),
new Point3D(- 0.5, 0.5, 0.5),new Point3D(0.5, 0.5, 0.5), new Point3D(0.5, 0.5, -0.5)
}),
new Point3DCollection(new Point3D[] {
new Point3D(-0.5,0.5,-0.5),new Point3D(-0.5,-0.5,-0.5), new Point3D(-0.5,-0.5,0.5),
new Point3D(-0.5,-0.5,0.5),new Point3D(-0.5,0.5,0.5),new Point3D(-0.5,0.5,-0.5)
}),
new Point3DCollection(new Point3D[] {
new Point3D(-0.5,-0.5,0.5),new Point3D(0.5,-0.5,0.5),new Point3D(0.5,0.5,0.5),
new Point3D(0.5,0.5,0.5),new Point3D(-0.5,0.5,0.5), new Point3D(-0.5,-0.5,0.5)
}),
new Point3DCollection( new Point3D[] {
new Point3D(-0.5,-0.5,-0.5),new Point3D(-0.5,0.5,-0.5),new Point3D(0.5,0.5,-0.5),
new Point3D(0.5,0.5,-0.5), new Point3D(0.5,-0.5,-0.5),new Point3D(-0.5,-0.5,-0.5)
}),
new Point3DCollection( new Point3D[] {
new Point3D(-0.5,-0.5,-0.5), new Point3D(0.5,-0.5,-0.5), new Point3D(0.5,-0.5,0.5),
new Point3D(0.5,-0.5,0.5),new Point3D(-0.5,-0.5,0.5), new Point3D(-0.5,-0.5,-0.5)
}),
new Point3DCollection( new Point3D[] {
new Point3D(0.5,-0.5,-0.5),new Point3D(0.5,0.5,-0.5),new Point3D(0.5,0.5,0.5),
new Point3D(0.5,0.5,0.5), new Point3D(0.5,-0.5,0.5), new Point3D(0.5,-0.5,-0.5)
})
}; PointCollection[] texturepoints = new PointCollection[6]
{
new PointCollection {new Point(1,0), new Point(0,0), new Point(0,1), new Point(0,1), new Point(1,1),new Point(1,0) },
new PointCollection { new Point(0,0),new Point(0,1),new Point(1,1), new Point(1,1),new Point(1,0),new Point(0,0) },
new PointCollection { new Point(0,1), new Point(1,1),new Point(1,0), new Point(1,0),new Point(0,0),new Point(0,1) },
new PointCollection { new Point(1,1), new Point(1,0),new Point(0,0), new Point(0,0), new Point(0,1), new Point(1,1) },
new PointCollection { new Point(0,1), new Point(1,1), new Point(1,0),new Point(1,0), new Point(0,0), new Point(0,1)},
new PointCollection{ new Point(1,1), new Point(1,0),new Point(0,0),new Point(0,0), new Point(0,1),new Point(1,1)}
}; string[] filename = new string[6] //430251
{
"pack://application:,,,/Images/4.jpg",
"pack://application:,,,/Images/3.jpg",
"pack://application:,,,/Images/0.jpg",
"pack://application:,,,/Images/2.jpg",
"pack://application:,,,/Images/5.jpg",
"pack://application:,,,/Images/1.jpg"
}; Viewport3D viewport3D = new Viewport3D(); PerspectiveCamera perspectiveCamera = new PerspectiveCamera()
{
Position = new Point3D(0, 0, 3),
LookDirection = new Vector3D(0, 0, -3),
FieldOfView = 50,
UpDirection = new Vector3D(0, 1, 0),
FarPlaneDistance = 20,
NearPlaneDistance = 0,
};
viewport3D.Camera = perspectiveCamera; ModelVisual3D modelVisual3D = new ModelVisual3D();
Model3DGroup model3DGroup = new Model3DGroup(); for (int i = 0; i < 6; i++)
{
GeometryModel3D geometryModel3D = new GeometryModel3D(); DiffuseMaterial diffuse = new DiffuseMaterial();
VisualBrush visualBrush = new VisualBrush();
Image image = new Image();
image.Source = new BitmapImage(new Uri(filename[i]));
visualBrush.Visual = image;
diffuse.Brush = visualBrush;
geometryModel3D.Material = diffuse; MeshGeometry3D meshGeometry3D = new MeshGeometry3D()
{
Positions = positions[i],
TextureCoordinates = texturepoints[i],
};
geometryModel3D.Geometry = meshGeometry3D;
model3DGroup.Children.Add(geometryModel3D);
}
AmbientLight ambientLight = new AmbientLight();
model3DGroup.Children.Add(ambientLight); modelVisual3D.Content = model3DGroup;
viewport3D.Children.Add(modelVisual3D); main.Children.Add(viewport3D); /*
axisAngleRotation3D = new AxisAngleRotation3D(new Vector3D(1, 1, 0), 0);
RotateTransform3D rotateTransform3D = new RotateTransform3D(axisAngleRotation3D);
rotateTransform3D.Rotation = axisAngleRotation3D;
modelVisual3D.Transform = rotateTransform3D;
*/
// 如恢复上面,以下可以注销
axisAngleRotation3D = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0);
RotateTransform3D rotate = new RotateTransform3D(axisAngleRotation3D);
modelVisual3D.Transform = rotate;
DoubleAnimation animation = new DoubleAnimation();
animation.From = 0;
animation.To = 360;
animation.Duration = new Duration(TimeSpan.FromSeconds(10.0));
animation.RepeatBehavior = RepeatBehavior.Forever;
NameScope.SetNameScope(main, new NameScope());
main.RegisterName("cubeaxis", axisAngleRotation3D);
Storyboard.SetTargetName(animation, "cubeaxis");
Storyboard.SetTargetProperty(animation, new PropertyPath(AxisAngleRotation3D.AngleProperty));
Storyboard RotCube = new Storyboard();
RotCube.Children.Add(animation);
RotCube.Begin(main);
} double tmp = 0; private void Main_MouseDown(object sender, MouseButtonEventArgs e)
{
// 点击对象手动转动
/*
tmp = axisAngleRotation3D.Angle;
if (e.LeftButton == MouseButtonState.Pressed)
{
tmp -= 5;
}
if (e.RightButton == MouseButtonState.Pressed)
{
tmp += 5;
}
axisAngleRotation3D.Angle = tmp;
*/
} private void Button_Click(object sender, RoutedEventArgs e)
{
axisAngleRotation3D.Axis = new Vector3D(1,0,0);
axisAngleRotation3D.Angle = 0;
} private void Button_Click_1(object sender, RoutedEventArgs e)
{
axisAngleRotation3D.Axis = new Vector3D(0,1,0);
axisAngleRotation3D.Angle = 0;
} private void Button_Click_2(object sender, RoutedEventArgs e)
{
axisAngleRotation3D.Axis = new Vector3D(1, 1, 0);
axisAngleRotation3D.Angle = 0;
} private void Button_Click_3(object sender, RoutedEventArgs e)
{
axisAngleRotation3D.Axis = new Vector3D(0, 1, 1);
axisAngleRotation3D.Angle = 0;
}
}
}
下载:https://pan.baidu.com/s/1yJGS1_jJPL68nPX28NTl4g
WPF 2D纹理的准确映射的更多相关文章
- DirectX11--深入理解与使用2D纹理资源
前言 写教程到现在,我发现有关纹理资源的一些解说和应用都写的太过分散,导致连我自己找起来都不方便.现在决定把这部分的内容整合起来,尽可能做到一篇搞定所有2D纹理相关的内容,其中包括: DDSTextu ...
- Unity牛逼的2D纹理功能
[Unity牛逼的2D纹理功能] 1.可直接将贴图生成成为Cubemap. 2.自动生成Mipmap. 3.查看纹理被当前场景哪些对象引用.在Project窗口中,右击图像,选择 参考:file:// ...
- Cg入门21:Fragment shader - 2D纹理採样
体纹理:是啥? tex2D 曾经仅仅能在Fragment程序中纹理採样 UV坐标系:事实上点为左下角,范围为[0,1].U为x轴,V为y轴 watermark/2/text/aHR0cDovL2Jsb ...
- WPF属性与特性的映射(TypeConverter)
1,定义一个类 public class Human { public string Name { get; set; } public Human Child { get; set; } } 2在X ...
- WPF 2D图形 Shape入门(一)--Shape
本文是篇WPF Shape的入门文章 Shape 首先看看shape的继承链关系: 一个Shape具有哪些重要属性: 属性 说明 DefiningGeometry 默认的几何形状 RenderedGe ...
- WPF 2D 碰撞检测
var intersectionDetail = path1.Data.FillContainsWithDetail(path2.Data); if (intersectionDetail ! ...
- 从位图图像中读取2D纹理(C ++,OpenGL)
一共有2个.cpp文件和1个.h头文件 步骤: 需要安装GLUT,因为GLUT是第三方库,即它不是OpenGL的一部分.因此,它不是Windows系统API的一部分,因此不属于标准Windows SD ...
- 在CG/HLSL中访问着色器属性(Properties)
在CG/HLSL中访问着色器属性 Shader在Properties块中访问材质属性.如果你想在一个着色程序中访问一些属性,你需要声明一个Cg/HLSL具有相同的名称和一个匹配的类型的变量. Prop ...
- WebGL高级编程:开发Web3D图形 PDF(中文版带书签)
WebGL高级编程:开发Web3D图形 目录 WebGL简介11.1 WebGL基础11.2 浏览器3D图形吸引人的原因21.3 设计一个图形API31.3.1 即时模式API31.3.2 保留模式A ...
随机推荐
- 用struts的action运行jsp页面
struts是开源框架.使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间.如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,struts是一个不错的 ...
- 变分(图)自编码器不能直接应用于下游任务(GAE, VGAE, AE, VAE and SAE)
自编码器是无监督学习领域中一个非常重要的工具.最近由于图神经网络的兴起,图自编码器得到了广泛的关注.笔者最近在做相关的工作,对科研工作中经常遇到的:自编码器(AE),变分自编码器(VAE),图 ...
- 01.Markdown学习
Markdown学习 一.标题 在想要设置为标题的文字前面加#来表示(#后面有空格) 一个#是一级标题,二个#是二级标题,以此类推.支持六级标题. 示例: # 这是一级标题 ## 这是二级标题 ### ...
- Crypto++ AES 加密解密流程
// aesdemo.cpp : 定义控制台应用程序的入口点. // #include <stdio.h>#include <tchar.h>#include <iost ...
- 怎样在LaTeX中使用中文
因为疫情在家中上课,作业提交都必须使用PDF.反正时间充裕,不如趁机回顾一下LaTeX的使用. 之前一直用的是Vimtex,但是感觉还是不太方便,于是改用了Texpad.Texpad的强大之处在于它支 ...
- 个人工作用SQL短句,不定时更新
表字段操作 --一.修改字段默认值 alter table 表名 drop constraint 约束名字 ------说明:删除表的字段的原有约束 alter table 表名 add constr ...
- 学习ASP.NET Core(11)-解决跨域问题与程序部署
上一篇我们介绍了系统日志与测试相关的内容并添加了相关的功能:本章我们将介绍跨域与程序部署相关的内容 一.跨域 1.跨域的概念 1.什么是跨域? 一个请求的URL由协议,域名,端口号组成,以百度的htt ...
- RocketMQ启动
下载RocketMQ解压启动 > unzip rocketmq-all-4.4.0-source-release.zip > cd rocketmq-all-4.4.0/ > mvn ...
- elasticsearch中query和filter的区别
参考博客来自: https://mp.weixin.qq.com/s/tiiveCW3W-oDIgxvlwsmXA?utm_medium=hao.caibaojian.com&utm_sour ...
- 【SpringBoot MQ 系列】RabbitListener 消费基本使用姿势介绍
[MQ 系列]RabbitListener 消费基本使用姿势介绍 之前介绍了 rabbitmq 的消息发送姿势,既然有发送,当然就得有消费者,在 SpringBoot 环境下,消费可以说比较简单了,借 ...