summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/Components
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/Unit/Components')
-rw-r--r--Assets/Scripts/Unit/Components/UnitLensEffect.cs17
-rw-r--r--Assets/Scripts/Unit/Components/UnitPreprocessing.cs128
-rw-r--r--Assets/Scripts/Unit/Components/UnitPreprocessing.cs.meta11
3 files changed, 148 insertions, 8 deletions
diff --git a/Assets/Scripts/Unit/Components/UnitLensEffect.cs b/Assets/Scripts/Unit/Components/UnitLensEffect.cs
index a3c5d2c8..ef8e6ee6 100644
--- a/Assets/Scripts/Unit/Components/UnitLensEffect.cs
+++ b/Assets/Scripts/Unit/Components/UnitLensEffect.cs
@@ -60,19 +60,20 @@ public class UnitLensEffect : UnitComponent
}
MainCamera.Instance.customRenderingPipeline.onPreCull += OnWillRenderUnit;
- MainCamera.Instance.customRenderingPipeline.onPostRender += OnRenderUnit;
-
- /////
+ MainCamera.Instance.customRenderingPipeline.onPostRender += OnRenderUnit;
+
+ /////
//m_Effects.Add(new LensEffect_MotionBlur());
- //m_Effects.Add(new LensEffect_BlurRim(Color.blue));
+ //m_Effects.Add(new LensEffect_BlurRim(Color.blue));
+ m_Effects.Add(new LensEffect_Buzz());
}
public override void Release()
- {
- base.Release();
-
- MainCamera.Instance.customRenderingPipeline.onPreRender -= OnWillRenderUnit;
+ {
+ MainCamera.Instance.customRenderingPipeline.onPreCull -= OnWillRenderUnit;
MainCamera.Instance.customRenderingPipeline.onPostRender -= OnRenderUnit;
+
+ base.Release();
}
private void OnWillRenderUnit()
diff --git a/Assets/Scripts/Unit/Components/UnitPreprocessing.cs b/Assets/Scripts/Unit/Components/UnitPreprocessing.cs
new file mode 100644
index 00000000..30357615
--- /dev/null
+++ b/Assets/Scripts/Unit/Components/UnitPreprocessing.cs
@@ -0,0 +1,128 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.Rendering;
+
+// 给每个角色准备一些特殊的前置渲染
+public class UnitPreprocessing : UnitComponent
+{
+ [Flags]
+ public enum EUnitPreprocessing
+ {
+ None ,
+ DepthTexture, // Unit Depth Texture
+ WorldNormalTexture, // Unit World Normal Texture
+ }
+
+ public EUnitPreprocessing preprocessing;
+
+ public RenderTexture unitDepthTexture { get; private set; }
+ public RenderTexture unitWorldNormalTexture { get; private set; }
+
+ private CommandBuffer m_CBBeforeDepthTexture;
+
+ private Material m_MaterialDepth;
+
+ public override void Initialize()
+ {
+ base.Initialize();
+ }
+
+ public override void OnPostInitialize()
+ {
+ base.OnPostInitialize();
+
+ MainCamera.Instance.customRenderingPipeline.onPreCull += OnWillRenderUnit;
+ MainCamera.Instance.customRenderingPipeline.onPostRender += OnRenderUnit;
+
+ m_CBBeforeDepthTexture = new CommandBuffer();
+
+ PrepareRenderTextures();
+ PrepareMaterials();
+ }
+
+ void PrepareRenderTextures()
+ {
+ int unitHash = owner.GetHashCode();
+
+ int width = MainCamera.Instance.camera.pixelWidth;
+ int height = MainCamera.Instance.camera.pixelHeight;
+ unitDepthTexture = new RenderTexture(width, height, 24, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear);
+ unitDepthTexture.name = "UnitDepthTexture_" + owner.GetHashCode();
+ }
+
+ void PrepareMaterials()
+ {
+ m_MaterialDepth = new Material(Shader.Find(StaticDefine.shaders[EShader.UnitDepth].name));
+ }
+
+ public override void OnUpdate()
+ {
+ base.OnUpdate();
+ }
+
+ public override void Release()
+ {
+ MainCamera.Instance.customRenderingPipeline.onPreCull -= OnWillRenderUnit;
+ MainCamera.Instance.customRenderingPipeline.onPostRender -= OnRenderUnit;
+
+ base.Release();
+ }
+
+ private void OnWillRenderUnit()
+ {
+ RenderDepthTexture();
+ //RenderWorldNormal();
+
+ MainCamera.Instance.camera.AddCommandBuffer(CameraEvent.BeforeDepthTexture, m_CBBeforeDepthTexture);
+ }
+
+ void RenderDepthTexture()
+ {
+ var cb = m_CBBeforeDepthTexture;
+ cb.Clear();
+ cb.SetRenderTarget(unitDepthTexture);
+ cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
+
+ cb.SetGlobalVector("unity_LightShadowBias", Vector4.zero);
+
+ foreach (var r in GetRenderers())
+ {
+ BodyPartRenderer br = r as BodyPartRenderer;
+ if (br == null)
+ continue;
+ Renderer renderer = br.renderer as Renderer;
+ if (renderer == null)
+ continue;
+ cb.DrawRenderer(renderer, m_MaterialDepth);
+ }
+ }
+
+ //void RenderWorldNormal()
+ //{
+ // var cb = m_CBBeforeDepthTexture;
+ // cb.Clear();
+ // cb.GetTemporaryRT(unitWorldNormalTextureID, -1, -1, 24, FilterMode.Point, RenderTextureFormat.RG16, RenderTextureReadWrite.Linear);
+ // cb.SetRenderTarget(unitWorldNormalTextureID);
+ // cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
+ //}
+
+ IEnumerable GetRenderers()
+ {
+ IBodyRendererAgent body = owner.unitRender.body;
+ if (body == null || body.renderers == null || body.renderers.Length == 0)
+ yield break;
+ for (int j = 0; j < body.renderers.Length; ++j)
+ {
+ yield return body.renderers[j];
+ }
+ }
+
+ private void OnRenderUnit()
+ {
+ //m_CBBeforeDepthTexture.ReleaseTemporaryRT(unitDepthTextureID);
+
+ MainCamera.Instance.camera.RemoveCommandBuffer(CameraEvent.BeforeDepthTexture, m_CBBeforeDepthTexture);
+ }
+} \ No newline at end of file
diff --git a/Assets/Scripts/Unit/Components/UnitPreprocessing.cs.meta b/Assets/Scripts/Unit/Components/UnitPreprocessing.cs.meta
new file mode 100644
index 00000000..734df2f2
--- /dev/null
+++ b/Assets/Scripts/Unit/Components/UnitPreprocessing.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 1998e85be970d1541843ff6166f6c771
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant: