<languageVersion: 1.0;>
kernel TangentToObject
<
	namespace: "com.away3d";
	vendor: "Away3d";
	version: 1;
	description: "When used as a shaderFill, converts tangent space normals to object space normals";
>
{
	input image3 normalMap;
	output pixel4 dst;
	
	parameter float3x3 tbn
	<
		minValue: float3x3(-10.0);
		maxValue: float3x3(10.0);
		defaultValue: float3x3(0.0);
		description: "The tangent space matrix for the triangle";
	>;
	
	void evaluatePixel()
	{
		float3 normal = 2.0*(sampleNearest(normalMap, outCoord())-float3(0.5, 0.5, 0.5));
		// different handedness
		normal.z = -normal.z;
		normal = tbn*normal;
		normal /= length(normal);
		normal.xz = -normal.xz;
		// need to output 4 channels for fill
		dst.xyz = (normal+float3(1.0, 1.0, 1.0))*.5;
		dst.a = 1.0;
	}
}
