CameraDepthNormalsTexture.shader 11 KB

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