Jump to content

  • Log In with Google      Sign In   
  • Create Account

In-engine environment map probes questions


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.

  • You cannot reply to this topic
36 replies to this topic

#1 lipsryme   Members   -  Reputation: 1045

Like
1Likes
Like

Posted 13 January 2013 - 10:34 AM

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, 13 January 2013 - 10:35 AM.


Sponsor:

#2 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 13 January 2013 - 12:57 PM

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.



#3 MJP   Moderators   -  Reputation: 11741

Like
1Likes
Like

Posted 14 January 2013 - 12:19 AM

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.

 



#4 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 04:28 AM

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?

#5 Hodgman   Moderators   -  Reputation: 31800

Like
0Likes
Like

Posted 14 January 2013 - 05:12 AM

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.

#6 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 05:17 AM

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

#7 TiagoCosta   Crossbones+   -  Reputation: 2446

Like
1Likes
Like

Posted 14 January 2013 - 05:18 AM

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, 14 January 2013 - 01:45 PM.


#8 TiagoCosta   Crossbones+   -  Reputation: 2446

Like
1Likes
Like

Posted 14 January 2013 - 05:22 AM

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, 14 January 2013 - 05:23 AM.


#9 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 05:23 AM

sry cant edit somehow on iphone:
Now that I think about it it does seem stupidly redundant ...

#10 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 07:59 AM

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);
	
	// Incident radiance
	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);


	// Add ambient light
	Lr.rgb += ambient;

	// Add diffuse light
	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;

 

 



#11 TiagoCosta   Crossbones+   -  Reputation: 2446

Like
1Likes
Like

Posted 14 January 2013 - 08:57 AM

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, 14 January 2013 - 01:51 PM.


#12 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 09:10 AM

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, 14 January 2013 - 09:52 AM.


#13 TiagoCosta   Crossbones+   -  Reputation: 2446

Like
0Likes
Like

Posted 14 January 2013 - 10:02 AM

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, 14 January 2013 - 10:02 AM.


#14 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 10:07 AM

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. 



#15 TiagoCosta   Crossbones+   -  Reputation: 2446

Like
0Likes
Like

Posted 14 January 2013 - 10:22 AM

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.



#16 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 10:38 AM

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.



#17 TiagoCosta   Crossbones+   -  Reputation: 2446

Like
0Likes
Like

Posted 14 January 2013 - 10:49 AM

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.



#18 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 14 January 2013 - 11:11 AM

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, 14 January 2013 - 11:12 AM.


#19 TiagoCosta   Crossbones+   -  Reputation: 2446

Like
1Likes
Like

Posted 14 January 2013 - 01:59 PM

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, 14 January 2013 - 01:59 PM.


#20 lipsryme   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 22 January 2013 - 09:10 AM

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.
Also what about shadows and occluded parts ?
Maybe I'm just thinking wrong but what I'm trying to achieve is something like this:
http://freesdk.crydev.net/download/attachments/131635/IBL_Mats.jpg?version=1&modificationDate=1313185175000
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, 22 January 2013 - 09:15 AM.





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