Implement Phong shading for color output?

Is it possible to impelmenet a colour output that behaves like the phong shader?

I would like to have the specular as a black and white output to use as as a factor to other nodes.



shader testPhong(float lightIntensity = 100,

vector lightDir = vector(-0.98717,-6.82603,0.5882),   //manually entered since getAttribute does not seem to work
vector cameraSpacePosition = vector(2.24144,7.65891,7.92298),
output color outputColor = color(0,0,0))
{




vector surfaceNormal = normalize(N);
float cosAngIncidence = dot(surfaceNormal, lightDir);
       cosAngIncidence = clamp(cosAngIncidence, 0, 1);
	
vector viewDirection = normalize(-cameraSpacePosition);
	
vector halfAngle = normalize(lightDir + viewDirection);
float blinnTerm = dot(surfaceNormal, halfAngle);
      blinnTerm = clamp(blinnTerm, 0, 1);
      blinnTerm = cosAngIncidence != 0.0 ? blinnTerm : 0.0;
blinnTerm = pow(blinnTerm, 10);


float d = blinnTerm*2;
    
outputColor = color(d,d,d);
}


I only get something that looks like a diffuse shader. Someone have any idea of whats wrong? Please stop me if this is impossible somehow.

@poromaa You needed a smoothstep function in order to limit the values that are above zero. I took the liberty of changing a few things to my liking. I hope this is what you were aiming for.



shader testPhong(float lightIntensity = 1,
float Ceiling = 1, /* Sets the sharpness of the edge of the highlight; lower = sharper */
float Floor = .9, /* Sets the value below which all values are 0 */
vector lightDir = vector(-0.98717,-6.82603,0.5882),   //manually entered since getAttribute does not seem to work
vector cameraSpacePosition = vector(2.24144,7.65891,7.92298),
output color outputColor = color(0,0,0))
{
float f = pow(Floor, .1); /* Power function gives the values more subtlety */
float c = pow(Ceiling, .1);


vector surfaceNormal = normalize(N);
float cosAngIncidence = abs(dot(surfaceNormal, lightDir));
 /* Replaced the clamp with abs() since the dot() always lies between -1 and 1 */
    
vector viewDirection = normalize(-cameraSpacePosition);
    
vector halfAngle = normalize(lightDir + viewDirection);
float blinnTerm = dot(surfaceNormal, halfAngle);
      blinnTerm = cosAngIncidence != 0.0 ? blinnTerm : 0.0;
blinnTerm = smoothstep(f, c, blinnTerm); /* Cuts off the values lower than the f */


float d = blinnTerm * lightIntensity;


outputColor = color(d,d,d);
}

It seems like I made a mistake in my original script, because I cannot get this to work.
My idea was that if the camera and the light was still, you would be able to move the object around and see the specular update.
Because of some reason this does not work. Tried your refined script but above test fails, unfortunately.

The reason for researching this was to be able to fake color changes in specularity by applying a ramp to the above node. That could for exampe be used for special metallic shaders or fake volumetric shading.

Thanks for your effort and sorry for my incomplete script above.

I think you’re better off just using the phong_ramp. Try it:
#include <stdosl.h>


shader node_phong_ramp_bsdf(
	float Exponent = 10.0,
	color Color1 = 0,
	color Color2 = 0,
	color Color3 = 0,
	color Color4 = 0,
	color Color5 = 0,
	color Color6 = 0,
	color Color7 = 0.125,
	color Color8 = .8,
	normal Normal = N,
	output closure color BSDF = 0)
{
	color Color[8] = {Color1, Color2, Color3, Color4, Color5, Color6, Color7, Color8};


	BSDF = phong_ramp(Normal, Exponent, Color);
}

work to help you reverse the problem of slow metabolism by speeding it up naturally. The active ingredient, fucus,bee pollen diet, helps to speed up the body’s metabolism. So if the side effect of a slow metabolism has been the result of endless diets that starve your body of the energy it needs to run, can and will work for you… Beyond diets deficient in soluble fiber making dropping excessive body weight more difficult, more health conditions are likely to come about too. Insoluble fiber is needed in order to remove toxins out of a human body. When those pollutants will not be eliminated, risks for diseases including diverticulitis, cardiovascular disease and colon cancer are elevated. However, be aware that drinks aren’t always served in standard amounts and may contain a lot more alcohol than you think. You will need to ask the server if the drink is a standard serve if it’s not clear. Take a certain amount of money with you if you go out and hold yourself to that amount. Although it is not compulsory and you might be able to do without,super slim diet pills review, there are people who decide to use natural fat burning supplements. They can be a great adjuvant in the process of losing 20 pounds fast. Also don’t forget about drinking plenty of water (just water, no soft drinks or alcohol) and to rest properly. My mother says the same thing stop eating and you will lose wieght. Little as she knows, that can cause you to starve or make you want to eat more. The best thing to do is to start off slow taking it one day at a time. It also lowers down the degrees of cholesterol. Mavik also works quite as by relaxing the bloodstream and widening them. It facilitates considerably lowering the blood pressure levels… Supplements are a good idea if you’re having trouble getting enough omega-3s from the foods you eat, for instance if you’re limiting your fish intake because of mercury concerns. (Nursing moms should eat no more than 12 ounces of cooked fish or canned “light” tuna a week, according to joint FDA/EPA guidelines, and avoid having more than 6 ounces of canned albacore tuna.) Omega-3 supplements made from fish body oils are considered safe for nursing moms a 2001 analysis found no detectable levels of mercury or toxins in the top-selling brands. But avoid cod liver oil supplements if you’re breastfeeding, because they can contain excessive amounts of vitamins A and D… These days I’m smarter,Meizi Evolution, healthier - no pills, no extreme calorie reduction. But I can tell you I’ve tried several types and none of them have made any difference than when I was just watching what I ate and working out good. And I had ill-side effects I believe were due to some of the pills I took…