Skip to content

A WIP extension allowing you to use baked 3D SDF textures in Mudbun.

License

Notifications You must be signed in to change notification settings

thnewlands/3D-Texture-Brush-for-Mudbun

Repository files navigation

SDF Texture Solid Brush

This repository contains code for extending Long Bunny Labs's MudBun to support 3D SDF Textures. It currently supports up to 64 R16 or R32 SFloat 128x128x128 textures at a time by packing them into one R32 512x512x512 texture.

How to Install

  1. Drag and drop contents of this repo into Mudbun's subfolder: Assets/Mudbun/Customization

  2. Add the brush function to CustomBrush.cginc

Inside of CustomBrush.cginc, just above the sdf_custom_brush function add:

#include "SDFTextureSolidBrush.cginc"

and inside of the switch statement for brush.type add:

case kSDFTextureSolid:
{
   res = sdf_texture3D_brush(brush, pRel);
  break;
}

It should look something like this:


#include "SDFTextureSolidBrush.cginc"

// returns custom SDF value, the signed distance from solid surface
float sdf_custom_brush
(
  float res,      // current SDF result before brush is applied
  inout float3 p, // sample position in world space (distortion brushes modify this)
  float3 pRel,    // sample position in brush space (relative to brush transform)
  SdfBrush brush  // brush data (see BrushDefs.cginc for data layout)
)
{
  float3 h = 0.5f * brush.size;

  // add/modify custom brushes in this switch statement
  switch (brush.type)
  {
    case kCustomSolid:
    {
      // box
      res = sdf_box(pRel, h, brush.radius);
      break;
    }
    case kSDFTextureSolid:
    {
        res = sdf_texture3D_brush(brush, pRel);
        break;
    }
...
...
...
  1. This part is a huge hack that deals with a requirement for textures to be bound in compute shaders. I'm not happy with it and hope to fix in the future. Inside of MudRenderer's OnEnable() add:

SDFTextureCollection.Instance.Init();

This should look like:

    protected override void OnEnable()
    {
      base.OnEnable();
      SDFTextureCollection.Instance.Init();
#if UNITY_EDITOR
      RegisterEditorEvents();
      SelectionManager.Init();
#endif
    }
  1. For ease of creation, add a dropdown item to CustomCreationMenu.

Inside of CustomCreationMenu.cs add:

  [MenuItem("GameObject/MudBun/Custom/SDF Texture Solid", priority = 4)]
  public static GameObject CreateSDFTextureSolid()
  {
    var go = CreateGameObject("Mud SDF Texture Solid");
    go.AddComponent<SDFTextureSolidBrush>();

    return OnBrushCreated(go);
  }
  1. At this point you should be able to drag and drop a 3D Texture into the scene!

Now, when you click the dropdown item "GameObject -> MudBun -> Custom -> SDF Texture Solid" you should see Suzzane appear in your scene. If she doesn't, you probably are missing .meta files from this repo.

  1. Suzzane facing down.
  2. Suzzane facing up with a few more voxels in resolution.
  3. Clumpy SDF blending with multiple 3D Textures.

Making Textures

Unity's SDF Bake Tool, built into their VFX Graph Package, works great for this. There are two things to remember:

  1. Remember to make cube shaped textures with uniform scaling using fit cube to mesh.

image

  1. Make sure that SDFs are 128x128x128 in resolution.

Once created you can drag the SDF into SDFTextureSolidBrush's Sdf Texture slot and if you did everything right it should just work!

Known Issues

  1. Sometimes triangles disappear.
  2. This works best with models that aren't pressed against the edges of the bounding box. Because things are packed together in a cube there's sometimes room for other textures to bleed in.

Unknown Issues

  1. No idea if this works in builds! I've been using this for level editing.
  2. Yet unknown!

Please send pull requests if you make improvements!

Tested with Unity 2022.3.21f and Mudbun 1.5.47

When figuring out blending at a distance with bounded SDF textures I referenced EmmetOT's Isomesh and Kosmonaut's very informative post on the subject. I also highly recommend giving Mudbun a shot.

About

A WIP extension allowing you to use baked 3D SDF textures in Mudbun.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published