summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/Components/UnitPreprocessing.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/Unit/Components/UnitPreprocessing.cs')
-rw-r--r--Assets/Scripts/Unit/Components/UnitPreprocessing.cs128
1 files changed, 128 insertions, 0 deletions
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