• Create Account

Banner advertising on our site currently available from just \$5!

# Metallic in UE 4

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1B_old  Members   -  Reputation: 673

Like
0Likes
Like

Posted 03 November 2013 - 12:37 PM

I'm curious how the metallic parameter as in the Disney and UE 4 material model could be used.

Following these course notes I implemented the UE 4 style GGX specular model and use metallic like this:

vec3 specular_color = lerp(vec3(1.f, 1.f, 1.f), diffuse_color, metallic);
float f0 = lerp(0.04f, 1.f, metallic);

vec3 specular = specular_color * specular_f(v_dot_h, f0) * specular_d(n_dot_h, a2) * specular_g(n_dot_l, n_dot_v, a2);

vec3 diffuse = (1.f - metallic) * diffuse_color;

return n_dot_l * (diffuse + specular);


From the notes it didn't become quite clear to me what I should do with metallic. It feels strange that non-metallic materials have a specular color of 1, but due to the small f0 it doesn't seem to be a big problem.

Any ideas how this could be improved upon?

### #2CryZe  Members   -  Reputation: 768

Like
2Likes
Like

Posted 04 November 2013 - 04:49 AM

Non-metallic materials are achromatic most of a time and thus don't cause the reflections to be colored in any way. In their implementation the more the material is metallic, the more its reflections are colored by the diffuse color. Also the more metallic the material is, the stronger it reflects light in a specular way.

Edited by CryZe, 04 November 2013 - 04:49 AM.

### #3Hodgman  Moderators   -  Reputation: 37512

Like
1Likes
Like

Posted 04 November 2013 - 05:17 AM

Spec colour and f0 are the same thing. IMO they shouldn't be two different parameters. The Fresnel function will make an RGB f0 term change to white at glancing angles, but if you use an RGB spec colour and a 1D f0 term, you don't get this behaviour.

I implemented it like this:
float3 albedo = ...
float specMask = ...
float metallic = ...
float3 f0 = lerp( specMask.xxx, specMask * albedo, metallic.xxx );
float3 diffuse = albedo * (1-metallic);
Later on I removed the metallic parameter because the artists didn't like having it as basically a second spec-mask parameter, and I replaced it with this:
float metallic = saturate(specMask*2-1);
float nonMetalSpec = saturate(specMask*2)*0.2;
float3 f0 = lerp( nonMetalSpec.xxx, albedo, metallic.xxx );
float3 diffuse = albedo * (1-metallic);
^Basically any spec-mask value below 50% (<186 in sRGB) is a non-metal, with 50%/186 being slightly shinier than diamonds. Above 50% is metallic, with 100% (255 in sRGB) being pure metal. Pure metals use the albedo colour as the spec mask instead, and set the diffuse colour to black. Non-pure metals (187 - 254) use a darkened diffuse term, and a not-fully-coloured specular term.

Edited by Hodgman, 04 November 2013 - 05:19 AM.

### #4B_old  Members   -  Reputation: 673

Like
0Likes
Like

Posted 04 November 2013 - 06:00 AM

Spec colour and f0 are the same thing. IMO they shouldn't be two different parameters. The Fresnel function will make an RGB f0 term change to white at glancing angles, but if you use an RGB spec colour and a 1D f0 term, you don't get this behaviour.

I implemented it like this:

float3 albedo = ...
float specMask = ...
float metallic = ...
float3 f0 = lerp( specMask.xxx, specMask * albedo, metallic.xxx );
float3 diffuse = albedo * (1-metallic);
Later on I removed the metallic parameter because the artists didn't like having it as basically a second spec-mask parameter, and I replaced it with this:
float metallic = saturate(specMask*2-1);
float nonMetalSpec = saturate(specMask*2)*0.2;
float3 f0 = lerp( nonMetalSpec.xxx, albedo, metallic.xxx );
float3 diffuse = albedo * (1-metallic);
^Basically any spec-mask value below 50% (<186 in sRGB) is a non-metal, with 50%/186 being slightly shinier than diamonds. Above 50% is metallic, with 100% (255 in sRGB) being pure metal. Pure metals use the albedo colour as the spec mask instead, and set the diffuse colour to black. Non-pure metals (187 - 254) use a darkened diffuse term, and a not-fully-coloured specular term.

I used to have a float3 f0 and now I want to fit it back in, following your argumentation.

I notice that you have another parameter called spec mask. Isn't it more confusing to differentiate between roughness and spec mask?

### #5Hodgman  Moderators   -  Reputation: 37512

Like
0Likes
Like

Posted 04 November 2013 - 06:31 AM

Spec-mask / f0 are based on the index-of-refraction. They determine the percentage of light that will reflect off the surface boundary. This is based on the type of the material, and should be a pretty flat/boring texture without much detail (unless there are many different types of material in the texture -- bricks, mortar, plastic, dirt, metal all would have different spec-mask values).

Roughness / spec-power is like a microscopic normal map. If you zoomed in with a microscope, would you have a flat normal map or a bumpy normal map.
Low roughness / high power / smooth micro-normal == small, crisp specular highlights.
High roughness / low power / bumpy micro-normal == large, dull specular highlights.

You can have a plastic material with a high spec-mask / f0 value, which means it's quite reflective -- you can then have a polished version (low roughness) and a dull version that's been attacked with rough sandpaper (high roughness). Those are two physically different parameters.

I've seen some engines lately have completely discarded the spec-mask value, because for most common (non-metal) materials, its value is around 4%.
The code you posted does this -- non-metals are 4% reflective and metals can be anything, including 100% (if you use a white diffuse colour).
In that case, your parameters would be colour, roughness/spec-power and metallic, which is also nice and simple.
If you used a vec3 f0, your code could look like:

vec3 f0 = lerp(vec3(0.04f), diffuse_color, metallic);

But yes, the more parameters you have, the more confusing it is

I have lots of reference charts and documentation to remind the artists what the parameters do, and how they should be used (e.g. if something is made up of one clean material, it should probably have one single spec-mask value, not lots of noise/detail in the spec-mask texture).

Edited by Hodgman, 04 November 2013 - 06:51 AM.

### #6B_old  Members   -  Reputation: 673

Like
0Likes
Like

Posted 04 November 2013 - 07:20 AM

Thanks for the explanation!

After my previous post I remembered the UE 4 course notes to also mention a specular parameter which was later replaced by cavity. I'm having a hard time understand what cavity is used for as it seems similar to roughness.

Picking up your example, lets say we have a highly reflective/metallic material that's been polished. In my case that might be metallic 1 and roughness 0. Now I want to have some small details with less highlights. I could say the roughness is higher here, or the cavity is higher. What is the difference?

### #7Hodgman  Moderators   -  Reputation: 37512

Like
0Likes
Like

Posted 04 November 2013 - 06:20 PM

After my previous post I remembered the UE 4 course notes to also mention a specular parameter which was later replaced by cavity. I'm having a hard time understand what cavity is used for as it seems similar to roughness.

my guess is that it's a final multiplier, like the spec color in your original code -- white = no change, black = all spec lighting is removed.

In a physically based shader, even if you set f0/spec-mask to 0%, if you view the material at a glancing angle, 1-100% of the light will still be reflected due to Fresnel.
When you have little cavities, lake gaps between plates of armor, this looks weird. In reality, the shadow system should take care of this, but on a low-poly model, that cavity may not even exist (so it can't be shadowed). So, it's like doing a very fina AO bake on your model, or letting you artist hand paint areas where they want to remove the specular reflections.

Picking up your example, lets say we have a highly reflective/metallic material that's been polished. In my case that might be metallic 1 and roughness 0. Now I want to have some small details with less highlights. I could say the roughness is higher here, or the cavity is higher. What is the difference?

If the roughness is 0, you'll only see a reflection if H•N is exactly 1 (or in other words if R•L is exactly 1). Increasing the roughness relaxes this, so H•N can be smaller and smaller (less aligned) but a highlight is still produced. Yes, as roughness increases, the intensity of the highlight also decreases, because it's being spread over a large area (assuming were conserving energy).
Tweaking the cavity on the other hand just reduces the intensity directly, without affecting the size/shape of the highlight.

### #8B_old  Members   -  Reputation: 673

Like
0Likes
Like

Posted 05 November 2013 - 03:11 AM

In a physically based shader, even if you set f0/spec-mask to 0%, if you view the material at a glancing angle, 1-100% of the light will still be reflected due to Fresnel.
When you have little cavities, lake gaps between plates of armor, this looks weird. In reality, the shadow system should take care of this, but on a low-poly model, that cavity may not even exist (so it can't be shadowed). So, it's like doing a very fina AO bake on your model, or letting you artist hand paint areas where they want to remove the specular reflections.

OK, I never thought about the fresnel still being present on very rough surfaces. I think I can see a use case for the cavity parameter now. Thanks for the explanation!

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS