summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/Effect/UnitCamera.cs
blob: abb13a006073f64303a01adde8fdf46eff843185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 专门用来渲染character的相机,用来做特效
[RequireComponent(typeof(Camera))]
public class UnitCamera : SingletonMB<UnitCamera>
{
	PCController pc { get { return UnitManager.Instance.pc; } }

	Camera m_Camera;

	private void OnEnable()
	{
		m_Camera = GetComponent<Camera>();
        m_Camera.enabled = false;
    }

    // 手动渲染
    public void Render()
    {
        m_Camera.Render();
    }

    // 这里要用OnPreCull,不能是OnPreRender,因为设置了可见性,OnPreRender是在可见性剔除之后
    void OnPreCull() 
	{
		if (pc == null)
			return;
        if (pc.unitImageEffect == null || pc.unitImageEffect.effects.Count == 0)
            return;
		if (m_Camera == null)
			return;

		Vector3 pos = transform.position;
		transform.position = new Vector3(pc.center.x, pc.center.y, pos.z);

		float dz = Mathf.Abs(pos.z - pc.center.z);
		float fov = 2 * Mathf.Atan2(pc.unitDetail.snapshotBound / 2, dz) * Mathf.Rad2Deg;

		m_Camera.fieldOfView = fov;

        // 开启unit的渲染
        pc.unitRender.SetVisibilityInAllCameras(true);
    }

    void OnPostRender()
    {
        if (pc.unitImageEffect == null || pc.unitImageEffect.effects.Count == 0)
            return;

        pc.unitRender.SetVisibilityInAllCameras(false);
    }

}