sort triangles sup opengl

This commit is contained in:
StarBeats 2026-02-05 20:09:22 +08:00
parent 7084bacb84
commit 965117b271

View File

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