summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-09-20 10:23:59 +0800
committerchai <chaifix@163.com>2021-09-20 10:23:59 +0800
commit4551d1d12fc3029fcf553cfdeb10402e42de705d (patch)
tree7c59a8c5a06e9b0f12d669c0b62fd3ef29ead953 /Assets/Scripts/Unit
parent02b44c07adfcf921da594120b4cd8fc18b982725 (diff)
*misc
Diffstat (limited to 'Assets/Scripts/Unit')
-rw-r--r--Assets/Scripts/Unit/Components/UnitLensEffect.cs91
-rw-r--r--Assets/Scripts/Unit/LensEffect/LensEffects.cs166
-rw-r--r--Assets/Scripts/Unit/LensEffect/LensEffects.cs.meta11
-rw-r--r--Assets/Scripts/Unit/UnitDetail.cs1
4 files changed, 223 insertions, 46 deletions
diff --git a/Assets/Scripts/Unit/Components/UnitLensEffect.cs b/Assets/Scripts/Unit/Components/UnitLensEffect.cs
index 4b710aa4..5a6a309e 100644
--- a/Assets/Scripts/Unit/Components/UnitLensEffect.cs
+++ b/Assets/Scripts/Unit/Components/UnitLensEffect.cs
@@ -1,4 +1,5 @@
-using System.Collections;
+using System;
+using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
@@ -7,24 +8,27 @@ using UnityEngine.Rendering;
// Unit效果之一,镜头效果,通过command buffer实现
-//public class MaterailEntry
-//{
-// public Material material;
-//}
-
public class UnitLensEffect : UnitComponent
{
private List<RendererProxy> renderers;
- private static ObjectPool<CommandBuffer> m_CommandBufferPool = new ObjectPool<CommandBuffer>(null, null);
+ private static ObjectPool<CommandBuffer> m_CommandBufferPool;
+
+ private List<LensEffect> m_Effects;
+
+ private CommandBuffer m_CommandBuffer;
- CommandBuffer m_CommandBuffer;
+ static UnitLensEffect()
+ {
+ m_CommandBufferPool = new ObjectPool<CommandBuffer>(null, null);
+ }
public override void Initialize()
{
base.Initialize();
renderers = new List<RendererProxy>();
+ m_Effects = new List<LensEffect>();
}
public override void OnPostInitialize()
@@ -41,15 +45,19 @@ public class UnitLensEffect : UnitComponent
continue;
RendererProxy proxy = renderer.renderer.gameObject.GetOrAddComponent<RendererProxy>();
proxy.Initialize(renderer);
- proxy.onWillRenderObject = OnWillRenderObject;
- proxy.onRenderObject = OnRenderObject;
+ proxy.onWillRenderObject = OnWillRenderObj;
+ proxy.onRenderObject = OnRenderObj;
renderers.Add(proxy);
}
m_CommandBuffer = ClaimCommandBuffer();
+ m_CommandBuffer.name = "Unit Render";
MainCamera.Instance.lensEffectHandler.onPreCull += OnWillRenderUnit;
MainCamera.Instance.lensEffectHandler.onPostRender += OnRenderUnit;
+
+ /////
+ m_Effects.Add(LensEffect.MotionBlur());
}
public override void Release()
@@ -62,54 +70,45 @@ public class UnitLensEffect : UnitComponent
private void OnWillRenderUnit()
{
+ IBodyRendererAgent body = owner.unitRender.body;
+ if (body == null || body.renderers == null)
+ return;
+ if (m_Effects == null || m_Effects.Count == 0)
+ return;
+
m_CommandBuffer.Clear();
- MainCamera.Instance.camera.AddCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, m_CommandBuffer);
+ for (int i = 0; i < m_Effects.Count; ++i)
+ {
+ m_Effects[i].beforeIterate(m_CommandBuffer);
+ for (int j = 0; j < body.renderers.Length; ++j)
+ {
+ var renderer = body.renderers[j];
+ if (renderer == null)
+ continue;
+ m_Effects[i].iterate(m_CommandBuffer,renderer);
+ }
+ m_Effects[i].afterIterate(m_CommandBuffer);
+ }
+
+ MainCamera.Instance.camera.AddCommandBuffer(CameraEvent.AfterImageEffectsOpaque, m_CommandBuffer);
}
private void OnRenderUnit()
{
-
- MainCamera.Instance.camera.RemoveCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, m_CommandBuffer);
+ MainCamera.Instance.camera.RemoveCommandBuffer(CameraEvent.AfterImageEffectsOpaque, m_CommandBuffer);
+ for(int i = 0; i < m_Effects.Count; ++i)
+ {
+ m_Effects[i].finishRender?.Invoke();
+ }
}
- private void OnWillRenderObject(BodyPartRenderer renderer)
+ private void OnWillRenderObj(BodyPartRenderer renderer)
{
- //Camera cam = SceneManager.Instance.mainCamera;
- //if (cam == null)
- // return;
- //Material mat = new Material(Shader.Find(StaticDefine.shaders[EShader.SolidColor].name));
- //mat.SetColor("_Color", Color.red);
-
- //Matrix4x4 obj2Wod = Matrix4x4.identity;
- ////obj2Wod = TransformUtility.GetLocalToWorldMatrixRootBone(owner.unitDetail.rootBone.transform);
- //Vector3 pos = owner.unitDetail.rootBone.transform.position;
- //Quaternion rot = owner.unitDetail.rootBone.transform.rotation;
- //obj2Wod = MatrixUtility.RotateAndTranslate(pos, rot);
- //mat.SetMatrix("_ObjectToWorld", obj2Wod);
- //mat.SetTexture("_MainTex", renderer.renderer.material.GetTexture("_MainTex"));
-
- //cb.Clear();
- //cb.name = "Unit Renderer";
-
- //// create render texture for glow map
- //int tempID = Shader.PropertyToID("_Temp1");
- //cb.GetTemporaryRT(tempID, -1, -1, 24, FilterMode.Bilinear);
- //cb.SetRenderTarget(tempID);
- //cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
- //cb.DrawRenderer(renderer.renderer, mat);
-
- //cb.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
- //Material blur = new Material(Shader.Find(StaticDefine.shaders[EShader.Blur].name));
- //cb.Blit(tempID, BuiltinRenderTextureType.CameraTarget, blur);
-
- //cam.AddCommandBuffer(CameraEvent.AfterImageEffectsOpaque, cb);
}
- private void OnRenderObject(BodyPartRenderer renderer)
+ private void OnRenderObj(BodyPartRenderer renderer)
{
- //Camera cam = SceneManager.Instance.mainCamera;
- //cam.RemoveCommandBuffer(CameraEvent.AfterImageEffectsOpaque, cb);
}
static CommandBuffer ClaimCommandBuffer()
diff --git a/Assets/Scripts/Unit/LensEffect/LensEffects.cs b/Assets/Scripts/Unit/LensEffect/LensEffects.cs
new file mode 100644
index 00000000..4083eb88
--- /dev/null
+++ b/Assets/Scripts/Unit/LensEffect/LensEffects.cs
@@ -0,0 +1,166 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.Rendering;
+
+public class MaterialEntry
+{
+ public Material material;
+ public string shader;
+}
+
+public struct ScaleOffset
+{
+ public Vector2 scale;
+ public Vector2 offset;
+}
+
+public class LensEffect
+{
+ #region callbacks
+ public delegate void BeforeIterateHandler(CommandBuffer cb);
+ public BeforeIterateHandler beforeIterate;
+
+ public delegate void IterationHandler(CommandBuffer cb, BodyPartRenderer renderer);
+ public IterationHandler iterate;
+
+ public delegate void AfterIterateHandler(CommandBuffer cb);
+ public AfterIterateHandler afterIterate;
+
+ // 这一帧的这个特效结束
+ public delegate void FinishRenderHandler();
+ public FinishRenderHandler finishRender;
+ #endregion
+
+ private static Dictionary<string/*shaderName*/, List<MaterialEntry>> m_MaterailPool;
+
+ public static LensEffect Create(Func<LensEffect> create)
+ {
+ return create();
+ }
+
+ static LensEffect()
+ {
+ m_MaterailPool = new Dictionary<string, List<MaterialEntry>>();
+ }
+
+ static MaterialEntry ClaimMaterial(string shader)
+ {
+ List<MaterialEntry> mats;
+ if (m_MaterailPool.TryGetValue(shader, out mats) && mats.Count > 0)
+ {
+ MaterialEntry me = mats[mats.Count - 1];
+ mats.RemoveAt(mats.Count - 1);
+ return me;
+ }
+ Material mat = new Material(Shader.Find(shader));
+ MaterialEntry entry = new MaterialEntry();
+ entry.material = mat;
+ entry.shader = shader;
+ return entry;
+ }
+
+ static void ReleaseMaterial(MaterialEntry entry)
+ {
+ List<MaterialEntry> mats;
+ if (!m_MaterailPool.TryGetValue(entry.shader, out mats))
+ {
+ mats = new List<MaterialEntry>();
+ m_MaterailPool.Add(entry.shader, mats);
+ }
+ mats.Add(entry);
+ }
+
+ #region LensEffects
+
+ public static LensEffect MotionBlur()
+ {
+ LensEffect eff = LensEffect.Create(() =>
+ {
+ LensEffect e = new LensEffect();
+
+ List<MaterialEntry> materials = ListPool<MaterialEntry>.Get();
+ int tempID = Shader.PropertyToID("_Temp1");
+
+ ScaleOffset scaleOffset = new ScaleOffset();
+
+ e.beforeIterate = (CommandBuffer cb) =>
+ {
+ cb.GetTemporaryRT(tempID, -1, -1, 24, FilterMode.Bilinear);
+ cb.SetRenderTarget(tempID);
+ cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
+ };
+ e.iterate = (CommandBuffer cb, BodyPartRenderer renderer) =>
+ {
+ Matrix4x4 obj2Wod = Matrix4x4.identity;
+ int subMeshCount = 0;
+ if (renderer.renderer is SkinnedMeshRenderer)
+ {
+ SkinnedMeshRenderer smr = renderer.renderer as SkinnedMeshRenderer;
+ Vector3 pos = smr.rootBone.transform.position;
+ Quaternion rot = smr.rootBone.transform.rotation;
+ obj2Wod = MatrixUtility.RotateAndTranslate(pos, rot);
+ subMeshCount = smr.sharedMesh.subMeshCount;
+ }
+ else if (renderer.renderer is MeshRenderer)
+ {
+ obj2Wod = renderer.renderer.transform.localToWorldMatrix;
+ subMeshCount = renderer.renderer.GetComponent<MeshFilter>().sharedMesh.subMeshCount;
+ }
+
+ for (int i = 0; i < subMeshCount; ++i)
+ {
+ MaterialEntry mat = ClaimMaterial(StaticDefine.shaders[EShader.SolidColor].name);
+ materials.Add(mat);
+ mat.material.SetColor("_Color", Color.red);
+ mat.material.SetMatrix("_ObjectToWorld", obj2Wod);
+
+ cb.DrawRenderer(renderer.renderer, mat.material, i);
+ }
+ };
+ e.afterIterate = (CommandBuffer cb) =>
+ {
+ MaterialEntry blur = ClaimMaterial(StaticDefine.shaders[EShader.Blur].name);
+ materials.Add(blur);
+
+ cb.Blit(tempID, BuiltinRenderTextureType.CameraTarget, blur.material);
+ };
+ e.finishRender = () =>
+ {
+ for (int i = materials.Count - 1; i >= 0; --i)
+ {
+ MaterialEntry mat = materials[i];
+ ReleaseMaterial(mat);
+ materials.RemoveAt(i);
+ }
+ };
+ return e;
+ });
+ return eff;
+ }
+
+ public static LensEffect Glitch()
+ {
+
+ return null;
+ }
+
+ public static LensEffect OutLine()
+ {
+
+ return null;
+ }
+
+ public static LensEffect RimLight()
+ {
+ return null;
+ }
+
+ public static LensEffect Decals()
+ {
+ return null;
+ }
+
+ #endregion
+}
diff --git a/Assets/Scripts/Unit/LensEffect/LensEffects.cs.meta b/Assets/Scripts/Unit/LensEffect/LensEffects.cs.meta
new file mode 100644
index 00000000..ec32bb75
--- /dev/null
+++ b/Assets/Scripts/Unit/LensEffect/LensEffects.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: c5acf04ae1e72ed42a1d20ee038ac73e
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/Scripts/Unit/UnitDetail.cs b/Assets/Scripts/Unit/UnitDetail.cs
index 09eefa51..16067425 100644
--- a/Assets/Scripts/Unit/UnitDetail.cs
+++ b/Assets/Scripts/Unit/UnitDetail.cs
@@ -114,6 +114,7 @@ public class UnitReferencePointDictionary : SerializableDictionary<EUnitReferenc
public enum EBodyPart
{
Body = 0, // main body
+ Sword = 1,
}
[Serializable]