From 97ea5499f0237dcf531d66dd8846d2689d1cc00f Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 22 Apr 2021 15:43:02 +0800 Subject: *ray cast --- Assets/LensFlare/FlareBatch.cs | 3 ++- Assets/LensFlare/FlareSource.cs | 58 ++++++++++++++++++++++++++++++++++++---- Assets/LensFlare/LensFlare.unity | 54 +++++++++++-------------------------- 3 files changed, 71 insertions(+), 44 deletions(-) diff --git a/Assets/LensFlare/FlareBatch.cs b/Assets/LensFlare/FlareBatch.cs index 41c7991..b8c8e4f 100644 --- a/Assets/LensFlare/FlareBatch.cs +++ b/Assets/LensFlare/FlareBatch.cs @@ -92,7 +92,7 @@ public class FlareBatch : MonoBehaviour void UpdateGeometry(FlareSource source) { Vector3 viewportPos = source.ViewportPosition; - Vector2 center = Vector2.zero; // 光晕“中心”,后续这个值可以变 + Vector2 center = source.Center; // 光晕“中心”,后续这个值可以变 Vector2 flareSpacePos = ViewportToFlareSpace(viewportPos); // 光源在flare space的坐标 Vector2 flareVec = flareSpacePos - center; float angle = Mathf.Atan2(flareSpacePos.y, flareSpacePos.x) * Mathf.Rad2Deg; @@ -118,6 +118,7 @@ public class FlareBatch : MonoBehaviour Vec3(halfSize.x, -halfSize.y, -i*0.01f), }; Color col = flare.Color; + col.a *= source.AlphaBase; col.a *= alpha; vh.color = new Color[] { col, col, col, col }; diff --git a/Assets/LensFlare/FlareSource.cs b/Assets/LensFlare/FlareSource.cs index 68cc7f1..2e88ba5 100644 --- a/Assets/LensFlare/FlareSource.cs +++ b/Assets/LensFlare/FlareSource.cs @@ -50,14 +50,16 @@ public class FlareSource : MonoBehaviour [SerializeField] private Camera m_FlareCamera; public float PixelPerUnit = 100; // 用来控制缩放,在flare space生成网格的大小依据 - public AnimationCurve FadeInCurve; // 淡入的alpha曲线 - public AnimationCurve FadeOutCurve; // 淡出的alpha曲线 + public AnimationCurve FadeCurve; // 淡入淡出的曲线 + public float FadeDuration;// 淡入淡出的时间间隔 public AnimationCurve ScaleCurve; public AnimationCurve AlphaCurve; - [Range(0.1f, 5)] - public float SpreadAmount = 5; // 总长度/光源到光晕中心的长度,用来求得总长度 + [Range(0.1f, 5)] + public float SpreadAmount = 5; // 总长度/光源到光晕中心的长度,用来求得总长度 public float SpreadMaxminum = 5; // 最大扩散长度,单位Unit + public Vector2 Center; + [TextArea(1, 10)] public string Comment; public List Flares; @@ -71,14 +73,27 @@ public class FlareSource : MonoBehaviour } } + private float m_AlphaBase = 0f; + public float AlphaBase + { + get + { + return m_AlphaBase; + } + } + private float m_FadeTime = 0; + public bool IsVisible { get;private set; } + private bool IsHitLast = true; + // Start is called before the first frame update void Start() { + IsHitLast = true; IsVisible = false; if (m_GameCamera == null) m_GameCamera = Camera.main; @@ -94,11 +109,39 @@ public class FlareSource : MonoBehaviour RaycastHit hit; if(Physics.Raycast(ray, out hit)) { - IsVisible = false; + if (IsVisible) + { + if (!IsHitLast) + m_FadeTime = (1 - m_AlphaBase) * FadeDuration; + m_FadeTime += Time.deltaTime; + float fac = GetFadeCurveValue(m_FadeTime / FadeDuration); + m_AlphaBase = Mathf.Lerp(1, 0, fac); + if (m_AlphaBase <= 0) + { + IsVisible = false; + m_FadeTime = 0; + m_AlphaBase = 0; + } + } + IsHitLast = true; } else { + if(IsHitLast) + m_FadeTime = m_AlphaBase * FadeDuration; + if(m_AlphaBase < 1) + { + m_FadeTime += Time.deltaTime; + float fac = GetFadeCurveValue(m_FadeTime / FadeDuration); + m_AlphaBase = Mathf.Lerp(0, 1, fac); + if (m_AlphaBase >= 1) + { + m_FadeTime = 0; + m_AlphaBase = 1; + } + } IsVisible = true; + IsHitLast = false; } } @@ -132,4 +175,9 @@ public class FlareSource : MonoBehaviour return AlphaCurve.Evaluate(fac); } + public float GetFadeCurveValue(float fac) + { + return FadeCurve.Evaluate(fac); + } + } diff --git a/Assets/LensFlare/LensFlare.unity b/Assets/LensFlare/LensFlare.unity index 2696886..f052459 100644 --- a/Assets/LensFlare/LensFlare.unity +++ b/Assets/LensFlare/LensFlare.unity @@ -208,13 +208,13 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 378154549} - m_LocalRotation: {x: 0.46999806, y: -0, z: -0, w: 0.88266754} + m_LocalRotation: {x: 0.96671486, y: -0, z: -0, w: 0.25585616} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 1303527991} m_RootOrder: 0 - m_LocalEulerAnglesHint: {x: 56.068, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 150.351, y: 0, z: 0} --- !u!1 &449180655 GameObject: m_ObjectHideFlags: 0 @@ -434,54 +434,31 @@ MonoBehaviour: m_GameCamera: {fileID: 2134045979} m_FlareCamera: {fileID: 1380528559} PixelPerUnit: 600 - FadeInCurve: + FadeCurve: serializedVersion: 2 m_Curve: - serializedVersion: 3 time: 0 value: 0 - inSlope: 1 - outSlope: 1 - tangentMode: 34 + inSlope: 2 + outSlope: 2 + tangentMode: 0 weightedMode: 0 inWeight: 0 outWeight: 0 - serializedVersion: 3 time: 1 value: 1 - inSlope: 1 - outSlope: 1 - tangentMode: 34 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - m_PreInfinity: 2 - m_PostInfinity: 2 - m_RotationOrder: 4 - FadeOutCurve: - serializedVersion: 2 - m_Curve: - - serializedVersion: 3 - time: 0 - value: 0 - inSlope: 1 - outSlope: 1 - tangentMode: 34 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - - serializedVersion: 3 - time: 1 - value: 1 - inSlope: 1 - outSlope: 1 - tangentMode: 34 + inSlope: 0 + outSlope: 0 + tangentMode: 0 weightedMode: 0 inWeight: 0 outWeight: 0 m_PreInfinity: 2 m_PostInfinity: 2 m_RotationOrder: 4 + FadeDuration: 0.2 ScaleCurve: serializedVersion: 2 m_Curve: @@ -540,7 +517,8 @@ MonoBehaviour: m_PostInfinity: 2 m_RotationOrder: 4 SpreadAmount: 5 - SpreadMaxminum: 5 + SpreadMaxminum: 9 + Center: {x: 0, y: 0} Comment: "0-9 \u5149\u6E90\n10-29 \u5C0F\u6591\u70B9\n30-35 \u5927\u6591\u70B9\n30-31\u6700\u8FDC\u5904\u7684\u5149\u8292" Flares: - Comment: "0 \u5149\u6E90\u80CC\u540E\u7684\u5F69\u8679" @@ -939,7 +917,7 @@ MonoBehaviour: OverridePixelPerUnit: 0 PixelPerUnit: 0 Distance: 4.69 - - Comment: "33 \u5C0F\u6591\u70B9" + - Comment: "33 \u5927\u6591\u70B9" IsActive: 1 Atlas: {fileID: 11400000, guid: 317e5903581b97148afb8a08854d474d, type: 2} Index: 2 @@ -979,8 +957,8 @@ MonoBehaviour: IsActive: 1 Atlas: {fileID: 11400000, guid: 317e5903581b97148afb8a08854d474d, type: 2} Index: 22 - MaxScale: 6.36 - MinScale: 1.22 + MaxScale: 8 + MinScale: 1 Color: {r: 1, g: 1, b: 1, a: 0.23921569} Rotation: 182 RotateWith: 1 @@ -1359,7 +1337,7 @@ BoxCollider: m_GameObject: {fileID: 1607455291} m_Material: {fileID: 0} m_IsTrigger: 0 - m_Enabled: 0 + m_Enabled: 1 serializedVersion: 2 m_Size: {x: 1, y: 1, z: 1} m_Center: {x: 0, y: 0, z: 0} -- cgit v1.1-26-g67d0