Saturday, April 9, 2011

Shaders.

Have been working with Unity's built in shaders recently and have had a couple of issues with how they work.
In Overkill we want the player to be able to walk behind buildings and then those buildings will turn halftransparent while they are obstructing the view of the avatar.

First problem came when we discovered that the specular (reflective glow) and transparency were both taken from the Alpha channel of a texture. In a specular texture you define what things on the texture will reflect light by making them more white, so a black part will not reflect any light at all. In transparency black means 100% transparency and white 0% transparency. So what happened when we walked behind a wood building with windows was that when the building were to go halftransparent it vanished complelty except the windows. ( since wood = no reflection = black = 100% transparency)

Second problem was that the shaders did not take into consideration what parts of the building were closer to the camera then other parts in it. So when rendering the house it showed us all the sides of all the parts in the model and that makes it look really weird. We dont want to see the wall inside the building facing us when its transparent, we just want to see the front of the model as transparent.

Everything might become clearer if you see this example on the knight model.
The feathers in the hat don't reflect any light and therefor they instantly vanish if we turn on a transparent shader.
The shoulders show the problem with considering on what depth the parts rendered.

It is now fixed after hard work, testing and research. First and foremost learning shaders scripting, Unity shaderlab and then getting it right.
End result is a shader that has a separate specular texture. Alpha channel on the main texture alpha and it also considers the depth when rendering.

Enjoy.

0 comments:

Post a Comment