Home Unity

Lighting for mobile in Unity (optimization)

polycounter lvl 7
Offline / Send Message
salimmatta polycounter lvl 7
Hey guys,

I'm working on a game as environmental artist, and I'm still pretty new in Unity, I honestly have no idea how to execute good and cheap lighting for mobile games using that engine. Can anyone recommend a tutorial or something that might help me? 

Thank you

Replies

  • Eric Chadwick
    A search is a good place to start. 
    http://polycount.com/search?Search=unity+lighting+mobile
    There's a lot of good info here already. Don't just rely on the search results, click on them to see what's inside. I did and found some good relevant info.

    Also did you read the Unity manual? Lots of good info there about mobile lighting.
  • salimmatta
    Offline / Send Message
    salimmatta polycounter lvl 7
    Hey again!

    No I didnt yet, but thanks man to everything really, life savoir!
  • RyanB
    Use forward rendering + single pass shaders that make use of forward rendering.  This will allow each object to use a single directional light + any number of vertex lights but usually limited to four in the shader.

    You can add the vertex lighting calculation into the vert section of your shader.  UnityCG.cginc has two different versions you can use.

    Example calculating four vertex lights  (can't give you a complete shader unfortunately):

                VertexOutput vert (VertexInput v) {
                    ...
                    // from UnityCG.inc
                    #ifdef VERTEXLIGHT_ON
                     // this is the point light version, all lights converted to points
                     // The other version of the vertex lighting calculation (ShadeVertexLightsFull) from UnityCG.cginc lets you use spotlights   
                    o.vertexLighting.rgb = Shade4PointLights(                                  
                        unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
                        unity_LightColor[0].rgb, unity_LightColor[1].rgb,
                        unity_LightColor[2].rgb, unity_LightColor[3].rgb,
                        unity_4LightAtten0,  mul(_Object2World, v.vertex), o.normalDir
                        );
                    #endif
                  ...
                }

    // then in the frag section, use the vertex lighting with lightmaps
                float4 frag(VertexOutput i) : COLOR {
                    ...
                    fixed4 finalRGBA = fixed4((colorNoLighting * (i.vertexLighting.rgb + lightmapMix.rgb*lightmapMix.rgb*12) * attenuation ),1);
                  ..
                    return finalRGBA;
                }

    // OR then in the frag section, use the vertex lighting with a single directional light (_LightColor0.rgb)

                    fixed3 finalColor = emissive + ((_LightColor0.rgb*attenuation*_Diffuse_var.rgb*...*_Light_Multiplier)+((_Diffuse_var.rgb*i.vertexLighting.rgb)*_Light_Multiplier)+(...*_Specular_var.rgb));
                    ...
                    return finalRGBA;

    This might not make sense now, but the key things are:
    1)  Use forward rendering
    2)  Set up your shaders to use forward rendering
    3)  Limit your shaders to a single pass
    4)  Put your vertex lighting calculation, which you can steal from UnityCG.cginc, in your vert section and pass that to the frag section
    5)  In the frag section, you can use just the vertex lighting passed from the vert section or mix it with lightmaps and/or a single real-time directional light
    6)  In Edit -> Project Settings -> Quality, go to the Rendering section and set the pixel light count to 1.  This will prevent multiple pixel lights from being used in random shaders you haven't optimized.

    Doing these things will give you some nice lighting that is relatively cheap to calculate.




Sign In or Register to comment.