ARinChina-增强现实中国技术论坛
标题:
Capricorn|Unity如何使3D物体在屏幕中尽可能最大化(2)
[打印本页]
作者:
zzlple
时间:
2016-6-4 18:47
标题:
Capricorn|Unity如何使3D物体在屏幕中尽可能最大化(2)
上一节教程我们讲述了3D物体在屏幕中尽可能最大化的原理本节教程我们就实操来实现全屏最大化:
首先,我们需要给模型标记四个点,开始想到用四个点由editor编辑,后来想想工作量有点大,然后就灵感凸显,想到RectTransform这个继承于Transfrom的组件,这个Transfrom的子类有个好处就是提供了四个点Vector[0-3],我们只需要用这个组件作为模型的父物体,就可以准确的把握模型的比例了,有了比例,再将相机移向跟这个组件同一个Transfrom就可以根据这个长宽比例相对于屏幕的长宽比例计算出合适的缩放比例,下面是一段具体实现代码:
void
Adapter
(
RectTransform
rt
)
{
Vector3
[]
positionRect
=
new
Vector3
[
4
;
rt
.
GetWorldCorners
(
positionRect
)
;
float
width
=
Mathf
.
Abs
(
Camera
.
main
.
WorldToScreenPoint
(
positionRect
[
1
])
.
x
-
Camera
.
main
.
WorldToScreenPoint
(
positionRect
[
2
])
.
x
)
;
float
height
=
Mathf
.
Abs
(
Camera
.
main
.
WorldToScreenPoint
(
positionRect
[
0
])
.
y
-
Camera
.
main
.
WorldToScreenPoint
(
positionRect
[
1
])
.
y
)
;
float
scale
=
1
;
Camera
.
main
.
transform
.
localPosition
=
new
Vector3
(
rt
.
transform
.
localPosition
.
x
,
rt
.
transform
.
localPosition
.
y
,
rt
.
transform
.
localPosition
.
z
-
10
)
;
Camera
.
main
.
transform
.
localEulerAngles
=
rt
.
transform
.
localEulerAngles
;
if
(
width
>
height
)
{
scale
=
Screen
.
width
/
width
;
}
else
{
scale
=
Screen
.
height
/
height
;
}
transform
.
localScale
*=
scale
;
}
到这里,3D物体在屏幕中尽可能最大化教程就结束了,有不懂得同学可以@我。
欢迎光临 ARinChina-增强现实中国技术论坛 (http://dev.arinchina.com/)
Powered by Discuz! X3.2