Class MudRenderer
A renderer generates and renders dynamic meshes based on the brushes under its transform hierarchy.
It can generate SDF texture generations at dynamically at run-time, which can be combined with Unity's VFX graph for things like GPU particle collision detection.
It also provides various CPU-based utilities that can used for gameplay purposes without needing GPU readbacks:
- SDF evaluation.
- SDF normal (normalized gradient) evaluation.
- Raycasts against mesh surface.
Namespace: MudBun
Assembly: cs.temp.dll.dll
Syntax
public class MudRenderer : MudRendererBase
Methods
AddCollider(GameObject, Boolean, Mesh, Boolean, Boolean)
Declaration
public override Mesh AddCollider(GameObject go, bool async, Mesh mesh = null, bool forceConvexCollider = false, bool makeRigidBody = false)
Parameters
Type | Name | Description |
---|---|---|
GameObject | go | |
Boolean | async | |
Mesh | mesh | |
Boolean | forceConvexCollider | |
Boolean | makeRigidBody |
Returns
Type | Description |
---|---|
Mesh |
EvaluateNormal(Vector3, Single)
Synchronous CPU-based SDF normal (normalized gradient) evaluation that takes a single sample position and returns a single result. Function computes on the main thread and only returns when the entire computation is done.
Declaration
public Sdf.Result EvaluateNormal(Vector3 p, float maxSurfaceDistance)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | p | The sample position. |
Single | maxSurfaceDistance | The range of evaluation from the sample position. Only brushes within this range are considered for evaluation. Use a conservative estimate of distance from the SDF's zero isosurface. |
Returns
Type | Description |
---|---|
Sdf.Result | Result containing the SDf normal. The SDF value field of the result would be left empty. |
EvaluateNormalAsync(NativeArray<Vector3>, NativeArray<Sdf.Result>, Single)
Asynchronous CPU-based SDF normal (normalized gradient) evaluation. Function schedules a job that will compute on multiple threads.
Declaration
public Sdf.EvalJobHandle EvaluateNormalAsync(NativeArray<Vector3> samples, NativeArray<Sdf.Result> results, float maxSurfaceDistance)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Vector3> | samples | Array of sample positions. |
NativeArray<Sdf.Result> | results | Array of output results containing the SDF normals. |
Single | maxSurfaceDistance |
Returns
Type | Description |
---|---|
Sdf.EvalJobHandle | A job handle that can be waited on for completion. The SDF value field of the result would be left empty. |
EvaluateSdf(Vector3, Single, Boolean)
Synchronous CPU-based SDF evaluation that takes a single sample position and returns a single result. Function computes on the main thread and only returns when the entire computation is done.
The result is inaccurate for scaled renderers, and is likely to be less accurate when non-union/blended brushes are involved due to SDF approximation (that still works for meshing algorithms and SDF raycasts). In these cases, use the more expensive EvaluateSnapToSurface
and EvaluateSnapToSurfaceAsync
and compute the distance between the sample point and contact point.
Declaration
public Sdf.Result EvaluateSdf(Vector3 p, float maxSurfaceDistance, bool computeMaterials)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | p | The sample position. |
Single | maxSurfaceDistance | The range of evaluation from the sample position. Only brushes within this range are considered for evaluation. Use a conservative estimate of distance from the SDF's zero isosurface. |
Boolean | computeMaterials | Whether to compute the material at the sample point as well. |
Returns
Type | Description |
---|---|
Sdf.Result | Result containing the SDF value, plus material if |
EvaluateSdfAndNormal(Vector3, Single, Boolean)
Synchronous CPU-based SDF value and normal (normalized gradient) evaluation that takes a single sample position and returns a single result. Function computes on the main thread and only returns when the entire computation is done.
The SDF result is inaccurate for scaled renderers, and is likely to be less accurate when non-union/blended brushes are involved due to SDF approximation (that still works for meshing algorithms and SDF raycasts). In these cases, use the more expensive EvaluateSnapToSurface
and EvaluateSnapToSurfaceAsync
and compute the distance between the sample point and contact point.
Declaration
public Sdf.Result EvaluateSdfAndNormal(Vector3 p, float maxSurfaceDistance, bool computeMaterials)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | p | The sample position |
Single | maxSurfaceDistance | The range of evaluation from the sample position. Only brushes within this range are considered for evaluation. Use a conservative estimate of distance from the SDF's zero isosurface. |
Boolean | computeMaterials | Whether to compute the materials at sample points as well. |
Returns
Type | Description |
---|---|
Sdf.Result | Result containing the SDF value and normal, plus material if |
EvaluateSdfAndNormalAsync(NativeArray<Vector3>, NativeArray<Sdf.Result>, Single, Boolean)
Asynchronous CPU-based SDF value and normal (normalized gradient) evaluation. Function schedules a job that will compute on multiple threads.
SDF results are inaccurate for scaled renderers, and are likely to be less accurate when non-union/blended brushes are involved due to SDF approximation (that still works for meshing algorithms and SDF raycasts). In these cases, use the more expensive EvaluateSnapToSurface
and EvaluateSnapToSurfaceAsync
and compute the distances between the samples point and contact points.
Declaration
public Sdf.EvalJobHandle EvaluateSdfAndNormalAsync(NativeArray<Vector3> samples, NativeArray<Sdf.Result> results, float maxSurfaceDistance, bool computeMaterials)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Vector3> | samples | Array of sample positions |
NativeArray<Sdf.Result> | results | Array of output results containing the SDF values and normals, plus materials if |
Single | maxSurfaceDistance | The range of evaluation from the sample positions. Only brushes within this range are considered for evaluation. Use a conservative estimate of distance from the SDF's zero isosurface. |
Boolean | computeMaterials | Whether to compute the materials at sample points as well. |
Returns
Type | Description |
---|---|
Sdf.EvalJobHandle | A job handle that can be waited on for completion. |
EvaluateSdfAsync(NativeArray<Vector3>, NativeArray<Sdf.Result>, Single, Boolean)
Asynchronous CPU-based SDF evaluation. Function schedules a job that will compute on multiple threads.
Results are inaccurate for scaled renderers, and are likely to be less accurate when non-union/blended brushes are involved due to SDF approximation (that still works for meshing algorithms and SDF raycasts). In these cases, use the more expensive EvaluateSnapToSurface
and EvaluateSnapToSurfaceAsync
and compute the distances between the samples point and contact points.
Declaration
public Sdf.EvalJobHandle EvaluateSdfAsync(NativeArray<Vector3> samples, NativeArray<Sdf.Result> results, float maxSurfaceDistance, bool computeMaterials)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Vector3> | samples | Array of sample position |
NativeArray<Sdf.Result> | results | Array of output results containing the SDF values, plus materials if |
Single | maxSurfaceDistance | The range of evaluation from the sample positions. Only brushes within this range are considered for evaluation. Use a conservative estimate of distance from the SDF's zero isosurface. |
Boolean | computeMaterials | Whether to compute the materials at sample points as well. |
Returns
Type | Description |
---|---|
Sdf.EvalJobHandle | A job handle that can be waited on for completion. The normal field of the result would be left empty. |
GenerateSdf(RenderTexture, Vector3, Vector3)
Generates an SDF texture into a RenderTexture object.
Declaration
public override void GenerateSdf(RenderTexture sdf, Vector3 origin, Vector3 dimension)
Parameters
Type | Name | Description |
---|---|---|
RenderTexture | sdf | The output texture. |
Vector3 | origin | Point in renderer space mapped to the center of output texture. You can use the renderer's inverse transform to convert a point in world space into the renderer's local space. |
Vector3 | dimension | Dimensions in renderer space mapped to the size output texture. |
GenerateSdf(Texture3D, Vector3, Vector3)
Generates an SDF texture into a Texture3D object.
Declaration
public override void GenerateSdf(Texture3D sdf, Vector3 origin, Vector3 dimension)
Parameters
Type | Name | Description |
---|---|---|
Texture3D | sdf | The output texture. |
Vector3 | origin | Point in renderer space mapped to the center of output texture. You can use the renderer's inverse transform to convert a point in world space into the renderer's local space. |
Vector3 | dimension | Dimensions in renderer space mapped to the size output texture. |
LockMesh(Boolean, Boolean, Mesh, Boolean, Boolean, Boolean, Boolean)
Declaration
public override void LockMesh(bool autoRigging, bool async, Mesh mesh = null, bool generateTextureUV = false, bool generateLightMapUV = false, bool weldVertices = false, bool optimizeMeshForRendering = false)
Parameters
Type | Name | Description |
---|---|---|
Boolean | autoRigging | |
Boolean | async | |
Mesh | mesh | |
Boolean | generateTextureUV | |
Boolean | generateLightMapUV | |
Boolean | weldVertices | |
Boolean | optimizeMeshForRendering |
Raycast(Vector3, Vector3, Single)
Raycast against all renderers.
Declaration
public static Sdf.Contact Raycast(Vector3 from, Vector3 direction, float maxDistance)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | from | The starting point of the ray. |
Vector3 | direction | The direction of the ray. |
Single | maxDistance | The maximum distance traveldd by the ray. |
Returns
Type | Description |
---|---|
Sdf.Contact | Raycast result. |
Raycast(Vector3, Vector3, Single, Boolean, Int32, Single, Boolean)
Synchronous CPU-based SDF raycast that takes a single ray and returns the raycast result. Function computes on the main thread and only returns when the entire computation is done.
Declaration
public Sdf.Contact Raycast(Vector3 from, Vector3 direction, float maxDistance, bool computeMaterials, int maxSteps = 128, float margin = 0.01F, bool forceZeroBlendUnion = false)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | from | The starting point of the ray. |
Vector3 | direction | The direction of the ray. |
Single | maxDistance | The maximum travel distance the ray. |
Boolean | computeMaterials | Whether to compute the material at the contact point (if hit) as well. |
Int32 | maxSteps | The maximum number of iterations to step the ray. If a hit hasn't been found after the maximum steps have been taken, then the raycast is a miss. |
Single | margin | The raycast is considered a hit if it ever reaches a point where the absolute SDF value is less than the margin. Smaller margin typically requires more steps to find a hit. |
Boolean | forceZeroBlendUnion | Whether to force all brushes to be treated as if they have zero blends with union operators. Useful for when needing to raycast against each brush's raw shape (e.g. for click-selection of mostly subtractive brushes). |
Returns
Type | Description |
---|---|
Sdf.Contact | Raycast result. |
RaycastAsync(NativeArray<Sdf.Ray>, NativeArray<Sdf.Contact>, Boolean, Int32, Single, Boolean)
Asynchronous CPU-based raycasts. Function schedules a job that will compute on multiple threads.
Declaration
public Sdf.EvalJobHandle RaycastAsync(NativeArray<Sdf.Ray> casts, NativeArray<Sdf.Contact> results, bool computeMaterials, int maxSteps = 128, float margin = 0.01F, bool forceZeroBlendUnion = false)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Sdf.Ray> | casts | Array of rays. |
NativeArray<Sdf.Contact> | results | Array of raycast results. |
Boolean | computeMaterials | Whether to compute the materials at contact points (if hit) as well. |
Int32 | maxSteps | The maximum number of iterations to step a ray. If a hit hasn't been found after the maximum steps have been taken, then the raycast is a miss. |
Single | margin | A raycast is considered a hit if it ever reaches a point where the absolute SDF value is less than the margin. Smaller margin typically requires more steps to find a hit. |
Boolean | forceZeroBlendUnion | Whether to force all brushes to be treated as if they have zero blends with union operators. Useful for when needing to raycast against each brush's raw shape (e.g. for click-selection of mostly subtractive brushes). |
Returns
Type | Description |
---|---|
Sdf.EvalJobHandle | A job handle that can be waited on for completion. |
RaycastChain(NativeArray<Vector3>, Boolean, Int32, Single)
Synchronous CPU-based SDF raycast chain takes a single chain of sample points and returns a raycast result. Function computes on the main thread and only returns when the entire computation is done.
Declaration
public Sdf.Contact RaycastChain(NativeArray<Vector3> chain, bool computeMaterials, int maxSteps = 512, float margin = 0.01F)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Vector3> | chain | Array of points representing a series of consecutive rays that will be cast sequentially until a hit is found or if the array is exhausted. |
Boolean | computeMaterials | Whether to compute the materials at the contact point (if hit) as well. |
Int32 | maxSteps | The maximum number of iterations to step all the rays. If a hit hasn't been found after the maximum steps have been taken, then the raycast chain is a miss. |
Single | margin | A raycast chain is considered a hit if it ever reaches a point where the absolute SDF value is less than the margin. Smaller margin typically requires more steps to find a hit. |
Returns
Type | Description |
---|---|
Sdf.Contact | Raycast chain result. |
RaycastChainAsync(NativeArray<Vector3>, NativeArray<Sdf.Contact>, Boolean, Int32, Single)
Asynchronous CPU-based raycast chain. Function schedules a job that will compute on multiple threads.
Declaration
public Sdf.EvalJobHandle RaycastChainAsync(NativeArray<Vector3> chain, NativeArray<Sdf.Contact> result, bool computeMaterials, int maxSteps = 128, float margin = 0.01F)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Vector3> | chain | Array of points representing a series of consecutive rays that will be cast sequentially until a hit is found or if the array is exhausted. |
NativeArray<Sdf.Contact> | result | Array of a single raycast result for the entire raycast chain. |
Boolean | computeMaterials | Whether to compute the materials at the contact point (if hit) as well. |
Int32 | maxSteps | The maximum number of iterations to step all the rays. If a hit hasn't been found after the maximum steps have been taken, then the raycast chain is a miss. |
Single | margin | A raycast chain is considered a hit if it ever reaches a point where the absolute SDF value is less than the margin. Smaller margin typically requires more steps to find a hit. |
Returns
Type | Description |
---|---|
Sdf.EvalJobHandle | A job handle that can be waited on for completion. |
SnapToSurface(Vector3, Single, Boolean, Int32, Single)
Synchronous CPU-based SDF zero isosurface snapping that takes a single sample point and returns a potentially hit contact. Function computes on the main thread and only returns when the entire computation is done.
Snapping is done by first evaluating the normal (normalized gradient) at the sample point, and then raycasting towards the SDF zero isosurface.
Declaration
public Sdf.Contact SnapToSurface(Vector3 p, float maxSurfaceDistance, bool computeMaterials, int maxSteps = 128, float margin = 0.01F)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | p | The sample position. |
Single | maxSurfaceDistance | The range of evaluation from the sample position. Only brushes within this range are considered for evaluation. Use a conservative estimate of distance from the SDF's zero isosurface. |
Boolean | computeMaterials | Whether to compute the materials at the contact point (if snapping is successful) as well. |
Int32 | maxSteps | The maximum number of iterations to step the ray. If a hit hasn't been found after the maximum steps have been taken, then the raycast is a miss. |
Single | margin | The raycast is considered a hit if it ever reaches a point where the absolute SDF value is less than the margin. Smaller margin typically requires more steps to find a hit. |
Returns
Type | Description |
---|---|
Sdf.Contact | Surface snapping result. |
SnapToSurfaceAsync(NativeArray<Vector3>, NativeArray<Sdf.Contact>, Single, Boolean, Int32, Single)
Asynchronous CPU-based SDF zero isosurface snapping. Function schedules a job that will compute on multiple threads.
Snapping is done by first evaluating normals (normalized gradients) at the sample points, and then raycasting towards the SDF zero isosurface.
Declaration
public Sdf.EvalJobHandle SnapToSurfaceAsync(NativeArray<Vector3> samples, NativeArray<Sdf.Contact> results, float maxSurfaceDistance, bool computeMaterials, int maxSteps = 128, float margin = 0.01F)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Vector3> | samples | Array of sample points. |
NativeArray<Sdf.Contact> | results | Array of surface snapping results. |
Single | maxSurfaceDistance | The range of evaluation from the sample positions. Only brushes within this range are considered for evaluation. Use a conservative estimate of distance from the SDF's zero isosurface. |
Boolean | computeMaterials | Whether to compute the materials at the contact point (if snapping is successful) as well. |
Int32 | maxSteps | The maximum number of iterations to step a ray. If a hit hasn't been found after the maximum steps have been taken, then the raycast is a miss. |
Single | margin | A raycast is considered a hit if it ever reaches a point where the absolute SDF value is less than the margin. Smaller margin typically requires more steps to find a hit. |
Returns
Type | Description |
---|---|
Sdf.EvalJobHandle | A job handle that can be waited on for completion. |
UnlockMesh()
Declaration
public override void UnlockMesh()
Events
OnMeshGenerated
Declaration
public static event MudRenderer.MeshGenerated OnMeshGenerated
Event Type
Type | Description |
---|---|
MudRenderer.MeshGenerated |