当前位置:网站首页>Explain the camera in unity and its application
Explain the camera in unity and its application
2022-07-28 20:54:00 【Dream small day young】
Preface
Unity in , Show players the wonderful game world through the camera . Developers can fix the player's perspective through the camera , Render a specific picture to present to the player . This paper introduces Camera Relevant parameters of components and some application examples and misunderstandings of how to use the camera .
Catalog
- Preface
- One 、 Introduction to camera components and parameters
- 1.ClearFlags: Clear the mark ( background )
- 2.Culling Mask: Select a render layer
- 3.Projection: Projection mode ( perspective ( Default ) and orthogonal )
- 4.Clipping Planes: Clipping plane
- 5.Viewport Rect: View rectangle ( Camera position width and height )
- 6.Depth: depth ( Camera rendering order )
- 7.Occlusion Culling: Shielding elimination switch
- 8. Other properties , Temporarily unavailable , I won't go into that
- Two 、 Example application of camera
- 3、 ... and 、 Summary and references
Front row reminders : This article only represents personal views , For exchange and learning , If you have different opinions, please comment , The author must study hard , Day day up .
When reading this article , If there is something you don't understand , It is recommended to watch the references listed in this article for comparison .
Unity edition [2019.4.10f1] Dream small day young & Prohibited reproduced 2022.4.3
Video Explanation :
Detailed explanation Unity The video camera _BiLiBiLi
One 、 Introduction to camera components and parameters
When you create a scene ,Unity It will automatically create one Main Camera, If you delete it , Then your world will be dark . We can do it in Hierarchy Window right , Create a Camera, Then you will find , Your world is bright again .
When we build Camera, Then you will find , It mounts three components , Namely Transform、Camera and Audio Listener, among Transform It is owned by every game object ,Camera and Audio Listener The ears and eyes of this camera . Now let's introduce in detail about Camera Component related parameters .
Create a new one Camera, Automatically add these three components .
1.ClearFlags: Clear the mark ( background )
When the camera renders the view , Will cache color and depth information , The part of the drawn image that is not drawn is blank , By default, the color of the sky box will be displayed .
- Sky Box: Use the sky box to fill the blank space , Render settings are used by default [Window-Rendering-Lighting Settings] Sky box in , If the rendering settings do not set the sky box , Will use the background color .
- Solid Color: Fill the blank with the camera background color
- Depth Only: No fill
It's easy to understand , Parts other than painting are filled with default black , If there are other cameras at this time (Depth Lower than current ), Fill in other camera pictures .
- Don’t Clear: Do not clear the color and depth cache .( Not much use here , No explanation. , Anyway, I don't understand )
Background: The background color ( When ClearFlags choice Solid Color when , This option works )
2.Culling Mask: Select a render layer
Render the object of the specified layer by selecting the layer ,Unity Each object in is assigned a layer , The default is “Default” layer
I put the cylinder and cube on the layer “Camera Culling Mask”, Through the demonstration, the reader should be able to understand that this parameter does
adopt Camera.cullingMask To modify this value in the code ,Int type , The default value is 0
3.Projection: Projection mode ( perspective ( Default ) and orthogonal )
- FOV Axis: Visual field axis
Vertical: Vertical axis , After this item is set , adjustment FieldOfView It will change the opening and closing angle of the camera
Horizontal: Horizontal axis , After this item is set , adjustment FieldOfView It will change the opening and closing angle of the camera - Field Of View: Range of view ( The camera's field of view is stretched ), The bigger the perspective , The larger the field of vision , At the same time, the shorter the focal length
Code can be passed through Camera.fieldOfView Class variable to modify this value , Unit degree , When the camera is in orthographic mode , This parameter is ignored
- Physical Camera: later , I haven't figured it out yet
- Size( Orthographic mode options ): Camera viewport size , Half of the area drawn by the camera in the vertical direction of the screen .
4.Clipping Planes: Clipping plane
seeing the name of a thing one thinks of its function ,Near and Far Specifies the region range of its clipping , If it is not in this range, it will not be rendered , It can save the amount of calculation , How many settings are appropriate , See the demand .
In the code, you can use Camera.farClipPlane and Camera.nearClipPlane Class variable to modify this value
5.Viewport Rect: View rectangle ( Camera position width and height )
Here are four values , among X and Y Represents the origin of the camera , Generally no change .
W and H Represents the width and height of the camera , Default 1 It is full screen , The screen coordinate system is used here to calculate , Readers who don't understand can go to my column to check about Unity Explanation of coordinate system in ( expect 22.4.10 Will write it out ),
It can be modified by W and H The effect of superimposing the views of two cameras . How to apply , Let's talk about it later
Here I set up two cameras , The main camera is rendered first , Then the camera in the lower left corner is rendered , And set the camera ClearFlags by Depth Only, This will not render superfluous parts , The effect shown in the above figure is achieved .
6.Depth: depth ( Camera rendering order )
Used to control the camera rendering order , Generally, those with small values will be rendered first , Larger values will be superimposed on the previous camera , The last example used , The order of cameras in the lower left corner is :0, The order of main background cameras is :-1, Therefore, it can ensure that the main background camera is rendered first , Then it is superimposed by the camera in the lower left corner .
adopt Camera.depth Class variable to modify this value in the code
7.Occlusion Culling: Shielding elimination switch
Checking this option will help improve game performance , For example, objects outside the scope of the camera , Will not be rendered
It should be noted that , Objects in the scene need to be pre calculated , And it is a static object , When checked Unity Can quickly and real-time judge whether the object is within the range of the camera .
8. Other properties , Temporarily unavailable , I won't go into that
In fact, I didn't understand , Add it after I understand
Two 、 Example application of camera
With the above parameters , Readers should basically understand Unity The camera in , Let's consolidate it with a few simple cases .
1. Control camera scene roaming
demand : adopt WASD To move the camera back and forth, left and right , adopt QE It can be lifted and lowered , Hold down Shift Can speed up .
You can zoom the view through the mouse wheel , Press the right mouse button and move , You can rotate the camera
The dynamic diagram of the demonstration is as follows :
In fact, the whole part of the code does not apply too many camera related parameters , The example is simple . But the code is smelly and long , For novice reference , No quality . And this code has Bug, The rotation is not locked Z Axis , This will cause the camera to rotate around the center for no reason .
High EQ : Please find out Bug And repair
Low EQ : I'm too lazy to write
public Camera camera_;
// Camera movement speed and rotation sensitivity
public float mouseSenstivity = 100;
public float speed = 5;
void Update()
{
// The keyboard controls the camera movement
Move_Update();
// The mouse wheel controls the scene zoom
MousewheelZoom_Update();
// Right click to control the scene rotation
MousergihtRot_Update();
}
private void Move_Update()
{
if (Input.GetKey(KeyCode.W))
{
camera_.transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.S))
{
camera_.transform.Translate(Vector3.back * Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.A))
{
camera_.transform.Translate(Vector3.left * Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.D))
{
camera_.transform.Translate(Vector3.right * Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.Q))
{
camera_.transform.Translate(Vector3.up * Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.E))
{
camera_.transform.Translate(Vector3.down * Time.deltaTime * speed);
}
}
private void MousewheelZoom_Update()
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
camera_.fieldOfView += 2;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
camera_.fieldOfView -= 2;
}
}
private void MousergihtRot_Update()
{
if (Input.GetMouseButton(1))
{
// X The shaft controls up and down rotation ,Y The axis controls the left and right rotation
float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * mouseSenstivity;
float mouseY = Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSenstivity;
// Pay attention here because X The axis controls up and down ,Y The axis controls the left and right
// and mouseX The left and right values of the screen are returned , All of them switched
// Again because mouseY Downward is negative , And the camera Y When the axis is downward, it is positive , On the other hand mouseY A negation is made .
// If you can't understand it here , I recommend reading the first blog post in resources , Sum up Unity Related movement control methods .
camera_.transform.Rotate(new Vector3((0f - mouseY), mouseX, 0));
}
}
3、 ... and 、 Summary and references
1. summary
There's nothing to summarize , Mainly remember the functions of several parameters of the camera .
2. Reference material
[1]. Dream of a young man . Detailed explanation Unity Mobile control implementation
[2]. Dream of a young man . Detailed explanation Unity Several mobile ways of
[3]. Xueting .unity in camera Camera control details
[4]. Lingbao'er .Unity Physical Camera in Focal Length and Field of View Relationship
[5].syddf_shadow. Sky box (SkyBox) The implementation principle and details of
[6]. Game bulls .[ Manniu tutorial ] Unity3D elementary analysis -Camera( The camera )
[7]. Xingxiaopeng .unity movie camera depth Pattern _Unity3d Camera details
边栏推荐
- js可拖拽alert弹窗插件
- 企业如何成功完成云迁移?
- Mongoose condition queries part of the data of the specified attribute
- What is "security"? Volvo tells you with its unique understanding and action
- 云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
- JS fly into JS special effect pop-up login box
- 阿里云 MSE 支持 Go 语言流量防护
- Space shooting Lesson 11: sound and music
- Unity object path query tool
- Interpretation of ue4.25 slate source code
猜你喜欢
一文了解 Rainbond 云原生应用管理平台
太空射击第11课: Sound and Music
js飞入js特效弹窗登录框
Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent
十七年运维老兵万字长文讲透优维低代码~
想画一张版权属于你的图吗?AI作画,你也可以
Random talk on GIS data (VI) - projection coordinate system
UE4 3dui widget translucent rendering blur and ghosting problems
Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
Nat experiment demonstration (Huawei switch equipment configuration)
随机推荐
Redis 3.0源码分析-数据结构与对象 SDS LIST DICT
#yyds干货盘点# 面试必刷TOP101:链表中的节点每k个一组翻转
研发效能的思考总结
不懂就问,快速成为容器服务进阶玩家!
华为云数字资产链,“链”接数字经济无限精彩
Redis review summary
Space shooting Lesson 15: props
太空射击第13课: 爆炸效果
JS chart scatter example
JS fly into JS special effect pop-up login box
微信公众号授权登录后报redirect_uri参数错误的问题
Random talk on GIS data (VI) - projection coordinate system
The average altitude is 4000 meters! We built a cloud on the roof of the world
Nat experiment demonstration (Huawei switch equipment configuration)
Want to draw a picture that belongs to you? AI painting, you can also
NAT实验演示(Huawei交换机设备配置)
7/27 training log (bit operation + suffix array)
Explain the life cycle function in unity in detail
Lvs+keepalived high availability deployment practical application
Unity typewriter teaches you three ways