Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ApplyLowResData(LOD lod, NativeMesh nativeMesh, Mesh mesh)
- {
- var parentLod = lod.GetParent(size);
- if (!meshCache.TryGetValue(out var parentMesh))
- throw new Exception("We should have always generated the parent");
- var biomes = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
- var weight = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
- mesh.GetUVs(1, biomes);
- mesh.GetUVs(2, weight);
- if (parentMesh == null)
- {
- // no verts in the parent, set the data to the same as the current position
- mesh.SetUVs(3, nativeMesh.vertices);
- mesh.SetUVs(4, nativeMesh.normals);
- mesh.SetUVs(5, biomes); // mesh should have biome info at this point
- mesh.SetUVs(6, weight);
- return;
- }
- var parentNativeMesh = new NativeMesh(parentMesh);
- var aabb = parentLod.ToBounds(size);
- VoxelBounds parentBounds = new(aabb.Min, aabb.Size, subdivs);
- var parentBiom = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
- var parentWeig = new NativeArray<float3>(parentNativeMesh.vertices.Length, Allocator.TempJob);
- parentMesh.GetUVs(5, parentBiom);
- parentMesh.GetUVs(6, parentWeig);
- var lrVert = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
- var lrNorm = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
- var lrBiom = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
- var lrWeig = new NativeArray<float3>(nativeMesh.vertices.Length, Allocator.TempJob);
- var job = new TransferLowResDataJob()
- {
- // put all the stuff in here, copilot should do this.
- };
- job.Run();
- mesh.SetUVs(3, lrVert);
- mesh.SetUVs(4, lrNorm);
- mesh.SetUVs(5, lrBiom);
- mesh.SetUVs(6, lrWeig);
- biomes.Dispose();
- weight.Dispose();
- parentNativeMesh.Dispose();
- parentBiom.Dispose();
- parentWeig.Dispose();
- lrVert.Dispose();
- lrNorm.Dispose();
- lrBiom.Dispose();
- lrWeig.Dispose();
- }
- private struct TransferLowResDataJob : IJob
- {
- VoxelBounds parentBounds;
- [ReadOnly] NativeMesh nativeMesh;
- [ReadOnly] NativeArray<float3> biomes;
- [ReadOnly] NativeArray<float3> weight;
- [ReadOnly] NativeMesh parentNativeMesh;
- [ReadOnly] NativeArray<float3> parentBiom;
- [ReadOnly] NativeArray<float3> parentWeig;
- NativeArray<float3> lrVert;
- NativeArray<float3> lrNorm;
- NativeArray<float3> lrBiom;
- NativeArray<float3> lrWeig;
- public void Execute()
- {
- var lrVertMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
- var lrNormMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
- var lrBiomMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
- var lrWeigMap = new NativeArray<float3>(parentBounds.TotalCubeCount, Allocator.Temp);
- for (int i = 0; i < parentNativeMesh.vertices.Length; i++)
- {
- int3 i3 = Index.PositionToIndex(parentNativeMesh.vertices[i], parentBounds.cellSize, parentBounds.position);
- int index = Index.Int3ToFlat(i3, parentBounds.cubeCount);
- lrVertMap[index] = parentNativeMesh.vertices[i];
- lrNormMap[index] = parentNativeMesh.normals[i];
- lrBiomMap[index] = parentBiom[i];
- lrWeigMap[index] = parentWeig[i];
- }
- for (int i = 0; i < nativeMesh.vertices.Length; i++)
- {
- int3 i3 = Index.PositionToIndex(nativeMesh.vertices[i], parentBounds.cellSize, parentBounds.position);
- int index = Index.Int3ToFlat(i3, parentBounds.cubeCount);
- var hasData = lrVertMap[i] == default;
- lrVert[index] = hasData ? lrVertMap[i] : nativeMesh.vertices[i];
- lrNorm[index] = hasData ? lrNormMap[i] : nativeMesh.normals[i];
- lrBiom[index] = hasData ? lrBiomMap[i] : biomes[i];
- lrWeig[index] = hasData ? lrWeigMap[i] : weight[i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement