Working with HoloLens: Learning to Write Shaders

May 18, 2016

IC Staff

Over the last six months or so, I’ve taken on the task of learning and implementing shaders for our group. For anyone who doesn’t know, a shader is the code which your GPU runs to take your object and turn it into something you can see on the screen. As you can imagine, a shader for a mobile computer with a transparent display, such as Microsoft HoloLens, might want to be different from one which your desktop uses to render to a standard screen. You need to think about both the performance on the device and the fact that you can design specifically with augmented reality in mind.

Using shaders in Unity is a process that requires learning Unity’s own Shaderlab as well as Cg code together, and it feels very different from anything I’ve written before. Cg seems to barely speak with Shaderlab, as you still have to declare things, like the inspector properties, in both languages. The hardest part, for sure, is you get no intellisense. This means that if you don’t already know the language you can’t write anything. Errors will be prevalent and won’t be well explained when they show up, meaning that hunting down a bug can be frustrating at times.

Once you make it past the learning curve, however, shaders are really powerful and fun to use. I will go more into depth with shaders in future blog posts, but I will start by pointing you at the documentation and examples that I use whenever I’m writing shaders.

First of all, there is the section in the Unity manual on writing vertex and fragment shaders. This section has many good examples and walks you through the general structure of a shader. Though it can be lacking in some of the deeper aspects of writing shaders, this is a great resource to start with: Unity: Writing Vertex and fragment shaders

For more info on all of the built-in functions you have access to, I use an appendix of the Nvidia CG Tutorial. Knowing what is available really opens up what you can do with a shader, as using a different operation can have huge impacts on what shows up on screen. In more complicated shaders, especially ones which aren’t trying to mimic reality, some of the more exotic functions can be really helpful. Nvidia: CG Tutorial appendix e

I hope this helps you get started. There are plenty of great blogs and tutorials out there that dive into some of the more complicated implementations of shaders – these are great once you get a hang of the basics. I will go more into depth in future posts, but until then I highly recommend trying what I did initially: look at some basic shaders and try to tweak them to see what each of the parts do.

  • Henry Eastman

IC Staff

Posted in