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. Kali下载安装以及基础配置

    Kali官网:Kali Linux | Penetration Testing and Ethical Hacking Linux Distribution Kali下载地址:Get Kali | K ...

  2. 每日一题 力扣 445 https://leetcode.cn/problems/add-two-numbers-ii/

    可以直接用栈去做就行,逆序想到栈的做法 然后算完一个就直接赋值给答案数组  我用的是常见 public ListNode addTwoNumbers(ListNode l1, ListNode l2) ...

  3. 配置http协议访问Harbor镜像仓库

    解决http: server gave HTTP response to HTTPS client问题,此问题在上传与下载时均可能出现. 由于docker镜像拉取与推送服务使用的是https协议,但是 ...

  4. 360OS张焰:AI视觉在教育中的应用

    11月24日,由即构科技主办的2020GET大会教育科技分论坛在北京成功召开,来自叮咚课堂.小冰.360OS.蕃茄田艺术.即构科技的6位资深教育/科技大咖,在论坛上进行深度分享. 以下为360OSAI ...

  5. Node: Module not found: Can't resolve 'xlsx'

    报错信息 解决方案 npm install xlsx --save 参考链接 https://github.com/securedeveloper/react-data-export/issues/8 ...

  6. OpenAI API访问速度不佳?试试用Vercel来加速!

    前言 众所周知,使用openAI API在国内访问体验并不佳,经常遇到访问较慢或者访问失败的问题.本文着重讲讲怎么解决这个问题,让我们日常开发和使用能够更方便的体验到AI带来的便利 为了帮大家省钱,也 ...

  7. Hugging News #0731: 新课程重磅发布、用户交流群邀请你加入、真实图像编辑方法 LEDTIS 来啦!

    每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...

  8. 更快的训练和推理: 对比 Habana Gaudi®2 和英伟达 A100 80GB

    通过本文,你将学习如何使用 Habana Gaudi2 加速模型训练和推理,以及如何使用 Optimum Habana 训练更大的模型.然后,我们展示了几个基准测例,包括 BERT 预训练.Stabl ...

  9. [论文阅读] 颜色迁移-Illuminant Aware Gamut-Based

    [论文阅读] 颜色迁移-Illuminant Aware Gamut-Based 文章: [Illuminant Aware Gamut-Based Color Transfer], [python代 ...

  10. 8、Spring之基于注解的自动装配

    8.1.场景模拟 8.1.1.UserDao接口及实现类 package org.rain.spring.dao; /** * @author liaojy * @date 2023/8/5 - 18 ...