Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How do you create a good metal shader?

For different metals and say more or less eroded / rusty and so on.

I know that one difference from ordinary materials is that metal should 'colour' the specular light but when I do that with gold for example it just looks "yellow", not metallic at all.

Any help appreciated!

share|improve this question
1  
Your question sounds like it's something simple and specific, but it's really not. You can't replicate a metallic look with a shader alone. Doing so requires proper textures, so you need a specular shininess texture, probably a bump map if the metal is at all irregular, etc. If the metal is polished to any degree, you need to reflect the environment (or some caricature thereof) as well. It's not a simple "here's some metal shader code" issue. –  Nicol Bolas Jan 30 '12 at 16:49
    
Look into the subject of anisotropic reflections too, a phenomenom usually seen in brushed metals. –  David Gouveia Jan 30 '12 at 19:06
    
@David: That looks really cool, maybe it's a bit overkill for me at the moment but I definitely check it out and use it if I can :-) –  Valmond Jan 30 '12 at 20:05
    
@Nicol Thanks for the comment, I'm completely OK with using textures, my 'normal' shader is composed of 3: Diffuse, Glow and a controlmap including Specular, Spec-power and reflection (from a cube map) –  Valmond Jan 30 '12 at 20:10
    
A quick search turns up many different metal shaders and straight from the horse's mouth: developer.download.nvidia.com/shaderlibrary/webpages/… –  Patrick Hughes Jan 30 '12 at 21:06
add comment

2 Answers

up vote 4 down vote accepted

I have recently created shaders for both clean and brushed metals for my game in progress and I'm quite happy with the results.

Clean metal: The most important feature in my experiments was the environment reflections with cube maps. I implemented them with realtime render-to-texture environments, but also static images often gives good results and it's easier to blur them. The second most important feature was fresnel effect, which makes the silhuettes of the objects better. This for example gives a rounder look for balls. Specular lighting is not necessarily needed, as the cube map can provide the highlights directly. HDR rendering is also important to make the cube map reflections look good.

Brushed metal: For brushed metals you get good results quite easily by using a brush texture, something like this, together with anisotropic lighting. For anisotropic lighting you can find tutorials from the web, but I implemented my own by using blinn-phong as the base, but from the half-vector I eliminated 80% of the tangent direction. This means that you need tangents or bitangents in addition to normals in your models. The environment reflections are tricky for brushed metal. You can sample from multiple directions from the cube map based on the tangent/bitangent directions, but you might need lots of samples for good results. This however is not necessarily needed, as the texture and anisotropic lighting with multiple light sources already looks pretty nice. The end result in my case looks like this:

Brushed metal material

share|improve this answer
add comment

As Nicol said in his comment, there are a lot of things that go into a high-quality metal shader. For best results, your renderer needs to be gamma-correct and use physically-based shading models (see particularly the two papers by Naty Hoffman at that link).

Given those things, the typical way to make a metal material is to have a very low or nonexistent diffuse color and a high specular reflectance, ranging from 50% to 100% (in linear color space). As you noted, for some metals the specular will be colored.

Because the metal's appearance is entirely specular with little or no diffuse, reflection will be extremely important. If it only specularly reflects point lights, you will get the appearance of a black material since the rest of the environment is missing (even a very rough metal will reflect a blurred image of its surroundings). A typical way to get more reflection is to use a cubemap. Realistic reflection in real-time graphics is a huge topic, though, and there's a lot of different approaches out there.

share|improve this answer
    
Thanks, this is exactly what I was looking for! As I do reflections with a cube map, maybe I should use a sort of environmental lighting as a very high specular lighting? –  Valmond Jan 30 '12 at 20:04
    
@Valmond I'm not sure what you mean by "use a sort of environmental lighting as a very high specular lighting". All I meant was that specular reflection should include both light sources and everything else in the environment (added together), where the "everything else" part could be implemented as a cubemap. A typical way is to generate several blurred cubemaps for different roughnesses as shown here. –  Nathan Reed Jan 30 '12 at 21:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.