Home Technical Talk

Shader - Unormalized Light Vector

polycounter lvl 12
Offline / Send Message
Ace-Angel polycounter lvl 12
Heya peeps, it's me again!

Question: I was wondering how I could create an Unormalized Light Vector from scratch?

I was taking a look at several engines out there for RT shaders, and most, if not, all of them, only had premade Light Vectors which were Normalized right off the bat.

So yeah, any helpsies here? I tried a google search, but I'm getting some non-so-standard-language which I'm unable to read, such as this:
/ compute the light vector in model space float3
vec = refPos - pos;
Which I have no idea on how to read it. Does RefPos = to ObjectSpace? And what does Pos refer to? Argh, my poor little mortal brain!

Anyhow, cheers and ciao!

PS: I'm using UDK if that matters, but any HLSL explanation is more then welcome for me.

Replies

  • LoTekK
    Options
    Offline / Send Message
    LoTekK polycounter lvl 17
    For the example code, I read that as
    Light Vector = objectPos (World Space) - lightPos (World Space)

    which would give you the unnormalised light vector in object space...

    Curious, though, why do you need an unnormalised light vector?
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    Official Story: Trying to get some kind of pseudo-attenuation for a backscatter SSS effect I'm trying to create on some plants (since from what I understood, to get the proper Attenuation, I need an Unormalized Light Vector which is has it's Length calculated instead).

    Unofficial Story: I like learning about shaders ;P

    Cheers and thanks.
  • Gestalt
    Options
    Offline / Send Message
    Gestalt polycounter lvl 11
    Unnormalized as in distance? A normalized light vector would give you a direction, unnormalized would deal with the length, so multiplied by a scalar. I guess x,y,z distance with the light as the origin would give you one with a scalar referring to the distance, which I guess could be pretty useful info (knowing that scalar distance for falloff or something).
  • LoTekK
    Options
    Offline / Send Message
    LoTekK polycounter lvl 17
    Ah, gotcha. Yeah, basically your unnormalised light vector in the case above would be the same as your normalised light vector multiplied by the scalar distance from light to object.
  • Ben Cloward
    Options
    Offline / Send Message
    Ben Cloward polycounter lvl 18
    A light vector is calculated by subtracting the object's position from the light's position. In order for the vector to be correct, both the light position value and the object position value must be in the same space (object space, world space, camera space etc.) So you'll probably need to transform one of the vectors into the space of the other before you do the subtraction. I generally prefer to work in world space. Most shader environments give you the light's position in world space, but the object's position in object space - but this totally depends on what the engine gives you. I'm not familiar enough with UDK to get more specific.
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    Gestalt wrote: »
    Unnormalized as in distance? A normalized light vector would give you a direction, unnormalized would deal with the length, so multiplied by a scalar. I guess x,y,z distance with the light as the origin would give you one with a scalar referring to the distance, which I guess could be pretty useful info (knowing that scalar distance for falloff or something).

    Yep, that's the idea ;)
    LoTekK wrote: »
    Ah, gotcha. Yeah, basically your unnormalised light vector in the case above would be the same as your normalised light vector multiplied by the scalar distance from light to object.

    Cool to know, cheers!
    A light vector is calculated by subtracting the object's position from the light's position. In order for the vector to be correct, both the light position value and the object position value must be in the same space (object space, world space, camera space etc.) So you'll probably need to transform one of the vectors into the space of the other before you do the subtraction. I generally prefer to work in world space. Most shader environments give you the light's position in world space, but the object's position in object space - but this totally depends on what the engine gives you. I'm not familiar enough with UDK to get more specific.

    Ah, got you, that makes sense. Cheers!
  • ambershee
    Options
    Offline / Send Message
    ambershee polycounter lvl 17
    I did this (also an SSS backscatter effect) by passing in the light data from UnrealScript.=; it seems to work smoothly enough :)
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    I was thinking of doing that, but frankly, US is kinda out of my league at this point, especially for materials.
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    Hey guys, it's me again, some quick questions:

    1- When transforming my Light Vector to World Space, does it need to be Local, or is there something more to it?

    I currently have this LV -> Transform from Local to World -> Etc... (Object World Position - World Light Vector).

    2- I was looking up the math for Length, which seems to be this V, is it the correct one, or is there more to it?

    Vector3Lenght = (R^2 + G^2 + B^2) SquareRoot'ed.

    3- Pretty daft question, but how exactly would I know if my attenuation is working in the 'correct' way? Currently, due to Post-Processing, the results looking like a glowing ball of fire, in UDK atleast.

    Cheers!
  • JordanW
    Options
    Offline / Send Message
    JordanW polycounter lvl 19
    1. Light vectors in UE3 are provided in tangent space, so you need to transform tangent space to world space.

    2. UE3 has a node called distance: http://udn.epicgames.com/Three/MaterialsCompendium.html#Distance
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    Cheers Jordan! Should prove most useful in my case.

    EDIT: OK, to UDK pro's out there, let me ask this, is it even possible to over-ride UDK's shadows when it comes to the materials, in a friendly way, to achieve a controllable SSS?

    The reason I'm asking this, is because I noticed the SSS only appears in the non-shadow-culled parts of the mesh, so you're limited in what you can do, unless you very specifically get rid of ALL shadows, which is what I would like to avoid.

    The reason I'm asking all this, is because my attenuation doesn't seem to be working at all, while on the other hand, controlling the radius of my light does what I'm trying to do with the material...if that makes sense.

    Basically, controlling the Light Radius = Light Propagation on Mesh vs. Attenuation Material = Nothing.

    Here is the material I currently have, not sure if anything is wrong with it. Kudos to Ben for his examples, from which I copied to test out the results.

    40088733.png
  • ambershee
    Options
    Offline / Send Message
    ambershee polycounter lvl 17
    I'm not sure if you can get around shadows in custom lighting. These days, I've been tending towards avoiding custom lighting and using the emissive channel for custom lighting effects instead.
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    That explains it then, it was vexing me on why the shadows aren't playing nice with the Attenuation, yet manually changing the site of the Lights Radius did.

    Also, you can control the Emissive Channel? Mind telling me how, that would be most welcome in my case. I tried playing around with the Emissive as a cheap hack, but it doesn't respect the values I put in it.

    For example, putting in an Inverted Lambert term, with my SSS mask will make ALL of my mesh Glow, even if I 'cut' the Lambert term in Half with a Power Node, the Emissive will make all of my mesh Glow, it doesn't respect any Terms I put in it.
  • LoTekK
    Options
    Offline / Send Message
    LoTekK polycounter lvl 17
    Ace-Angel:
    I just spent some time trying to get that working, and it appears that the emissive input, for whatever reason, doesn't like the Light Vector node. If I use world up, I can get the emissive playing nicely, but the moment I switch the dot from world up to Light Vector, I get a constant value over the entire surface of the mesh...
  • ambershee
    Options
    Offline / Send Message
    ambershee polycounter lvl 17
    That's annoying - I have used the light vector with emissive before and not had any real problems, but not with this kind of effect. If I get some time on Sundya I'll have a tinker.
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    @LotekK: Cheers, I was hoping you would say that, or else I think I would be going insane, haha.

    @Ambershee: Fingers crossed, maybe the old versions of UDK didn't have this issue? Argh, silly UDK.
Sign In or Register to comment.