summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/LensEffect/LensEffect_Dash.cs
blob: a779b6d18b882f22439646b0cdaa07c7c81dfc2e (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class LensEffect_Dash : LensEffectBase
{
	public override ERenderingEvent renderingEvents => ERenderingEvent.AfterForwardOpaque;

	Color rimColor;
	int tempID;
	float lifeTime;
	UnitSnapshot snapshot;
	TRS trs;
	float angle;

	float curTime = 0;

	public LensEffect_Dash(Color color, float lifeTime, float angle, UnitSnapshotInfo snapshot) : base()
	{
		rimColor = color;
		tempID = Shader.PropertyToID("RT_Dash");
		this.lifeTime = lifeTime;
		trs = snapshot.trs;
		this.snapshot = UnitManager.Instance.ClaimSnapshotSolo(snapshot);
		this.angle = angle;
	}

	public override void AfterForwardOpaque(EStage stage, CommandBuffer cb)
	{
		if (stage == EStage.Before)
		{
			Before(cb);
		}
		else if (stage == EStage.After)
		{
			After(cb);
		}
		else if (stage == EStage.Finished)
		{
		}
	}

	void Before(CommandBuffer cb)
	{
		cb.GetTemporaryRT(tempID, -1, -1, 24, FilterMode.Bilinear);
		cb.SetRenderTarget(tempID);
		cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));

		// renderer
		snapshot.transform.position = trs.position;
		snapshot.transform.rotation = trs.rotation;
		snapshot.transform.localScale = trs.scale;

		Matrix4x4 obj2Wod = Matrix4x4.identity;
		SkinnedMeshRenderer smr = snapshot.renderers[0] as SkinnedMeshRenderer;
		Vector3 pos = smr.rootBone.transform.position;
		Quaternion rot = smr.rootBone.transform.rotation;
		obj2Wod = MatrixUtility.RotateAndTranslate(pos, rot);

		MaterialEntry mat = ClaimMaterial(StaticDefine.shaders[EShader.SolidColor].name);
		mat.material.SetColor("_Color", rimColor);
		mat.material.SetMatrix("_ObjectToWorld", obj2Wod);
		mat.material.SetTexture("_MainTex", snapshot.renderers[0].sharedMaterial.GetTexture("_MainTex"));

		cb.DrawRenderer(snapshot.renderers[0], mat.material);
	}

	void After(CommandBuffer cb)
	{
		curTime += Time.deltaTime;

		MaterialEntry blur = ClaimMaterial(StaticDefine.shaders[EShader.MotionBlur].name);

		Vector4 tileOffset = RenderingUtility.GetTillingOffset(MainCamera.Instance.camera, trs.position, 6);
		blur.material.SetVector("_UnitTileOffset", tileOffset);
		blur.material.SetFloat("_Angle", Mathf.Rad2Deg * angle);
		blur.material.SetFloat("_AlphaMultiplier", Mathf.Clamp(1 - curTime / lifeTime, 0, 1));

		cb.Blit(tempID, BuiltinRenderTextureType.CameraTarget, blur.material);
		cb.ReleaseTemporaryRT(tempID);
	}

	public override bool CanDestroy()
	{
		return curTime > lifeTime;
	}

	public override void OnDestroy()
	{
		UnitManager.Instance.ReleaseSnapshot(ref snapshot);
	}

}