游戏开发Unity渲染场景光照性能优化 ShaderLOD

SubShader {

        LOD 200

        Pass{

            Tags {"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag

            #include "Lighting.cginc"

            fixed4 _Color;

            sampler2D _MainTex;

            float4 _MainTex_ST;

            fixed4 _Specular;

            float _Gloss;

            struct a2v{

                float4 vertex : POSITION;

                float3 normal : NORMAL;

            };

            struct v2f{

                float4 pos:SV_POSITION;

                float3 worldNormal:TEXCOORD0;

                float3 worldPos:TEXCOORD1;

            };

            v2f vert(a2v v){

                v2f o;

                o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

                o.worldNormal = UnityObjectToWorldNormal(v.normal);

                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;

                return o;

            }

            fixed4 frag(v2f i) : SV_Target{

                fixed3 worldNormal = normalize(i.worldNormal);

                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                fixed3 diffuse = _LightColor0.rgb * max(0,dot(worldNormal,worldLightDir));

                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));

                fixed3 halfDir = normalize(worldLightDir + viewDir);

                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(worldNormal,halfDir)),_Gloss);

                return fixed4(ambient + diffuse + specular ,1.0);

            }

            ENDCG

        }

    }

    SubShader {

        Lod 100

        Pass{

            Tags {"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag

            #include "Lighting.cginc"

            fixed4 _Specular;

            float _Gloss;

            fixed4 _Color;;

            struct a2v{

                float4 vertex : POSITION;

                float4 normal : NORMAL;

            };

            struct v2f{

                float4 pos : SV_POSITION;

                fixed3 color : COLOR;

            };

            v2f vert(a2v v){

                v2f o;

                o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                fixed3 worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));

                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

                fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(worldNormal,worldLightDir));

                fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));

                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, v.vertex).xyz);

                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);

                o.color = ambient + diffuse + specular;

                return o;

            }

            fixed4 frag (v2f i) : SV_Target{

                return fixed4(i.color,1.0);

            }

            ENDCG

        }

    }

相关知识

游戏开发Unity渲染场景光照性能优化 ShaderLOD
Unity游戏优化技巧.pdf
Unity 3D性能优化实战指南
游戏图形渲染技巧与性能优化
更新!!!Unity移动端游戏性能优化简谱
Unity2D游戏优化策略详解
常用Unity的优化技巧集锦
优化Unity WebGL的运行性能
移动游戏性能优化通用技法
C#游戏开发初学指南:理论与实践

网址: 游戏开发Unity渲染场景光照性能优化 ShaderLOD http://www.hyxgl.com/newsview355710.html

推荐资讯