kernel NormalMap2TextureMap < namespace : "com.dafishinsea"; vendor : "David Wilhelm"; version : 2; description : "kernel to apply shading to a texture map using a normal map"; > { input image4 src; input image4 texture; output pixel4 dst; parameter float xr < minValue: -1.0; maxValue: 1.0; defaultValue: 0.1; >; parameter float yr < minValue: -1.0; maxValue: 1.0; defaultValue: 0.0; >; parameter float zr < minValue: -1.0; maxValue: 1.0; defaultValue: 0.0; >; void evaluatePixel() { //get currrent pixel color pixel4 p = sampleNearest(src,outCoord()); pixel4 tp = sampleNearest(texture,outCoord()); //image4 is a 4 element vector of RGBA channels //we want a vector of the first three as the normal float xd = 2.0*p.r - 1.0; float yd = 2.0*p.g - 1.0; float zd = 2.0*p.b - 1.0; float3 normal = float3(xd, yd, zd); normal = normalize(normal); float3 light = float3(xr, yr, zr) ; light = normalize(light); float dotp = dot(light, normal); //angle between vectors = acos (a dot b) float angle = acos(dotp); float shade = angle/3.14159265358; //add contrast shade = shade*shade; dst = pixel4(shade*tp.r, shade*tp.g, shade*tp.b, 1.0); } }