DESKTOP-BGJIU14\ck vor 4 Monaten
Ursprung
Commit
9b43cf7dda

+ 76 - 0
Assets/Scripts/GameLogic/Combat/Hero/HeroGPU/GPURender.cs

@@ -0,0 +1,76 @@
+using System;
+using UnityEngine;
+
+namespace GameLogic.Combat.Hero.HeroGPU
+{
+    public class GPURender
+    {
+        private static readonly int shader_enableShaderId = Shader.PropertyToID("_EnableAnimation");
+        readonly int shader_animtionStateId = Shader.PropertyToID("_AnimationState");
+        readonly int shader_edgeColor = Shader.PropertyToID("_edgeColor");
+        readonly int shader_edgeStrength = Shader.PropertyToID("_edgeStrength");
+        readonly int shader_injuriedStrength = Shader.PropertyToID("_injuriedStrength");
+        public Material Material;
+        public Mesh Mesh;
+        public MaterialPropertyBlock _materialPropertyBlock;
+        BetterList<Matrix4x4> instData = new BetterList<Matrix4x4>();
+        BetterList<Matrix4x4> animtionStata = new BetterList<Matrix4x4>();
+        BetterList<float> enableAnimations = new BetterList<float>();
+        BetterList<Vector4> edgeColor = new BetterList<Vector4>();
+        BetterList<float> edgeStrength = new BetterList<float>();
+        BetterList<float> injuriedStrength = new BetterList<float>();
+
+        private RenderParams rp;
+
+        public void Init(Material Material, Mesh Mesh)
+        {
+            this.Material = Material;
+            this.Mesh = Mesh;
+            _materialPropertyBlock = new MaterialPropertyBlock();
+            rp = new RenderParams(Material);
+            rp.matProps = _materialPropertyBlock;
+            rp.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
+            rp.receiveShadows = false;
+        }
+
+        public void Render(BetterList<HeroGPUMono> gpuMonoList)
+        {
+            int count = gpuMonoList.Count;
+            if (count <= 0)
+            {
+                return;
+            }
+            _materialPropertyBlock.Clear();
+
+            // 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];
+            instData.Clear();
+            animtionStata.Clear();
+            enableAnimations.Clear();
+            edgeColor.Clear();
+            edgeStrength.Clear();
+            injuriedStrength.Clear();
+            for (int i = 0; i < count; i++)
+            {
+                HeroGPUMono heroGPUMono = gpuMonoList[i];
+                instData.Add(heroGPUMono.rootTrans.localToWorldMatrix);
+                animtionStata.Add( heroGPUMono.animtionState);
+                edgeColor.Add(heroGPUMono.edgecolor);
+                edgeStrength.Add( heroGPUMono.edgeStength);
+                injuriedStrength.Add( heroGPUMono.injuriedStrength);
+                enableAnimations.Add( 1);
+            }
+            
+            _materialPropertyBlock.SetFloatArray(shader_enableShaderId, enableAnimations.ToArray());
+            _materialPropertyBlock.SetMatrixArray(shader_animtionStateId, animtionStata.ToArray());
+            _materialPropertyBlock.SetVectorArray(shader_edgeColor, edgeColor.ToArray());
+            _materialPropertyBlock.SetFloatArray(shader_edgeStrength, edgeStrength.ToArray());
+            _materialPropertyBlock.SetFloatArray(shader_injuriedStrength, injuriedStrength.ToArray());
+            Graphics.RenderMeshInstanced(rp, Mesh, 0, instData.ToArray(), count,0);
+        }
+    }
+}

+ 3 - 0
Assets/Scripts/GameLogic/Combat/Hero/HeroGPU/GPURender.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 2a75912db01546fd9cdc25880c318d60
+timeCreated: 1732801630

+ 34 - 32
Assets/Scripts/GameLogic/Combat/Hero/HeroGPU/HeroGPURenderManager.cs

@@ -7,12 +7,8 @@ namespace GameLogic.Combat.Hero.HeroGPU
     {
         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");
-
+       protected Map<string, GPURender> gpuRender = new Map<string, GPURender>();
+        // MaterialPropertyBlock _materialPropertyBlock = new MaterialPropertyBlock();
         public HeroGPURenderManager()
         {
             StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
@@ -50,34 +46,40 @@ namespace GameLogic.Combat.Hero.HeroGPU
                     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++)
+                if (!gpuRender.TryGetValue(gpuRenderMap.Key, out GPURender render))
                 {
-                    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;
+                    render = new GPURender();
+                    render.Init(gpuRenderMap.Value[0].Material,gpuRenderMap.Value[0].Mesh);
+                    gpuRender.Add(gpuRenderMap.Key, render);
                 }
-
-                _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);
+                render.Render(gpuRenderMap.Value);
+                // 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);
             }
         }
     }