Advertisement
SomeDev

Untitled

Nov 11th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. Shader "Unlit/NewPSXTEST"
  2. {
  3. Properties
  4. {
  5. _Color("Main Color", Color) = (1,1,1,1)
  6. [Header(Player position)][Space]
  7. _Player ("Player Position", Vector) = (0,0,0,0)
  8. [Header(Diffuse Alpha Cutoff)][Space]
  9. _MainTex ("Texture (Alpha Cutoff)", 2D) = "white" {}
  10. _VertexTex ("Color Map (Vertex)", 2D) = "white" {}
  11. _Distortion ("Geometric Resolution",Float) = 40
  12. _Distance ("Texture Draw Distance" , Float) = 2
  13. _LodDistance ("LOD Change" , Range(0,1)) = .5
  14. [Space]
  15. _Translucency ("Translucency Off/On", Range (0,1)) = 1
  16. _TranCutOff ("Cutoff Threshold", Range (0,1)) = .5
  17. [Space][Header(Emission)][Space]
  18. _EmiVal ("Intensity Off/Range", Range(0,10)) = 0
  19. _EmiMap ("Emission Map (Alpha Cutout)", 2D) = "white" {}
  20. _EmiTexture ("Emission texture", 2D) = "gray" {}
  21. //_EmiDistance ("Texture Draw Distance" , Float) = 2
  22. [HDR]_EmiColor ("Color", color) = (1., 1., 1., 1.)
  23. [Space][Header(Reflection)][Space]
  24. _RefValue ("Reflection Off/On", Range(0,1)) = 1
  25. _RefMapTex ("Reflection Map (Alpha Cutout)", 2D) = "white" {}
  26. _RefCubemap ("Reflection Cubemap", CUBE) = "" {}
  27. [Space][Header(Specular)][Space]
  28. _SpecLerp ("Specular Off/On", Range(0, 1)) = .5
  29. _Shininess ("Shininess", Range(0.1, 10)) = 1.
  30. [Space][Header(Shadow)][Space]
  31. _IlumStrength ("Shadow Strength", Range (0,1)) = 0.9
  32. _IlumDistance ("Shadow Draw Distance", Float) = 3
  33. }
  34. Category
  35. {
  36. Tags{ "RenderType"="TransparentCutout"}
  37. LOD 150
  38.  
  39. SubShader
  40. {
  41. Pass
  42. {
  43. Name "COLOR"
  44. Tags{ "LightMode" = "Vertex" }
  45. //ZWrite Off
  46. CGPROGRAM
  47.  
  48. #pragma vertex vert
  49. #pragma fragment frag
  50. #pragma target 2.0
  51. #pragma multi_compile_fog
  52. #include "UnityCG.cginc"
  53. #include "UnityLightingCommon.cginc" // for _LightColor0
  54. //> INPUT STRUCT
  55. struct appdata
  56. {
  57. float4 vertex : POSITION;
  58. float2 uv : TEXCOORD0;
  59. fixed4 color : COLOR0;
  60. float3 normal : NORMAL;
  61. };
  62.  
  63. struct v2f
  64. {
  65. float4 pos : SV_POSITION;
  66. float4 worldSpacePosition : TEXCOORD0;
  67. float3 uv : TEXCOORD1;
  68. float4 screenPos : TEXCOORD2;
  69. half3 worldRefl : TEXCOORD3;
  70. float4 color : COLOR0;
  71. };
  72.  
  73. //> TEXTURES
  74. float4 _Player;
  75. sampler2D _MainTex;
  76. float4 _MainTex_ST;
  77. sampler2D _VertexTex;
  78. sampler2D _VertexTex_ST;
  79. float _Distortion;
  80. //> DISTANCES
  81. float _Distance;
  82. float _LodDistance;
  83. //> REFLECTION
  84. sampler2D _RefMapTex;
  85. samplerCUBE _RefCubemap;
  86. float _RefValue;
  87. //> EMISSION
  88. sampler2D _EmiMap;
  89. sampler2D _EmiTexture;
  90. fixed4 _EmiColor;
  91. float _EmiVal;
  92. //> SPECULAR
  93. float _Shininess;
  94. float _SpecLerp;
  95.  
  96. uniform float _Translucency;
  97.  
  98. v2f vert(appdata v)
  99. {
  100. v2f o;
  101. //o.pos = UnityObjectToClipPos(v.vertex);
  102.  
  103. //> VERTEX SNAPPING
  104. //
  105. float3 wp = UnityObjectToViewPos (v.vertex); //converts object space to view space
  106. o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
  107. wp = floor(wp * _Distortion) / _Distortion;
  108. //
  109. float4 sp = mul(UNITY_MATRIX_P, fixed4(wp.xyz,1));//converts to projection matrix
  110. o.pos = sp;
  111. //
  112. float2 uv = TRANSFORM_TEX(v.uv, _MainTex);
  113. o.uv = float3(uv * sp.w, sp.w);
  114. o.screenPos = ComputeScreenPos(o.pos);
  115.  
  116.  
  117.  
  118. //> ILUMINATION
  119. // world space normal
  120. half3 worldNormal = UnityObjectToWorldNormal(v.normal);
  121. half nDotl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
  122. //assign darkness to the vertex colors while factoring in the light color
  123. float4 lightColor = v.color * _LightColor0 ;//* tex2Dlod(_VertexTex, float4(uv.xy, 0.0, 0.0));
  124.  
  125. //> DISTANCE
  126. //convert the World Space and calculate distance between vertices and player
  127. o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
  128.  
  129. //> SPECULAR
  130. float3 refl = reflect(-(normalize(_WorldSpaceLightPos0.xyz)), worldNormal);
  131. float RdotV = max(0., dot(refl, normalize(_WorldSpaceCameraPos.xyz - o.worldSpacePosition.xyz)));
  132. fixed3 spec = pow(RdotV, _Shininess) * _LightColor0.rgb * ceil(nDotl);
  133. //spec.a = lerp(spec.a, 0, _Translucency);
  134. fixed3 specColor = lerp(0, spec, _SpecLerp) * _Shininess;
  135.  
  136. //> FINAL COLOR
  137. o.color = lightColor + fixed4(specColor.rgb, 0);
  138.  
  139. //> RESULT
  140. return o;
  141. }
  142.  
  143. fixed4 frag(v2f i) : SV_Target
  144. {
  145. //> TEXTURE
  146. i.worldSpacePosition = floor(i.worldSpacePosition);
  147. //get distance from player to world position of vertices
  148. float dist = distance(i.worldSpacePosition, _Player) / (_Distance*10);
  149. //lerp between the texture and the vertex colors calculated in vertex shader
  150. float2 uv = i.uv.xy / i.uv.z;
  151. fixed4 fragTexture = lerp(tex2Dlod(_MainTex, float4(uv.xy, 0.0, 0.0)),
  152. tex2Dlod(_MainTex, float4(uv.xy, 0.0, 1.0)), dist * _LodDistance) * step(dist, _LodDistance);
  153. fragTexture += lerp(tex2Dlod(_MainTex, float4(uv.xy, 0.0, 1.0)),
  154. tex2Dlod(_MainTex, float4(uv.xy, 0.0, 2.0)), 1 - _LodDistance) * step(_LodDistance, dist);
  155. //
  156. fixed4 fragColor = lerp(fragTexture * i.color, i.color, dist);
  157. if(dist > 1)
  158. fragColor = i.color;
  159.  
  160. //> Emission
  161. //add calculated Emission to vertex colors
  162. fixed4 emiTexture = tex2Dlod(_EmiMap, float4(uv.xy, 0.0, 0.0));
  163. fixed4 emiColor = tex2Dlod(_EmiTexture, float4(uv.xy, 0.0, 0.0)).r * _EmiColor;
  164. emiColor = lerp(emiColor, _EmiColor, dist + 1);
  165. if(dist > 1)
  166. emiColor = _EmiColor;
  167. fragColor.rgb += lerp(0, emiColor.rgb, _EmiVal) * emiTexture.a;
  168.  
  169. //> REFLECTION
  170. // sample the default reflection cubemap, using the reflection vector
  171. half4 skyData = texCUBE(_RefCubemap, i.worldRefl);
  172. // decode cubemap data into actual color
  173. half3 skyColor = lerp(DecodeHDR (skyData, unity_SpecCube0_HDR), 0, dist);
  174. // get the transparent objects
  175. fixed4 refTexture = tex2Dlod(_RefMapTex, float4(uv.xy, 0.0, 0.0));
  176. //
  177. if(dist > 1)
  178. skyColor = 0;
  179. fragColor.rgb += lerp(0, skyColor.rgb, _RefValue) * refTexture.a;
  180.  
  181. //> TRANSPARENCY
  182. fragColor.a =lerp(0, fragColor.a, 1 - _Translucency) * tex2Dlod(_MainTex, float4(uv.xy, 0.0, 0.0)).a;
  183. //Screen-door transparency: Discard pixel if below threshold.
  184. float4x4 thresholdMatrix =
  185. { 1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
  186. 13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
  187. 4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
  188. 16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
  189. };
  190. float4x4 _RowAccess = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
  191. float2 pos = i.screenPos.xy / i.screenPos.w;
  192. pos *= _ScreenParams.xy; // pixel position
  193. clip(fragColor.a - thresholdMatrix[fmod(pos.x, 4)] * _RowAccess[fmod(pos.y, 4)]);
  194.  
  195. //> RESULT
  196. return fragColor;
  197. }
  198.  
  199. ENDCG
  200.  
  201. }
  202.  
  203. //> DIRECTIONAL LIGHT
  204. Pass
  205. {
  206. Name "VERTELIT"
  207. Tags{ "LightMode" = "Vertex" }
  208. //Blend One One //ZWrite Off
  209. Blend SrcAlpha OneMinusSrcAlpha //ZWrite Off
  210. Lighting On
  211.  
  212. CGPROGRAM
  213.  
  214. #pragma vertex vert
  215. #pragma fragment frag
  216. //#pragma target 2.0
  217. #pragma multi_compile_fog
  218. #pragma multi_compile_fwdbase
  219. #include "UnityCG.cginc"
  220. #include "AutoLight.cginc"
  221.  
  222. //> INPUT STRUCT
  223. struct appdata
  224. {
  225. float4 vertex : POSITION;
  226. float2 uv : TEXCOORD0;
  227. fixed4 color : COLOR0;
  228. float3 normal : NORMAL;
  229. };
  230.  
  231. //> OUTPUT STRUCT
  232. struct v2f {
  233. float4 color : COLOR0;
  234. float4 pos : SV_POSITION;
  235. float4 worldSpacePosition : TEXCOORD0;
  236. float3 uv : TEXCOORD1;
  237. float4 screenPos : TEXCOORD2;
  238. SHADOW_COORDS(3)
  239. float distance : TEXCOORD4;
  240. };
  241.  
  242. uniform float4 _Color;
  243.  
  244. //> TEXTURES
  245. float4 _Player;
  246. sampler2D _MainTex;
  247. float4 _MainTex_ST;
  248. sampler2D _VertexTex;
  249. sampler2D _VertexTex_ST;
  250. float _Distortion;
  251.  
  252. float _IlumDistance;
  253. float _IlumStrength;
  254. float _TranCutOff;
  255. //> TRANSPARENCY
  256. uniform float _Translucency;
  257.  
  258. //>
  259. v2f vert(appdata v)
  260. {
  261. v2f o;
  262.  
  263. //> VERTEX SNAPPING
  264. // including the line below will tell Unity not to upgrade the matrix mul() operations below...
  265. // UNITY_SHADER_NO_UPGRADE
  266. float3 wp = UnityObjectToViewPos (v.vertex);
  267. o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
  268. wp = floor(wp * _Distortion) / _Distortion;
  269. //
  270. float4 sp = mul(UNITY_MATRIX_P, fixed4(wp.xyz,1));
  271. o.pos = sp;
  272. //
  273. float2 uv = TRANSFORM_TEX(v.uv, _MainTex);
  274. o.uv = float3(uv * sp.w, sp.w);
  275. o.screenPos = ComputeScreenPos(o.pos);
  276.  
  277. //> DISTANCE
  278. //convert the World Space and calculate distance between vertices and player
  279. o.worldSpacePosition = floor(o.worldSpacePosition);
  280. o.distance = distance(o.worldSpacePosition, _Player) / (_IlumDistance);
  281.  
  282. //> FINAL COLOR
  283. o.color = v.color;
  284.  
  285. TRANSFER_VERTEX_TO_FRAGMENT(o)
  286. //TRANSFER_SHADOW(o)
  287. //> RESULT
  288. return o;
  289. }
  290.  
  291. //>
  292. fixed4 frag(v2f i) : SV_Target
  293. {
  294.  
  295. //> SHADING + SHADOWS
  296. //compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
  297. fixed shadow = SHADOW_ATTENUATION(i);
  298. // darken light's illumination with shadow, keep ambient intact
  299. fixed4 lighting = shadow;// + i.ambient;
  300. lighting.a = (1 - shadow);
  301.  
  302. //>
  303.  
  304. float2 uv = i.uv.xy / i.uv.z;
  305. half4 color = fixed4(0, 0, 0, 1);// i.color;//fixed4(1,1,1,1);
  306. color.a = lerp(.2, 1, 1 - i.distance) * _IlumStrength * tex2Dlod(_MainTex, float4(uv.xy, 0.0, 1.0)).a;
  307. lerp(1, 0, i.distance) * _IlumStrength;
  308. //> TRANSPARENCY
  309. if(i.distance > 1)
  310. color = 0;
  311. //calculate transparency cutoff
  312. if(lighting.a < _TranCutOff)
  313. color = 0.0;
  314. // Screen-door transparency: Discard pixel if below threshold.
  315. float4x4 thresholdMatrix = {
  316. 1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
  317. 13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
  318. 4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
  319. 16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
  320. };
  321. float4x4 _RowAccess = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
  322. float2 pos = i.screenPos.xy / i.screenPos.w;
  323. pos *= _ScreenParams.xy;
  324. clip((color.a) - thresholdMatrix[fmod(pos.x, 4)] * _RowAccess[fmod(pos.y, 4)]);
  325.  
  326. //> FOG
  327. //UNITY_APPLY_FOG(i.fogCoord, fragColor);
  328.  
  329. //> RETURN
  330. return color;
  331. }
  332.  
  333. ENDCG
  334. }
  335. //> VERTEX LIT, emulated in shaders (8 lights max, no specular)
  336. Pass
  337. {
  338. Name "VERTEXLIT"
  339. Tags{ "LightMode" = "Vertex" }
  340. //Blend SrcAlpha OneMinusSrcAlpha
  341. Blend One One
  342. Lighting Off
  343.  
  344. CGPROGRAM
  345.  
  346. #pragma vertex vert
  347. #pragma fragment frag
  348. #pragma target 2.0
  349. #pragma multi_compile_fog
  350. #pragma multi_compile_fwdbase
  351. #include "UnityCG.cginc"
  352. #include "UnityLightingCommon.cginc" // for _LightColor0
  353. //> INPUT STRUCT
  354. struct appdata
  355. {
  356. float4 vertex : POSITION;
  357. float2 uv : TEXCOORD0;
  358. fixed4 color : COLOR0;
  359. float3 normal : NORMAL;
  360. };
  361.  
  362. //> OUTPUT STRUCT
  363. struct v2f {
  364. float4 color : COLOR0;
  365. float4 pos : SV_POSITION;
  366. float4 worldSpacePosition : TEXCOORD0;
  367. float3 uv : TEXCOORD1;
  368. float4 screenPos : TEXCOORD2;
  369. float distance : TEXCOORD3;
  370. };
  371.  
  372. uniform float4 _Color;
  373.  
  374. //> TEXTURES
  375. float4 _Player;
  376. sampler2D _MainTex;
  377. float4 _MainTex_ST;
  378. sampler2D _VertexTex;
  379. sampler2D _VertexTex_ST;
  380. float _Distortion;
  381.  
  382. //> LIGHTING
  383. float _IlumStrength;
  384. float _IlumDistance;
  385.  
  386. //> TRANSPARENCY
  387. uniform float _Translucency;
  388.  
  389. //>
  390. v2f vert(appdata v)
  391. {
  392. v2f o;
  393.  
  394. //> VERTEX SNAPPING
  395. // including the line below will tell Unity not to upgrade the matrix mul() operations below...
  396. // UNITY_SHADER_NO_UPGRADE
  397. float3 wp = UnityObjectToViewPos (v.vertex);
  398. o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
  399. wp = floor(wp * _Distortion) / _Distortion;
  400. //
  401. float4 sp = mul(UNITY_MATRIX_P, fixed4(wp.xyz,1));
  402. o.pos = sp;
  403.  
  404. //
  405. float2 uv = TRANSFORM_TEX(v.uv, _MainTex);
  406. o.uv = float3(uv * sp.w, sp.w);
  407. o.screenPos = ComputeScreenPos(o.pos);
  408.  
  409.  
  410. //> DISTANCE
  411. //convert the World Space and calculate distance between vertices and player
  412. o.worldSpacePosition = floor(o.worldSpacePosition);
  413. o.distance = distance(o.worldSpacePosition, _Player) / (_IlumDistance );
  414.  
  415. //> VERTEX LIGHTS
  416. float3 lighting = float4(ShadeVertexLightsFull(v.vertex, v.normal, 8, true),1);
  417. o.color.rgb = lighting *.05;
  418. o.color.a = v.color.a ;//* tex2Dlod(_MainTex, float4(uv.xy, 0.0, 0.0)).a;
  419. //> RESULT
  420. return o;
  421. }
  422.  
  423. //>
  424. fixed4 frag(v2f i) : SV_Target
  425. {
  426. float2 uv = i.uv.xy / i.uv.z;
  427. fixed4 c;
  428. c = i.color;//* shadow;
  429. c.a = lerp(1, 0, i.distance) * _IlumStrength * tex2Dlod(_MainTex, float4(uv.xy, 0.0, 1.0)).a;
  430.  
  431. // Screen-door transparency: Discard pixel if below threshold.
  432. float4x4 thresholdMatrix = {
  433. 1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
  434. 13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
  435. 4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
  436. 16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
  437. };
  438. float4x4 _RowAccess = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
  439. float2 pos = i.screenPos.xy / i.screenPos.w;
  440. pos *= _ScreenParams.xy;
  441. clip((c.a) - thresholdMatrix[fmod(pos.x, 4)] * _RowAccess[fmod(pos.y, 4)]);
  442.  
  443. //> RESULT
  444. return c;
  445. }
  446.  
  447. ENDCG
  448. }
  449. //> SHADOWCASTER PASS
  450. Pass
  451. {
  452. Tags {"LightMode"="ShadowCaster"}
  453. CGPROGRAM
  454. #pragma vertex vert
  455. #pragma fragment frag
  456. #pragma multi_compile_shadowcaster
  457. #include "UnityCG.cginc"
  458. #include "UnityShaderVariables.cginc"
  459. //>
  460. struct appdata
  461. {
  462. float4 vertex : POSITION;
  463. float2 uv : TEXCOORD0;
  464. fixed4 color : COLOR0;
  465. float3 normal : NORMAL;
  466. };
  467. //>
  468. struct v2f {
  469. V2F_SHADOW_CASTER;
  470. float3 uv : TEXCOORD0;
  471. float4 screenPos : TEXCOORD1;
  472. float4 worldSpacePosition : TEXCOORD2;
  473. float4 color : COLOR0;
  474. };
  475.  
  476. //>
  477. sampler2D _MainTex;
  478. float4 _MainTex_ST;
  479. float _Distortion;
  480.  
  481. //>
  482. v2f vert(appdata v)
  483. {
  484. //> STRUCT
  485. v2f o;
  486.  
  487. //>
  488. //> VERTEX SNAPPING
  489. // including the line below will tell Unity not to upgrade the matrix mul() operations below...
  490. // UNITY_SHADER_NO_UPGRADE
  491.  
  492. float3 wp = UnityObjectToViewPos (v.vertex);
  493. wp = floor(wp * _Distortion) / _Distortion;
  494.  
  495.  
  496.  
  497. //v.vertex = mul(unity_WorldToObject, o.worldSpacePosition);
  498. TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
  499. return o;
  500. }
  501.  
  502. //>
  503. float4 frag(v2f i) : SV_Target
  504. {
  505. SHADOW_CASTER_FRAGMENT(i);
  506. }
  507. ENDCG
  508. }
  509. }
  510. }
  511.  
  512. //FallBack "Legacy Shaders/VertexLit"
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement