Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Confused: A noob's question(s) about attenuation

Discussion in 'Shaders' started by erlGrey, May 16, 2014.

  1. erlGrey

    erlGrey

    Joined:
    Apr 8, 2014
    Posts:
    19
    Hello all,

    I'm sure some of this will sound dumb; I've been reading and tutorializing for a tick, now, and my little walnut brain is aching, so bear with me.

    So: 1) Am I right in thinking that returning the attenuation of a spotlight is only possible in a fragment shader? If I'm wrong, would someone mind nudging me in the right direction? I found this, but I think he's using a directional light. (And correct me if I'm wrong in thinking attenuation = intensity at a given location pretty much directly.)

    2) Ehh, when you add custom lighting models, is surf() reading from Lighting[nameOfFunction]()? I'm hazy on the big picture of returning a value (called 'c' by convention) in the lighting portion and how it interacts with surf(). I mean, I can see the obvious cause and effect, but I don't understand how it's possible/what it's generally doing under the hood--it's a confusing structure to a non-shader coding coder.

    K, I'm gonna stop this here. Thanks a million for any guidance. My ultimate goal is very similar to the question that was posted in the link above (I want to drive emission values based on light received), but I think I need more basic understanding before I get there.

    Thanks again!
     
  2. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    You COULD calculate the attenuation of any light in a surface shader, but if you're going to change lighting, you might as well use plain fragment shaders, because the whole point of surface shaders is that you don't have to handle lighting manually.
    My understanding of attenuation is that it's a measure how much energy is lost relative to the source. The LIGHT_ATTENUATION macro from AutoLight.cginc basically gives a value of how much light is left after distance attenuation, and shadows have been applied. Wikipedia might be able to give you more general explanation.

    If you look at the stuff generated by a Surface Shader using #pragma debug, you can see exactly what happens.
    Your custom surf function is called by the actual fragment shader function, and the struct it outputs is fed to the lighting function, which is then return as output of the fragment shader.

    The emission values bypass the regular lighting. They're added to the result of lighting. If you only used Emission, you could just use a Vertex/Fragment shader instead.
     
  3. erlGrey

    erlGrey

    Joined:
    Apr 8, 2014
    Posts:
    19
    Thanks!

    Yeah, on attenuation, I guess I just need to figure out how to make it work for spotlights; I'll keep at it.

    Oh #pragma debug, genious. Right.

    Yeah, I'm only going to use Emission, but I want effectively zero emission wherever there's no light (for what I think are good reasons ^^). By the sound of it, I think I need to start diving into fragment shaders more.
     
  4. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    It's going to be even easier soon™.

    Sounds like a good enough reason to me.
     
  5. erlGrey

    erlGrey

    Joined:
    Apr 8, 2014
    Posts:
    19
    YYYAAAAAAYYYYYYYY

    I'm so happy, guys. I thought I'd never understand this junk ; )

    For other noobs who come searching in the future: the wiki entries (which I couldn't really comprehend the first time I saw them) ended up being easier for me to learn from than #pragma debug on, say, the standard diffuse shader, which is really cluttered from the catch-all's and "if lightmap" stuff. And to get that emissive look from a working frag shader I just clamped the dot(normalDirection, lightDirection) value.

    Welp, this is probably a worthless comment, all-in-all, but I was so excited I had to post something and get it out of my system since I have no real life coding friends to talk to.
     
  6. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Definitely not a worthless comment. It's good to hear that you got the result you were looking for, and how you achieved it. And at some point someone's going to find your thread through a search engine.