Random Topic of the Week - Shaders

Hello everyone,

Welcome to this learning experiment.

Making games is an endeavor that requires a multitude of different things to come together into one product that creates something special. I love this fact but sometimes find myself reading about things that are part of making games that I've never even heard about. This thread is my attempt to teach and to learn about all the different parts that go into making games.

I will pick a topic (seemingly at random) each week to open for discussion. Hopefully it will be something that some people know nothing about, but at the same time something that some people know everything about. The idea is to share information about the topic in any way you want.

Ask questions, post articles, tell stories. Anything to create a better understanding of the subject matter of the topic. If you are part of the industry, share some good industry practices, show some work where you actually used the techniques or just share your experiences on the subject. Whatever is related to the topic, post it.

So to start this off I have chosen

Shaders

[url = http://en.wikipedia.org/wiki/Shader]Here is the wiki on the shaders[/url] to give some background.

And my first question is : How does one go about actually creating a custom shader?

tl;dr:Post something cool about shaders :P

Comments

  • I can maybe type more later, but I think the way that's the most fun for learning shaders is to use a node based shader editor, like the one found in the UDK, or the Unity plugin called Strumpy (although I've personally never used it).

    It just gives you a good idea about what kinds of functions are available to you, and a quick way to see the results of your changes. :)

    Something else that I've found useful is the Cg Tutorial book on the Nvidia developer site. It's got a handy appendix that lists available functions too. (On phone, so linking is hard. Google! :))
  • edited
    UDK is really where the shader fun happens. Thanks for the Strumpy mention @Elyaradine ... looks interesting!

    Unity isn't fun, but it is getting better with it's documentation. I actually got to grips with how a surface shader works in their shader code on the weekend.

    But because it's all very cody I wish they'd actually document all the language, or link to some resource that does. For instance I struggled for 15 minutes or so trying to figure out what the "Albedo" property of an output meant in their code (it was the r g b value, they list it a million times here http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html but never actually explain it).

    But basically I got what I wanted. I got a shader that I could control the culling based on a second texture. Essentially what I did was:

    1. Search for a shader that sort of did what I wanted.
    2. Figure out what it was doing.
    3. Modified it to my purposes.

    Most of the time this method works. Though I'd love to one day learn the shader language in Unity.

    [Edit] Although looking at their docs again, I should really have had the patience to read this: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html ... and I suspect their documentation is fairly comprehensive if I were to actually go through it: http://docs.unity3d.com/Documentation/Components/SL-Reference.html
  • I've been meaning to work through this video series for a while now. Seems promising.
    http://cgcookie.com/unity/cgc-series/noob-to-pro-shader-writing-for-unity-4-beginner/
  • http://unitygems.com/

    There's a 6-part shader thang that seems pretty good.
  • I've been messing around with shaders quite a bit these last few months and I found these resources invaluable. (I made a post about it a while ago, but in case anyone missed it)

    https://www.shadertoy.com/
    http://www.iquilezles.org/www/index.htm
  • I like it! :) Keep the information coming in. Can anyone give some numbers on how many shaders they use on average, or when it's a good time to use them. (sorry if the questions shows how little I understand. :P )
  • Um.

    If you didn't have shaders, you wouldn't be able to see anything in your game. :P Unity just comes with several common shaders out-of-the-box. Knowing how to write shaders just vastly increases the number of visual things you can do, especially for fx-type work.
  • edited
    I recently went through the nightmare of piecing together shaders in Unity. Some very useful things to know before you start:

    HLSL vs Cg vs OpenGL

    These are shader API's from Microsoft (direct-x), Nvidia and the open source world. Unity uses direct-x on pc and Open-GL on mobile. Cg and HLSL are kinda almost the same, while OpenGL is quite radically different. Thankfully Unity has built a pretty good abstraction layer on top of all these, so you (mostly) don't have to worry about the differences.

    Okay, now that we know we have an abstraction sitting on top of the underlying hardware API (Directx/openGl) we begin to have a good place to start. Next things to understand is this comes in three flavours:

    -Fixed function pipeline
    -ShaderLab/CG shaders (Programmable Pipeline)
    -Surface Shaders

    All three are substantially different, and this can be confusing as hell before you realize what's going on. Fixed function are basically "vertex" shaders, and have a very limited set of things you can do with them - and a relic from way back when Riva TnT 2 was still a thing. Next up is the programmable pipeline. This is usually where anything interesting happens - as you have far more control over what actually happens. The confusing thing here is that it is written in "ShaderLab", which is unity's own extension of Cg, Nvidia's C-like api language. So whilst cg references are useful, there are several parts of the dance specific to unity, that you have know about as well: like "magic" variables you just have to know exist and when to use.

    Next up are surface shaders - which are designed as kind of another abstraction on top of regular shaders, which provides you convenient things like lighting models and such to tap into. They are a great aid to noobs and provide a great productivity boost, but it's important to understand they only make sense if you're using dynamic lighting, and therefore they aren't really suited to mobile where most of that is optimized away. Strumpy shader editor is pretty fantastic, but only produces surface shaders.

    Confused yet?

    I recommend starting here - with surface shaders, as ( @Elyaradine mentioned):
    http://unitygems.com/

    Then once you actually understand the jargon, the unity documentation starts to become more penetrable,

    And then once you are feeling brave enough for Cg shaders, this is BY FAR AND AWAY THE BEST RESOURCE ON THE SUBJECT I'VE FOUND:
    http://en.wikibooks.org/wiki/Cg_Programming/Unity
Sign In or Register to comment.