1. 程式人生 > >自發光效果(控制自發光的顏色、強度,呼吸光可選)

自發光效果(控制自發光的顏色、強度,呼吸光可選)

通過一個貼圖控制自發光的顏色,一個控制自發光主色調的顏色(注意此處mask直接用rgb格式即可,不需要阿爾法通道控制發光程度,因為顏色自帶一個亮度的屬性)
_EmissionMask (“Emission Mask”, 2D) = “white” {}
_EmissionColor (“Emission Color”, Color) = (1, 1, 1, 1)
_EmissionColorScale (“Emission Color Scale”, Range(0, 0.5)) = 0

sampler2D _EmissionMask;
fixed4 _EmissionColor;
float _EmissionColorScale;

fixed3 emissionMaskColor = lerp(tex2D(_EmissionMask, i.uv).rgb, _EmissionColor.rgb, _EmissionColorScale);

fixed3 emission = lerp(albedo, emissionMaskColor, emissionScale);
fixed3 finalRGB = ambient + diffuse + specular + emission;

return fixed4(finalRGB, finalA);

呼吸光v:增加控制自發光變化的三角函式 包含頻率引數和振幅引數
_EmissionTimeFrequency(“Emission Time Frequency”,Float) =1.0
_EmissionAreaScale (“Emission Area Scale”,Range(0,5)) = 1.0

float _EmissionTimeFrequency;
float _EmissionAreaScale;

struct v2f {
float4 pos : SV_POSITION;
float3 uv : TEXCOORD0;
^}
o.uv.z = (sin(_Time.w * _EmissionTimeFrequency) + 1)_EmissionAreaScale 0.5;
fixed3 emission = lerp(albedo, emissionMaskColor*i.uv.z, emissionScale);

要下拉列表選擇是否用呼吸光,用到列舉
[KeywordEnum(Enable, Disable)] _Breathe (“Enable Breathe”, Float) = 1,#pragma multi_compile _BREATHE_ENABLE _BREATHE_DISABLE,#if _BREATHE_ENABLE
float scale;
#endif,#if _BREATHE_ENABLE
o.scale = (sin(_Time.w * _EmissionBreTimeFrequency) + 1)_EmissionBreAreaScale 0.5;
#endif, #if _BREATHE_ENABLE
float scale = (sin(_Time.w * _EmissionBreTimeFrequency) + 1)_EmissionBreAreaScale 0.5;
o.Emission = emissionMaskColor * _EmissionScale * IN.scale;
#else
o.Emission = emissionMaskColor * _EmissionScale;
#endif
看到部落格也能用shader_feature表示toggle,但在用AssetsBundle打包的時候會出現只有某種情況有定義,另一種情況沒有定義,還是用上面的。用法如下:[Toggle(ENABLE_BREATHE)] _Breather (“Enable Breather”, Float) = 0,
//#pragma shader_feature ENABLE_BREATHE
//#if defined(ENABLE_BREATHE)
// float scale;
//#endif
//#if _BREATHE_ENABLE
// o.Emission = emissionMaskColor * _EmissionScale * IN.scale;
//#elif _BREATHE_DISABLE
// o.Emission = emissionMaskColor * _EmissionScale;
//#endif

用頂點shader會出現一系列問題:
不接收lightmap,不接收霧,場景光為mix的時候、烘焙後還能接收實時光。如果用surface shader寫,這些問題都會迎刃而解.surface shader也分應用到頂點和頂點到畫素,vert函式和frag函式,Input, SurfaceOutput,appdatafull等結構。