Search Unity

bypassing linear lighting limitation for the iPAD manually

Discussion in 'Shaders' started by flapyfox, Mar 17, 2012.

  1. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    the linear color space lighting is not supported for the iPAD in Unity 3.5.
    I am trying to manually bypass this limitation by having the final result for a lightmaped shader to be gamma corrected?
    is it possible to tell a shader to ungamma the input textures before performing calculations and just before writing the final result to gamma correct the final pixel?
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    s.Albedo = pow(tex2D(_MainTex, IN.uv_MainTex).rgb, 2.2);

    // do lighting calcs, etc...

    finalColor = pow (finalColor.rgb, 1/2.2);
    return finalColor;

    ?
     
  3. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    :) cant wait to try it! but can you tell where exactly should i include those lines in the "lightmaped/diffuse shader"
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Uh, not sure. Basically you want to do pow(2.2) to your diffuse texture whenever you sample it.

    pow(yourDiffuseTexture, 2.2);

    and then once your shader is about to return it's final result you put

    pow(yourFinalResult, 1/2.2);
     
  5. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    the lightmaped shader has thousans of lines if you open it, i am really having diificulty placing the lines :(
    *btw i think i need to swap your lines, ungamma (so 1/2.2) when sampling and gamma (2.2) when write.
     
  6. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
  7. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Hey,

    I worked on the linear lighting implementation for 3.5.

    You will need to do the following:
    1) Ensure you have the editor set to 'linear lighting' when you bake the lightmaps (as we have to linearize the light colors that we pass to beast when baking them).
    2) In the shader whenever you sample a texture or use a color passed in from unity apply the gamma curve (2.2)
    3) Add the 'Final Color' section to your surface shader (See: Custom Fog with Final Color Modifier here - http://unity3d.com/support/documentation/Components/SL-SurfaceShaderExamples.html) and then apply the inverse gamma curve (1.0 / 2.2)
    4) Ensure that you are only degammaing textures on sample that NEED to be degammad (i.e some lookup / greyscale do NOT need to have gamma removed)

    Just a question, if you are making a mobile game are you only using lightmaps or do you also wish to use realtime lighting. You can write some pretty simple shaders if you do not care about surface shaders / realtime lighting, and just want lightmaps / lightprobes.
     
  8. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    Hello stramit,
    1) Regarding texture baking, I am doing this in Maya via Mental Ray and not in Unity. I have a physical light setup combining different light types such as point, spot and area lights with physical attributes attached to them such IES profiles as quadratic falloff (aiming photorealisme). But textures are linearized in Maya before baking the 32 bit . EXR lightmaps, so I am doing the same thing here.
    2) Testing in process.
    3) Testing in process.
    4) I knowing exactly what you mean, I have been dealing with this issue for years in Maya, only RGB color textures or swatches should be gamma corrected (degamma) and not greyscale textures for bump, normal, displacement, reflection or specular maps.(even if they expresses in RGB) , so that is also checked.

    I will post my results as soon as step 2 and 3 are tested.

    Regarding your last question, I guess I will be using a hybrid technique for 2 reasons:
    A-I definitely need my photoreal lightmaps to affect the diffuse attribute of my materials
    B-I should mention that i am simulating architectural materials such as ceramic,leather,glossy wood… So I really need the specular attributes for my material to simulate all these materials.
    And the only way to have specular highlights work in Unity is to have them reflect a bright source of light So I need the dynamic lights to only affect my specular attribute in the material and not the diffuse attribute because we would be lighting the diffuse attribute twice (lightmaps+dynamic lights) witch is incorrect. At the end what I want is simply to have the diffuse channel to be lit by the baked EXR lightmap and the specular channel to be lit by the Unity dynamic light (as if I checked “emit specular only” in the dynamic lights) and of course lights casting secularity will be replicated in to match the lights baked into the lightmap.
     
  9. jasksonlai

    jasksonlai

    Joined:
    Aug 6, 2013
    Posts:
    1
    Just found out this now, will try if how it will work with me. Thanks.