Anisotropic Specular

Started by
7 comments, last by Chris_F 11 years, 1 month ago

I'm trying to implement an anisotropic version of GGX in UDK. Here is what the shader looks like:


float3 H = normalize(L + V);
float NdotL = saturate(dot(N, L));
float NdotV = dot(N, V);
float NdotH = dot(N, H);
float LdotH = dot(L, H);
float HdotX = dot(H, X);
float HdotY = dot(H, Y);

float NdotH_2 = NdotH * NdotH;
float HdotX_2 = HdotX * HdotX;
float HdotY_2 = HdotY * HdotY;
float ax_2 = ax * ax;
float ay_2 = ay * ay;

float GGX = (ax * ay * pow(HdotX_2 / ax_2 + HdotY_2 / ay_2 + NdotH_2, 2.0));

float3 F = Ks + (1.0 - Ks) * exp(-6 * LdotH);

float3 Rs = F / (4 * GGX * LdotH * LdotH);
float3 Rd = Kd * (1 - Ks);

return (Rs + Rd) * NdotL;

The problem is I'm not sure what to use for vectors X and Y. Because this is UDK, vectors L and V are relative to the surface (tangent space), so would I simply use X = [1, 0, 0], Y = [0, 1, 0]?

Advertisement

I believe X and Y should be set to your tangent and bitangent vectors. So if your L and V vectors are already in TBN space, then yes, you should set them to the tangent and bitangent's representation in this coordinate space, which is just (1, 0, 0) and (0, 1, 0).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

In that case, I'm not sure what I'm doing wrong. I don't have this issue with isotropic shaders. sad.png

[attachment=13625:aniso.png]

That because isotropic shaders don't care about the tangent and bitangent, all they need is the normal (since they are isotropic about the normal axis). Are you sure your TBN matrix is correct for every face here? The bottom looks good but there's a discontinuity.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Well, this is UDK that I'm working in ATM, so as far as I know I have no control over the tangents. The discontinuities seem to correlate to the UVs.

Tangents are usually generated from the UVs, so that makes sense. Fixing up the mesh should solve the issue.

Tangents are usually generated from the UVs, so that makes sense. Fixing up the mesh should solve the issue.

I've UV unwrapped the meshes six ways from Sunday, and I have found no unwrap that doesn't produce a horrible discontinuity along the UV seam(s).

I've rewritten your code into my own game engine, that has correct tangent space vectors computation from UV coordinates, here are the results for different UV coordinate modes (just quickly made sphere in 3D modeller and used 3 different UV mappings). Note that I don't transform light and view vectors into tangent space (I keep them in view space), but this won't make a difference (as I use actual tangent & bitangent vectors in world space).

In your case (when you transfer light and view vectors to tangent space, your tangent and bitangent vectors in that space are tangent space basis -> tangent = (1, 0, 0), bitangent = (0, 1, 0) and normal = (0, 0, 1).

1. Correct one (spherical UV coordinates - works fine):

vxn1qs.jpg

2. Box UV coordinates (obviously wrong)

2mq6v4o.jpg

3. Face unwrap (e.g. each face is mapped between 0.0 - 1.0 UV coordinates) - again obviously wrong:

28i2dqv.jpg

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

Here I used a spherical unwrap.

[attachment=13707:sphere.png]

This topic is closed to new replies.

Advertisement