CameraDepthTexture.shader 11 KB

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