AR中經常遇到一些物體我們想給他加光影的時候,那我們就必須給他一個接受陰影的平面,而我們如果給他一個接受物體的平面那么就會跟現實脫離,下邊我們就可以用一些方法來實現光影效果,下邊就是效果。
Paste_Image.png
原理很簡單,分兩步實現,第一步用AR相機把模型給展現出來,第二步用兩個相機一個相機渲染實體模型,另一個只渲染影子,我們在這里邊需要用到層級
ARObjectCamera
Paste_Image.png
Paste_Image.png
一是注意Culling Mask還有一個是注意Depth,這樣就可保證ARObjectCamera渲染的畫面在最上層
Paste_Image.png
LightCamera
上邊那個是照射實體模型相機,以及實體模型的設置,對于LightCamera相機他只用于渲染Shadow,我們在這個里邊需要注意幾點
Paste_Image.png
1,Cube和Sphere
Paste_Image.png
2,plane用于接受Shadow,他的坐標要低于Imatarget,也就是Y軸我們可以變為
Paste_Image.png
這樣做的目的就是讓Plane不被渲染到相機里邊,還有一點需要注意就是ARObject和LightObject里邊的Cube(Sphere)坐標要一樣,這樣影子才會和實物保持一致。
除了這些,我們也要保持相機的角度,廣度都一樣,所以我們寫了一個腳本
Paste_Image.png
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插件,
Paste_Image.png
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;
//}
}
Paste_Image.png