AR中经常遇到一些物体我们想给他加光影的时候,那我们就必须给他一个接受阴影的平面,而我们如果给他一个接受物体的平面那么就会跟现实脱离,下边我们就可以用一些方法来实现光影效果,下边就是效果。

原理很简单,分两步实现,第一步用AR相机把模型给展现出来,第二步用两个相机一个相机渲染实体模型,另一个只渲染影子,我们在这里边需要用到层级
ARObjectCamera


一是注意Culling Mask还有一个是注意Depth,这样就可保证ARObjectCamera渲染的画面在最上层

LightCamera
上边那个是照射实体模型相机,以及实体模型的设置,对于LightCamera相机他只用于渲染Shadow,我们在这个里边需要注意几点

1,Cube和Sphere

2,plane用于接受Shadow,他的坐标要低于Imatarget,也就是Y轴我们可以变为

这样做的目的就是让Plane不被渲染到相机里边,还有一点需要注意就是ARObject和LightObject里边的Cube(Sphere)坐标要一样,这样影子才会和实物保持一致。
除了这些,我们也要保持相机的角度,广度都一样,所以我们写了一个脚本

using UnityEngine;
using System.Collections;
public class CopyCameraData : MonoBehaviour
{
public Camera targetCamera;
// Use this for initialization
IEnumerator Start()
{
yield return new WaitForSeconds(1.0f);
Camera _cam = gameObject.GetComponent<Camera>();
_cam.aspect = targetCamera.aspect;
_cam.fieldOfView = targetCamera.fieldOfView;
}
}
在两个脚本都挂载一下
注意
LightCamera是渲染出了一个RenderTexture,然后我们把这个图片和ARCamera用图片后处理技术做一个Blend,在这里边我们用到了ColorfulFX插件,

using UnityEngine;
using System.Collections;
using Colorful;
public class RendderTextureBlend : MonoBehaviour
{
public static RendderTextureBlend instance;
public RenderTexture _texture;
public Blend _Blend;
public bool isBlend;
private void Awake()
{
instance = this;
}
// Use this for initialization
void Start()
{
_texture = new RenderTexture(Screen.width, Screen.height, 16);
gameObject.GetComponent<Camera>().targetTexture = _texture;
_Blend.Texture = _texture;
}
//public void blendTexture()
//{
// _texture = new RenderTexture(Screen.width, Screen.height, 16);
// gameObject.GetComponent<Camera>().targetTexture = _texture;
// _Blend.Texture = _texture;
//}
}

网友评论