/// <summary> /// Like `EntityArray.ToArray` but for CDA. /// </summary> public static List <T> CopyToList <T>(this ComponentDataArray <T> cda) where T : struct, IComponentData { using (var na = new NativeArray <T>(cda.Length, Allocator.Temp)) { cda.CopyTo(na); List <T> list = new List <T>(na); return(list); } }
public void Execute() { UnityEngine.Assertions.Assert.AreEqual(sizeof(Matrix4x4), sizeof(EntityInstanceRendererTransform)); var matricesSlice = Unity.Collections.LowLevel.Unsafe.NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <EntityInstanceRendererTransform>(matricesPtr, sizeof(Matrix4x4), length); #if ENABLE_UNITY_COLLECTIONS_CHECKS Unity.Collections.LowLevel.Unsafe.NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle()); #endif transforms.CopyTo(matricesSlice, beginIndex); }
//----------------------------------------------------------------------------- public unsafe static void CopyMatrices(ComponentDataArray <TransformMatrix> transforms, int beginIndex, int length, Matrix4x4[] outMatrices) { fixed(Matrix4x4 *matricesPtr = outMatrices) { Assert.AreEqual(sizeof(Matrix4x4), sizeof(TransformMatrix)); var matricesSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <TransformMatrix>(matricesPtr, sizeof(Matrix4x4), length); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle()); #endif transforms.CopyTo(matricesSlice, beginIndex); } }
public unsafe static void CopyMatrices(ComponentDataArray <EntityInstanceRendererTransform> transforms, int beginIndex, int length, Matrix4x4[] outMatrices) { // @TODO: This is only unsafe because the Unity DrawInstances API takes a Matrix4x4[] instead of NativeArray. /// And we also want the code to be really fast. fixed(Matrix4x4 *matricesPtr = outMatrices) { UnityEngine.Assertions.Assert.AreEqual(sizeof(Matrix4x4), sizeof(EntityInstanceRendererTransform)); var matricesSlice = Unity.Collections.LowLevel.Unsafe.NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <EntityInstanceRendererTransform>(matricesPtr, sizeof(Matrix4x4), length); #if ENABLE_UNITY_COLLECTIONS_CHECKS Unity.Collections.LowLevel.Unsafe.NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle()); #endif transforms.CopyTo(matricesSlice, beginIndex); } }
void Render(int i, ref ComponentDataArray <Position> positionDataArray, ref ComponentDataArray <Heading> headingDataArray) { var length = positionDataArray.Length; if (length == 0) { return; } var sprite = sharedComponents[i]; var index = sharedIndices[i]; if (!buffers.TryGetValue(index, out var buffer)) { buffers.Add(index, buffer = new ComputeBuffer(length, PositionHeading.Stride)); sprite.material.SetBuffer(ShaderProperty_PositionBuffer, buffer); } if (buffer.count < length) { buffer.Release(); buffers.Add(index, buffer = new ComputeBuffer(length, PositionHeading.Stride)); sprite.material.SetBuffer(ShaderProperty_PositionBuffer, buffer); } //メッシュの頂点数 args[0] = sprite.mesh.GetIndexCount(0); // 何体描画するか args[1] = (uint)length; argsList[i].SetData(args, 0, 0, 2); // 時代はSpan<T>、ただしUnityの場合はNativeSlice<T> // Allocator.Persistentにしてクラスフィールドにしたほうがいいのかどうなのかいまいちわからない。 using (var position = new NativeArray <Position>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory)) using (var heading = new NativeArray <Heading>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory)) { var positionHeading = new NativeArray <PositionHeading>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); positionDataArray.CopyTo(position); for (int j = 0; j < positionHeading.Length; j++) { positionHeading[j] = new PositionHeading { Position = position[j].Value, HeadingX = heading[j].Value.x }; } buffer.SetData(positionHeading, 0, 0, length); positionHeading.Dispose(); } // 影を描画しないしさせない鉄の意志。 Graphics.DrawMeshInstancedIndirect(sprite.mesh, 0, sprite.material, Bounds, argsList[i], 0, null, ShadowCastingMode.Off, false, 0, Camera, LightProbeUsage.Off, null); }
// This is the ugly bit, necessary until Graphics.DrawMeshInstanced supports NativeArrays pulling the data in from a job. public unsafe static void CopyMatrices(ComponentDataArray <TransformMatrix> transforms, int beginIndex, int length, Matrix4x4[] outMatrices) { // @TODO: This is using unsafe code because the Unity DrawInstances API takes a Matrix4x4[] instead of NativeArray. // We want to use the ComponentDataArray.CopyTo method // because internally it uses memcpy to copy the data, // if the nativeslice layout matches the layout of the component data. It's very fast... fixed(Matrix4x4 *matricesPtr = outMatrices) { Assert.AreEqual(sizeof(Matrix4x4), sizeof(TransformMatrix)); var matricesSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <TransformMatrix>(matricesPtr, sizeof(Matrix4x4), length); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle()); #endif transforms.CopyTo(matricesSlice, beginIndex); } }