Cel Shading in Unity 5

This week marks the beginning of development for a group project, based on the theme of "trust". "Atl and Ollin" is about two children who in their mischievous ways, manage to break a forgotten shrine. In doing so they cause a mysterious god to become outraged, finding themselves hunted by a demonic fog which must be escaped by working together. The art style is quite surreal, drawing inspiration from Piazza D’Italia, an artistic piece shown below by Giorgio de Chirico, alongside titles such as Rime and Journey.






I wanted to produce a shader that was reasonably accurate in regards to the art style as early as possible. This way visuals assets and how they are represented in the game could be designed and refined in an environment showing what closely resembles their final state. While I did briefly try the toon shader included as part of Unity's standard assets, I decided to write my own and introduce the addition of rim lighting and support for normal maps. There is another toon shader I found on Unity's asset store with rim lighting per se, however I found it odd looking having not been based on the same diffuse lighting ramp nor did it support shadows.

Initially I was calculating the lighting based on a hard coded formula with only two possible shades, dark and light. I soon changed to a ramp texture for finer control over not only the progression of shades, but their colours allowing for the subtle introduction of warm and cool tones. Due to a silly mistake having forgotten to multiply light by its attenuation, for a short period of time I was under the impression that shadows were only possible in a differed pre-pass lighting model. Maybe I needed more coffee.

It seems that Unity 5 won't allow you to modify the internal lighting model when rendering in deferred mode, or at least not that I'm aware of. Switching the rendering mode to legacy deferred as used in Unity 4x did prove successful, albeit this was not something I wanted to do. I reverted back to a forward shader after spotting my earlier mistake, the added complication of having to do anti-aliasing manually was also beyond the technical scope of the project so my decision was final.




With shadows now working, I added rim lighting and had it computed based on the same diffuse ramp texture. Both the base and rim colour are adjustable along with rim power and a version with support for normal maps. The images below shows the above ramp texture used on some untextured models with the shaders default configuration. Here are links to the source code for the diffuse and bumped diffuse versions of the shader See update below. Leave a comment should you use it in your own project as I'd love to see what you're working on!






Update: Here are links to the source code for updated versions of the shaders, with added support for transparency:

Cel-Bumped.shader
Cel-Transparent-Diffuse.shader

Ensure ramp textures have their wrapping mode set to clamp and not repeat, to avoid potentially unwanted artifacts.

Comments

  1. Hi Tim - would love to talk to you sometime about some of these affects you've achieved in Unity 5. Let me know if interested. The project I work on is Edorble (edorble.com).

    ReplyDelete
  2. Hi, this is awesome, keep up good work.

    ReplyDelete

Post a Comment

Popular Posts