1. The space forums use a separate registration system from the main virtual world. To comment on these forums, you need to create a separate forum account.
    Dismiss Notice
  2. Hey there, if you want some direct help without hopping inworld - try our Discord channel at https://discord.gg/mptfycQ
    Dismiss Notice

A brief friday morning/afternoon discussion with Adam re: Shaders

Discussion in 'General' started by James Stallings, Oct 28, 2016.

Share This Page

  1. James Stallings

    By:James StallingsOct 28, 2016

    Sep 28, 2016
    Likes Received:

    Just read the italic parts if you don't want to imagine the rocks in my head rattling when I nod ;)

    [8:13:27 AM] James Stallings: so Adam, when you have a few spare moments, would you mind commenting on the potential difficulty of recreating this shader with the 'Standard Specular Setup', in the interest of making it somewhat more water-like?

    [8:13:47 AM] Adam F (swc): OK - it's not too hard, but you will need to get down & dirty and code up a lot of the shader inputs yourself.

    [8:14:00 AM] James Stallings: ;) I knew it was gonna come to that eventually

    [8:14:04 AM] Adam F (swc): This'd probably be a good project to learn shader coding
    [8:14:08 AM] Adam F (swc): It's actually way simpler than it sounds.
    [8:14:14 AM] Adam F (swc): Start here: https://docs.unity3d.com/Manual/SL-SurfaceShaders.html
    [8:14:27 AM] Adam F (swc): and take a peek at the examples here too (may be a better start point):
    [8:14:27 AM] Adam F (swc): https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html

    [8:14:29 AM] James Stallings: I've had a peek at a few, it's not incredibly intimidating :)

    [8:14:49 AM] Adam F (swc): Basically, this is the info I needed when I started doing shaders that I wish someone had told me:
    [8:15:11 AM] Adam F (swc): - A shader is a combination of two small programs which run on the GPU. Programs is a overstatement - they're actually single functions.
    [8:15:29 AM] Adam F (swc): - Those two are a vertex program (function), which runs once per vertex being rendered by this shader
    [8:15:48 AM] Adam F (swc): - And secondly, a fragment shader, which is run once per pixel being rendered (so the function returns the pixels colour value)
    [8:16:10 AM] Adam F (swc): Vertex shaders run first, and the outputs from the vertex shaders are interpolated and fed into the fragment shaders

    [8:16:19 AM] James Stallings: nods

    [8:16:38 AM] Adam F (swc): Shaders have no concept of other data - a vertex shader cannot peek at another vertex's data/memory, since they run massively multithreaded (you don't know what's rendered when)
    [8:16:46 AM] Adam F (swc): likewise, fragment shaders can't peek at neighbouring pixels values

    [8:16:53 AM] James Stallings: right, all decoupled parallel

    [8:17:04 AM] Adam F (swc): The only way to do this is to render another 'pass' - which will run after everything has run; then you can run another batch of programs

    [8:17:14 AM] James Stallings: nods

    [8:17:23 AM] Adam F (swc): Sometimes effects use multiple passes for things like blurring (motion blur in particular is often fairly multipass)
    [8:17:27 AM] Adam F (swc): So - that's the basics.

    [8:17:34 AM] James Stallings: excellent

    [8:17:38 AM] Adam F (swc): Now; Unity has some built in functions to make writing shaders a lot easier.
    [8:17:51 AM] Adam F (swc): "Surface Shaders" are one --- instead of having to calculate everything.
    [8:17:58 AM] Adam F (swc): Normally each shader has to implement an entire lighting pipeline, etc.
    [8:18:13 AM] Adam F (swc): (no such things as 'libraries' or shared code between shaders here)

    [8:18:23 AM] James Stallings: right

    [8:18:29 AM] Adam F (swc): Surface Shaders basically let you just give some values in your fragment shaders for common outputs
    [8:18:54 AM] Adam F (swc): so, you output the surface colour pre-lighting, and Unity will do all the lighting on top for you. You can then output extra variables to hint that -- like the specular data, etc.
    [8:19:16 AM] Adam F (swc): The main 'outputs' from a standard surface shader (depends a little on your setup) are:
    [8:19:24 AM] Adam F (swc): - Albedo (the base 'texture/colour')
    [8:19:37 AM] Adam F (swc): - Emission (lighting sources, e.g. 'fullbright' materials, so the illuminated areas, etc.)
    [8:20:07 AM] Adam F (swc): - Specular OR Metallicity + Smoothness --- defines how the lighting reflects, you need to pick one of the two here depending on which lighting model you specify in your surface shader
    [8:20:19 AM] Adam F (swc): There's also others for Occlusion, Normals, etc.

    [8:20:24 AM] James Stallings: nods

    [8:20:28 AM] Adam F (swc): Other quick notes:
    [8:20:51 AM] Adam F (swc): - Don't use "if" statements. The GPU has to run the program twice, one for each possible combination - it then picks the correct result and disregards the other (it's faster to implement branching this way)
    [8:21:13 AM] Adam F (swc): But the net result is they have a big perf cost - instantly doubling your fillrate expenses

    [8:21:36 AM] James Stallings: right, I picked up on some of that in my reading

    [8:21:59 AM] Adam F (swc): - If you're doing something stupidly complex, try do it in the vertex shader, there's a lot less vertices than pixels on screen.
    [8:22:15 AM] Adam F (swc): Otherwise yeah - the Unity docs are good

    [8:22:15 AM] James Stallings: makes sense
    [8:22:46 AM] James Stallings: yeah I read through this one doc on making a shader that basically drops a light accumulation of snow on everything
    [8:22:57 AM] James Stallings: it was a pretty good introduction

    [8:23:06 AM] Adam F (swc): Ah, OK - yeah there's lots of nifty stuff you can do with shaders.
    [8:23:16 AM] Adam F (swc): and that's a understatement of the year.

    [8:23:20 AM] James Stallings: heh
    [8:23:37 AM] James Stallings: ultimately, it's all done with shaders of one sort or another
    [8:23:41 AM] James Stallings: yes?

    [8:23:42 AM] Adam F (swc): Yeah
    [8:23:49 AM] Adam F (swc): Everything on screen is pretty much being pumped in by one shader or another.

    [8:24:11 AM] James Stallings: excellent, that's how I've been conceptualizing