Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2.  
  3. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  4.  
  5. Shader "Custom/LitSprite"
  6. {
  7. Properties
  8. {
  9. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  10. _LightStrength ("Light Strength", Float) = 1
  11. _Color ("Tint", Color) = (1,1,1,1)
  12. _LightPos ("LightPos", Vector) = (0,0,0,0)
  13. [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
  14. [MaterialToggle] _Pixelated ("Pixelated", Float) = 1
  15.  
  16.  
  17. }
  18.  
  19. SubShader
  20. {
  21. Tags
  22. {
  23. "Queue"="Transparent"
  24. "IgnoreProjector"="True"
  25. "RenderType"="Transparent"
  26. "PreviewType"="Plane"
  27. "CanUseSpriteAtlas"="True"
  28. }
  29.  
  30. Cull Off
  31. Lighting Off
  32. ZWrite Off
  33. Blend One OneMinusSrcAlpha
  34.  
  35. Pass
  36. {
  37. CGPROGRAM
  38. #pragma vertex vert
  39. #pragma fragment frag
  40. #pragma target 2.0
  41. #pragma multi_compile _ PIXELSNAP_ON
  42. #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
  43.  
  44. #include "UnityCG.cginc"
  45.  
  46. struct appdata_t
  47. {
  48. float4 vertex : POSITION;
  49. float4 color : COLOR;
  50. float2 texcoord : TEXCOORD0;
  51. float2 uv : TEXCOORD1;
  52. };
  53.  
  54. struct v2f
  55. {
  56. float4 vertex : SV_POSITION;
  57. fixed4 color : COLOR;
  58. float2 texcoord : TEXCOORD0;
  59. float4 worldSpacePosition : float4;
  60. float2 uv : TEXCOORD1;
  61. };
  62.  
  63. fixed4 _Color;
  64.  
  65. v2f vert(appdata_t IN)
  66. {
  67. v2f OUT;
  68.  
  69. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  70. OUT.texcoord = IN.texcoord;
  71. OUT.color = IN.color * _Color;
  72.  
  73. #ifdef PIXELSNAP_ON
  74. OUT.vertex = UnityPixelSnap (OUT.vertex);
  75. #endif
  76.  
  77. OUT.worldSpacePosition = mul(unity_ObjectToWorld, OUT.vertex);
  78. OUT.uv = IN.uv;
  79.  
  80. return OUT;
  81. }
  82.  
  83. sampler2D _MainTex;
  84. sampler2D _AlphaTex;
  85. float _LightStrength;
  86. Vector _LightPos;
  87. float _Pixelated;
  88.  
  89. fixed4 SampleSpriteTexture (v2f IN)
  90. {
  91. float2 uv = IN.texcoord;
  92. float4 worldPos= IN.worldSpacePosition;
  93.  
  94. //x and y are light position.
  95. float aS = worldPos.x-_LightPos.x;
  96. float bS = worldPos.y-_LightPos.y;
  97.  
  98. if(_Pixelated == 1) {
  99. float pixelWidth = 1.0f / _LightPos.z;
  100. float pixelHeight = 1.0f / _LightPos.w;
  101. }
  102.  
  103.  
  104. fixed4 color = tex2D (_MainTex, uv);
  105.  
  106. #if ETC1_EXTERNAL_ALPHA
  107. // get the color from an external texture (usecase: Alpha support for ETC1 on android)
  108. color.a = tex2D (_AlphaTex, uv).r;
  109. #endif //ETC1_EXTERNAL_ALPHA
  110.  
  111. float dist = sqrt((aS * aS) + (bS * bS));
  112.  
  113. //light strength.
  114. float percentClose = 1-(dist/_LightStrength);
  115.  
  116. if(color.r + color.g + color.b > 0) {
  117. color.r = lerp(0, color.r, percentClose);
  118. color.g = lerp(0, color.g, percentClose);
  119. color.b = lerp(0, color.b, percentClose);
  120. }
  121.  
  122.  
  123. return color;
  124. }
  125.  
  126. fixed4 frag(v2f IN) : SV_Target
  127. {
  128. fixed4 c = SampleSpriteTexture (IN) * IN.color;
  129. c.rgb *= c.a;
  130. return c;
  131. }
  132. ENDCG
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement