源战役客户端
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

85 líneas
3.2 KiB

  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Unlit/DissolveEffect"
  3. {
  4. Properties{
  5. _Diffuse("Diffuse", Color) = (1,1,1,1)
  6. _DissolveColor("Dissolve Color", Color) = (0,0,0,0)
  7. _DissolveEdgeColor("Dissolve Edge Color", Color) = (1,1,1,1)
  8. _MainTex("Texture", 2D) = "white" {}
  9. _DissolveMap("DissolveMap", 2D) = "white"{}
  10. _DissolveThreshold("DissolveThreshold", Range(0,1)) = 0
  11. _ColorFactor("ColorFactor", Range(0,1)) = 0.7
  12. _DissolveEdge("DissolveEdge", Range(0,1)) = 0.8
  13. }
  14. CGINCLUDE
  15. #include "Lighting.cginc"
  16. uniform fixed4 _Diffuse;
  17. uniform fixed4 _DissolveColor;
  18. uniform fixed4 _DissolveEdgeColor;
  19. uniform sampler2D _MainTex;
  20. uniform float4 _MainTex_ST;
  21. uniform sampler2D _DissolveMap;
  22. uniform fixed _DissolveThreshold;
  23. uniform fixed _ColorFactor;
  24. uniform fixed _DissolveEdge;
  25. struct v2f
  26. {
  27. half4 pos : SV_POSITION;
  28. // half3 worldNormal : TEXCOORD0;
  29. half2 uv : TEXCOORD1;
  30. };
  31. v2f vert(appdata_base v)
  32. {
  33. v2f o;
  34. o.pos = UnityObjectToClipPos(v.vertex);
  35. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  36. //o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
  37. return o;
  38. }
  39. fixed4 frag(v2f i) : SV_Target
  40. {
  41. //采样Dissolve Map
  42. fixed4 dissolveValue = tex2D(_DissolveMap, i.uv);
  43. //小于阈值的部分直接discard
  44. if (dissolveValue.r < _DissolveThreshold)
  45. {
  46. discard;
  47. }
  48. //Diffuse + Ambient光照计算
  49. //fixed3 worldNormal = normalize(i.worldNormal);
  50. //fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
  51. //fixed3 lambert = saturate(dot(worldNormal, worldLightDir));
  52. // fixed3 albedo = lambert * _Diffuse.xyz * _LightColor0.xyz + UNITY_LIGHTMODEL_AMBIENT.xyz;
  53. fixed3 color = tex2D(_MainTex, i.uv).rgb; //* albedo;
  54. //优化版本,尽量不在shader中用分支判断的版本,但是代码很难理解啊....
  55. fixed percentage = _DissolveThreshold / dissolveValue.r;
  56. //如果当前百分比 - 颜色权重 - 边缘颜色
  57. fixed lerpEdge = sign(percentage - _ColorFactor - _DissolveEdge);
  58. //貌似sign返回的值还得saturate一下,否则是一个很奇怪的值
  59. fixed3 edgeColor = lerp(_DissolveEdgeColor.rgb, _DissolveColor.rgb, saturate(lerpEdge));
  60. //最终输出颜色的lerp值
  61. fixed lerpOut = sign(percentage - _ColorFactor);
  62. //最终颜色在原颜色和上一步计算的颜色之间差值(其实经过saturate(sign(..))的lerpOut应该只能是0或1)
  63. fixed3 colorOut = lerp(color, edgeColor, saturate(lerpOut));
  64. return fixed4(colorOut, 1);
  65. }
  66. ENDCG
  67. SubShader
  68. {
  69. Tags{ "RenderType" = "Opaque" }
  70. Pass
  71. {
  72. CGPROGRAM
  73. #pragma vertex vert
  74. #pragma fragment frag
  75. ENDCG
  76. }
  77. }
  78. //FallBack "Diffuse"
  79. }