I am really getting into this OSL thing and I like to share a second shader with you all. Detailed instructions and an example noodle can be found here: http://blenderthings.blogspot.nl/2012/11/a-scales-osl-shader-for-blender.html
I have called it a scales texture but it can be used for things like roof tiles (or shingles) or perhaps for a pine cone as well. I have built in a neat trick to output a random-but-constant vector for each scale so by plugging that vector into a noise texture you can give each scale its own, unique color or texture. In the image below I have used that to color the scales on this abstract Koi carp (yeah I know, it takes some imaganition ) Anyway, the example noodle in the article is the one that produced this image so you can have a look at how it was done.
Are you sure it’s not just a combination of the shape of the bumpmap and the lighting, because any bumpmap in this kind of situation will have sides that are in shadow and sides that are in light like they’re supposed to.
The only reason there wouldn’t be shadow is if it was a fake bumpmap that’s really a diffuse map with an emboss filter applied in a paint program.
the texture doesn’t produce normals but a distance to the centre of a scale. The example noodle takes this distance, inverts it and plugs it into the displacement input of the material output node. In Cycles this means a real displacement, not just a slightly altered normal, so you really can get shadows here.
I do agree that the inverse of the distance to centre of the scale is a rather crude way to determine the displacement because at the edge of the scale you get a very abrupt step which will probably cause ugly shadows. In a shader it is difficult to blur a value like you might do in the compositor so a better approach would be to produce a smooth function instead of just the distance to the centre. I will think about it but it is not easy to come up with such a function. Simply reducing the amount of displacement may already improve the appearance.
Have you tried stringing the input through a bump node so as to plug it into the new normal input of the shader nodes, because I believe that might solve your issue on how to create normals for the scales. (this is for creating bumpmaps only though).
I am afraid you cannot, or not in any direct way. (If anyone thinks differently, please let me know!). In OSL there is no direct information about the light, only so called closures that behave like parameterized functions that incorporate the behavior of a surface in response to light, including sampling probability and what not.
Having said that, a diffuse() shader does more or less what you want, i.e. returns a closure that is the sum over all products of lights and surface normal, however the sad thing is that you can only multiply or add colors and other closures to closures (you cannot access the color components for example) so I don’t see a way to for example make the color completely black if the luminance is less than some threshold.
Thank you for your reply，a diffuse() could be use in this way. It’s a clue for me to try.
I think though there arn’t a light direct infomation in OSL, should we add a new node which can give arbitrarily mesh/object position in scene. If this can be implement，as in OSL/cycles any light is surface, we should get the light position and calculate the dot which light direct and surface normal.