diff --git a/Assets/Scripts/Features/OIT/SortTriangles/SortTrianglesOIT.cs b/Assets/Scripts/Features/OIT/SortTriangles/SortTrianglesOIT.cs index f802daf..2c0d2e2 100644 --- a/Assets/Scripts/Features/OIT/SortTriangles/SortTrianglesOIT.cs +++ b/Assets/Scripts/Features/OIT/SortTriangles/SortTrianglesOIT.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.Rendering; @@ -27,6 +28,8 @@ public class SortTrianglesOIT : MonoBehaviour private int indexStart; [SerializeField, HideInInspector] private uint meshIndicesCnt; + [SerializeField, HideInInspector] + private List subMeshDescriptors = new(); private static readonly Vector3[] viewDirections = { @@ -67,7 +70,7 @@ public class SortTrianglesOIT : MonoBehaviour return; } #if UNITY_EDITOR - if(Time.frameCount % 3 != 0 + if (Time.frameCount % 3 != 0 && Application.isPlaying ) { @@ -96,13 +99,38 @@ public class SortTrianglesOIT : MonoBehaviour var indices = sortedIndes[index].Indices; - //mesh.SetIndexBufferParams((int)meshIndicesCnt, mesh.indexFormat); - //mesh.SetIndexBufferData(indices, 0, indexStart, indices.Length, - // MeshUpdateFlags.DontValidateIndices | - // MeshUpdateFlags.DontResetBoneBounds | - // MeshUpdateFlags.DontNotifyMeshUsers | - // MeshUpdateFlags.DontRecalculateBounds - // ); + //mesh.SetIndexBufferParams((int)meshIndicesCnt, IndexFormat.UInt32); + //var submesh0Indices = mesh.GetIndices(0); + //var allIndices = new int[meshIndicesCnt]; + //Buffer.BlockCopy(submesh0Indices, 0, allIndices, 0, submesh0Indices.Length); + //Buffer.BlockCopy(indices, 0, allIndices, submesh0Indices.Length, indices.Length); + + //mesh.SetIndexBufferData(allIndices, 0, 0, allIndices.Length, + // MeshUpdateFlags.DontValidateIndices | + // MeshUpdateFlags.DontResetBoneBounds | + // MeshUpdateFlags.DontNotifyMeshUsers | + // MeshUpdateFlags.DontRecalculateBounds + // ); + + ////mesh.SetIndexBufferData(indices, 0, indexStart, indices.Length, + //// MeshUpdateFlags.DontValidateIndices | + //// MeshUpdateFlags.DontResetBoneBounds | + //// MeshUpdateFlags.DontNotifyMeshUsers | + //// MeshUpdateFlags.DontRecalculateBounds + //// ); + //mesh.subMeshCount = subMeshDescriptors.Count; + //for (int i = 0; i < subMeshDescriptors.Count; i++) + //{ + // mesh.SetSubMesh(i, subMeshDescriptors[i], + // MeshUpdateFlags.DontValidateIndices | + // MeshUpdateFlags.DontResetBoneBounds | + // MeshUpdateFlags.DontNotifyMeshUsers | + // MeshUpdateFlags.DontRecalculateBounds + // ); + // mesh.bounds.Encapsulate(subMeshDescriptors[i].bounds); + //} + + mesh.SetIndices(indices, MeshTopology.Triangles, SubMeshIndex, false); lastIndex = index; } @@ -135,6 +163,11 @@ public class SortTrianglesOIT : MonoBehaviour public int CompareTo(PrimitiveDepth other) { var i = depth - other.depth; + if(!SystemInfo.usesReversedZBuffer) + { + i *= -1; + } + if (i > 0) { return 1; @@ -202,8 +235,10 @@ public class SortTrianglesOIT : MonoBehaviour Bounds bounds = GetComponent().bounds; float radius = bounds.extents.magnitude; + subMeshDescriptors.Clear(); for (int i = 0; i < mesh.subMeshCount; i++) { + subMeshDescriptors.Add(mesh.GetSubMesh(i)); meshIndicesCnt += mesh.GetIndexCount(i); } if (SubMeshIndex >= mesh.subMeshCount)