对于游戏,始于好玩,陷于好玩,忠于好玩。
建议看一下shaderlab blending 官方文档,熟悉一下渲染流程及blend的用法,参数等。https://docs.unity3d.com/Manual/SL-Blend.html
整理了几个关于剔除,深度测试,alpha测试的示例:
一.可调透明度
Shader "WC/可调透明度" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Cutoff ("透明度",Range(0,1)) = 0.01 _Color ("主颜色", Color) = (1,1,1,0) _SpecColor ("高光颜色", Color) = (1,1,1,1) _Emission ("光泽颜色", Color) = (0,0,0,0) _Shininess ("光泽度", Range (0.01, 1)) = 0.7 } SubShader { Pass { AlphaTest Greater [_Cutoff] //透明度测试,只渲染alpha大于_CutOff的像素 Material { Diffuse [_Color] Ambient [_Color] Shininess [_Shininess] Specular [_SpecColor] Emission [_Emission] } Lighting On SetTexture[_MainTex] { combine primary * texture } } } } 效果:
一.剔除正面
Shader "WC/剔除正面" { Properties { _MainTex ("基础纹理 (RGB)-透明度(A)", 2D) = "white" {} } SubShader { Pass { Material //设置顶点光照 { Emission(0.3,0.3,0.3,0.3) Diffuse(1,1,1,1) } Lighting On //开灯 Cull Front //剔除正面(不绘制面向观察者的几何面) SetTexture[_MainTex] { combine primary * texture } } } }
剔除效果如下:
三.剔除正面加强版
Shader "WC/剔除正面加强版" { Properties { _Color ("主颜色", Color) = (1,1,1,0) _SpecColor ("高光颜色", Color) = (1,1,1,1) _Emission ("光泽颜色", Color) = (0,0,0,0) _Shininess ("光泽度", Range (0.01, 1)) = 0.7 _MainTex ("基础纹理 (RGB)-透明度(A)", 2D) = "white" {} } //--------------------------------【子着色器】-------------------------------- SubShader { //---------------------------【通道一】------------------------------ // 说明:绘制对象的前面部分,使用简单的白色材质,并应用主纹理 //---------------------------------------------------------------------- Pass { //【1】设置顶点光照 Material { Diffuse [_Color] Ambient [_Color] Shininess [_Shininess] Specular [_SpecColor] Emission [_Emission] } //【2】开启光照 Lighting On // 【3】将顶点颜色混合上纹理 SetTexture [_MainTex] { Combine Primary * Texture } } //--------------------------【通道二】------------------------------- // 说明:采用海水颜色来渲染背面,并应用主纹理 //---------------------------------------------------------------------- Pass { Color (0,1,1,1) Cull Front //剔除前面绘制的 SetTexture [_MainTex] //绘制后面的纹理 { Combine Primary * Texture } } } }
效果:
四.简单植被
Shader "WC/简单植被" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Color("主颜色",Color) = (.5,.5,.5,.5) _CutOff("Alpha透明度",Range(0,1)) = 0.5 } SubShader { Material { Diffuse[_Color] Ambient[_Color] } Lighting On Cull Off Pass { AlphaTest Greater[_CutOff] SetTexture[_MainTex] { combine texture * primary,texture } } Pass { ZWrite Off //渲染纯色物体 ZWrite On 渲染半透明 ZWrite Off,默认 ZWrite On ZTest Less //只渲染小于AlphaValue值的像素,默认LEqual ps:对于熟悉ZWrite和ZTest,建议看一遍这篇文章http://blog.csdn.net/lyh916/article/details/45317571 AlphaTest LEqual[_CutOff] //透明度测试,这个比较好理解,只渲染alpha小于等于_CutOff的像素 Blend SrcAlpha OneMinusSrcAlpha //Blend的参数 http://blog.csdn.net/wdsdsdsds/article/details/52535352 SetTexture[_MainTex] { combine texture * primary,texture } } } }
效果:
完。
