找回密码
 立即注册
查看: 5176|回复: 59

[脚本] 如何在unity3d中做放大境效果

[复制链接]
发表于 2015-11-12 16:53 | 显示全部楼层 |阅读模式
这篇文章的主要目的是 要给你一个想法 如何在unity3d中做放大境效果 。
在unity中可以简单的实现放大镜效果啊 . 那么现在就来一步一步实现这个:
1.创建一个摄像机对象,设置 projection 类型为 perspective 或者 orthographic.
2.设置相机的 orthographicSize 或者 fieldOfView (依赖于相机的 projection 类型 ).
3.设置其 pixelrect . 例如如果您想要在你鼠标位置显示放大境 和其大小是 100 x 100 , 然后设置pixelrect 为 :magnifyCamera.pixelRect = new Rect (Input.mousePosition.x – 100f / 2.0f, Input.mousePosition.y – 100f / 2.0f, 100f, 100f);
4.设置相机的位置。 例如 如果你想在 你的鼠标位置显示放大镜效果 ,那么设置相机的位置为 mousePosition世界点。
你能看到最终的效果图
下面的 C# 脚本将创建一个 MagnifyGlass,并将它移动到 mousePosition位置 。
MagnifyGlass 脚本: 添加到一个空的游戏对象。
using Unity Engine;
using System.Collections;
public class MagnifyGlass : MonoBehaviour
{
private Camera magnifyCamera;
private GameObject magnifyBorders;
private LineRenderer LeftBorder, RightBorder, TopBorder, BottomBorder; // Reference for lines of magnify glass borders
private float MGOX,MG0Y; // Magnify Glass Origin X and Y position
private float MGWidth = Screen.width/5f,MGHeight = Screen.width/5f; // Magnify glass width and height
private Vector3 mousePos;
void Start ()
{
createMagnifyGlass ();
}
void Update ()
{
// Following lines set the camera's pixelRect and camera position at mouse position
magnifyCamera.pixelRect = new Rect (Input.mousePosition.x - MGWidth / 2.0f, Input.mousePosition.y - MGHeight / 2.0f, MGWidth, MGHeight);
mousePos = getWorldPosition (Input.mousePosition);
magnifyCamera.transform.position = mousePos;
mousePos.z = 0;
magnifyBorders.transform.position = mousePos;
}
// Following method creates MagnifyGlass
private void createMagnifyGlass()
{
GameObject camera = new GameObject("MagnifyCamera");
MGOX = Screen.width / 2f - MGWidth/2f;
MG0Y = Screen.height / 2f - MGHeight/2f;
magnifyCamera = camera.AddComponent();
magnifyCamera.pixelRect = new Rect(MGOX, MG0Y, MGWidth, MGHeight);
magnifyCamera.transform.position = new Vector3(0,0,0);
if(Camera.main.isOrthoGraphic)
{
magnifyCamera.orthographic = true;
magnifyCamera.orthographicSize = Camera.main.orthographicSize / 5.0f;//+ 1.0f;
createBordersForMagniyGlass ();
}
else
{
magnifyCamera.orthographic = false;
magnifyCamera.fieldOfView = Camera.main.fieldOfView / 10.0f;//3.0f;
}
}
// Following method sets border of MagnifyGlass
private void createBordersForMagniyGlass()
{
magnifyBorders = new GameObject ();
LeftBorder = getLine ();
LeftBorder.SetVertexCount(2);
LeftBorder.SetPosition(0,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y,0)).y-0.1f,-1));
LeftBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).y+0.1f,-1));
LeftBorder.transform.parent = magnifyBorders.transform;
TopBorder = getLine ();
TopBorder.SetVertexCount(2);
TopBorder.SetPosition(0,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).y,-1));
TopBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y+MGHeight,0)).x,getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y+MGHeight,0)).y,-1));
TopBorder.transform.parent = magnifyBorders.transform;
RightBorder = getLine ();
RightBorder.SetVertexCount(2);
RightBorder.SetPosition(0,new Vector3(getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y+MGWidth,0)).x,getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y+MGWidth,0)).y+0.1f,-1));
RightBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).y-0.1f,-1));
RightBorder.transform.parent = magnifyBorders.transform;
BottomBorder = getLine ();
BottomBorder.SetVertexCount(2);
BottomBorder.SetPosition(0,new Vector3(getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).y,-1));
BottomBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y,0)).y,-1));
BottomBorder.transform.parent = magnifyBorders.transform;
}
// Following method creates new line for MagnifyGlass's border
private LineRenderer getLine()
{
LineRenderer line = new GameObject("Line").AddComponent();
line.material = new Material(Shader.Find("Diffuse"));
line.SetVertexCount(2);
line.SetWidth(0.2f,0.2f);
line.SetColors(Color.black, Color.black);
line.useWorldSpace = false;
return line;
}
private void setLine(LineRenderer line)
{
line.material = new Material(Shader.Find("Diffuse"));
line.SetVertexCount(2);
line.SetWidth(0.2f,0.2f);
line.SetColors(Color.black, Color.black);
line.useWorldSpace = false;
}
// Following method calculates world's point from screen point as per camera's projection type
public Vector3 getWorldPosition(Vector3 screenPos)
{
Vector3 worldPos;
if(Camera.main.isOrthoGraphic)
{
worldPos = Camera.main.ScreenToWorldPoint (screenPos);
worldPos.z = Camera.main.transform.position.z;
}
else
{
worldPos = Camera.main.ScreenToWorldPoint (new Vector3 (screenPos.x, screenPos.y, Camera.main.transform.position.z));
worldPos.x *= -1;
worldPos.y *= -1;
}
return worldPos;
}
}
好了,本篇unity3d教程关于在unity3d中实现放大镜的介绍到此结束。
资源地址: http://cg.silucg.com/dongman/unity3d/7969.html

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2016-3-9 15:59 | 显示全部楼层
请问,pixelrect 在哪里设置啊
发表于 2016-9-21 18:30 | 显示全部楼层
it is very nice
发表于 2016-12-30 10:28 | 显示全部楼层

感谢楼主的无私分享!
发表于 2017-4-5 21:46 | 显示全部楼层
很不错
发表于 2017-4-5 21:17 | 显示全部楼层
好帖就是要顶
发表于 2017-4-5 21:12 | 显示全部楼层
说的非常好
发表于 2017-4-5 21:22 | 显示全部楼层
很好哦
发表于 2017-4-5 21:34 | 显示全部楼层
不错不错
发表于 2017-5-13 11:36 | 显示全部楼层
很不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 00:03 , Processed in 0.100945 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表