您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 Thepoly 于 2022-3-24 20:06 编辑
Hello,大家好。 今天由到Kinglary分享 游戏引擎相关内容
2.4 变焦一个“望远镜”摄像机2.4 Zooming a telescopic camera In this recipe, we will create a telescopic camera that zooms in whenever the left mouse button is pressed. This can be very useful, for instance, if we have a sniper in our game. 在本章节,我们将创建一个望远镜摄像机,当你左键鼠标的时候会进行变焦。这一功能十分的有用。比如说,在我们的游戏中有一个狙击手。 Getting ready(准备工作) In order to follow this recipe, please import the basicTerrain package, available in the 0423_02_04_05 folder, into your project. The package includes a basic terrain and a camera that can be rotated via a mouse. 为了学习本章节内容,请从0423_02_04_05文件夹中,选择basicTerrain包导入到我们的项目(工程)之中。这个包,包括一个基本地形,和一个可以通过鼠标进行旋转的摄像机。 How to do it...(如何去做) To create a telescopic camera, follow these steps: 创建一个望远镜摄像机,需要遵循以下步骤:
1. Import the Unity package and open the 02_04_05 scene. 1. 导入Unity package,并打开02_04_05场景。
2. We need to create a script. In the Project view, click on the Create drop-down menuand choose C# Script. Rename it TelescopicView and open it in your editor. 2. 我们需要创建一个脚本,在项目浏览器中,点击Create,在下拉菜单中选择 C# Scrip,将其重命名为TelescopicView,并在编译器中打开脚本。
3. Open your script and replace everything with the following code: 3. 打开你的脚本,并替换成以下代码: using UnityEngine; using System.Collections; public class TelescopicView : MonoBehaviour{ public float ZoomLevel = 2.0f; public float ZoomInSpeed = 100.0f; public float ZoomOutSpeed = 100.0f; private float initFOV; //private Vignetting vignette; //public float vignetteAmount = 10.0f; Using Cameras void Start(){ initFOV = Camera.main.fieldOfView; //vignette = this.GetComponent("Vignetting") as Vignetting; } void Update(){ if (Input.GetKey(KeyCode.Mouse0)){ ZoomView(); }else{ ZoomOut(); } } void ZoomView(){ if (Mathf.Abs(Camera.main.fieldOfView - (initFOV / ZoomLevel)) < 0.5f){ Camera.main.fieldOfView = initFOV / ZoomLevel; // vignette. intensity = vignetteAmount; }else if (Camera.main.fieldOfView - (Time.deltaTime * ZoomInSpeed) >= (initFOV / ZoomLevel)){ Camera.main.fieldOfView -= (Time.deltaTime * ZoomInSpeed); // vignette. intensity = vignetteAmount * (Camera.main. fieldOfView - initFOV)/((initFOV / ZoomLevel) - initFOV); } } void ZoomOut(){ if (Mathf.Abs(Camera.main.fieldOfView - initFOV) < 0.5f){ Camera.main.fieldOfView = initFOV; // vignette. intensity = 0; }else if (Camera.main.fieldOfView + (Time.deltaTime * ZoomOutSpeed) <= initFOV){ Camera.main.fieldOfView += (Time.deltaTime * ZoomOutSpeed); //vignette.intensity = vignetteAmount * (Camera.main. fieldOfView - initFOV)/((initFOV / ZoomLevel) - initFOV); } } }
4. Save your script and apply it to the Main Camera game object. 4. 保存你的代码,让后将它挂载到你的主摄像机游戏对象上。
5. Play your scene. You should be able to zoom in the camera view by clicking the mouse button and zoom out by releasing it.
5. 运行你的场景,你应该可以通过点击鼠标变焦摄像机,松开鼠标便可以退出变焦。
6. The next steps are for only those using Unity Pro. If you are using Unity Pro, please stop the scene.
6. 接下来的步骤,只能在unity职业版中操作。如果你正在使用职业版,首先请停止游戏场景的运行。
7. Import Unity's Image Effects package by navigating to Assets | Import Package | Image Effects (Pro Only).
7. 点击Assets | Import Package | Image Effects (专业版),导入Image Effects包。
8. Select the Main Camera option and apply the Vignette image effect (by navigating to Component | Image Effects | Vignette).
8. 选择Main Camera选项,添加Vignette图像效果(点击Component | Image Effects | Vignette完成,这里译者一般喜欢称Vignette为虚光照)。
9. Open the TelescopicView script and uncomment every commented line (these are the ones referencing the vignetteAmount variable).
9. 打开TelescopicView脚本,反注释掉所有注释行(作者在上文中的注释,并不是注释的已有代码,而是代码,当注释的时候不参与编译,如使用的是专业版,去掉双斜线,这些代码便参与编译)。
10. Save the script and play the level. You should see an animated vignetting effect in addition to the zooming:
10. 保存脚本并运行,你应该可以看到一个添加到变焦行为上的虚光照效果,如下图: How it works...(工作原理) The zooming effect is actually caused by the increment made by the script on the value of the camera's Field of View property, while the mouse button is pressed. 变焦效果,实际上,就是利用脚本,调节摄像机的视窗属性的值,当点击鼠标左键的时候,便可以实现该效果。 There's more...(更多) If you are using Unity Pro, you could also add a variable to control the Blur Vignette level of the Vignette image effect. 如果你是用的是unity职业版,你也可以添加一个变量,来控制虚光照效果的边缘模糊的级别。
|