Advertisement
cajphrase

ApplyLowResData

Aug 11th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. private void ApplyLowResData(LOD lod, NativeMesh nativeMesh, Mesh mesh)
  2. {
  3.     var parentLod = lod.GetParent(size);
  4.     if (!meshCache.TryGetValue(out var parentMesh))
  5.         throw new Exception("We should have always generated the parent");
  6.  
  7.     var biomes = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
  8.     var weight = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
  9.     mesh.GetUVs(1, biomes);
  10.     mesh.GetUVs(2, weight);
  11.  
  12.     if (parentMesh == null)
  13.     {
  14.         // no verts in the parent, set the data to the same as the current position
  15.         mesh.SetUVs(3, nativeMesh.vertices);
  16.         mesh.SetUVs(4, nativeMesh.normals);
  17.         mesh.SetUVs(5, biomes); // mesh should have biome info at this point
  18.         mesh.SetUVs(6, weight);
  19.         return;
  20.     }
  21.  
  22.  
  23.     var parentNativeMesh = new NativeMesh(parentMesh);
  24.     var aabb = parentLod.ToBounds(size);
  25.     VoxelBounds parentBounds = new(aabb.Min, aabb.Size, subdivs);
  26.  
  27.     var parentBiom = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
  28.     var parentWeig = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
  29.     parentMesh.GetUVs(5, parentBiom);
  30.     parentMesh.GetUVs(6, parentWeig);
  31.  
  32.     var lrVert = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
  33.     var lrNorm = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
  34.     var lrBiom = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
  35.     var lrWeig = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
  36.  
  37.     var job = new TransferLowResDataJob()
  38.     {
  39.         // put all the stuff in here, copilot should do this.
  40.     };
  41.     job.Run();
  42.  
  43.     mesh.SetUVs(3, lrVert);
  44.     mesh.SetUVs(4, lrNorm);
  45.     mesh.SetUVs(5, lrBiom);
  46.     mesh.SetUVs(6, lrWeig);
  47.  
  48.     biomes.Dispose();
  49.     weight.Dispose();
  50.     parentNativeMesh.Dispose();
  51.     parentBiom.Dispose();
  52.     parentWeig.Dispose();
  53.     lrVert.Dispose();
  54.     lrNorm.Dispose();
  55.     lrBiom.Dispose();
  56.     lrWeig.Dispose();
  57. }
  58.  
  59. private struct TransferLowResDataJob : IJob
  60. {
  61.     VoxelBounds parentBounds;
  62.  
  63.     [ReadOnly] NativeMesh nativeMesh;
  64.     [ReadOnly] NativeArray<float3> biomes;
  65.     [ReadOnly] NativeArray<float3> weight;
  66.     [ReadOnly] NativeMesh parentNativeMesh;
  67.     [ReadOnly] NativeArray<float3> parentBiom;
  68.     [ReadOnly] NativeArray<float3> parentWeig;
  69.  
  70.     NativeArray<float3> lrVert;
  71.     NativeArray<float3> lrNorm;
  72.     NativeArray<float3> lrBiom;
  73.     NativeArray<float3> lrWeig;
  74.  
  75.     public void Execute()
  76.     {
  77.         var lrVertMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
  78.         var lrNormMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
  79.         var lrBiomMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
  80.         var lrWeigMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
  81.  
  82.         for (int i = 0; i < parentNativeMesh.vertices.Length; i++)
  83.         {
  84.             int3 i3 = Index.PositionToIndex(parentNativeMesh.vertices[i], parentBounds.cellSize, parentBounds.position);
  85.             int index = Index.Int3ToFlat(i3, parentBounds.cubeCount);
  86.             lrVertMap[index] = parentNativeMesh.vertices[i];
  87.             lrNormMap[index] = parentNativeMesh.normals[i];
  88.             lrBiomMap[index] = parentBiom[i];
  89.             lrWeigMap[index] = parentWeig[i];
  90.         }
  91.  
  92.  
  93.         for (int i = 0; i < nativeMesh.vertices.Length; i++)
  94.         {
  95.             int3 i3 = Index.PositionToIndex(nativeMesh.vertices[i], parentBounds.cellSize, parentBounds.position);
  96.             int index = Index.Int3ToFlat(i3, parentBounds.cubeCount);
  97.             var hasData = lrVertMap[i] == default;
  98.             lrVert[index] = hasData ? lrVertMap[i] : nativeMesh.vertices[i];
  99.             lrNorm[index] = hasData ? lrNormMap[i] : nativeMesh.normals[i];
  100.             lrBiom[index] = hasData ? lrBiomMap[i] : biomes[i];
  101.             lrWeig[index] = hasData ? lrWeigMap[i] : weight[i];
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement