22.02.20 《Unity 2018 Shaders and Effects Cookbook》笔记Chapter8-9

8.Mobile Shader Adjustment

  1. 变量类型优化——使用更少的内存

    1. float 32位;half 16位;fixed 11位
    2. 在复杂的三角函数和指数函数中,float是必要的
    3. half只有三位的精度
    4. fixed可以用于照明计算、颜色或纹理
  2. 在法线贴图和漫反射纹理上共享同一个UV数据(在Input结构中就可以删除一个UV的变量)

  3. 在#pragma语句中声明noforwardadd

    物体每个像素的光照只能从单个方向光中获取,其他光线被强制按照逐顶点的光照处理(这个值来自Unity内部的球面谐波值spherical harmonic);如果场景中有很多灯光,那么该物体应该将哪个光源作为主光源?在Unity的Light设置下,RenderMode设置为Important,Unity就会让这个灯光作为更多的主光源

  4. 在#pragma语句中声明excluded_pass:prepass

    声明着色器不会接受任何来自延迟渲染器(deferred renderer)的自定义照明,即只能在前向渲染器中使用这个着色器

  5. Profile的使用——主要是看GPU块中的详细信息,在此略过

  6. 适合移动端使用的着色器 优化点

    1. 声明:#pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap noforwardadd halfasview
    2. nolightmap:防止着色器为物体寻找lightmap
    3. halfasview:用半程向量作为视角方向而不是viewDir
    4. 删除#pragma target 3.0,因为没有用上
    5. 其余地方的优化同上

9.Screen Effects with Unity RenderTexture

  1. Unity 函数

    1. OnRenderTexture(RenderTexture sourceTexture, RenderTexture destTexture)
    2. Graphics.Blit(Texture source, RenderTexture dest, Material mat)
  2. 屏幕灰度调整

                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;
    
  3. 取屏幕深度

    UnityCG.cginc 中的变量:sampler2D _CameraDepthTexture:来自相机的深度信息

    float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv.xy)); 从深度图获取深度信息

  4. 亮度、饱和度和对比度 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;
    
  5. 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);
    
  6. 使用if语句,如:

    if xx blended= renderTex * blendTex;

    else blended= renderTex + blendTex;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值