Skip to content

Instantly share code, notes, and snippets.

@feilen
Created September 1, 2023 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feilen/190ec4d9e8b7fe35da1ae6ff32c958dd to your computer and use it in GitHub Desktop.
Save feilen/190ec4d9e8b7fe35da1ae6ff32c958dd to your computer and use it in GitHub Desktop.
Using Stable Diffusion to add Albedo texture variation

Tiled textures are a fantastic way to spread a little memory around widely, but they often come with a rather noticable lack of variation.

Using this method you can add a lot of variation to a preexisting material, leveraging Stable Diffusion.

I'll be using this map:

image

Create a tiled map

To do this, we'll be using the unity Detail map as the 'large' map, instead of as the small repeating map.

I usually do this with ImageMagick, a command line image manipulation utility.

convert \( carpet.png -colorspace RGB -distort resize 32x32 -unsharp 0x1 \) -colorspace sRGB untiled-novariation.png
convert -size $((40 * 32))x$((40 * 32)) tile:untiled-novariation.png tiled-novariation.png

image

Adding AI noise

I use dream-textures. Assuming you've set that up the default way, load up your 'tiled-novariation.png' texture, usually in the shader view:

Give it a relatively sane prompt for the item in question, and ensure the following settings:

  • Prompt: Texture
  • Source Image: Open Image, Modify, Noise strength 0.1
  • Iterations: 9
  • Seamless Axes: Both

Launch that from the side bar to the shader window. Doing so will spit out an image node for every produced image, making it easier to dig through.

This will 'mutate' the original texture and spit out 9 different variations. Select whichever one is the closest to the variation you're going for, and save it as 'tiled-variation.png'. You may wish to keep mutating until some interesting extra variations pop up.

image

Create a gamma-adjusted difference of the resulting files

Unity's Standard detail albedo claims that it's "2x", but it's actually gamma-adjusted by default, which a dig into the shader source reveals it to be ~4.59x.

We'll use ImageMagick again to create a divided map that will apply the new albedo map over the old one.

convert \( tiled-variation.png -colorspace RGB tiled-novariation.png -colorspace RGB -fx "(((0.21763762282433653497 * u)/max(v, .01)))" \) -colorspace sRGB DetailDiffuse.png

If your map ended up with weird sizes you may need to resize them:

convert \( \( tiled-variation.png -colorspace RGB -distort resize 512x512 \) \( tiled-novariation.png -colorspace RGB -distort resize 512x512 \) -fx "(((0.21763762282433653497 * u)/max(v, .01)))" \) -colorspace sRGB DetailDiffuse.png

image

Create your material

Create a material with detail map tiled 1x1, and base map tiled 20x20. This will line up the adjusted map over the base map. Often results are a little better if you make the Detail Diffuse map only 128x128 px, so it applies as more of a general color tint.

image

Tada!

image

image

image

Enjoy your new carpet stains! This technique works best for non-organic maps, but also works great for artificial maps which are intended to be 'worn' to some degree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment