Coder Social home page Coder Social logo

3d-texture-brush-for-mudbun's Introduction

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.

3d-texture-brush-for-mudbun's People

Contributors

thnewlands avatar

Stargazers

Andy Baker avatar

Watchers

 avatar

3d-texture-brush-for-mudbun's Issues

Normals are "a bit funky"

I baked a very high poly sphere (with a little edge room for blending) to compare with a normal mudsphere. the normals look a little wacky especially when you transform the brush around.
obviously the SDF is limited by its 3DTex resolution, but it's pretty obvious even with mudbun voxels set to MUCH lower resolution than the 3DTex itself ๐Ÿค” maybe there is a different way values could be sampled? but I have no idea what i'm talking about lol

I recorded video of it at some different voxel resolutions. but the compression makes it hard to see ๐Ÿ˜… it's very obvious in-person though

https://streamable.com/ignz84

image

3D tex sometimes switches to a different 3D tex (build only)

In a build of our game, you can reproduce (SOMETIMES?) by doing the following:

  • instantiate a new 3dtex brush (just one)
  • instantiate another; the previous 3dTex brush starts using the 3dTex from the new prefab.
  • if 3Dtex "A" has more than one copy in the renderer (for example, if I had placed 2 t rex heads) then the issue won't occur

Video:
https://streamable.com/bfp4pw

let me know if there's anything I can do to help debug this ๐Ÿ™

(mudbun 1.5.47, unity 2021.3.34f1)

Auto Rigging not supported

no errors, but any Verts that would be skinned to a 3Dtex brush bone all get sucked into the center point of the model instead

non-uniform scaling always makes some triangles disappear

i know triangles disappearing is a known issue ๐Ÿ˜… but Uneven Scaling is a consistent way to always trigger the issue, even on a Thick volume like a sphere. So I figured it might be helpful to report, sorry if you already knew haha
image

Breaks in build or when leaving Play mode if SDFBrush isn't being used

when starting my build or leaving play mode, if you have no SDFTextureSolidBrushes in the scene, then all mudRenderers will throw these 2 errors and won't render anything.
in editor, you can fix the error by adding an SDFTextureSolidBrush primitive to any mudrenderer. then all the mud renderers in the scene will start working again, and you can safely delete the SDFbrush.

image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.