Jump to content
  • Advertisement
Sign in to follow this  
InvalidPointer

OpenGL Sub-surface scattering hack ( comments wanted! )

This topic is 3911 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was fiddling around in FX Composer last night and came up with a pretty nice-looking fake sub-surface scattering effect, as follows: http://img131.imageshack.us/img131/1489/sss1je8.jpg http://img131.imageshack.us/img131/2131/sss2tt2.jpg http://img131.imageshack.us/img131/3440/sss3gu5.jpg http://img141.imageshack.us/img141/3603/sss4nu5.jpg I'll post my full algorithm/code when I get back from work later today. In a nutshell, I have a simple half-lambert base diffuse term, nothing special. The sub-surface scattering effect is divided up into three parts: 1) A dot product between the inverse surface normal and the light, clamped from 0 to 1, and finally distance attenuated; 2) Another half-lambert term between the inverse normal and view, also distance attenuated; 3) Scaling of subsurface color by red, green and blue extinction coefficients. The final shot demonstrates the effects of tweaking these to produce a blue scatter color. In addition, I have the 1 - N dot V squared rim lighting term put forth in the Dawn demo by nVidia (but this time scaled by the clamped dot product between light and normal, to avoid that weird backlighting) and a basic Blinn-Phong specular added on top. Since those pictures were taken I've done some basic work on extending my algorithm to account for colored lighting and non-uniform material thickness, although it doesn't look quite as nice as it does in those earlier screens. So, my questions are: 1) Anyone know of a good way to make this handle the non-uniform thickness more gracefully (I'll post some pictures later for review) 2) Any additonal suggestions for improving this to look more skin-like as a whole? Feedback of any sort is appreciated, though. I originally wrote this to replace some shaders in Oblivion and possibly even Doom 3, and as such lack the ability to use depth buffers, spherical harmonics or any of that stuff, unless any and all information can be passed in through textures and shader constants ONLY. This is also targeted at SM3.0, although it works just fine in SM2.0 and the OpenGL equivalents (this has been ported to ARB assembly without incident)

Share this post


Link to post
Share on other sites
Advertisement
Alright, that took decidedly longer than expected to post-- that's what you get for being busy, I guess :(

Anyways, here's a shot with the translucency map and a few tweaks:
http://img143.imageshack.us/img143/2391/sssab9.jpg

I obviously fixed the eyebrow and hair thing, but it looks less... soft. Any suggestions? Should I even bother posting the code?

Share this post


Link to post
Share on other sites
For being so simple, these screenies look quite impressive. I'd certainly love to see that fx/fxproj to play with it :)

I only miss a little rougness (pores) and some more oilyness (specular) to be convinced that this is skin. But again, for being a quite simple approximation, this already looks awesome.

To address the thickness problem, maybe xNormal can help? That tool supports among many other things producing a thickness map. Having thickness from every point of view as shader input, the wrong lighting visible on the ear in the last shot should disappear.
One could also overlay two thickness maps, to account for bones (this would need another model for the skull to be created, but I guess that wouldn't be too hard to derive from the original mesh - it doesn't have to be perfect). This would make the head in the last shot look less like an obsidian statue and more like a flesh and bone thing (well, if red was used for scattering).

Share this post


Link to post
Share on other sites
Code isn't a problem, here's all the relevant shader stuff

string description = "Subsurface Scattering Hack";
//------------------------------------
float4x4 worldViewProj : WorldViewProjection;
float4x4 world : World;
float4x4 worldInverseTranspose : WorldInverseTranspose;
float4x4 viewInverse : ViewInverse;

float4 LightPosition1 : POSITION
<
string Object = "PointLight";
string UIName = "Light 1";
string Space = "World";
> = {1403.0f, 1441.0f, 1690.0f, 0.0f};

float3 specColor
<
string UIWidget = "color";
string UIName = "Specular Color";
> = {0.9f, 0.9f, 1.0f};

float3 lightColor
<
string UIWidget = "color";
string UIName = "Light Color";
> = {1.0f, 1.0f, 1.0f};

float materialThickness
<
string UIWidget = "Slider";
float UIMin = 0.0f;
float UIMax = 1.0f;
float UIStep = 0.01f;
string UIName = "Material Thickness Factor";
> = 0.6f;

float rimScalar
<
string UIWidget = "Slider";
float UIMin = 0.0f;
float UIMax = 1.0f;
float UIStep = 0.01f;
string UIName = "Rim Light Strength";
> = 1.0f;

float extinctionCoefficientRed
<
string UIWidget = "Slider";
float UIMin = 0.0f;
float UIMax = 1.0f;
float UIStep = 0.01f;
string UIName = "Extinction Coefficient, Red";
> = 0.80f;

float extinctionCoefficientBlue
<
string UIWidget = "Slider";
float UIMin = 0.0f;
float UIMax = 1.0f;
float UIStep = 0.01f;
string UIName = "Extinction Coefficient, Blue";
> = 0.12f;

float extinctionCoefficientGreen
<
string UIWidget = "Slider";
float UIMin = 0.0f;
float UIMax = 1.0f;
float UIStep = 0.01f;
string UIName = "Extinction Coefficient, Green";
> = 0.20f;

float specularPower
<
string UIWidget = "Slider";
float UIMin = 0.0f;
float UIMax = 100.0f;
float UIStep = 0.50f;
string UIName = "Blinn Specular Power";
> = 1.0f;

texture diffuseTex;
texture thicknessTex;
texture normalTex;

sampler normalSampler = sampler_state
{
texture = <normalTex>;
MinFilter = point;
MagFilter = point;
MipFilter = point;
AddressU = CLAMP;
AddressV = CLAMP;
};

sampler2D diffuseSampler = sampler_state
{
Texture = <diffuseTex>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = WRAP;
AddressV = WRAP;
};

sampler2D thicknessSampler = sampler_state
{
Texture = <diffuseTex>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = WRAP;
AddressV = WRAP;
};

struct VSOut
{
float4 position : POSITION;
float2 texCoord : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float3 eyeVec : TEXCOORD2;
float3 lightVec : TEXCOORD3;
float3 worldTangent : TEXCOORD4;
float3 worldBinormal : TEXCOORD5;
float3 vertPos : TEXCOORD6;
};

struct AppData {
float4 position : POSITION;
float2 UV : TEXCOORD0;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
};

VSOut SkinVS(AppData IN, uniform float4 lightPosition)
{
VSOut OUT;

OUT.worldNormal = normalize(mul(IN.normal, worldInverseTranspose).xyz);
OUT.worldTangent = normalize(mul(IN.tangent, worldInverseTranspose).xyz);
OUT.worldBinormal = normalize(mul(IN.binormal, worldInverseTranspose).xyz);

float3 worldSpacePos = mul(IN.position, world);
OUT.lightVec = lightPosition - worldSpacePos;
OUT.texCoord = IN.UV;
OUT.eyeVec = viewInverse[3].xyz - worldSpacePos;
OUT.position = mul(IN.position, worldViewProj);
OUT.vertPos = worldSpacePos;
return OUT;

};

float halfLambert(float3 vec1, float3 vec2)
{
float product = dot(vec1, vec2);
product *= 0.5;
product += 0.5;
return product;
}

float blinnPhongSpecular(float3 normalVec, float3 lightVec, float specPower)
{
float3 halfAngle = normalize(normalVec + lightVec);
return pow(saturate(dot(normalVec, halfAngle)), specPower);
}

float4 SkinPS(VSOut IN) : COLOR0
{
float attenuation = (1.0f/distance(LightPosition1, IN.vertPos));
attenuation *= 10.0f;
float3 eyeVec = normalize(IN.eyeVec);
float3 lightVec = normalize(IN.lightVec.xyz);
float3 worldNormal = normalize(IN.worldNormal);
//float3 nMap = tex2D(normalSampler, IN.texCoord);
//worldNormal.x = dot(nMap.x, IN.worldTangent);
//worldNormal.y = dot(nMap.y, IN.worldBinormal);
//worldNormal.z = dot(nMap.z, IN.worldNormal);
float4 dotLN = halfLambert(lightVec, worldNormal) * attenuation;
float3 indirectLightComponent = (float3)(materialThickness * max(0, dot(-worldNormal, lightVec)));
indirectLightComponent += materialThickness * halfLambert(-eyeVec, lightVec);
indirectLightComponent *= attenuation;
indirectLightComponent.r *= extinctionCoefficientRed;
indirectLightComponent.g *= extinctionCoefficientGreen;
indirectLightComponent.b *= extinctionCoefficientBlue;
indirectLightComponent.rgb *= tex2D(thicknessSampler, IN.texCoord).r;
float3 rim = (float3)(1.0f - max(0.0f, dot(worldNormal, eyeVec)));
rim *= rim;
dotLN *= tex2D(diffuseSampler, IN.texCoord);
float4 finalCol = dotLN + float4(indirectLightComponent, 1.0f);
rim *= max(0.0f, dot(worldNormal, lightVec)) * specColor;
finalCol.rgb += (rim * rimScalar * attenuation * finalCol.a);
finalCol.rgb += (blinnPhongSpecular(worldNormal, lightVec, specularPower) * attenuation * specColor * finalCol.a * 0.05f);
finalCol.rgb *= lightColor;
return float4(finalCol);
};

technique subSurfaceScattering
{
pass p0
{
VertexShader = compile vs_2_0 SkinVS(LightPosition1);
PixelShader = compile ps_2_0 SkinPS();
ZEnable = true;
ZWriteEnable = true;
AlphaBlendEnable = false;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
CullMode = CW; //None, CW, or CCW
}
}


That's regular HLSL, could be converted to Cg, GLSL or whatever other shading language you prefer. I just use the Dawn lambert skin fxproj and apply that skin shader to the face. I made the thickness map myself, as you can see it's pretty hack-y, so I'll leave you be on that. Diffuse map comes with FX Composer.

Thanks for the tips! :) I'm working on a normal map thing as we speak, although the ones supplied with the Dawn demo just look kind of funny when I apply them-- perhaps they're in the wrong space or something.

Share this post


Link to post
Share on other sites
Thanks for the shader code, InvalidPointer. Unluckily, it'll take some work this weekend to get it to work, since FX Composer doesn't like something, it crashes right away :(

I've been thinking about thickness and all for a while, there seems to be no easy way to get it perfect everywhere. Even a thickness map won't do, because no matter how you turn it, the ears will always be shaded wrong. There's of course the option to calculate per-pixel thicknes like in nVidia's "Luna" demo, but then we're not talking about a fast and efficient implementation any more (render several passes, blur, etc...). Also, the algorithm again is only an approximation. Mostly it doesn't matter much, but for example looking at the oral cavity from the side, it will produce a false "thick" result, when in fact it's mostly "empty" air with two thin layers of cheeks.

So, I was wondering if shadow mapping the skull wouldn't be helpful, and still cheap enough (a single z-only render of a possibly much simplified model).
The idea is to scale the head model down a bit, and remove/flatten ears and nose and render that into the shadow map. It probably doesn't even have to be perfectly anatomically accurate.
So that will leave you with an "outer part" where there is skin and flesh (and rim light scattering), and an "inner part" where bones absorb all of the back light (regardless of thickness), so only scattering from incidential light and half-lambert lighting account to the colour.

Thickness should not be something to worry about much then. Around the border, anything N dot V isn't a terribly bad approximation, since the normals will point more "sidewards" the thinner the cross-section.
Now, in the "deeper" regions it does not work so well, but we have these masked out with the shadow map. Also, the disturbing artefacts when the ear is rim-lit although it obviously couldn't be (as the head is in the way) are gone.

Share this post


Link to post
Share on other sites
Quote:
Original post by samoth
Thanks for the shader code, InvalidPointer. Unluckily, it'll take some work this weekend to get it to work, since FX Composer doesn't like something, it crashes right away :(

I've been thinking about thickness and all for a while, there seems to be no easy way to get it perfect everywhere. Even a thickness map won't do, because no matter how you turn it, the ears will always be shaded wrong. There's of course the option to calculate per-pixel thicknes like in nVidia's "Luna" demo, but then we're not talking about a fast and efficient implementation any more (render several passes, blur, etc...). Also, the algorithm again is only an approximation. Mostly it doesn't matter much, but for example looking at the oral cavity from the side, it will produce a false "thick" result, when in fact it's mostly "empty" air with two thin layers of cheeks.

So, I was wondering if shadow mapping the skull wouldn't be helpful, and still cheap enough (a single z-only render of a possibly much simplified model).
The idea is to scale the head model down a bit, and remove/flatten ears and nose and render that into the shadow map. It probably doesn't even have to be perfectly anatomically accurate.
So that will leave you with an "outer part" where there is skin and flesh (and rim light scattering), and an "inner part" where bones absorb all of the back light (regardless of thickness), so only scattering from incidential light and half-lambert lighting account to the colour.

Thickness should not be something to worry about much then. Around the border, anything N dot V isn't a terribly bad approximation, since the normals will point more "sidewards" the thinner the cross-section.
Now, in the "deeper" regions it does not work so well, but we have these masked out with the shadow map. Also, the disturbing artefacts when the ear is rim-lit although it obviously couldn't be (as the head is in the way) are gone.


Could the thickness map not be tweaked? I can use XNormal with some settings fiddles to make a decent thickness thing and then just work in Photoshop to make a nice-looking approximation for some of the effects you describe.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!