miduo_client/Assets/LuaFramework/Scripts/Utility/FastShadow/ShadowMap.cs

41 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using UnityEngine;
using System.Collections;
/// <summary>
/// 接受阴影的物体
/// </summary>
public class ShadowMap : MonoBehaviour
{
private Material _mat;
private Camera _lightCamera;
void Start()
{
MeshRenderer render = GetComponent<MeshRenderer>();
_mat = render.material;
foreach (Camera item in Camera.allCameras)
{
if (item.CompareTag("LightCamera"))
{
_lightCamera = item;
break;
}
}
}
void OnWillRenderObject()
{
if (_mat != null && _lightCamera != null)
{
//Gl
//_mat.SetMatrix("_ViewProjectionMat", _lightCamera.projectionMatrix * _lightCamera.worldToCameraMatrix);// unity的camera projectionMatrix是GL风格的列矩阵 http://docs.unity3d.com/ScriptReference/Camera-projectionMatrix.html 投影后的z-[-w,w]
//真正的平台相关的投影矩阵
_mat.SetMatrix("_ViewProjectionMat", GL.GetGPUProjectionMatrix(_lightCamera.projectionMatrix, true) * _lightCamera.worldToCameraMatrix);
_mat.SetTexture("_DepthMap", _lightCamera.targetTexture);
_mat.SetFloat("_NearClip", _lightCamera.nearClipPlane);
_mat.SetFloat("_FarClip", _lightCamera.farClipPlane);
}
}
}