123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using System;
- using UnityEngine;
- using System.Runtime.InteropServices;
- using GPUECSAnimationBaker.Engine.AnimatorSystem;
- using Random = UnityEngine.Random;
- public class MyPlugin : MonoBehaviour
- {
-
-
-
-
- public Material material;
- public Mesh mesh;
- const int numInstances = 10;
- struct MyInstanceData
- {
- public Matrix4x4 objectToWorld;
- public float myOtherData;
- public uint renderingLayerMask;
- };
- // void Update()
- // {
- // RenderParams rp = new RenderParams(material);
- // MyInstanceData[] instData = new MyInstanceData[numInstances];
- // for(int i=0; i<numInstances; ++i)
- // {
- // instData[i].objectToWorld = Matrix4x4.Translate(new Vector3(-4.5f+i, 0.0f, 5.0f));
- // instData[i].renderingLayerMask = (i & 1) == 0 ? 1u : 2u;
- // }
- // Graphics.RenderMeshInstanced(rp, mesh, 0, instData);
- // }
-
- void Update()
- {
- MaterialPropertyBlock _materialPropertyBlock = new MaterialPropertyBlock();
-
- RenderParams rp = new RenderParams(material);
-
- Matrix4x4[] instData = new Matrix4x4[numInstances];
- Vector4[] colors = new Vector4[numInstances];
- Matrix4x4[] animMatrix4X4s = new Matrix4x4[numInstances];
- for (int i = 0; i < numInstances; ++i)
- {
- colors[i] = new Color(Random.Range(0f,1f), Random.Range(0f,1f), Random.Range(0f,1f));
- instData[i] = Matrix4x4.Translate(new Vector3(-4.5f + i, 0.0f, 5.0f));
- animMatrix4X4s[i]= Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(1, 1, 1));
- }
- // colors[0] = Color.green;
- // colors[1] = Color.red;
- // _materialPropertyBlock.
- _materialPropertyBlock.SetVectorArray("_mainColor",colors);
- _materialPropertyBlock.SetMatrixArray("_AnimationState",animMatrix4X4s);
- rp.matProps = _materialPropertyBlock;
- Graphics.RenderMeshInstanced(rp, mesh, 0, instData, numInstances,0);
- }
-
-
-
-
-
- // public GPUAnimtion GPUAnimtion;
- //
- // public int index;
- //
- // private void Start()
- // {
- // GPUAnimtion = new GPUAnimtion();
- // GpuEcsAnimatorBehaviour gpuEcsAnimatorBehaviour= gameObject.transform.GetComponentInChildren<GpuEcsAnimatorBehaviour>();
- // GPUAnimtion.Init(gpuEcsAnimatorBehaviour);
- // }
- //
- // [ContextMenu("dsada")]
- // public void TestSet()
- // {
- // GPUAnimtion.SetAnimtionIndex(index);
- // }
- //
- // private void Update()
- // {
- // GPUAnimtion.Update();
- // // Graphics.DrawMeshInstanced();
- // }
-
- // // 声明C++函数
- // [DllImport("ThreadTool")]
- // private static extern int Add(int a, int b);
- //
- // [DllImport("ThreadTool")]
- // private static extern void CallCSharpFunction(Callback callback);
- //
- // // 定义回调委托
- // public delegate int Callback(int value);
- //
- // // C#回调函数
- // private int MyCallback(int value)
- // {
- // Debug.Log("C# received value from C++: " + value);
- // return value * 2;
- // }
- //
- // void Start()
- // {
- // // 调用C++函数
- // int result = Add(5, 3);
- // Debug.Log("Result from C++: " + result); // 输出: Result from C++: 8
- //
- // // 注册C#回调函数并调用C++函数
- // CallCSharpFunction(MyCallback);
- // }
- }
|