the result vector3 of UnpackNormal command...

if I use :normal=UnpackNormal(tex2D(normalMap,uv);then I get a normal Vector.
well,the red component of the map refers to the right direction of X,the green component the upward direction of Y. and the range of red and green component is 0-1.0,both is greater than 0;
So if what I have told is correct.then the normal Vector cannot be negtive(this cannot be true ,because normal can be any direction)…
Where is wrong???I am new 2 shader programming

Have a look at the definition of UnpackNormal.

It is not possible to encode an image with negative values. So it gets normalized into 0 to 1, then unnormalized back int -1 to 1 using that function (which also does swizzling to allow higher precision).

As Martin says, the exact code is in the .cginc files that come with Unity, it’s worth checking those and the documentation and the built-in shader code before posting threads about everything - you’ll find a lot of answers in there.

OK,I am dull…

but where is the documentation of .cginc files??I can not find it

There is no documentation of the .cginc files. But as farfarer said: it is useful to look at the code. You find the file in …/Unity/Editor/Data/CGIncludes/UnityCG.cginc

Well, here it is:

inline fixed3 UnpackNormal(fixed4 packednormal)
{
#if defined(SHADER_API_GLES)  defined(SHADER_API_MOBILE)
	return packednormal.xyz * 2 - 1;
#else
	fixed3 normal;
	normal.xy = packednormal.wy * 2 - 1;
	normal.z = sqrt(1 - normal.x*normal.x - normal.y * normal.y);
	return normal;
#endif
}

As you hopefully see, the components (between 0 and 1) are multiplied with 2 (resulting in a range from 0 to 2) and then 1 is subtracted (resulting in a range from -1 to 1).

Martin Kraus,you are the most kind man I ve ever met…Thank you more than 3 times.

1 Like