Skip to content

Commit 10c6dc3

Browse files
committed
Adding multi-mesh SDF script.
Takes arbitrary skinned meshes, bakes out one combined and 3D scaled SDF.
1 parent 14f2ba6 commit 10c6dc3

7 files changed

+342
-17
lines changed

MeshToSDF.unitypackage

8.8 KB
Binary file not shown.

MeshToSDF/MeshToSDF.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
4-
using UnityEngine.Experimental.VFX;
4+
using UnityEngine.VFX;
55

66
[ExecuteAlways]
77
public class MeshToSDF : MonoBehaviour
@@ -38,15 +38,14 @@ public class MeshToSDF : MonoBehaviour
3838
public int sdfResolution = 64;
3939

4040
[Tooltip("Offset the mesh before voxelization")]
41-
public Vector3 offset;
41+
public Vector3 offset = new Vector3(0.5f,0.5f,0.5f);
4242
[Tooltip("Scale the mesh before voxelization")]
43-
public float scaleBy = 1.0f;
43+
public Vector3 scaleBy = Vector3.one;
4444
[Tooltip("Number of points to sample on each triangle when voxeling")]
4545
public uint samplesPerTriangle = 10;
4646
[Tooltip("Thicken the signed distance field by this amount")]
4747
public float postProcessThickness = 0.01f;
4848

49-
5049
Mesh prevMesh;
5150

5251
// kernel ids
@@ -178,7 +177,7 @@ private Vector3 Div(Vector3 a, Vector3 b) {
178177
}
179178

180179
public RenderTexture MeshToVoxel(int voxelResolution, Mesh mesh,
181-
Vector3 offset, float scaleMeshBy, uint numSamplesPerTriangle,
180+
Vector3 offset, Vector3 scaleMeshBy, uint numSamplesPerTriangle,
182181
RenderTexture voxels = null) {
183182
var indicies = mesh.triangles;
184183
var numIdxes = indicies.Length;
@@ -201,7 +200,7 @@ public RenderTexture MeshToVoxel(int voxelResolution, Mesh mesh,
201200
MtVImplementation.SetInt("tris", numTris);
202201
MtVImplementation.SetFloats("offset", offset.x, offset.y, offset.z);
203202
MtVImplementation.SetInt("numSamples", (int)numSamplesPerTriangle);
204-
MtVImplementation.SetFloat("scale", scaleMeshBy);
203+
MtVImplementation.SetVector("scale3d", scaleMeshBy);
205204
MtVImplementation.SetInt("voxelSide", (int)voxelResolution);
206205

207206
if(voxels == null) {

MeshToSDF/MeshToSDF.prefab

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ MonoBehaviour:
4343
m_Script: {fileID: 11500000, guid: 9045d133f640d214cbce5e5af2b582bd, type: 3}
4444
m_Name:
4545
m_EditorClassIdentifier:
46+
executeInEditMode: 1
4647
JFAImplementation: {fileID: 7200000, guid: 3c29260ac4ca05f4780f9a4c44f20268, type: 3}
4748
MtVImplementation: {fileID: 7200000, guid: 39d9f785515d9ba49a5753410e0020fb, type: 3}
4849
outputRenderTexture: {fileID: 0}
49-
offsetTransform: {fileID: 0}
50-
scaleTransform: {fileID: 0}
51-
mesh: {fileID: 10208, guid: 0000000000000000e000000000000000, type: 0}
50+
staticMesh: {fileID: 4493585093827132993, guid: 733b559a024179547b08373652d74704,
51+
type: 3}
5252
skinnedMeshRenderer: {fileID: 0}
5353
materialOutput: {fileID: 2100000, guid: 92f7ad2f404ea904ba517bfd0b0d54ca, type: 2}
5454
materialProperty: _Texture3D
5555
vfxOutput: {fileID: 0}
5656
vfxProperty: Texture3D
57+
fieldsChanged: 1
5758
sdfResolution: 64
5859
offset: {x: 0.5, y: 0.5, z: 0.5}
59-
scaleBy: 0.5
60+
scaleBy: {x: 1, y: 1, z: 1}
6061
samplesPerTriangle: 64
6162
postProcessThickness: 0.01

MeshToSDF/MeshToVoxel.compute

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ int numSamples;
99
half scale;
1010
half3 offset;
1111
uint voxelSide;
12+
half3 scale3d;
1213

1314
StructuredBuffer<half3> VertexBuffer;
1415
StructuredBuffer<int> IndexBuffer;
@@ -19,7 +20,6 @@ half2 plastic(half index) {
1920
return half2(0.5 + frac(0.7548776662466927 * index), 0.5 + frac(0.5698402909980532 * index));
2021
}
2122

22-
2323
// sample num, triangle basis vectors
2424
half3 triangleSample(int n, half3 A, half3 B) {
2525
half2 s = plastic(half(n));
@@ -38,9 +38,9 @@ void MeshToVoxel(uint3 id : SV_DispatchThreadID)
3838
{
3939
uint triID = id.x * 3;
4040
if (triID >= tris * 3) return;
41-
half3 a = VertexBuffer[IndexBuffer[triID + 0]] * scale + offset;
42-
half3 b = VertexBuffer[IndexBuffer[triID + 1]] * scale + offset;
43-
half3 c = VertexBuffer[IndexBuffer[triID + 2]] * scale + offset;
41+
half3 a = VertexBuffer[IndexBuffer[triID + 0]] * scale3d + offset;
42+
half3 b = VertexBuffer[IndexBuffer[triID + 1]] * scale3d + offset;
43+
half3 c = VertexBuffer[IndexBuffer[triID + 2]] * scale3d + offset;
4444
half3 AB = b - a;
4545
half3 AC = c - a;
4646
half side = half(voxelSide);
@@ -49,7 +49,7 @@ void MeshToVoxel(uint3 id : SV_DispatchThreadID)
4949
half2 s = half2(frac(0.7548776662466927 * i), frac(0.5698402909980532 * i));
5050
s = s.x + s.y > 1.0 ? 1.0 - s : s;
5151
half3 pointOnTri = a + s.x * AB + s.y * AC;
52-
half3 scaled = pointOnTri * side;
52+
half3 scaled = pointOnTri * (side);
5353
uint3 voxelIdx = uint3(floor(scaled));
5454
if (!(any(voxelIdx < 0) || any(voxelIdx >= voxelSide))) {
5555
half distFromCenter = 1.0 - length(frac(scaled) - half3(0.5, 0.5, 0.5));

0 commit comments

Comments
 (0)