8.Mobile Shader Adjustment
-
变量类型优化——使用更少的内存
- float 32位;half 16位;fixed 11位
- 在复杂的三角函数和指数函数中,float是必要的
- half只有三位的精度
- fixed可以用于照明计算、颜色或纹理
-
在法线贴图和漫反射纹理上共享同一个UV数据(在Input结构中就可以删除一个UV的变量)
-
在#pragma语句中声明noforwardadd
物体每个像素的光照只能从单个方向光中获取,其他光线被强制按照逐顶点的光照处理(这个值来自Unity内部的球面谐波值spherical harmonic);如果场景中有很多灯光,那么该物体应该将哪个光源作为主光源?在Unity的Light设置下,RenderMode设置为Important,Unity就会让这个灯光作为更多的主光源
-
在#pragma语句中声明excluded_pass:prepass
声明着色器不会接受任何来自延迟渲染器(deferred renderer)的自定义照明,即只能在前向渲染器中使用这个着色器
-
Profile的使用——主要是看GPU块中的详细信息,在此略过
-
适合移动端使用的着色器 优化点
- 声明:#pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap noforwardadd halfasview
- nolightmap:防止着色器为物体寻找lightmap
- halfasview:用半程向量作为视角方向而不是viewDir
- 删除#pragma target 3.0,因为没有用上
- 其余地方的优化同上
9.Screen Effects with Unity RenderTexture
-
Unity 函数
- OnRenderTexture(RenderTexture sourceTexture, RenderTexture destTexture)
- Graphics.Blit(Texture source, RenderTexture dest, Material mat)
-
屏幕灰度调整
fixed4 renderTex = tex2D(_MainTex, i.uv); loat luminosity = 0.299 * renderTex.r + 0.587 * renderTex.g + 0.114 * renderTex.b; fixed4 finalColor = lerp(renderTex, luminosity, **_Luminosity**); renderTex.rgb = finalColor; return renderTex;
-
取屏幕深度
UnityCG.cginc 中的变量:sampler2D _CameraDepthTexture:来自相机的深度信息
float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv.xy)); 从深度图获取深度信息
-
亮度、饱和度和对比度 frag():
//Operation for brightness float3 LuminanceCoeff = float3(0.2125, 0.7154, 0.0721); float3 brtColor = color * brt; float intensityf = dot(brtColor, LuminanceCoeff); float3 intensity = float3(intensityf, intensityf, intensityf); //Operation for Saturation float3 satColor = lerp(intensity, brtColor, sat); //Operation for Contrast float AvgLumR = 0.5; float AvgLumG = 0.5; float AvgLumB = 0.5; float3 AvgLumin = float3(AvgLumR, AvgLumG, AvgLumB); float3 conColor = lerp(AvgLumin, satColor, con); return conColor;
-
Photo混合模式(指图层的混合模式)frag():
//Perform a multiply Blend mode fixed4 blendedMultiply = renderTex * blendTex;//三选一 //Perform an additive Blend mode fixed4 blendedAdd = renderTex + blendTex;//三选一 //Perform screen blending mode fixed4 blendedScreen = (1.0 - ((1.0 - renderTex) * (1.0 - blendTex)));//三选一 return lerp(renderTex, blendedScreen, _Opacity);
-
使用if语句,如:
if xx blended= renderTex * blendTex;
else blended= renderTex + blendTex;
文章评论