Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GLSL light data – what actually works?

Discussion in 'Shaders' started by Jessy, Jun 29, 2011.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    gl_LightSource[0] and uniform vec4 _LightColor0 work sometimes; sometimes they don't. #pragma debug with Mobile/VertexLit (Only Directional Lights) seems to tell me that only _LightColor0 is used, but that can't possibly right. It works with multiple lights and only a single pass. The lack of information on what to use, and the inability of Unity to decide when to allow something to work, is very frustrating. :mad:
     
  2. iotadev

    iotadev

    Joined:
    Jun 29, 2011
    Posts:
    3
    I know how you feel...
    I'm trying to create a glsl shader that requires even more control than what the "surface shader" provides... so needs to use the traditional way of using the vertex and fragment shader pair.

    I've been trying to access the spotlight data(angle, range, intensity...etc) with no success, the gl_LightSource[x] will compile but always return empty for its .spotDirection or .spotCutoff etc...where does unity put them =_="....

    It seems the position of the light is accessible by using "_WorldSpaceLightPos0"... but that's all i can find...

    I've been geting some useful helps from:
    http://en.wikibooks.org/wiki/GLSL_Programming/Unity
    but most of the codes it provides doesn't work... outdated version perhaps?


    I wish they have better documentations for these in the future
     
  3. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Hi iotadev!

    You found my wikibook, nice! :)
    The deal is: that wikibook is under heavy construction; thus, of course, things won't always work.
    That said, hey!, it's a wikibook: just complain on the discussion page if something doesn't work. You don't even need a login to do that. And you would probably be the first to write a comment; thus, I would very much appreciate it.

    Anyway, could you be more specific about which code doesn't work for you, i.e. which specific pages? I'm working with Unity 3.3, thus, I think it shouldn't be outdated.

    I would appreciate any help in improving this wikibook!
     
  4. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Also, I should mention that I work with the free version of Unity under Windows and haven't tested any code on Mac (but could easily do so if something doesn't work).
     
  5. iotadev

    iotadev

    Joined:
    Jun 29, 2011
    Posts:
    3
    Hi Martin,
    You really have some great knowledge in that wikibook,
    Sorry I had to conclude that many codes doesn't work,
    I keep getting compile errors like:
    "GLSL Error in Vertex Shader: ERROR: 0:149: 'constructor' : constructing matrix from matrix (reserved) at line 0"
    I think the main cause is the "mat3(modelMatrixInverse)" or specifically mat3(_World2Object).
    And because almost all the shaders in the wikibook uses that line.... I can't get anything to work...

    However when i remove that and provide my own normal some codes will run and I think look what's intended but some I get this:
    "GLSL Error in Fragment Shader: ERROR: 0:158: 'assign' : I-value required "normalDirection" (can't modify a varying) at line 0"

    So I deleted the first line in the fragment shader "normalDirection = normalize(normalDirection);" as it says you can't modify a varying value... which makes things compile ok....but when i run the game, I don't know if it looks like what's intended...some looks broken...(might just be the normal i gave is wrong...)

    Sorry I'm still a newbie to glsl programming or unity shaders so I'm basically left scratching my head and concluded probably the codes are outdated...

    I'm using Unity iPhone Basic on mac, but the error doesn't sound like a mac vs window or iphone vs indie issue

    Also would you happen to know how to get glsl to work with unity spotlight? I only have like _LightColor0, _WorldSpaceLightPos0 and some other custom calculated values to play around.....but with those I still can't access the spot angle and intensity etc so can't get that "cone lighting" effect going....:(
     
    Last edited: Jul 1, 2011
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    It really is a pain that you can't use mat3(mat4). I don't understand why – maybe Martin can tell us – but you can get around it by doing it manually. It really does seem to be a Mac vs. Windows issue, because Martin's on Windows and we're on OS X.

    Code (csharp):
    1. mat3(modelMatrixInverse[0].xyz, modelMatrixInverse[1].xyz, modelMatrixInverse[2].xyz)
     
    Last edited: Jul 1, 2011
  7. iotadev

    iotadev

    Joined:
    Jun 29, 2011
    Posts:
    3
    ah sweet~ got some shader looking like how it described :). my normal calc was wrong =_="...haha
    Thx Jessy
     
  8. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Thanks guys! Seems I have to do some testing on MacOS X and maybe get an iPhone Basic version.

    - I'll remove the assignments to varyings in fragment shaders. (I got warnings but it compiles OK under Windows; thus, I thought I get away with it.)
    - I'll check into the mat3(mat4) constructor. I saw it in the OpenGL ES 2.0 book (as far as I remember); it might also be a PowerVR thing that it doesn't work on iPhone. (I'll check.) If it doesn't work, I'll have to remove it.

    With respects to spotlights: as far as I understood it, Unity implements them by a light texture similar to a cookie/gobo. Therefore, I assume it doesn't send the data to the GLSL program. At least that's my understanding of it. You should check AutoLight.glslinc in Unity/Editor/Data/CGIncludes. I guess that will be another page in the wikibook... :)

    Cheers,

    Martin
     
  9. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    After looking at the GLSL specification and some testing with Xcode, I conclude that the constructor mat3(mat4) works on current iOS versions (at least on my iPod touch 4). I don't know why Unity doesn't accept it (maybe because some older versions of iOS had problems with this). One way around is to do a conversion as Jessy suggested. But I think I prefer to avoid mat3 matrices, e.g this way:
    instead of "gl_Normal * mat3(modelMatrixInverse)" use "vec3(vec4(gl_Normal, 0.0) * modelMatrixInverse)".

    The assignments to varyings in fragment shaders are apparently not allowed on iOS. I'll fix it in the wikibook.

    Thanks again for bringing this up!
     
  10. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    The problem with that, as I understand it, is that unless you're using high precision, PowerVR hardware will treat that as a scalar operation. (I've been using low precision or medium precision on everything.)

    Low would theoretically be faster, as long as your values only range from -/+ 2, but swizzling low precision variables is slow. I'm not knowledgeable enough to know what exactly constitutes swizzling, though (e.g. is working with .zw swizzling, or is it only when you rearrange variables?).

    Not that this is necessary relevant to your wikibook, Martin, but it is for me. I don't care about any other platforms that iOS at the moment, so I'm reading you book exclusively to learn to make better iOS software.
     
    Last edited: Jul 2, 2011
  11. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Well, it's difficult to tell what any given hardware does. I guess the only option is to measure the performance of the two variants and see what the difference is.
     
  12. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    I found this cool page (which is probably outdated but might still work): http://unity3d.com/support/documentation/Components/SL-Attenuation.html

    This might be useful to get GLSL shaders to work with standard Unity features (such as spot lights, shadows, etc.)

    Another useful link:
    http://forum.mentalimages.com/showthread.php?5266-Mental-Mill-Export-For-Unity#7
    which gives more built-in uniforms (although these are mainly vertex lights or otherwised exposed by the OpenGL compatibility uniforms).
     
  13. Somian

    Somian

    Joined:
    Nov 22, 2011
    Posts:
    38
    Hi,

    I'm trying to write a glsl shader that uses light properties.

    I also tried copying some sample code from the wiki book and there's already the problem. sometimes, they work, sometimes they don't :/

    for example, sometimes the _LightColor0 is just black, sometimes it's the color of the light. also, sometimes it only works while I move the light but ceases to work as soon as I stop moving the light etc. x...X How can I write that shader?

    compared to straightforward glsl I find the shaderLab far too complicated and high level. I just don't understand what's going on, I have no ideas how many texture, vector etc. operations i'll actually be doing, that's why I want to use glsl :/