• Create Account

## Specular light color (float vs float3)

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.

22 replies to this topic

### #1cozzie  Members

Posted 16 July 2013 - 02:15 PM

Hi,

I'm playing around with specular lights and now in doubt if I should multiply the specular term for directional and point lights, by the color of the light. If I understand correctly, then:

- for non-metals, specular highlights are 99% of the time 'white'

- for metals, the specular highlights are mostly the color the material itself

With this in mind my first thought would be to have 2 different shaders.

But I might have found a reasonable solution with relatively high performance benefits, where I need only single float for the specular component instead of float3's (what I would need when I multiply them all with the lightcolor).

Basically:

- calculate specular terms for directional lights and point lights (single floats, no specific color)

- in calculation the final pixel color I do:

return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (MatSpec * (specularDir + specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);



Where I basically use the specular material value that determines the color of the highlights.

Say for glass I set it to 1.0 / 1.0 / 1.0 and for metal, to the actual color of the metal.

The only down side here is that I don't let the specular highlights be dependent of the actual light colors (IF it's a down side..).

What do you think?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #2Styves  Members

Posted 16 July 2013 - 02:36 PM

IIRC, Crysis 2 used greyscale specular on PS3 to avoid some tech limitations (pretty sure this was solved for Crysis 3 though). They mention it in this paper (I think it's this one, I can't check since I don't have a ppt viewer on this machine and don't have time to install one, gotta go! ). As long as you don't have wildly different colored lights and strong specs, it should look fine.

### #3MJP  Moderators

Posted 16 July 2013 - 03:45 PM

Specular highlights are always dependent on the color of the light. This doesn't change for metals vs. non-metals.

### #4cozzie  Members

Posted 16 July 2013 - 03:55 PM

OK. But how would you put that into the equation then?

(if the final specular highlight always takes over the material specular color)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #5REF_Cracker  Members

Posted 16 July 2013 - 04:16 PM

Hi Cozzie,

Read some articles on Physically Based Shading and you'll get a good idea of how all this works.

These two blog posts are a great starting point

http://seblagarde.wordpress.com/2011/08/17/hello-world/
http://seblagarde.wordpress.com/2011/08/17/feeding-a-physical-based-lighting-mode/

As well as

Crafting Physically Motivated Shading Models for Game Development (Naty Hoffman)

There's a fair amount of information about this out there. It'll take a bit of reading but once you grind through it you'll understand a lot more about how it all fits together.

Greyscale specular was used mostly in light prepass renderers to cut down on bandwidth by storing only diffuse in rgb and specular in the alpha channel of the accumulation buffer.

Check out my project @ www.exitearth.com

### #6cowsarenotevil  Members

Posted 16 July 2013 - 04:36 PM

- for non-metals, specular highlights are 99% of the time 'white'

- for metals, the specular highlights are mostly the color the material itself

I'd change these "rules" to

-for non-metals, the specular highlights are the color of the light

-for metals, the specular highlights are the color of the light multiplied by the color of the material

The truth is that even this corrected version isn't based on any hard theory; it's just that, in practice, specular highlights on non-metals (e.g. plastic, skin, wood) are generally actually caused by a layer of "clear" material on top of the material, be it oil, varnish, etc.

For instance, if you have a shiny ceramic object with gold leaf/trim, there'll be a difference depending on whether the gold is on top of the varnish; if the gold is on top, the gold parts will only have gold-tinted reflections, whereas if the varnish is on top, you'll see something like the sum of the gold-tinted reflections and the un-tinted reflections (which are also present on the ceramic part).

-~-The Cow of Darkness-~-

### #7Styves  Members

Posted 17 July 2013 - 05:54 AM

Specular highlights are always dependent on the color of the light. This doesn't change for metals vs. non-metals.

- for non-metals, specular highlights are 99% of the time 'white'

- for metals, the specular highlights are mostly the color the material itself

I'd change these "rules" to

-for non-metals, the specular highlights are the color of the light

-for metals, the specular highlights are the color of the light multiplied by the color of the material

The truth is that even this corrected version isn't based on any hard theory; it's just that, in practice, specular highlights on non-metals (e.g. plastic, skin, wood) are generally actually caused by a layer of "clear" material on top of the material, be it oil, varnish, etc.

For instance, if you have a shiny ceramic object with gold leaf/trim, there'll be a difference depending on whether the gold is on top of the varnish; if the gold is on top, the gold parts will only have gold-tinted reflections, whereas if the varnish is on top, you'll see something like the sum of the gold-tinted reflections and the un-tinted reflections (which are also present on the ceramic part).

I was going to say the same things but I reread the ops post and realized that we're all reading it wrong. OP isn't saying that materials have white specular highlights, they're saying that 99% of the time their lights are white causing white specular highlights. I think the original message was lost in formatting.

At least that's what I understood. If all of OPs lights will be white, then it's totally fine to go with a greyscale specular term (diffuse alpha type stuff) because there isn't any color to reflect anyway.

Edited by Styves, 17 July 2013 - 05:55 AM.

### #8cowsarenotevil  Members

Posted 17 July 2013 - 08:12 AM

...

...

I was going to say the same things but I reread the ops post and realized that we're all reading it wrong. OP isn't saying that materials have white specular highlights, they're saying that 99% of the time their lights are white causing white specular highlights. I think the original message was lost in formatting.

At least that's what I understood. If all of OPs lights will be white, then it's totally fine to go with a greyscale specular term (diffuse alpha type stuff) because there isn't any color to reflect anyway.

Well, he also said "(...) in doubt if I should multiply the specular term for directional and point lights, by the color of the light." In any case I hope now he has enough information to figure out what he was looking for.

-~-The Cow of Darkness-~-

### #9cozzie  Members

Posted 17 July 2013 - 04:46 PM

Thanks.

I think I've got it.

To be sure, I've now assumed that:

- specular highlights for non-metals take the color of the actual light

- specular highlights for metals take the color of the specular material color

The non-metal variant is now (for point lights):

specularPoint = pow(saturate(dot(h, normal)), input.SpecPower) * att;

// calculating final color
return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (PointLightColInt * specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);



And the metals variant:

specularPoint = pow(saturate(dot(h, normal)), input.SpecPower) * att;

// calculating final color
return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (MatSpec * specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);


Any suggestions/ remarks?

(please not that specular for directional light is not included yet, that's working it out later, first getting the basics right, before changing for multiple lights, point & directional)

Edited by cozzie, 17 July 2013 - 04:47 PM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #10MJP  Moderators

Posted 20 July 2013 - 01:49 PM

No that is still not right. It's really simple:

Metals: specular albedo is RGB

Non-metals: specular albedo is monochrome

The lighting is exactly the same in both cases: specular * lightInensity * specularAlbedo. You don't even need different shaders, for non-metals you just always have R, G, and B set to the same value in your material definition.

Edited by MJP, 20 July 2013 - 01:51 PM.

### #11cozzie  Members

Posted 20 July 2013 - 02:55 PM

Thanks.

I was trying to see if it's possible without using the color of the light, because that would save me 2 float3's.

Because the specular intensity is just a single float:

float diffIntPoint = saturate(dot(normal, lightDir) * att);
diffusePoint += saturate(diffIntPoint * PointLightColInt[i]);	// float3



In the end pixel calculation is now have:

return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (MatSpec * (specularDir + specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);



How should I define specularAlbedo within the shader with 2 'variants'.

I think I should use the MaterialSpec color to distinguish metals / non metals, but I don't understand how to define 'RGB' and 'monochrome' (distinguish them with 1 calculation).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #12Styves  Members

Posted 21 July 2013 - 07:32 AM

You don't need to distinguish anything - there are no variants. Monochrome just means the 3 values of your color are the same (255,255,255 for example). Nothing more to it - just pass in a float3 from your material into your shader and use that. Cutting it down to a single float probably isn't going to help much anyway.

...

...

I was going to say the same things but I reread the ops post and realized that we're all reading it wrong. OP isn't saying that materials have white specular highlights, they're saying that 99% of the time their lights are white causing white specular highlights. I think the original message was lost in formatting.

At least that's what I understood. If all of OPs lights will be white, then it's totally fine to go with a greyscale specular term (diffuse alpha type stuff) because there isn't any color to reflect anyway.

Well, he also said "(...) in doubt if I should multiply the specular term for directional and point lights, by the color of the light." In any case I hope now he has enough information to figure out what he was looking for.

I must've totally missed that. Guess my initial thought was right.

Edited by Styves, 21 July 2013 - 07:34 AM.

### #13cozzie  Members

Posted 21 July 2013 - 10:44 AM

Thanks, the good news is that I've got it setup right then already.

If the float's and more become an issue I can always bring back the number of lights, make a version without specular. Or maybe think about a deffered shader.

Thanks all for the help and understanding it better.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #14Styves  Members

Posted 22 July 2013 - 05:36 AM

If you want, you can try a static branch to detect whether the material uses specular or not. On most modern hardware this branch should be free because the constant is known at run-time. That way you don't need to make an entirely new shader or add permutations to it.

### #15cozzie  Members

Posted 23 July 2013 - 12:15 PM

Hi Styves,

I've tried your suggestion and did the following, is this what you mean?

Maybe there's a better way to check if the vector/float3 is 0,0,0.

I didn't profile yet (with pix) if there's an advantage in doing the if check/ branch, compared to calculating the specular values without result.

In my code/ engine:

		if(mMaterials[mc].d3dMaterial.Specular.r != 0.0f &&
mMaterials[mc].d3dMaterial.Specular.g != 0.0f &&
mMaterials[mc].d3dMaterial.Specular.b != 0.0f)
{
D3dcolorToFloat4Array(mMaterials[mc].d3dMaterial.Specular, mMaterials[mc].specularF);
}



(specularF is the float arrat that feeds the shader constant)

uniform extern float4	MatSpec		: MATERIAL_SPECULAR = {0.0f, 0.0f, 0.0f, 0.0f};

for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseDir	+= saturate(DirLightColInt[i] * dot(normal,  DirLightDir[i]));

if(MatSpec != 0.0f, 0.0f, 0.0f, 0.0f)
{
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + input.ViewDir);
specularDir += (pow(saturate(dot(h, normal)), MatSpecPower) * DirLightColInt[i]);
}
}

// and for point lights:

if(MatSpec != 0.0f, 0.0f, 0.0f, 0.0f)
{
float3 h = normalize(lightDir + input.ViewDir);

specularPoint += pow(saturate(dot(h, normal)), MatSpecPower) * att * PointLightColInt[i];

}



Edited by cozzie, 23 July 2013 - 12:20 PM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #16cozzie  Members

Posted 23 July 2013 - 12:25 PM

Update; although it compiles, my specular highlights are gone now, probably something with checking the MatSpec float4 array

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #17unbird  Members

Posted 23 July 2013 - 01:40 PM

Ugh, comma operator. I'm not ugh-ing about your lapse, but rather that such an ugly thing even exists. You don't even get a compiler warning here.

E.g. if(a,b,c,d) will actually just evaluate if(d), so in your case if(0.0f). This is false, so the whole if-clause will never be entered, no matter what's in MatSpec.

### #18cozzie  Members

Posted 24 July 2013 - 02:51 PM

Thanks, I've got working using the "if(any(..." statement.

Not sure though what the performance gain is, I'll profile it later.

Here's the result, might you have remarks..

The engine code:

		// only save specular material for shader constant when needed
// shader checks 'if(any)' and saves cycles when no specular needed
if(mMaterials[mc].d3dMaterial.Specular.r != 0 &&
mMaterials[mc].d3dMaterial.Specular.g != 0 &&
mMaterials[mc].d3dMaterial.Specular.b != 0)
{
D3dcolorToFloat4Array(mMaterials[mc].d3dMaterial.Specular, mMaterials[mc].specularF);
}
else
{
mMaterials[mc].specularF[0] = 0.0f;
mMaterials[mc].specularF[1] = 0.0f;
mMaterials[mc].specularF[2] = 0.0f;
mMaterials[mc].specularF[3] = 0.0f;
}



Relevant parts of the (pixel) shader:

/** 	DIRECTIONAL LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/
float3 diffuseDir = 0.0f;
float3 specularDir = 0.0f;

for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseDir	+= saturate(DirLightColInt[i] * dot(normal,  DirLightDir[i]));

if(any(MatSpec))
{
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + input.ViewDir);
specularDir += (pow(saturate(dot(h, normal)), MatSpecPower) * DirLightColInt[i]);
}
}


		if(any(MatSpec))
{
float3 h = normalize(lightDir + input.ViewDir);

specularPoint += pow(saturate(dot(h, normal)), MatSpecPower) * att * PointLightColInt[i];
}



In the calculation of the final pixel color I still take the specular components into account.

I might also do the check there (if any) and have 2 final pixel color calculations, don't think that's that big of a gain.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #19Styves  Members

Posted 25 July 2013 - 03:11 AM

I don't think it's a problem. If you reorganize your final color output into a series of MADs (a * b + c) then you're only adding a single instruction. Actually, in this case you're actually removing 4 instructions.

For exmaple:

// calculating final color
return float4(textureColor.rgb * (MatDiff * diffuseAcc + AmbientColInt) + (MatSpec * specularAcc + MatEmi), textureColor.a);



FYI, your diffuse color map shouldn't be applied to your specular component, only the diffuse lighting (and if it makes sense for you, the emissive). So I've moved the texture color in this code for that purpose. If you want texture on emissive then you can pre-multiply it by the diffuse color elsewhere or by a separate "emissive" texture.

You also don't need the saturate - if you're rendering to LDR then the light will clamp itself, if you're rendering to HDR then you'll want brighter lights anyway.

MADs, ADDs and MULs are all 1 instruction (pretty sure actually that all 3 are actually MADs as far as the GPU is concerned), so be sure to use as many MADS as you can to save some instructions!

Edited by Styves, 25 July 2013 - 03:12 AM.

### #20cozzie  Members

Posted 28 July 2013 - 04:18 PM

Thanks, I think I've got it.

So summarize:

- combining 2 multiplications + 1 add is done more efficiently on the GPU, so making 'sets' is better for performance

- I can combine the diffuse and specular light float3's for diffuse total and specular total (diffuseAcc/ specularAcc), for both the directional and point lights

I've made the changes and it's all working nice.

Here's my resulting pixelshader, how does it look?

float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
float3 normal = normalize(input.Normal);

float3 diffuseAcc = 0.0f;
float3 specularAcc = 0.0f;

/** 	DIRECTIONAL LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/
for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseAcc	+= saturate(DirLightColInt[i] * dot(normal,  DirLightDir[i]));

if(any(MatSpec))
{
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + input.ViewDir);
specularAcc += (pow(saturate(dot(h, normal)), MatSpecPower) * DirLightColInt[i]);
}
}

/** 	POINT LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/

for(int i=0;i<MaxPointLights;++i)
{
float3 lightDir = normalize(PointLightPos[i] - input.wPos);

// PER PIXEL ATTENUATION
float dist = length(PointLightPos[i] - input.wPos);

float att = saturate(1 - ((dist - PointLightFPRange[i]) / (PointLightRange[i] - PointLightFPRange[i])));
att *= att;	// optional, not correct for full power range !?!?

// DIFFUSE

float diffIntPoint = saturate(dot(normal, lightDir) * att);
diffuseAcc += diffIntPoint * PointLightColInt[i];		// float3

// SPECULAR; USING BLINN HALF ANGLE

if(any(MatSpec))
{
float3 h = normalize(lightDir + input.ViewDir);
specularAcc += pow(saturate(dot(h, normal)), MatSpecPower) * att * PointLightColInt[i];
}
}

/**	FINAL PIXEL COLOR **/

return float4(textureColor.rgb * (MatDiff * diffuseAcc + AmbientColInt) + (MatSpec * specularAcc + MatEmi), textureColor.a);
}


Ps.; I might be able to raise the max. number of point lights from 3 to 4 for my Shader Model 2.0 versions of my shaders, because of less instructions

On the optimization side;

- I could use one float2 for point light range and full power range, and save another 'instruction'/constant

Do you see any other small things to improve?

I think I've moved as much as possible from the PX to the VS and pre-calculate as much as possible/ acceptable on the CPU side.

Edited by cozzie, 28 July 2013 - 04:22 PM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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.