From 8268e4e308bd110dfea4ad849a7ff74e66951349 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 22 Oct 2020 08:57:28 +0800 Subject: =?UTF-8?q?*=E6=AE=8B=E5=BD=B1=E7=89=B9=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Effects/AfterImageEffects.cs | 2 +- Assets/Scripts/Effects/CharacterGhostEffect.cs | 127 +++++++++++++++++++++ .../Scripts/Effects/CharacterGhostEffect.cs.meta | 11 ++ Assets/Scripts/Effects/EffectHandler.cs | 18 +++ Assets/Scripts/Effects/EffectHandler.cs.meta | 11 ++ Assets/Scripts/Effects/IEffectHandler.cs | 18 --- Assets/Scripts/Effects/IEffectHandler.cs.meta | 11 -- 7 files changed, 168 insertions(+), 30 deletions(-) create mode 100644 Assets/Scripts/Effects/CharacterGhostEffect.cs create mode 100644 Assets/Scripts/Effects/CharacterGhostEffect.cs.meta create mode 100644 Assets/Scripts/Effects/EffectHandler.cs create mode 100644 Assets/Scripts/Effects/EffectHandler.cs.meta delete mode 100644 Assets/Scripts/Effects/IEffectHandler.cs delete mode 100644 Assets/Scripts/Effects/IEffectHandler.cs.meta (limited to 'Assets/Scripts/Effects') diff --git a/Assets/Scripts/Effects/AfterImageEffects.cs b/Assets/Scripts/Effects/AfterImageEffects.cs index a19be897..ba269279 100644 --- a/Assets/Scripts/Effects/AfterImageEffects.cs +++ b/Assets/Scripts/Effects/AfterImageEffects.cs @@ -1,4 +1,4 @@ -using UnityEngine; + using UnityEngine; using System.Collections; using System.Collections.Generic; /// diff --git a/Assets/Scripts/Effects/CharacterGhostEffect.cs b/Assets/Scripts/Effects/CharacterGhostEffect.cs new file mode 100644 index 00000000..d40adc17 --- /dev/null +++ b/Assets/Scripts/Effects/CharacterGhostEffect.cs @@ -0,0 +1,127 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CharacterGhostEffect : EffectHandler +{ + [Tooltip("目标skinMeshRenderer")] + public SkinnedMeshRenderer m_Renderer; + + [Tooltip("如果mesh包含多个submesh,需要每个submesh单独设置一个材质")] + public Material[] Materials; + + [Tooltip("Submesh是否使用同一个材质")] + public bool SubmeshShareMaterial; + + [Tooltip("使用范围内的submesh")] + public bool UseRangedSubmesh; + + [Tooltip("Submesh索引范围")] + public Vector2Int SubmeshRange; + + [Tooltip("是否开启残影效果")] + public bool IsEnable; + + [Tooltip("残影生成的时间间隔")] + public float Interval = 0.1f; + + [Tooltip("残影的生存时间")] + public float LifeTime = 0.5f; + + private float m_Time = 0; + private List m_Ghosts = new List(); + + void Update() + { + //if (!IsEnable) + //{ + // if (m_Ghosts.Count > 0) + // UpdateGhost(); // destroy ghosts + // return; + //} + + if (m_Ghosts == null || m_Renderer == null || Materials == null || Materials.Length == 0) + { + IsEnable = false; + return; + } + + m_Time += Time.deltaTime; + + //if(IsEnable) + // CreateGhost(); + + UpdateGhost(); + DrawGhost(); + } + + public void CreateGhost() + { + if(m_Time >= Interval) + { + m_Time -= Interval; + + Mesh mesh = new Mesh(); + m_Renderer.BakeMesh(mesh); + + Matrix4x4 mat = m_Renderer.localToWorldMatrix; + + m_Ghosts.Add(new GhostSnapshot(mesh, mat, Time.realtimeSinceStartup, LifeTime)); + } + } + + void UpdateGhost() + { + for(int i = m_Ghosts.Count - 1; i >= 0 ; --i) + { + GhostSnapshot ghost = m_Ghosts[i]; + float passTime = Time.realtimeSinceStartup - ghost.startTime; + + if (passTime > ghost.lifeTime) + { + m_Ghosts.Remove(ghost); + Destroy(ghost); + continue; + } + } + } + + void DrawGhost() + { + for(int i = 0; i < m_Ghosts.Count; ++ i) + { + GhostSnapshot ghost = m_Ghosts[i]; + int start = 0, end = ghost.mesh.subMeshCount - 1; + if(UseRangedSubmesh) + { + start = SubmeshRange.x; + end = SubmeshRange.y; + } + for (int j = start; j <= end; ++j) + { + Material material; + if (SubmeshShareMaterial) + material = Materials[0]; + else + material = Materials[j]; + Graphics.DrawMesh(ghost.mesh, ghost.matrix, material, gameObject.layer, null, j); + } + } + } +} + +class GhostSnapshot : Object +{ + public Mesh mesh; + public Matrix4x4 matrix; + public float startTime; + public float lifeTime; + + public GhostSnapshot(Mesh mesh, Matrix4x4 mat, float startTime, float lifeTime) + { + this.mesh = mesh; + this.matrix = mat; + this.startTime = startTime; + this.lifeTime = lifeTime; + } +} diff --git a/Assets/Scripts/Effects/CharacterGhostEffect.cs.meta b/Assets/Scripts/Effects/CharacterGhostEffect.cs.meta new file mode 100644 index 00000000..9a1f3a3a --- /dev/null +++ b/Assets/Scripts/Effects/CharacterGhostEffect.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 76b355b49906df24cac5ccfb0de69331 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Effects/EffectHandler.cs b/Assets/Scripts/Effects/EffectHandler.cs new file mode 100644 index 00000000..265a1001 --- /dev/null +++ b/Assets/Scripts/Effects/EffectHandler.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EffectHandler : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/Assets/Scripts/Effects/EffectHandler.cs.meta b/Assets/Scripts/Effects/EffectHandler.cs.meta new file mode 100644 index 00000000..81ee0c34 --- /dev/null +++ b/Assets/Scripts/Effects/EffectHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 83576b34fc8f74048a5c050c30ab463f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Effects/IEffectHandler.cs b/Assets/Scripts/Effects/IEffectHandler.cs deleted file mode 100644 index bdc4c6a0..00000000 --- a/Assets/Scripts/Effects/IEffectHandler.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class IEffectHandler : MonoBehaviour -{ - // Start is called before the first frame update - void Start() - { - - } - - // Update is called once per frame - void Update() - { - - } -} diff --git a/Assets/Scripts/Effects/IEffectHandler.cs.meta b/Assets/Scripts/Effects/IEffectHandler.cs.meta deleted file mode 100644 index 142a3f23..00000000 --- a/Assets/Scripts/Effects/IEffectHandler.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: cb3081a24820b3e438133b2ca6eb5549 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: -- cgit v1.1-26-g67d0