123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using UnityEngine;
- using Utility;
- namespace GameLogic.Combat.Hero.HeroGPU
- {
- public class HeroGPURenderManager : Singleton<HeroGPURenderManager>
- {
- protected Map<string, BetterList<HeroGPUMono>> gpuRenderMap = new Map<string, BetterList<HeroGPUMono>>();
- readonly int enableShaderId = Shader.PropertyToID("_EnableAnimation");
- readonly int animtionStateId = Shader.PropertyToID("_AnimationState");
- readonly int edgeColor = Shader.PropertyToID("_edgeColor");
- readonly int edgeStrength = Shader.PropertyToID("_edgeStrength");
- readonly int injuriedStrength = Shader.PropertyToID("_injuriedStrength");
- public HeroGPURenderManager()
- {
- StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
- }
- public void AddHeroGpuRender(HeroGPUMono gpuMono)
- {
- if (gpuRenderMap.TryGetValue(gpuMono.gameObject.name, out BetterList<HeroGPUMono> list))
- {
- list.Add(gpuMono);
- }
- else
- {
- list = new BetterList<HeroGPUMono>();
- list.Add(gpuMono);
- gpuRenderMap.Add(gpuMono.gameObject.name, list);
- }
- }
- public void RemoveHeroGpuRender(HeroGPUMono gpuMono)
- {
- if (gpuRenderMap.TryGetValue(gpuMono.gameObject.name, out BetterList<HeroGPUMono> list))
- {
- list.Remove(gpuMono);
- }
- }
- public void Update()
- {
- for (gpuRenderMap.Begin(); gpuRenderMap.Next();)
- {
- int count = gpuRenderMap.Value.Count;
- if (count <= 0)
- {
- continue;
- }
- MaterialPropertyBlock _materialPropertyBlock = new MaterialPropertyBlock();
- RenderParams rp = new RenderParams(gpuRenderMap.Value[0].Material);
- rp.matProps = _materialPropertyBlock;
- rp.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
- rp.receiveShadows = false;
- Matrix4x4[] instData = new Matrix4x4[count];
- Matrix4x4[] animtionStata = new Matrix4x4[count];
- float[] enableAnimations = new float[count];
- Vector4[] edgeColor = new Vector4[count];
- float[] edgeStrength = new float[count];
- float[] injuriedStrength = new float[count];
- for (int i = 0; i < count; i++)
- {
- HeroGPUMono heroGPUMono = gpuRenderMap.Value[i];
- instData[i] = heroGPUMono.rootTrans.localToWorldMatrix;
- animtionStata[i] = heroGPUMono.animtionState;
- edgeColor[i] = heroGPUMono.edgecolor;
- edgeStrength[i] = heroGPUMono.edgeStength;
- injuriedStrength[i] = heroGPUMono.injuriedStrength;
- enableAnimations[i] = 1;
- }
- _materialPropertyBlock.SetFloatArray(enableShaderId, enableAnimations);
- _materialPropertyBlock.SetMatrixArray(animtionStateId, animtionStata);
- _materialPropertyBlock.SetVectorArray(this.edgeColor, edgeColor);
- _materialPropertyBlock.SetFloatArray(this.edgeStrength, edgeStrength);
- _materialPropertyBlock.SetFloatArray(this.injuriedStrength, injuriedStrength);
- Graphics.RenderMeshInstanced(rp, gpuRenderMap.Value[0].Mesh, 0, instData);
- }
- }
- }
- }
|