Followers 0

# In-engine environment map probes questions

## 36 posts in this topic

I've got a few questions on the implementation of doing this.

1. In a deferred renderer (deferred shading) at what stage and how would I add the environment to use as glossy reflections ?
My first thought was ofc in the lighting/material pass but the problem here is you multiply all your lighting in a later compose pass with the albedo. But what if the albedo was pure black (e.g. metals). Whatever I do in the lighting shader it's going to be completely black in the end.
So how do I do that ? Add the environment reflections to the albedo during gbuffer ? I do need access to the normals for this to work so this won't work anywhere else.

2. I understand that you can use AMD's cubemap gen to generate blurred mip maps for an environment map where you can later fetch the texture mip based on the glossiness of the material. But for this to work with probes that were placed everywhere around the level, do I really have to save those to the hdd and manually create those mips with cubemap gen myself ? Edited by lipsryme
1

##### Share on other sites

1. You would need an extra specular buffer to composite correctly.

2. I believe the source code is available. You should be able to automate it.

0

##### Share on other sites

1. You add environment reflections the same way you'd add the specular contribution from a light source. They're both the same exact thing, they're just adding the contribution from different sources of reflections. So you'll want to use the same specular albedo and same roughness/glossiness/specular power that you'd use in your lighting equations.

2. There's a library version included in the download that you can integrate into your own tools.

1

##### Share on other sites
But whatever the lighting may be if I multiply this with a black diffuse albedo later on it's going to be black. How would I work around this?
0

##### Share on other sites
But whatever the lighting may be if I multiply this with a black diffuse albedo later on it's going to be black. How would I work around this?
You shouldn't be multiplying your specular lighting with the diffuse albedo in the first place. This "compose pass" simply sounds incorrect.
0

##### Share on other sites
I do my lighting (meaning diffuse+specular and + ambient) an later use this to multiply it with the albedo. Is that wrong? I've never seen it done differently
0

##### Share on other sites
You should add diffuse and specular. Use specular albedo as a parameter for the Fresnel function and use the result to "lerp" between diffuse and specular.

To add environment map, multiply the sample by the fresnel term (but calculate the Fresnel term using the view vector and the normal instead of the light direction and the half vector)

More info here (slide 54, Fresnel function some slides before). Edited by TiagoCosta
1

##### Share on other sites
I do my lighting (meaning diffuse+specular and + ambient) an later use this to multiply it with the albedo. Is that wrong? I've never seen it done differently
Yes it is. Diffuse albedo of a material should not be multiplied with the specular lighting because the specular highlight color of a light hitting a non-metallic material should be the color of the light.

You could multiply diffuse albedo with specular lighting to fake metallic surfaces, but only to fake.

Sorry for the double post. Only saw your reply after I made the first post. Edited by TiagoCosta
1

##### Share on other sites
sry cant edit somehow on iphone:
Now that I think about it it does seem stupidly redundant ...
0

##### Share on other sites

So it's correct that the brighter the specular gets the less "color" (texture color) it gets ? It looks a little odd to me at first sight.

Here's some code I'm using now

	float4 Ls = float4(0, 0, 0, 1);
float4 Lr = float4(0, 0, 0, 1);

float3 Li = _LightColor.rgb * _LightIntensity;

// Calculate cos(theta)
float NdotL = saturate(dot(N, L));

// Compute Fresnel Term (F) using Schlick's approximation
float base = 1.0 - dot(V, H_norm);
float exponential = pow(base, 5.0);
float3 F = exponential + 0.028f * (1.0 - exponential);
float FresnelFactor = 1.0f;
F = lerp(1.0f, 0.028f, FresnelFactor);

Lr.rgb += ambient;

Lr.rgb += NdotL * Li * shadowFactor;
Lr.rgb *= DiffuseAlbedo;
Lr.rgb *= 1.0f - F;

// Kelemen/Szirmay-Kalos (D) using a precomputed beckmann distribution
float NdotH = saturate(dot(N, H_norm));
float PH = pow(2.0f * BeckmannTarget.SampleLevel(LinearSampler, float2(NdotH, SpecularPower), 0).r, 10.0f);
float ksk = max(PH * F / dot(H, H), 0.0f);
float D = ksk;

// Cook-Torrance approximation (G) by Kelemen and Szirmay-Kalos
float NdotV = saturate(dot(N, V)) + 0.00001f;
float LdotH = saturate(dot(L, H_norm)); + 0.00001f;
float G = 1.0f / (LdotH * LdotH);

float3 nominator = G * D;
float denominator = 4.0f;

Ls.rgb += nominator / denominator;
Ls.rgb *= NdotL * Li * shadowFactor; // multiply by cosine factor and incoming radiance

return Lr + Ls;

0

##### Share on other sites
So it's correct that the brighter the specular gets the less "color" (texture color) it gets ? It looks a little odd to me at first sight.
// Compute Fresnel Term (F) using Schlick's approximation
float base = 1.0 - dot(V, H_norm);
float exponential = pow(base, 5.0);
float3 F = exponential + 0.028f * (1.0 - exponential);
float FresnelFactor = 1.0f;
F = lerp(1.0f, 0.028f, FresnelFactor);


Whats the 0.028f and fresnel factor = 1.0f supposed to be? Plus there are some errors in your calcs

The Fresnel function should look like this:
// Compute Fresnel Term (F) using Schlick's approximation
float base = 1.0 - dot(V, N);
float exponential = pow(base, 5.0);
float3 F = spec + (1-spec) * exponential;

Also remember that if your working with lights base should be (light direction and half vec instead of view and normal):
float base = 1.0 - dot(L, H_norm);
Edited by TiagoCosta
1

##### Share on other sites
This is inside the brdf function for skin materials.
FresnelFactor is just a variable for me to set how much it should be influenced by the fresnel term.
0.028f is the fresnel reflectance of skin.

And the fresnel calculation is from nvidia's paper:
http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html

edit: just noticed the last line is overwriting the actual fresnel calculation.

Going back to my question "So it's correct that the brighter the specular gets the less "color" (texture color) it gets ?"
Here's a quick screenshot showing how it looks:
http://d.pr/i/xqFw

It looks like the stronger the specular the more color gets "washed away", is that actually a realistic look ? Because it looks odd to me. Edited by lipsryme
0

##### Share on other sites
Going back to my question "So it's correct that the brighter the specular gets the less "color" (texture color) it gets ?"
On opaque materials (like metals, wood, rocks, etc) that is correct. I'm not sure about materials affected by subsurface scattering but I think so, maybe someone else can answer with more precision.

What colors are "washed away" in that screenshot? The ground? Are you using the light vector instead of the view in the ground Fresnel function?
If you're referring to the skin post a screenshot closer to the head. Edited by TiagoCosta
0

##### Share on other sites

Yes I'm now using the term from the PBS paper for the skin, too.

But I'm talking about the specular on the ground.

0

##### Share on other sites

What is the specular color and power of the ground? Keep in mind that you might to tweak those values when you switch to more physically based shaders...

Play a bit with the demo at the bottom of this article.

0

##### Share on other sites

Hmm so from playing around with that demo, I noticed that it does indeed trade it's diffuse color (and texture color) for specular. So basically loosing it's color. What I don't see in the demo is the increasing specular from the fresnel reflectance. Shouldn't it increase/decrease depending on the angle of reflectance ? In the demo it looks like it's rather constant.

0

##### Share on other sites

The Fresnel reflectance at normal incidence of a material is constant. Only the resulting fresnel term will vary depending on the angle between the light and half vector.

0

##### Share on other sites
Yes that's what I meant.
Now to go back to the original topic.
So I calculate the reflection vector from N and V and use this vector to lookup the environment map.
This reflection color is then multiplied by the fresnel term using V and H.
And then multiplied( or added?) to the specular output ? Edited by lipsryme
0

##### Share on other sites
It should be added to the final color.

float final = diffuse + specular + env;

Because the environment lighting is independent of any light.

Which made me notice an error in my responses:
To calculate the fresnel to use with the environment lighting you have to use the View vector and the surface normal (I previously said the half vector, which is incorrect).
Since you need the light direction to calculate the half vector so it didn't make sense.

So:
-Calculate the reflection vector from N and V and use this vector to lookup the environment map.
-This reflection color is then multiplied by the fresnel term using V and N.
-Add the reflection color to the final pixel color.

P.S: Already fixed the error in previous responses. Edited by TiagoCosta
1

##### Share on other sites
I still don't get how to correctly combine the reflection color sampled from the cubemap with the diffuse and specular color of my material.
If I just add the reflection to the (diffuse + specular) what I get is that the object is completely reflective and you won't even see any diffuse color at all.
On the other hand if I multiply the reflection color with the specular term and e.g. have a material that is highly glossy, this reflection will hardly be seen at all when the highlight is so small.
Maybe I'm just thinking wrong but what I'm trying to achieve is something like this:
or the reflections on the ground here: http://seblagarde.files.wordpress.com/2011/12/locallightingcube.png

Also I understand that reflections from the env map are basically "specular" so in my engine would I use this reflection color on every object ? And if I wanted an object that is completely reflective from all sides would I consider this to be a separate material type ? Edited by lipsryme
0

##### Share on other sites
I still don't get how to correctly combine the reflection color sampled from the cubemap with the diffuse and specular color of my material.
If I just add the reflection to the (diffuse + specular) what I get is that the object is completely reflective and you won't even see any diffuse color at all.
On the other hand if I multiply the reflection color with the specular term and e.g. have a material that is highly glossy, this reflection will hardly be seen at all when the highlight is so small.

So as you explained multiplying by the specular term doesn't work, as expected, because the reflection color has nothing to do with the specular term of a light.
Next lets think about the diffuse term:
Multiplying by the diffuse term doesn't make sense either because what happens if you have a glossy black material?
When you multiply the reflection color with the diffuse color (0,0,0) you will loose the reflection which isn't correct right?

That leaves you with the option to simply add.

If the reflection color is too strong maybe you should be using a lower specular color in the Fresnel calculations...

That's problem I haven't looked into yet, and I guess to have an accurate result some form of raycasting will be needed.

I believe you have can use ambient occlusion (eg SSAO) to approximate occluded pixels.

Maybe I'm just thinking wrong but what I'm trying to achieve is something like this:
or the reflections on the ground here: http://seblagarde.files.wordpress.com/2011/12/locallightingcube.png

Those spheres are metallic so the specular color isn't monochromatic, which changes the color of the reflections since you use the specular color in the Fresnel calculation.

Also I understand that reflections from the env map are basically "specular" so in my engine would I use this reflection color on every object ? And if I wanted an object that is completely reflective from all sides would I consider this to be a separate material type ?

Since the environment map color doesn't depend on any lighting it makes sense that it calculations should be done in a separate shader from the lights.

If you have multiply lights in the scene, make sure that you're not adding the environment map color multiple times to each pixel.
For example, if you have a deferred renderer, since you run the lighting shader once for each light, you need a separate shader to add the environment map color just once.

Check this article, for some info on how to make glossy reflections.

1

##### Share on other sites

Ok so this is how it looks like for me if I add the environment color to the final lighting:

http://d.pr/i/Mp2J

And this is how it looks without:

http://d.pr/i/XHC4

Is there some little thing I'm missing ?

	float3 reflectionColor = EnvironmentProbe.SampleLevel(LinearSampler, R, mipLevel).rgb;

// Compute Fresnel Term (F) using Schlick's approximation
float NdotV = saturate(dot(N_WS, V_WS));
float3 F_schlick = 0.3f + ((1.0f - 0.3f) * (1.0f - pow(NdotV, 5.0f)));
reflectionColor *= F_schlick;
0

##### Share on other sites

Is there some little thing I'm missing ?

float3 F_schlick = 0.3f + ((1.0f - 0.3f) * (1.0f - pow(NdotV, 5.0f)));

There's an error in that line...
It should be:

float3 F_schlick = 0.3f + ((1.0f - 0.3f) * pow(1.0f - NdotV, 5.0f));

Post a screenshot after changing this line

0

##### Share on other sites

Ah stupid me, didn't fix the reflection much though...

The object seems to retain more of its diffuse color now.

http://d.pr/i/opeH

0

##### Share on other sites

Are you calculating the lighting in linear space (instead of sRGB)?
If so, 0.3 as specular color is probably too high since the specular color of plastic is 0.03 (0.21 in sRGB).
(A list on specular color at slide 43)

Another thing I'm guessing is that you're not calculating the view vector correctly... It seems to be constant because the reflection is as strong in the ground close to the camera as from far away...

Make sure you're calculating it like this:

float3 view = cameraPositon-pixelPosition;

EDIT: Post a screenshot looking down directly at the ground.

0

## Create an account

Register a new account

Followers 0

• 10
• 28
• 14
• 11
• 33