x


What's the right way of getting the view space normal in a surface shader?

I did this for testing:

void surf (Input IN, inout SurfaceOutput o) {
    float3 viewN = mul ((float3x3)UNITY_MATRIX_IT_MV, o.Normal);
    o.Albedo = viewN * 0.5 + 0.5;
    o.Emission = viewN * 0.5 + 0.5;
}

Have applied it to a sphere, and if it was view space normals then the colors of the sphere should be the same no matter where you view it from right? They're not. Also rotating the sphere changes the color of it.

more ▼

asked May 06 '11 at 10:14 AM

metervara gravatar image

metervara
171 10 12 20

If there's no rotation on the sphere I get expected behavior, i.e. the color remains the same no matter what the viewing angle is.

May 06 '11 at 10:51 AM metervara
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Edit: Actually it doesn't, I was rotating the wrong object... :-(

This works:

float3 viewN = normalize( mul( UNITY_MATRIX_IT_MV, o.Normal.xyzz

).xyz );

This, from UnityCG.cginc works if the object is not rotated:

float3 viewN = mul ((float3x3)UNITY_MATRIX_IT_MV,

o.Normal);

/Patrik

more ▼

answered May 06 '11 at 10:59 AM

metervara gravatar image

metervara
171 10 12 20

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x128
x20

asked: May 06 '11 at 10:14 AM

Seen: 1411 times

Last Updated: May 06 '11 at 10:14 AM