Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. Shader "DecalMRT" {
  2. Properties {
  3. [Toggle] _ZWrite ("ZWrite", Range(0,1)) = 0
  4. [Space]
  5. _MainTex ("Texture", 2D) = "gray" {}
  6. [Space]
  7. _BumpScale(" Bump Scale", Range (0, 3)) = 1
  8. [Normal] _BumpTex ("Normal", 2D) = "gray" {}
  9. [Space]
  10. _Color ("Color", Color) = (1,1,1,1)
  11. _Contrast ("Contrast", Range (0, 10)) = 1
  12. _Albedo ("Albedo", Range (0, 1)) = 1
  13. _SpecularClr ("Specular Color", Range (0, 1)) = 1
  14. _SpecularAlp ("Specular Alpha", Range (0, 1)) = 1
  15. _BrightToAlpha ("BrightToAlpha", Range (0, 1)) = 0
  16. [Space]
  17. _Overlay ("Influence", Range (-2, 2)) = .2
  18. _OverlayTex ("Overlay", 2D) = "gray" {}
  19.  
  20. [Space]
  21. [Toggle(MASK)] _Mask ("Use mask", Float) = 0
  22. [Enum(Alpha,0,Brightness,1)] _MaskType ("Type", Float) = 0
  23. _MaskTex ("Texture", 2D) = "gray" {}
  24. _MaskSoft ("Smoothness", Range (0, 1)) = .05
  25. [Space]
  26. _MaskOffset ("Offset", Vector) = (0,0,0,0)
  27. }
  28.  
  29. Subshader {
  30.  
  31. Pass {
  32. Fog { Mode Off }
  33. ZWrite Off
  34.  
  35. CGPROGRAM
  36.  
  37. #pragma target 3.0
  38.  
  39. #pragma vertex vert
  40. #pragma fragment frag
  41.  
  42. #pragma exclude_renderers d3d11
  43. #pragma shader_feature __ MASK
  44. #include "UnityCG.cginc"
  45. #include "UnityStandardUtils.cginc"
  46.  
  47. struct v2f
  48. {
  49. float2 uv : TEXCOORD0; // texture coordinate
  50. float4 vertex : SV_POSITION; // clip space position
  51. float2 worldPosition: TEXCOORD1;
  52. float4 color : COLOR;
  53.  
  54. half3 orTang : TEXCOORD3;
  55. half3 orBinorm : TEXCOORD4;
  56. half3 orNorm : TEXCOORD5;
  57. };
  58.  
  59. struct appdata
  60. {
  61. float4 vertex : POSITION; // vertex position
  62. float2 uv : TEXCOORD0; // texture coordinate
  63. float4 color : COLOR;
  64. // float3 normal : NORMAL;
  65. // float4 tangent : TANGENT;
  66. };
  67.  
  68. uniform half _Brightness, _Contrast, _MaskSoft, _MaskType, _Albedo, _SpecularClr, _SpecularAlp, _Overlay, _BrightToAlpha, _BumpScale;
  69. uniform half4 _Color, _MaskOffset;
  70.  
  71. // vertex shader
  72. v2f vert (appdata v)
  73. {
  74. v2f o;
  75. o.vertex = UnityObjectToClipPos(v.vertex);
  76. // o.vertex.z -= .001;
  77.  
  78. o.worldPosition =
  79. // mul(UNITY_MATRIX_IT_MV, v.vertex.xy).xy
  80. mul(unity_ObjectToWorld, float4(v.vertex.xyz,1)).xy
  81. // mul(unity_ObjectToWorld, v.vertex.xyz).xy
  82. - _MaskOffset;
  83.  
  84. o.orNorm = normalize(UnityObjectToWorldDir(half3(0,0,-1)));
  85. o.orTang = float4(UnityObjectToWorldDir(half3(1,0,0)), -1);
  86. o.orBinorm = cross(o.orNorm, o.orTang) * -unity_WorldTransformParams.w;
  87.  
  88. o.uv = v.uv;
  89. o.color = half4(pow(v.color.rgb,_Contrast),v.color.a);
  90. return o;
  91. }
  92.  
  93. sampler2D _MainTex,_OverlayTex,_BumpTex;
  94.  
  95. sampler2D _MaskTex;
  96. half4 _MaskTex_ST, _OverlayTex_ST;
  97.  
  98. float calculateMask(half m, half ia){
  99. half _MaskThrs = frac((ia-.001)*2)+.001;
  100. half mm = _MaskThrs + _MaskThrs*_MaskSoft;
  101. return smoothstep(mm, mm-_MaskSoft, m);
  102. }
  103.  
  104. fixed4 fragAlbedo(v2f i, fixed2 txr, half a, half b, half overlay)
  105. {
  106. half3 col = b * max( lerp(txr.x, 1-txr.x, step(i.color.a*_Color.a,.5)), i.color.rgb*_Color.rgb) ;
  107. half4 fin = half4( col*_Albedo, a);
  108.  
  109. return fin * overlay;
  110. }
  111.  
  112. fixed4 fragSpecular(v2f i, fixed2 txr, half a, half b, half overlay)
  113. {
  114. half3 col = b * max( lerp(txr.x, 1-txr.x, step(i.color.a*_Color.a,.5)), i.color.rgb*_Color.rgb) ;
  115. half4 fin = half4( col*_SpecularClr, a*_SpecularAlp);
  116.  
  117. return fin * overlay;
  118. }
  119.  
  120. void frag(v2f i, out half4 outDiffuse : COLOR0, out half4 outSpecular : COLOR1, out half4 outNormal : COLOR2)
  121. {
  122. fixed2 txr = tex2D(_MainTex, i.uv).ra;
  123.  
  124. half a = lerp(txr.y, txr.x, _BrightToAlpha);
  125. half b = 1;
  126.  
  127. #if MASK
  128. half m = calculateMask(tex2D(_MaskTex, i.worldPosition.xy/_MaskTex_ST.xy + _MaskTex_ST.zw).r, i.color.a);
  129. a *= lerp(m, 1, _MaskType);
  130. b = max(m, 1-_MaskType);
  131. #endif
  132.  
  133. clip(a-.01);
  134.  
  135. half overlay = tex2D(_OverlayTex, i.worldPosition.xy/_OverlayTex_ST.xy + _OverlayTex_ST.zw).r;
  136. overlay = 1 + (overlay-.5)*_Overlay;
  137.  
  138. outDiffuse = _Color; //fragAlbedo(i, txr, a, b, overlay);
  139. outSpecular = _SpecularClr;//fragSpecular(i, txr, a, b, overlay);
  140. outSpecular.a = _SpecularAlp;
  141.  
  142. half3 normalTangent = UnpackScaleNormal(tex2D (_BumpTex, i.uv), _BumpScale);
  143. half3 normalWorld = i.orTang*normalTangent.x + i.orBinorm*normalTangent.y + i.orNorm*normalTangent.z;
  144.  
  145. outNormal = half4(normalWorld*.5+.5, 1);
  146.  
  147. }
  148.  
  149. ENDCG
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement