This commit is contained in:
Rob Cupisz 2022-11-18 23:21:03 +01:00
Родитель 8fc6ff407f
Коммит 47437fa29b
3 изменённых файлов: 17 добавлений и 13 удалений

Просмотреть файл

@ -105,9 +105,6 @@ public class MeshToSDFEditor : Editor
if (mesh.GetTopology(0) != MeshTopology.Triangles) if (mesh.GetTopology(0) != MeshTopology.Triangles)
EditorGUILayout.HelpBox("Only triangular topology meshes supported (MeshTopology.Triangles).", MessageType.Error); EditorGUILayout.HelpBox("Only triangular topology meshes supported (MeshTopology.Triangles).", MessageType.Error);
if (mesh.indexFormat != UnityEngine.Rendering.IndexFormat.UInt16)
EditorGUILayout.HelpBox("Only 16 bit indices supported. The mesh is probably way too large.", MessageType.Error);
if (mesh.GetIndexCount(0) > 3 * 10000) if (mesh.GetIndexCount(0) > 3 * 10000)
EditorGUILayout.HelpBox("This looks like a large mesh. For best performance and a smoother SDF, use a proxy mesh of under 10k triangles.", MessageType.Warning); EditorGUILayout.HelpBox("This looks like a large mesh. For best performance and a smoother SDF, use a proxy mesh of under 10k triangles.", MessageType.Warning);

Просмотреть файл

@ -18,13 +18,21 @@ float _Offset;
#define THREAD_GROUP_SIZE 64 #define THREAD_GROUP_SIZE 64
bool _IndexFormat16bit;
uint GetIndex(ByteAddressBuffer indexBuffer, uint i) uint GetIndex(ByteAddressBuffer indexBuffer, uint i)
{ {
// 2 byte indices, Load() grabs 4 bytes at a time (4 byte aligned) so need to do some unpacking if (_IndexFormat16bit)
uint entryIndex = i >> 1u; {
uint entryOffset = i & 1u; // 2 byte indices, Load() grabs 4 bytes at a time (4 byte aligned) so need to do some unpacking
uint read = indexBuffer.Load(entryIndex << 2); uint entryIndex = i >> 1u;
return entryOffset == 1u ? ((read >> 16) & 0xffff) : read & 0xffff; uint entryOffset = i & 1u;
uint read = indexBuffer.Load(entryIndex << 2);
return entryOffset == 1u ? ((read >> 16) & 0xffff) : read & 0xffff;
}
else
// 4 byte indices
return indexBuffer.Load(i << 2);
} }
float _VertexBufferStride; float _VertexBufferStride;

Просмотреть файл

@ -86,6 +86,7 @@ If you need signed distance or just need a limited shell around your surface, us
GraphicsBuffer m_IndexBuffer = null; GraphicsBuffer m_IndexBuffer = null;
int m_VertexBufferStride; int m_VertexBufferStride;
int m_VertexBufferPosAttributeOffset; int m_VertexBufferPosAttributeOffset;
IndexFormat m_IndexFormat;
CommandBuffer m_CommandBuffer = null; CommandBuffer m_CommandBuffer = null;
const int kThreadCount = 64; const int kThreadCount = 64;
@ -112,6 +113,7 @@ If you need signed distance or just need a limited shell around your surface, us
internal static int g_CellSize = Shader.PropertyToID("g_CellSize"); internal static int g_CellSize = Shader.PropertyToID("g_CellSize");
internal static int g_VertexBuffer = Shader.PropertyToID("g_VertexBuffer"); internal static int g_VertexBuffer = Shader.PropertyToID("g_VertexBuffer");
internal static int g_IndexBuffer = Shader.PropertyToID("g_IndexBuffer"); internal static int g_IndexBuffer = Shader.PropertyToID("g_IndexBuffer");
internal static int _IndexFormat16bit = Shader.PropertyToID("_IndexFormat16bit");
internal static int _VertexBufferStride = Shader.PropertyToID("_VertexBufferStride"); internal static int _VertexBufferStride = Shader.PropertyToID("_VertexBufferStride");
internal static int _VertexBufferPosAttributeOffset = Shader.PropertyToID("_VertexBufferPosAttributeOffset"); internal static int _VertexBufferPosAttributeOffset = Shader.PropertyToID("_VertexBufferPosAttributeOffset");
internal static int _JumpOffset = Shader.PropertyToID("_JumpOffset"); internal static int _JumpOffset = Shader.PropertyToID("_JumpOffset");
@ -278,6 +280,7 @@ If you need signed distance or just need a limited shell around your surface, us
cmd.SetComputeBufferParam(m_Compute, kernel, Uniforms.g_VertexBuffer, m_VertexBuffer); cmd.SetComputeBufferParam(m_Compute, kernel, Uniforms.g_VertexBuffer, m_VertexBuffer);
cmd.SetComputeBufferParam(m_Compute, kernel, Uniforms.g_IndexBuffer, m_IndexBuffer); cmd.SetComputeBufferParam(m_Compute, kernel, Uniforms.g_IndexBuffer, m_IndexBuffer);
cmd.SetComputeIntParam(m_Compute, Uniforms._IndexFormat16bit, m_IndexFormat == IndexFormat.UInt16 ? 1 : 0);
cmd.SetComputeFloatParam(m_Compute, Uniforms._VertexBufferStride, m_VertexBufferStride); cmd.SetComputeFloatParam(m_Compute, Uniforms._VertexBufferStride, m_VertexBufferStride);
cmd.SetComputeFloatParam(m_Compute, Uniforms._VertexBufferPosAttributeOffset, m_VertexBufferPosAttributeOffset); cmd.SetComputeFloatParam(m_Compute, Uniforms._VertexBufferPosAttributeOffset, m_VertexBufferPosAttributeOffset);
@ -392,11 +395,6 @@ If you need signed distance or just need a limited shell around your surface, us
Debug.LogError("MeshToSDF needs a mesh with triangle topology.", this); Debug.LogError("MeshToSDF needs a mesh with triangle topology.", this);
return false; return false;
} }
if (mesh.indexFormat != IndexFormat.UInt16)
{
Debug.LogError("MeshToSDF needs a mesh with 16 bit index format (mesh probably too big).", this);
return false;
}
int stream = mesh.GetVertexAttributeStream(VertexAttribute.Position); int stream = mesh.GetVertexAttributeStream(VertexAttribute.Position);
if (stream < 0) if (stream < 0)
@ -405,6 +403,7 @@ If you need signed distance or just need a limited shell around your surface, us
return false; return false;
} }
m_IndexFormat = mesh.indexFormat;
m_VertexBufferStride = mesh.GetVertexBufferStride(stream); m_VertexBufferStride = mesh.GetVertexBufferStride(stream);
m_VertexBufferPosAttributeOffset = mesh.GetVertexAttributeOffset(VertexAttribute.Position); m_VertexBufferPosAttributeOffset = mesh.GetVertexAttributeOffset(VertexAttribute.Position);
m_VertexBuffer = m_SkinnedMeshRenderer != null ? m_SkinnedMeshRenderer.GetVertexBuffer() : mesh.GetVertexBuffer(stream); m_VertexBuffer = m_SkinnedMeshRenderer != null ? m_SkinnedMeshRenderer.GetVertexBuffer() : mesh.GetVertexBuffer(stream);