# The math behind combining BC5 normals

**URL:** <https://forums.unrealengine.com/t/the-math-behind-combining-bc5-normals/365189>\
**Category:** Rendering\
**Tags:** normal-map, normals, unreal-engine, combine, question\
**Created:** [July 26, 2016, 10:24am UTC](https://forums.unrealengine.com/t/the-math-behind-combining-bc5-normals/365189 "2016-07-26T10:24:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MBaadsgaard](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MBaadsgaard](https://forums.unrealengine.com/u/MBaadsgaard)\
**Post date:** [July 26, 2016, 10:24am UTC](https://forums.unrealengine.com/t/the-math-behind-combining-bc5-normals/365189/1 "2016-07-26T10:24:22Z")

</div>

Hey guys.  
Unreal’s handling of normal maps confuses me.

I noticed alot of people just used an add-node to combine normal maps, which seemed really wrong to me (coming from Unity) so I started looking into it.

I learned that the maps are BC5 compressed so they are only RG and mapped to the -1\>1 range.  
Also, it seemed like a normalization occurs on anything fed into the Normal input in the material.  
So BC5 is really a vector2 so adding and then normalizing should be somewhat okay, no?  
Well it seems these maps then have the blue channel generated on import, as I have a texture open in UE4, which says BC5 compression, but it has info in the blue channel.

I then found this website:

> **[Normal Map Blending in Unreal Engine 4 — JACK CARON](http://www.jackcaron.com/techart/2014/11/14/ue4-normal-blending)**
>
> Blending normal maps can come in very handy in Unreal for layering tileable textures and getting an extra bit of detail without having to spend resources on larger textures.  After seeing the "Add" node being used in a few user level files, I...

This result is way nicer than an add.

So a BC5 is only RG, but B is then generated on import? Makes no sense for build size.  
So why does the blue channel play a role when combining normal maps in the material editor?

Shouldn’t it be that you really only have RG and then B is generated in the shader just before the normals are written to the buffer?

---

<div class="post-metadata">

**Author:** ![Deathrey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Deathrey](https://forums.unrealengine.com/u/Deathrey)\
**Post date:** [July 29, 2016, 2:14am UTC](https://forums.unrealengine.com/t/the-math-behind-combining-bc5-normals/365189/2 "2016-07-29T02:14:15Z")

</div>

Good day. BC5 compression throws away B channel and uses only R and G.  
Blue channel is displayed in texture view is most likely just for visuals. Compressed normal texture has it discarded.  
B is not generated on import, but rather in the shader, after the normal map has been sampled. Each time you use a normal map sampler node, function somewhat like this is called:

```
float4 UnpackNormalMap( float4 TextureSample )
{

		float2 NormalXY = TextureSample.rg;
	
	NormalXY = NormalXY * float2(2.0f,2.0f) - float2(1.0f,1.0f);
	float NormalZ = sqrt( saturate( 1.0f - dot( NormalXY, NormalXY ) ) );
	return float4( NormalXY.xy, NormalZ, 1.0f );
}

```

If you are not doing anything fancy with the normals in your shader, than yep, you can safely go with RG only up until final output. However many operations do rely on having blue channel. Blue channel plays role in blending normal maps, because adding up RG channels of two normals and deriving blue channel yields different result than retrieving blue channels separately, then adding RG components and multiplying B channels, with normalization afterwards.

---

<div class="post-metadata">

**Author:** ![MBaadsgaard](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MBaadsgaard](https://forums.unrealengine.com/u/MBaadsgaard)\
**Post date:** [July 31, 2016, 6:40pm UTC](https://forums.unrealengine.com/t/the-math-behind-combining-bc5-normals/365189/3 "2016-07-31T18:40:54Z")

</div>

So the shader “unpacks” the blue channel? And this is done before any of the logic done in the material editor node system? So you can kind of think of it as happening as part of the Texture Sample node?

Which would explain why the blue channel has any effect at all when doing anything with the normals before feeding them into the material input…

---

<div class="post-metadata">

**Author:** ![Deathrey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Deathrey](https://forums.unrealengine.com/u/Deathrey)\
**Post date:** [July 31, 2016, 9:23pm UTC](https://forums.unrealengine.com/t/the-math-behind-combining-bc5-normals/365189/4 "2016-07-31T21:23:42Z")

</div>

Exactly. It is happening as a part of Texture Sample node, before material logic is applied to it.
