S.A.Parkhid
S.A.Parkhid

Reputation: 2868

unity3d shaders level of detail

Maybe questions seems strange , but , please take a look at the summarized shader code listed below :

Shader "AngryBots/PlanarRealtimeReflection" {
    Properties {
        ...
    }

    SubShader {
        LOD 400

        Tags { "RenderType"="Opaque" }
        Fog { Mode Off }

        Pass {
            ... 


        }       
    }

    SubShader {
        LOD 200

        Tags { "RenderType"="Opaque" }
        Fog { Mode Off }

        Pass {


        ....

        }       
    } 
}

Every sub shader has a different LOD , I know the much bigger LOD has more details and wants more GPU Processing , So How should we number the LOD of our shaders ? how the device GPU understands the numbers !

So how Can I Find that my shader LOD is 500 or 700 or 300?

I had read the unity3d manual which tells me for example the BumpedMapDiffuse Shader For PC's are 500 ! But how can I convert it for mobile devices ?

Upvotes: 0

Views: 5636

Answers (2)

Jerdak
Jerdak

Reputation: 4056

Unity's ShaderLab level of detail (LOD)* is a convenience feature for developers that helps them support multiple platforms in a single shader. The basic idea is that an application might have a SweetLightingShader that only works on PCs. Suppose the iPad doesn't support SweetLightingShader but it does support SemiSweetLightingShader. You could maintain two separate shaders or, even better, merge them in to a single shader and use LOD to decide what functionality to use.

*Do not confuse ShaderLab LOD with geometric LOD, they are independent topics.

Here is an example shader with 2 levels of detail:

Shader "Custom/ExampleLighting" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader { // SweetLighting
        LOD 200
        Pass { Color (1,0,0,0) }
    }
    SubShader { // SemiSweetLighting
        LOD 100
        Pass { Color (0,1,0,0) }
    }
    FallBack "Diffuse"
}

The LOD can be set like so:

public class SwitchLod : MonoBehaviour {
    public Shader myShader;

    void Start() {
        myShader.maximumLOD = 100; //Use SemiSweetLighting -or-
        myShader.maximumLOD = 200; //Use SweetLighting
    }
}

The first subshader to meet the LOD requirements is the one used. From the docs:

When Unity renders a shader, it will go over all subshaders and use the first one that the hardware supports.

Note that the documentation for Shader.globalMaximumLOD states that it is the Shader LOD level for all shaders. This does not appear to be directly true, that's why my example sets the LOD for the shader directly. The AngryBots demo that comes with Unity uses the following snippet to apply the global maximum LOD to all shaders:

Shader.globalMaximumLOD = quality;
for (var s : Shader in shaders.shaders) {
    s.maximumLOD = quality;    
}

Upvotes: 3

game development germ
game development germ

Reputation: 1334

Shader LOD is extremely useful when you are need to support a range of GPUs with different performance. For example, if you want your game to run smoothly on both iPhone4 and iPhone5S.

Usual practice is to compose a shader as a set of several sub-shaders (consider them as a versions of the same shader) with different LODs. You may implement complex per-pixel lighting in sub-shaders with bigger LODs and rough per-vertex lighting in those with smaller LODs. Then you may enable appropriate versions of shaders in your code using Shader.globalMaximumLOD

Upvotes: 1

Related Questions