当前位置:网站首页>Unity - Fundamentals of 3D mathematics
Unity - Fundamentals of 3D mathematics
2022-07-28 21:00:00 【Small digital media members】
It took less than half a day today to get a preliminary understanding 3D Mathematical basis ! Enter tomorrow unity Game script writing !
One sentence per day :“ The sun on earth is just right The wind blows through the forest We were young at that time ”
Catalog
· overall situation ( The world ) Coordinate system :
· Local ( Model 、 object ) Coordinate system :
· Calculate the distance between two objects
Coordinate transformation principle
Conversion between world coordinates and local coordinates : Change global and local coordinates
Conversion between world coordinates and screen coordinates : Set world coordinates
· How to set the world coordinate position of an object by clicking the mouse
3D Coordinate system :
· overall situation ( The world ) Coordinate system :
A benchmark that describes the position and direction of all objects in the game scene
Hierrarchy In the view Position attribute by (1,2,3) It means that it is at... From the origin of the global coordinate system x There is... In the direction of the axis 1 Unit coordinates , stay y There is... In the direction of the axis 2 Unit coordinates , stay z There is... In the direction of the axis 3 Unit coordinates
When the game object Yes When the parent object , Its Hierrarchy In the view Position Is shown as Local coordinates , When the game object nothing When the parent object , Its Hierrarchy In the view Position Show Both local and global coordinates
· Local ( Model 、 object ) Coordinate system :
Every object has its own coordinate system , And move or rotate with the object
Model mesh The saved vertex coordinates are Coordinates in the local coordinate system
The coordinate system of the parent-child object is related , The child object will take a coordinate point of the parent object as its coordinate origin
· Camera coordinate system :
The coordinate system established according to the observation position and direction
Judge whether the object is in front of the camera , And the sequence of occlusion between objects
· Screen coordinate system :
Two dimensional coordinate system established on the screen , Used to describe the position of pixels on the screen , Where the origin position (0,0) In the lower left corner of the screen
vector
Used to describe physical quantities with two attributes of size and direction
Vector operation :
· Addition and subtraction
· Number multiplication
A vector is multiplied by a scalar
· Point multiplication
Multiply two vectors to get a scalar , The value is equal to the cosine of the angle between the two vectors multiplied by the length of the two vectors
· Cross riding
The cross multiplication of two vectors yields a new vector , The new vector is perpendicular to the original two vectors , And the length is equal to the sine of the angle multiplied by the length of the original vector
Vector3 class
Vector3 Member variables 
Vector3 Method 
Vector3 application :
· Calculate the distance between two objects
public class Distance : MonoBehaviour
{
// Start is called before the first frame update
public Transform objTrans1;
public Transform objTrans2;
void Start()
{
float dis = Vector3.Distance(objTrans1.position, objTrans2.position);
Debug.Log("Distance:" + dis);
}
// Update is called once per frame
void Update()
{
}
}(Debug.Log: Record the message to Unity Console )
Bind the script to the main camera in the scene Main Camera, And then again Inspector In the view objTrans1、objTrans2 Designated as 2 individual cube
function 、CTRL+SHIFT+C Call up the console
· Slow motion
Vector3.Lerp Method
The slow motion effect of moving an object from its current position to another position in the coordinate system
public class MoveToTarget : MonoBehaviour
{
public Transform endTrans;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.Lerp(transform.position, endTrans.position, Time.deltaTime);
}
}Time.deltaTime Express Update() Time taken to complete the previous frame on the method
Bind the script to Cube On , And then in it Inspector In view endTrans Designated as Cube(1)
Homogeneous coordinates
Put the original three-dimensional vector (x,y,z) Use four-dimensional vector (wx,wy,wz,w) To express
Purpose of introduction :
Better distinguish between vectors and points ;
The translation is uniformly expressed by matrix multiplication 、 rotate 、 Zoom transform
When the weight w=0 Time can be used to represent the point at infinity
Four yuan number
It contains a scalar component and a three-dimensional vector component , Four yuan number Q Write it down as Q=[w,(x,y,z)]
In the game object Transform In the component , Variable Transform.rotation Is the rotation of the object in the global coordinate system ; Variable Transform.localRotation Is the rotation of the object in the local coordinate system of the parent object
application : Control the rotation of objects through quaternions
Coordinate transformation principle
Conversion between world coordinates and local coordinates : Change global and local coordinates
ransform Component's Transform.TransformPoint Method can convert coordinate points from local coordinate system to global coordinate system ,Transform.InverseTransformPoint Method can convert coordinate points from global coordinate system to local coordinate system
Conversion between world coordinates and screen coordinates : Set world coordinates
stay Unity in , Because the screen picture is made by the camera (Camera) Presented , Therefore, the conversion between world coordinates and screen coordinates needs to take the camera as the medium
· How to set the world coordinate position of an object by clicking the mouse
newly build C# Script
public class ScreenToWorldPoint : MonoBehaviour
{
Vector3 screenPos;
// Start is called before the first frame update
void Start()
{
screenPos = Camera.main.WorldToScreenPoint(transform.position);
// Print coordinate information
Debug.Log(" Screen coordinates :" + screenPos);
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
// Get the position coordinates of the mouse
Vector3 mousePos = Input.mousePosition;
// Set the screen coordinates of the object z The axis value is assigned to the mouse coordinate z Axis
mousePos.z = screenPos.z;
// Change the screen coordinates into world coordinates
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
// Set object position
transform.position = worldPos;
}
}
}Bind script to Cube On the cube
When the run starts , The console will output this Cube The world coordinates of are converted into the coordinates of the screen ; When the mouse pointer is Game View , When left clicking , You can see Cube The position of will follow the position of the mouse pointer
After the world coordinates are converted into screen coordinates , The screen coordinates are z The axis is larger than 0, It means that the coordinate is in front of the camera , conversely , It means that the coordinate is behind the camera
边栏推荐
- Redis 3.0 source code analysis - data structure and object SDS list Dict
- Three deletion strategies and eviction algorithm of redis
- 【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】
- 图书馆借阅系统「建议收藏」
- Space shooting Lesson 16: props (Part 2)
- Nat experiment demonstration (Huawei switch equipment configuration)
- 网络各层性能测试
- dll反编译(反编译加密dll)
- Network layer performance test
- Record an error in runtime. Getruntime().Exec ("command")
猜你喜欢

How to balance security and performance in SQL?

JS chart scatter example

Explain the imported 3D model in unity

取色器实战(Qt含源码)

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

Explain the mobile control implementation of unity in detail

Explain rigid body and collider components in unity

Three deletion strategies and eviction algorithm of redis

Ask if you don't understand, and quickly become an advanced player of container service!

prometheus配置alertmanager完整过程
随机推荐
Understanding of C # delegate
蓝队入门之效率工具篇
Integrating database Ecology: using eventbridge to build CDC applications
Unity foundation 3- data persistence
H5 wechat shooting game source code
2 enjoy yuan mode
NAT实验演示(Huawei交换机设备配置)
[工具类] Map的util包, 常用 实体类转化为map等操作
阿里云 MSE 支持 Go 语言流量防护
What is data center? What value does the data center bring_ Light spot technology
Confusing knowledge points of software designer examination
取色器实战(Qt含源码)
mysql梳理复习内容--附思维导图
C language function program example (super complete)
Ask if you don't understand, and quickly become an advanced player of container service!
Unity foundation 4 common plug-ins
融合数据库生态:利用 EventBridge 构建 CDC 应用
融合数据库生态:利用 EventBridge 构建 CDC 应用
Introduction to redis I: redis practical reading notes
Explain the script data transmission and notification in unity