Introducing Dragonfly Cloud! Learn More

Question: Where Should You Put the Roughness Map in Unity?

Answer

When working with materials and textures in Unity, the placement of a roughness map is essential for achieving realistic surface properties on 3D models. A roughness map is a grayscale texture that dictates how rough or smooth the surface of a material appears, influencing how it interacts with light.

In Unity, roughness maps are usually incorporated within the Standard Shader using a Metallic workflow, but instead of a direct roughness map, you often use a smoothness map, which is the inverse of roughness.

Here's how to set up a roughness map in Unity:

  1. Convert Roughness to Smoothness: Unity's Standard Shader uses a smoothness value rather than roughness. You can convert a roughness map to a smoothness map by inverting the colors (black becomes white and vice versa). This can be done in an image editing program or via code if dynamic conversion is necessary.
// Example of converting roughness to smoothness in code Texture2D ConvertRoughnessToSmoothness(Texture2D roughnessMap) { Texture2D smoothnessMap = new Texture2D(roughnessMap.width, roughnessMap.height); Color[] pixels = roughnessMap.GetPixels(); for(int i = 0; i < pixels.Length; i++) { // Invert the color: black (rough) becomes white (smooth), white becomes black pixels[i].r = 1f - pixels[i].r; pixels[i].g = 1f - pixels[i].g; pixels[i].b = 1f - pixels[i].b; // Set alpha to 1 as we're assuming the roughness map doesn't contain transparency pixels[i].a = 1f; } smoothnessMap.SetPixels(pixels); smoothnessMap.Apply(); return smoothnessMap; }
  1. Apply Smoothness to Material: Once you have the smoothness map, apply it to the material using the Unity Editor by dragging the smoothness map into the Metallic Map's Alpha channel, which the Standard Shader interprets as smoothness information.

Alternatively, this can be done through code:

// Apply smoothness map to material Material material = GetComponent<Renderer>().material; Texture2D smoothnessMap = ConvertRoughnessToSmoothness(yourRoughnessMap); material.SetTexture("_MetallicGlossMap", smoothnessMap);

Note that in the Unity Standard Shader, smoothness often resides in the alpha channel of the metallic map. If your roughness data is separate from metallic information, you'll need to pack the converted smoothness information into the alpha channel of the metallic texture or use a custom shader that allows for a separate roughness map input.

  1. Shader Graph: If you're using Unity's Shader Graph, you would create a custom shader that can directly accept a roughness map and connect it appropriately to the PBR Master node. In the Shader Graph, you'd invert the roughness map to get the smoothness before connecting it to the Smoothness input.

Understanding how Unity's rendering engine interprets texture maps is crucial for achieving the desired visual results. Always test your materials under different lighting conditions to ensure they behave correctly.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.