Thursday, May 8, 2014

Physically Based Shader v0.11

RSRMs are up and running on my PBR shader.

Local point lighting.
Directional lighting from behind camera. Materials are exactly the same.
Here's a quick demo of the setup. You can see a material with various levels of smoothness and metallicity:

Sunday, May 4, 2014

Physically Based Shader v0.10

So I started learning CG a few weeks ago (partly inspired by an awesome GDC talk by Sean Murphy) in order to build a physically based shader. This here is the first version of that shader:


The shader takes four texture maps: "main color", "smoothness", "metallicity", and "normal". The main color gets split into the specular and diffuse map based on the smoothness and metallicity of the material. The specular contribution is a normalized Blinn-Phong and the diffuse contribution is an NdotL normalization that I made. Using a "light wrap" property, the material can transition from a lambert, to Valve's half-lambert, to a perfectly translucent material. This wrap can be used to help emulate partially bounced light and subsurface scattering with minimal additional cost. Both the spec and diffuse contributions are affected by a fresnel term that is softer on rough materials and shallower (due to a higher base reflectivity) on metallic materials. I also decided to use a physically-inaccurate attenuation model that uses a gaussian curve instead of the square of the distance. This sort of falloff is much easier to work with in a practical case because near to the illuminated object, it better models the light coming off an object that is not an infinitesimal point. This attenuation is currently calibrated to work best at ranges up to 15 meters (after which, the contribution of an individual light falls effectively to zero, and the light can be culled).

I had this shader nearly finished when Ludum Dare 29 started last weekend, so I used it all through the production of "The Fields" in order to test the shader's applicability in an actual production.

Rabbit in a minefield; lit by one directional light and an ambient.
I'm happy to report that it went pretty well! I discovered a bug that caused negative lighting in very rough, very metallic materials, but luckily, I was working in a game that only had rocks, dirt, wood, grass, and bunnies, so it wasn't much of an issue. After LD, I went back and fixed the bug (a problem in how I was normalizing my spec contribution that could cause it to go negative) and here we are at version 0.10!

Since it doesn't take very many maps and two of the maps are black and white, I found it pretty easy to work with and maps were generally pretty easy to generate. Everything except for the wood is hand-painted, and the only things in the scene not using the physical shader are the alpha cut-out materials. I hope to have a greater variety of colors and materials to shade in the next use case; World War I is not a very vibrant setting.

I still have a ways to go. The current shader uses the unity ambient light to determine non-direct shading and environment reflectivity. This looks okay on rough materials and diffuse materials, but becomes obviously flawed on specular metals -- especially when they have a not-very-noisy normal map. Next version will support RSRMs that will either be stand-alone reflectance maps, or will be overlayed on the ambient light and provide more value information than color. I think I'll add support for cube maps, since the main point of this shader is to be easy to work with and cheap to render, and I find cubemaps (and their mips) to be a pain in the ass to generate and work with (you can only ever appreciate them on extremely smooth surfaces, and if you have a lot of those, the failures of non-parallax corrected cubemaps start to show anyway).