当前位置:网站首页>[question 008: what is UV in unity?]
[question 008: what is UV in unity?]
2022-07-02 02:22:00 【valaki】
Video Explanation
Unity Chinese vs UV The understanding of the
One 、 What is? UV
UV What is it? ?UV In fact, it is another definition of texture coordinate axis ;
For example, we usually say Transform Coordinates of , That is to say Transform Specific position in Cartesian coordinate system , The visual representation of the location is (x,y,z); So usually when we talk about the coordinates of objects , The subconscious mind will think (x,y,z);
Why do textures have coordinates ?
For example, this picture below :
From this picture we can view its information , Know this picture size How much is the , What's the size , When was it created , But we don't care about the coordinates of this picture , Because we don't use its coordinates at this time , But it does exist .
When we put the above texture on a Quad When it's up there , You need to use the coordinates of the texture : Look at the following effect :
Above is the picture Unity The effect of operation , Among them is 5 The two are equal in size Quad, But they use the same texture to get different results ; This is because every Quad Of the texture obtained UV The coordinate ranges are different , Lead to Quad The rendering results are different ;
Two 、 The example analysis
UV The meaning of coordinates lies in a mapping relationship with object coordinates ;
For example, the texture size in the above figure is 512x512, We can't use 512*512 This size to map , If we are mapping to objects of the same size , They are 1 Yes 1 The relationship between , That is, the coordinates and texture of each object UV The coordinate points correspond to ; Otherwise, the mapping process is very troublesome ;
So what are object coordinates ?
Look at one below Quad Pictures of the :
The object in the above figure Pivot and Center All in the center , In wireframe mode, you can see that the object has 4 vertices ,2 Triangles ; To put all or part of a texture completely in this quad It shows correctly , It needs to be used uv coordinate , The following steps are required :
- To find the Quad Rendering of triangles , Is it counterclockwise or clockwise ; You will get a picture of the front or the back
- To find the Quad Of 4 individual UV The order of coordinates , That is to confirm uv[0]、uv[1]、uv[2]、uv[3] And vertex coordinates vertext[0]、vertext[1]、vertext[2]、vertext[3] Correspondence of , Only the correct correspondence can get the correct result ; You can use it here UV Any reasonable range of coordinates 4 individual uv Coordinate points to correspond to vertex coordinates , In this way, some or all of the texture can be rendered to quad On ;
- uv The value range of coordinates will be converted to 0-1 Between ; such as 512 The size picture range will be changed to :uv(0/512,0/512)-(512/512,512/512) Value range of , Convert according to image size ;
- Finally, reset the object UV Coordinates are OK ;
UV Is another name for texture coordinates ,UV The coordinates are xy coordinate , It is only for the convenience of describing the differences in the process that this statement is made ; So deal with uv Coordinates deal with Cartesian coordinates ; You can think of a texture as the first quadrant in a Cartesian coordinate system , In the lower left corner (0,0) coordinate , The maximum value range of the first quadrant of this coordinate is 0-1 Between ;
The mapping between texture and object is the mapping relationship between texture coordinates and object coordinates , The correct mapping of texture coordinates can get the correct appearance of the object .
3、 ... and 、 Source code
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ValakiShowUvPartDemos : MonoBehaviour
{
enum ShowUVPartType
{
None=0,
LeftTop,
LeftBottom,
Center,
RightTop,
RightBottom
};
List<Vector2> uvs;
Vector2[] backUv;
Mesh mesh;
[SerializeField] ShowUVPartType showUVPartType;
[SerializeField] bool isPlayRandomUvPart;
ShowUVPartType currentUvPartType = ShowUVPartType.None;
private void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
uvs = mesh.uv.ToList();
backUv = mesh.uv;
}
float totalTime;
private void LateUpdate()
{
if (isPlayRandomUvPart)
{
totalTime += Time.deltaTime;
if(totalTime >= 0.7f)
{
totalTime = 0;
int random = Random.Range(1, 6);
showUVPartType = (ShowUVPartType)random;
}
}
if (currentUvPartType != showUVPartType)
{
currentUvPartType = showUVPartType;
switch (showUVPartType)
{
case ShowUVPartType.Center:
mesh.SetUVs(0, backUv);
break;
case ShowUVPartType.LeftBottom:
ShowLeftBottomUvPart();
break;
case ShowUVPartType.LeftTop:
ShowLeftTopUvPart();
break;
case ShowUVPartType.RightBottom:
ShowRightBottomUvPart();
break;
case ShowUVPartType.RightTop:
ShowRightTopUvPart();
break;
}
}
}
void ShowLeftTopUvPart()
{
// Left - The lower right - Top left - The upper right
uvs[0] = new Vector2(0, 0.75f);
uvs[1] = new Vector2(0.25f, 0.75f);
uvs[2] = new Vector2(0, 1);
uvs[3] = new Vector2(0.25f, 1);
mesh.SetUVs(0, uvs);
}
void ShowRightTopUvPart()
{
uvs[0] = new Vector2(0.75f, 0.75f);
uvs[1] = new Vector2(1, 0.75f);
uvs[2] = new Vector2(0.75f, 1);
uvs[3] = new Vector2(1, 1);
mesh.SetUVs(0, uvs);
}
void ShowLeftBottomUvPart()
{
uvs[0] = new Vector2(0, 0);
uvs[1] = new Vector2(0.25f, 0);
uvs[2] = new Vector2(0, 0.25f);
uvs[3] = new Vector2(0.25f, 0.25f);
mesh.SetUVs(0, uvs);
}
void ShowRightBottomUvPart()
{
uvs[0] = new Vector2(0.75f, 0);
uvs[1] = new Vector2(1, 0);
uvs[2] = new Vector2(0.75f, 0.25f);
uvs[3] = new Vector2(1, 0.25f);
mesh.SetUVs(0, uvs);
}
}
Conclusion :
Don't ask me how many grades , I'm just a jerk 【valaki】
边栏推荐
- STM32F103 - two circuit PWM control motor
- 大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
- OpenCASCADE7.6编译
- C write TXT file
- How does MySQL solve the problem of not releasing space after deleting a large amount of data
- 2022 Q2 - résumé des compétences pour améliorer les compétences
- Sword finger offer 47 Maximum value of gifts
- How to turn off debug information in rtl8189fs
- Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
- JPM 2021 most popular paper released (with download)
猜你喜欢
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
As a software testing engineer, will you choose the bank post? Laolao bank test post
[liuyubobobo play with leetcode algorithm interview] [00] Course Overview
MySQL主从延迟问题怎么解决
Leetcode face T10 (1-9) array, ByteDance interview sharing
Medical management system (C language course for freshmen)
How to build and use redis environment
query词权重, 搜索词权重计算
JPM 2021 most popular paper released (with download)
leetcode373. 查找和最小的 K 对数字(中等)
随机推荐
设置状态栏颜色
trading
Software No.1
剑指 Offer 42. 连续子数组的最大和
2022安全员-C证考试题及模拟考试
Provincial election + noi Part IV graph theory
[pit] how to understand "parameter fishing"
剑指 Offer 29. 顺时针打印矩阵
Leetcode face T10 (1-9) array, ByteDance interview sharing
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
Kibana操控ES
query词权重, 搜索词权重计算
pytest 测试框架
Software development life cycle -- waterfall model
剑指 Offer II 031. 最近最少使用缓存
[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)
flutter 中間一個元素,最右邊一個元素
How to use redis ordered collection
Deployment practice and problem solving of dash application development environment based on jupyter Lab
Cesium dynamic diffusion point effect