SpriteDepthNormalsTexture.shader 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. Shader "Hidden/Internal-SpriteDepthNormalsTexture" {
  2. // Use this shader to render a DepthNormals texture for a camera with correct sprite normals (using camera.RenderWithShader)
  3. Properties {
  4. _MainTex ("", 2D) = "white" {}
  5. _Cutoff ("", Float) = 0.5
  6. _Color ("", Color) = (1,1,1,1)
  7. }
  8. SubShader {
  9. Tags { "RenderType"="Sprite" }
  10. Pass {
  11. Cull Off
  12. CGPROGRAM
  13. #pragma target 3.0
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #include "UnityCG.cginc"
  17. struct v2f {
  18. float4 pos : SV_POSITION;
  19. float2 uv : TEXCOORD0;
  20. float4 nz : TEXCOORD1;
  21. UNITY_VERTEX_OUTPUT_STEREO
  22. };
  23. uniform float4 _MainTex_ST;
  24. uniform float4 _FixedNormal;
  25. v2f vert( appdata_base v ) {
  26. v2f o;
  27. UNITY_SETUP_INSTANCE_ID(v);
  28. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  29. o.pos = UnityObjectToClipPos(v.vertex);
  30. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  31. o.nz.xyz = _FixedNormal.xyz;
  32. #if UNITY_REVERSED_Z
  33. o.nz.z = -o.nz.z;
  34. #endif
  35. o.nz.w = COMPUTE_DEPTH_01;
  36. return o;
  37. }
  38. uniform sampler2D _MainTex;
  39. uniform fixed _Cutoff;
  40. uniform fixed4 _Color;
  41. fixed4 frag(v2f i) : SV_Target {
  42. fixed4 texcol = tex2D( _MainTex, i.uv );
  43. clip( texcol.a*_Color.a - _Cutoff );
  44. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  45. }
  46. ENDCG
  47. }
  48. }
  49. SubShader {
  50. Tags { "RenderType"="Opaque" }
  51. Pass {
  52. CGPROGRAM
  53. #pragma target 3.0
  54. #pragma vertex vert
  55. #pragma fragment frag
  56. #include "UnityCG.cginc"
  57. struct v2f {
  58. float4 pos : SV_POSITION;
  59. float4 nz : TEXCOORD0;
  60. UNITY_VERTEX_OUTPUT_STEREO
  61. };
  62. v2f vert( appdata_base v ) {
  63. v2f o;
  64. UNITY_SETUP_INSTANCE_ID(v);
  65. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  66. o.pos = UnityObjectToClipPos(v.vertex);
  67. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  68. o.nz.w = COMPUTE_DEPTH_01;
  69. return o;
  70. }
  71. fixed4 frag(v2f i) : SV_Target {
  72. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  73. }
  74. ENDCG
  75. }
  76. }
  77. SubShader {
  78. Tags { "RenderType"="TransparentCutout" }
  79. Pass {
  80. CGPROGRAM
  81. #pragma target 3.0
  82. #pragma vertex vert
  83. #pragma fragment frag
  84. #include "UnityCG.cginc"
  85. struct v2f {
  86. float4 pos : SV_POSITION;
  87. float2 uv : TEXCOORD0;
  88. float4 nz : TEXCOORD1;
  89. UNITY_VERTEX_OUTPUT_STEREO
  90. };
  91. uniform float4 _MainTex_ST;
  92. v2f vert( appdata_base v ) {
  93. v2f o;
  94. UNITY_SETUP_INSTANCE_ID(v);
  95. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  96. o.pos = UnityObjectToClipPos(v.vertex);
  97. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  98. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  99. o.nz.w = COMPUTE_DEPTH_01;
  100. return o;
  101. }
  102. uniform sampler2D _MainTex;
  103. uniform fixed _Cutoff;
  104. uniform fixed4 _Color;
  105. fixed4 frag(v2f i) : SV_Target {
  106. fixed4 texcol = tex2D( _MainTex, i.uv );
  107. clip( texcol.a*_Color.a - _Cutoff );
  108. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  109. }
  110. ENDCG
  111. }
  112. }
  113. SubShader {
  114. Tags { "RenderType"="TreeBark" }
  115. Pass {
  116. CGPROGRAM
  117. #pragma target 3.0
  118. #pragma vertex vert
  119. #pragma fragment frag
  120. #include "UnityCG.cginc"
  121. #include "Lighting.cginc"
  122. #include "UnityBuiltin3xTreeLibrary.cginc"
  123. struct v2f {
  124. float4 pos : SV_POSITION;
  125. float2 uv : TEXCOORD0;
  126. float4 nz : TEXCOORD1;
  127. UNITY_VERTEX_OUTPUT_STEREO
  128. };
  129. v2f vert( appdata_full v ) {
  130. v2f o;
  131. UNITY_SETUP_INSTANCE_ID(v);
  132. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  133. TreeVertBark(v);
  134. o.pos = UnityObjectToClipPos(v.vertex);
  135. o.uv = v.texcoord.xy;
  136. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  137. o.nz.w = COMPUTE_DEPTH_01;
  138. return o;
  139. }
  140. fixed4 frag( v2f i ) : SV_Target {
  141. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  142. }
  143. ENDCG
  144. }
  145. }
  146. SubShader {
  147. Tags { "RenderType"="TreeLeaf" }
  148. Pass {
  149. CGPROGRAM
  150. #pragma target 3.0
  151. #pragma vertex vert
  152. #pragma fragment frag
  153. #include "UnityCG.cginc"
  154. #include "Lighting.cginc"
  155. #include "UnityBuiltin3xTreeLibrary.cginc"
  156. struct v2f {
  157. float4 pos : SV_POSITION;
  158. float2 uv : TEXCOORD0;
  159. float4 nz : TEXCOORD1;
  160. UNITY_VERTEX_OUTPUT_STEREO
  161. };
  162. v2f vert( appdata_full v ) {
  163. v2f o;
  164. UNITY_SETUP_INSTANCE_ID(v);
  165. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  166. TreeVertLeaf(v);
  167. o.pos = UnityObjectToClipPos(v.vertex);
  168. o.uv = v.texcoord.xy;
  169. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  170. o.nz.w = COMPUTE_DEPTH_01;
  171. return o;
  172. }
  173. uniform sampler2D _MainTex;
  174. uniform fixed _Cutoff;
  175. fixed4 frag( v2f i ) : SV_Target {
  176. half alpha = tex2D(_MainTex, i.uv).a;
  177. clip (alpha - _Cutoff);
  178. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  179. }
  180. ENDCG
  181. }
  182. }
  183. SubShader {
  184. Tags { "RenderType"="TreeOpaque" "DisableBatching"="True" }
  185. Pass {
  186. CGPROGRAM
  187. #pragma target 3.0
  188. #pragma vertex vert
  189. #pragma fragment frag
  190. #include "UnityCG.cginc"
  191. #include "TerrainEngine.cginc"
  192. struct v2f {
  193. float4 pos : SV_POSITION;
  194. float4 nz : TEXCOORD0;
  195. UNITY_VERTEX_OUTPUT_STEREO
  196. };
  197. struct appdata {
  198. float4 vertex : POSITION;
  199. float3 normal : NORMAL;
  200. fixed4 color : COLOR;
  201. UNITY_VERTEX_INPUT_INSTANCE_ID
  202. };
  203. v2f vert( appdata v ) {
  204. v2f o;
  205. UNITY_SETUP_INSTANCE_ID(v);
  206. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  207. TerrainAnimateTree(v.vertex, v.color.w);
  208. o.pos = UnityObjectToClipPos(v.vertex);
  209. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  210. o.nz.w = COMPUTE_DEPTH_01;
  211. return o;
  212. }
  213. fixed4 frag(v2f i) : SV_Target {
  214. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  215. }
  216. ENDCG
  217. }
  218. }
  219. SubShader {
  220. Tags { "RenderType"="TreeTransparentCutout" "DisableBatching"="True" }
  221. Pass {
  222. Cull Back
  223. CGPROGRAM
  224. #pragma target 3.0
  225. #pragma vertex vert
  226. #pragma fragment frag
  227. #include "UnityCG.cginc"
  228. #include "TerrainEngine.cginc"
  229. struct v2f {
  230. float4 pos : SV_POSITION;
  231. float2 uv : TEXCOORD0;
  232. float4 nz : TEXCOORD1;
  233. UNITY_VERTEX_OUTPUT_STEREO
  234. };
  235. struct appdata {
  236. float4 vertex : POSITION;
  237. float3 normal : NORMAL;
  238. fixed4 color : COLOR;
  239. float4 texcoord : TEXCOORD0;
  240. UNITY_VERTEX_INPUT_INSTANCE_ID
  241. };
  242. v2f vert( appdata v ) {
  243. v2f o;
  244. UNITY_SETUP_INSTANCE_ID(v);
  245. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  246. TerrainAnimateTree(v.vertex, v.color.w);
  247. o.pos = UnityObjectToClipPos(v.vertex);
  248. o.uv = v.texcoord.xy;
  249. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  250. o.nz.w = COMPUTE_DEPTH_01;
  251. return o;
  252. }
  253. uniform sampler2D _MainTex;
  254. uniform fixed _Cutoff;
  255. fixed4 frag(v2f i) : SV_Target {
  256. half alpha = tex2D(_MainTex, i.uv).a;
  257. clip (alpha - _Cutoff);
  258. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  259. }
  260. ENDCG
  261. }
  262. Pass {
  263. Cull Front
  264. CGPROGRAM
  265. #pragma target 3.0
  266. #pragma vertex vert
  267. #pragma fragment frag
  268. #include "UnityCG.cginc"
  269. #include "TerrainEngine.cginc"
  270. struct v2f {
  271. float4 pos : SV_POSITION;
  272. float2 uv : TEXCOORD0;
  273. float4 nz : TEXCOORD1;
  274. UNITY_VERTEX_OUTPUT_STEREO
  275. };
  276. struct appdata {
  277. float4 vertex : POSITION;
  278. float3 normal : NORMAL;
  279. fixed4 color : COLOR;
  280. float4 texcoord : TEXCOORD0;
  281. UNITY_VERTEX_INPUT_INSTANCE_ID
  282. };
  283. v2f vert( appdata v ) {
  284. v2f o;
  285. UNITY_SETUP_INSTANCE_ID(v);
  286. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  287. TerrainAnimateTree(v.vertex, v.color.w);
  288. o.pos = UnityObjectToClipPos(v.vertex);
  289. o.uv = v.texcoord.xy;
  290. o.nz.xyz = -COMPUTE_VIEW_NORMAL;
  291. o.nz.w = COMPUTE_DEPTH_01;
  292. return o;
  293. }
  294. uniform sampler2D _MainTex;
  295. uniform fixed _Cutoff;
  296. fixed4 frag(v2f i) : SV_Target {
  297. fixed4 texcol = tex2D( _MainTex, i.uv );
  298. clip( texcol.a - _Cutoff );
  299. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  300. }
  301. ENDCG
  302. }
  303. }
  304. SubShader {
  305. Tags { "RenderType"="TreeBillboard" }
  306. Pass {
  307. Cull Off
  308. CGPROGRAM
  309. #pragma target 3.0
  310. #pragma vertex vert
  311. #pragma fragment frag
  312. #include "UnityCG.cginc"
  313. #include "TerrainEngine.cginc"
  314. struct v2f {
  315. float4 pos : SV_POSITION;
  316. float2 uv : TEXCOORD0;
  317. float4 nz : TEXCOORD1;
  318. UNITY_VERTEX_OUTPUT_STEREO
  319. };
  320. v2f vert (appdata_tree_billboard v) {
  321. v2f o;
  322. UNITY_SETUP_INSTANCE_ID(v);
  323. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  324. TerrainBillboardTree(v.vertex, v.texcoord1.xy, v.texcoord.y);
  325. o.pos = UnityObjectToClipPos(v.vertex);
  326. o.uv.x = v.texcoord.x;
  327. o.uv.y = v.texcoord.y > 0;
  328. o.nz.xyz = float3(0,0,1);
  329. o.nz.w = COMPUTE_DEPTH_01;
  330. return o;
  331. }
  332. uniform sampler2D _MainTex;
  333. fixed4 frag(v2f i) : SV_Target {
  334. fixed4 texcol = tex2D( _MainTex, i.uv );
  335. clip( texcol.a - 0.001 );
  336. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  337. }
  338. ENDCG
  339. }
  340. }
  341. SubShader {
  342. Tags { "RenderType"="GrassBillboard" }
  343. Pass {
  344. Cull Off
  345. CGPROGRAM
  346. #pragma target 3.0
  347. #pragma vertex vert
  348. #pragma fragment frag
  349. #include "UnityCG.cginc"
  350. #include "TerrainEngine.cginc"
  351. struct v2f {
  352. float4 pos : SV_POSITION;
  353. fixed4 color : COLOR;
  354. float2 uv : TEXCOORD0;
  355. float4 nz : TEXCOORD1;
  356. UNITY_VERTEX_OUTPUT_STEREO
  357. };
  358. v2f vert (appdata_full v) {
  359. v2f o;
  360. UNITY_SETUP_INSTANCE_ID(v);
  361. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  362. WavingGrassBillboardVert (v);
  363. o.color = v.color;
  364. o.pos = UnityObjectToClipPos(v.vertex);
  365. o.uv = v.texcoord.xy;
  366. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  367. o.nz.w = COMPUTE_DEPTH_01;
  368. return o;
  369. }
  370. uniform sampler2D _MainTex;
  371. uniform fixed _Cutoff;
  372. fixed4 frag(v2f i) : SV_Target {
  373. fixed4 texcol = tex2D( _MainTex, i.uv );
  374. fixed alpha = texcol.a * i.color.a;
  375. clip( alpha - _Cutoff );
  376. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  377. }
  378. ENDCG
  379. }
  380. }
  381. SubShader {
  382. Tags { "RenderType"="Grass" }
  383. Pass {
  384. Cull Off
  385. CGPROGRAM
  386. #pragma target 3.0
  387. #pragma vertex vert
  388. #pragma fragment frag
  389. #include "UnityCG.cginc"
  390. #include "TerrainEngine.cginc"
  391. struct v2f {
  392. float4 pos : SV_POSITION;
  393. fixed4 color : COLOR;
  394. float2 uv : TEXCOORD0;
  395. float4 nz : TEXCOORD1;
  396. UNITY_VERTEX_OUTPUT_STEREO
  397. };
  398. v2f vert (appdata_full v) {
  399. v2f o;
  400. UNITY_SETUP_INSTANCE_ID(v);
  401. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  402. WavingGrassVert (v);
  403. o.color = v.color;
  404. o.pos = UnityObjectToClipPos(v.vertex);
  405. o.uv = v.texcoord;
  406. o.nz.xyz = COMPUTE_VIEW_NORMAL;
  407. o.nz.w = COMPUTE_DEPTH_01;
  408. return o;
  409. }
  410. uniform sampler2D _MainTex;
  411. uniform fixed _Cutoff;
  412. fixed4 frag(v2f i) : SV_Target {
  413. fixed4 texcol = tex2D( _MainTex, i.uv );
  414. fixed alpha = texcol.a * i.color.a;
  415. clip( alpha - _Cutoff );
  416. return EncodeDepthNormal (i.nz.w, i.nz.xyz);
  417. }
  418. ENDCG
  419. }
  420. }
  421. Fallback Off
  422. }