UI透视效果常见的就是绕x轴或y轴旋转,来达到近大远小的效果。正经要做透视UI的话肯定直接用透视相机,如果透视效果用的极少(就一张图)不改动相机类型才按这种思路进行。

最简单直接的想法就是把矩形的图片顶点扭成梯形,在PS里面扭一下很直观的就是透视效果嘛。但是在unity里面不行,一个矩形实际是两个三角面,改了顶点位置之后明显是能看出来扭曲的,所以要按照径向方向再切分,来削弱扭曲效果。

大概切8刀(分成9份)效果就还不错了,当然如果放一个正方形、圆形这种标准图形还是能明显看出来 需要再提高切分次数。

贴出主要思路 要用的话需要再补全一下

public class UIRawImagePerspective : BaseMeshEffect
{
[SerializeField]
private float m_DeltaRate = 0.1f; [SerializeField]
private int m_SplitCount = 8; // Properties 略 private Graphic m_Graphic;
private Vector3 m_LastRotation; protected override void OnEnable()
{
base.OnEnable();
m_LastRotation = transform.rotation.eulerAngles;
m_Graphic = this.GetComponent<Graphic>();
} private void Update()
{
if (!IsActive()) return;
Vector3 rotation = transform.rotation.eulerAngles;
if (rotation != m_LastRotation)
{
m_LastRotation = rotation;
m_Graphic.SetVerticesDirty();
}
} private override void ModifyMesh(VertexHelper vh)
{
if (!IsActive()) return;
if (m_SplitCount <= 0) return; Vector3 rotation = m_LastRotation;
if (rotation.x == 0 && rotation.y == 0) return;
if (vh.currentVertCount > 4)
{
// 不是单个矩形的不处理
return;
} // 这个函数是自定义的,因为直接用GetUIVertexStream()比较讨厌,写法也很别扭,实际逻辑就是取出数据
List<UIVertex> vertices = vh.GetUIVerticesEx();
List<int> indices = new List<int>();
UIVertex v; if (rotation.x != 0)
{
float left = vertices[0].position.x;
float right = vertices[2].position.x; Vector4 uvLeftBottom = vertices[0].uv0;
Vector4 uvRightBottom = vertices[3].uv0; float delta = Mathf.Sin((rotation.x / 180) * Mathf.PI) * m_DeltaRate;
float length = right - left; UIVertex tempBottom = vertices[0];
UIVertex tempTop = vertices[1];
vertices.Clear(); int parts = m_SplitCount + 1;
float topLeft = left + delta * length * 0.5f;
float bottomLeft = left - delta * length * 0.5f; for (int i = 0; i < m_SplitCount + 2; i++)
{
float partFactor = (float)i / parts;
float uvx = uvLeftBottom.x + partFactor * (uvRightBottom.x - uvLeftBottom.x); v = tempBottom;
v.position.x = bottomLeft + partFactor * length * (1 + delta);
v.uv0.x = uvx;
vertices.Add(v); v = tempTop;
v.position.x = topLeft + partFactor * length * (1 - delta);
v.uv0.x = uvx;
vertices.Add(v); if (i > 0)
{
int count = vertices.Count;
indices.Add(count - 4);
indices.Add(count - 3);
indices.Add(count - 1);
indices.Add(count - 1);
indices.Add(count - 2);
indices.Add(count - 4);
}
}
}
else
{
// 绕y轴旋转 换个方向再来一遍 略
} vh.Clear();
// 这个函数是自定义的,也是因为AddUIVertexStream()用起来比较讨厌
vh.SetFullData(vertices, indices);
}
}

unity UGUI 正交相机实现图片的透视旋转效果的更多相关文章

  1. Unity 3D 正交相机(Orthographic)

    1. Camera.aspect 表示摄像机显示区域的纵横比.宽高比,摄像机初始化的时候会默认设置成当前屏幕的宽高比,可以更改,也可以通过 Camera.ResetAspect 来重置. 2. Cam ...

  2. Unity UGUI实现鼠标拖动图片

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...

  3. Unity UGUI暂停按钮切换图片代码

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...

  4. Unity正交相机智能包围物体(组)方案

    Unity正交相机智能包围物体(组)方案 目录 Unity正交相机智能包围物体(组)方案 一.技术背景 二.相关概念 2.1 正交摄像机 2.2 正交相机的Size 2.3 相机的Aspect 2.4 ...

  5. Unity UGUI图文混排源码(三) -- 动态表情

    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...

  6. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  7. Unity UGUI图文混排源码(一)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  8. Unity UGUI实现图文混排

    目前在unity实现图文混排的好像都是通过自定义字体然后在文本获取字符的位置,用图片替换掉图片标签,这样对于支持英文来说,并没有什么影响.然后对于中文来说就是一个相当麻烦的事了,毕竟图文混排多用于游戏 ...

  9. Unity UGUI

    超详细的基础教程传送门:(持续更新中) Unity UGUI之Canvas&EventSystem:http://blog.csdn.net/qq992817263/article/detai ...

  10. Unity UGUI Layout自动排版组件用法介绍

    Unity UGUI布局组件 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...

随机推荐

  1. Multi-Modal Attention Network Learning for Semantic Source Code Retrieval 解读

    Multi-Modal Attention Network Learning for Semantic Source Code Retrieva Multi-Modal Attention Netwo ...

  2. 一:wince 开发环境

    1:下载相关文件,vs2008 可以自行搜索安装 链接:https://pan.baidu.com/s/1b2shwCqmc1o9x-zsy8CmeA 提取码:qing

  3. HTML超文本标记语言2

    二.基本标签 1.文件标签(结构) <html> 根标签 <head> <title>页面标题(标签)</title> </head> &l ...

  4. 自用 .net C# List集合和DataTable互转,可自定义表头

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  5. bash: pip3:未找到命令

    输入以下命令: 1 sudo apt-get install python3-pip 参考链接: https://www.cnblogs.com/banshaohuan/p/10963547.html

  6. Django:数据库驱动安装

    import pymysql pymysql.install_as_MySQLdb() 常见MySQL驱动介绍: MySQL-python:也就是MySQLdb.是对C语言操作MySQL数据库的一个简 ...

  7. 2023-08-04:村里面一共有 n 栋房子 我们希望通过建造水井和铺设管道来为所有房子供水。 对于每个房子 i,我们有两种可选的供水方案: 一种是直接在房子内建造水井 成本为 wells[i -

    2023-08-04:村里面一共有 n 栋房子 我们希望通过建造水井和铺设管道来为所有房子供水. 对于每个房子 i,我们有两种可选的供水方案: 一种是直接在房子内建造水井 成本为 wells[i - ...

  8. Spring Boot 最佳实践

    本文翻译自国外论坛 medium,原文地址:https://medium.com/@raviyasas/spring-boot-best-practices-for-developers-3f3bdf ...

  9. [nginx]lua读取请求体

    前言 nginx默认不读取请求体的数据,但可以通过$request_body内置变量来获取.$request_body存在内存中,如果它的字节大小超过nginx配置的client_body_buffe ...

  10. 高级SQL分析函数-窗口函数

    摘要:本文由葡萄城技术团队于博客园原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 SQL语句中,聚合函数在统计业务数据结果时起到了重要作用 ...