MyPlugin.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. using System.Runtime.InteropServices;
  4. using GPUECSAnimationBaker.Engine.AnimatorSystem;
  5. using Random = UnityEngine.Random;
  6. public class MyPlugin : MonoBehaviour
  7. {
  8. public Material material;
  9. public Mesh mesh;
  10. const int numInstances = 10;
  11. struct MyInstanceData
  12. {
  13. public Matrix4x4 objectToWorld;
  14. public float myOtherData;
  15. public uint renderingLayerMask;
  16. };
  17. // void Update()
  18. // {
  19. // RenderParams rp = new RenderParams(material);
  20. // MyInstanceData[] instData = new MyInstanceData[numInstances];
  21. // for(int i=0; i<numInstances; ++i)
  22. // {
  23. // instData[i].objectToWorld = Matrix4x4.Translate(new Vector3(-4.5f+i, 0.0f, 5.0f));
  24. // instData[i].renderingLayerMask = (i & 1) == 0 ? 1u : 2u;
  25. // }
  26. // Graphics.RenderMeshInstanced(rp, mesh, 0, instData);
  27. // }
  28. void Update()
  29. {
  30. MaterialPropertyBlock _materialPropertyBlock = new MaterialPropertyBlock();
  31. RenderParams rp = new RenderParams(material);
  32. Matrix4x4[] instData = new Matrix4x4[numInstances];
  33. Vector4[] colors = new Vector4[numInstances];
  34. Matrix4x4[] animMatrix4X4s = new Matrix4x4[numInstances];
  35. for (int i = 0; i < numInstances; ++i)
  36. {
  37. colors[i] = new Color(Random.Range(0f,1f), Random.Range(0f,1f), Random.Range(0f,1f));
  38. instData[i] = Matrix4x4.Translate(new Vector3(-4.5f + i, 0.0f, 5.0f));
  39. animMatrix4X4s[i]= Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(1, 1, 1));
  40. }
  41. // colors[0] = Color.green;
  42. // colors[1] = Color.red;
  43. // _materialPropertyBlock.
  44. _materialPropertyBlock.SetVectorArray("_mainColor",colors);
  45. _materialPropertyBlock.SetMatrixArray("_AnimationState",animMatrix4X4s);
  46. rp.matProps = _materialPropertyBlock;
  47. Graphics.RenderMeshInstanced(rp, mesh, 0, instData, numInstances,0);
  48. }
  49. // public GPUAnimtion GPUAnimtion;
  50. //
  51. // public int index;
  52. //
  53. // private void Start()
  54. // {
  55. // GPUAnimtion = new GPUAnimtion();
  56. // GpuEcsAnimatorBehaviour gpuEcsAnimatorBehaviour= gameObject.transform.GetComponentInChildren<GpuEcsAnimatorBehaviour>();
  57. // GPUAnimtion.Init(gpuEcsAnimatorBehaviour);
  58. // }
  59. //
  60. // [ContextMenu("dsada")]
  61. // public void TestSet()
  62. // {
  63. // GPUAnimtion.SetAnimtionIndex(index);
  64. // }
  65. //
  66. // private void Update()
  67. // {
  68. // GPUAnimtion.Update();
  69. // // Graphics.DrawMeshInstanced();
  70. // }
  71. // // 声明C++函数
  72. // [DllImport("ThreadTool")]
  73. // private static extern int Add(int a, int b);
  74. //
  75. // [DllImport("ThreadTool")]
  76. // private static extern void CallCSharpFunction(Callback callback);
  77. //
  78. // // 定义回调委托
  79. // public delegate int Callback(int value);
  80. //
  81. // // C#回调函数
  82. // private int MyCallback(int value)
  83. // {
  84. // Debug.Log("C# received value from C++: " + value);
  85. // return value * 2;
  86. // }
  87. //
  88. // void Start()
  89. // {
  90. // // 调用C++函数
  91. // int result = Add(5, 3);
  92. // Debug.Log("Result from C++: " + result); // 输出: Result from C++: 8
  93. //
  94. // // 注册C#回调函数并调用C++函数
  95. // CallCSharpFunction(MyCallback);
  96. // }
  97. }