pbrfog.shader 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Shader created with Shader Forge v1.40
  2. // Shader Forge (c) Freya Holmer - http://www.acegikmo.com/shaderforge/
  3. // Note: Manually altering this data may prevent you from opening it in Shader Forge
  4. /*SF_DATA;ver:1.40;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,cpap:True,lico:1,lgpr:1,limd:3,spmd:1,trmd:0,grmd:1,uamb:True,mssp:True,bkdf:True,hqlp:False,rprd:True,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:False,qofs:0,qpre:2,rntp:3,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,atwp:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:2865,x:32719,y:32712,varname:node_2865,prsc:2|diff-6343-OUT,spec-4712-OUT,gloss-1813-OUT,normal-5964-RGB,emission-9267-OUT,difocc-5414-R,clip-422-OUT;n:type:ShaderForge.SFN_Multiply,id:6343,x:31965,y:32543,varname:node_6343,prsc:2|A-7736-RGB,B-6665-RGB;n:type:ShaderForge.SFN_Color,id:6665,x:31717,y:32728,ptovrint:False,ptlb:Color,ptin:_Color,varname:_Color,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:0.5019608,c2:0.5019608,c3:0.5019608,c4:1;n:type:ShaderForge.SFN_Tex2d,id:7736,x:31762,y:32377,ptovrint:True,ptlb:Base Color,ptin:_MainTex,varname:_MainTex,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Tex2d,id:5964,x:32013,y:32974,ptovrint:True,ptlb:Normal Map,ptin:_BumpMap,varname:_BumpMap,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:3,isnm:True;n:type:ShaderForge.SFN_Slider,id:358,x:32013,y:32790,ptovrint:False,ptlb:Metallic,ptin:_Metallic,varname:node_358,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0,max:1;n:type:ShaderForge.SFN_Slider,id:1813,x:32143,y:32882,ptovrint:False,ptlb:Roughness,ptin:_Roughness,varname:_Metallic_copy,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0.8,max:1;n:type:ShaderForge.SFN_Tex2d,id:6117,x:32112,y:32611,ptovrint:False,ptlb:Met,ptin:_Met,varname:node_6117,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Multiply,id:4712,x:32419,y:32741,varname:node_4712,prsc:2|A-6117-R,B-358-OUT;n:type:ShaderForge.SFN_Slider,id:914,x:30202,y:32877,ptovrint:False,ptlb:Tile,ptin:_Tile,varname:node_914,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:10,max:50;n:type:ShaderForge.SFN_TexCoord,id:1838,x:30154,y:33097,varname:node_1838,prsc:2,uv:0,uaff:False;n:type:ShaderForge.SFN_Vector1,id:2015,x:30359,y:32684,varname:node_2015,prsc:2,v1:1;n:type:ShaderForge.SFN_Divide,id:4960,x:30556,y:32810,varname:node_4960,prsc:2|A-2015-OUT,B-914-OUT;n:type:ShaderForge.SFN_Multiply,id:8113,x:30443,y:33080,varname:node_8113,prsc:2|A-914-OUT,B-1838-UVOUT;n:type:ShaderForge.SFN_Ceil,id:7493,x:30618,y:33080,varname:node_7493,prsc:2|IN-8113-OUT;n:type:ShaderForge.SFN_Multiply,id:4612,x:30854,y:33027,varname:node_4612,prsc:2|A-4960-OUT,B-7493-OUT;n:type:ShaderForge.SFN_Tex2d,id:3693,x:31335,y:32710,ptovrint:False,ptlb:Edge,ptin:_Edge,varname:node_3693,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False|UVIN-4612-OUT;n:type:ShaderForge.SFN_Color,id:9046,x:31335,y:32936,ptovrint:False,ptlb:EdgeColor,ptin:_EdgeColor,varname:node_9046,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:0,c2:0.8317747,c3:1,c4:1;n:type:ShaderForge.SFN_Multiply,id:9267,x:31974,y:33125,varname:node_9267,prsc:2|A-3693-RGB,B-9046-RGB,C-3274-OUT;n:type:ShaderForge.SFN_RemapRange,id:4670,x:30275,y:33386,varname:node_4670,prsc:2,frmn:0,frmx:1,tomn:-1.2,tomx:1|IN-4612-OUT;n:type:ShaderForge.SFN_Length,id:3851,x:30504,y:33386,varname:node_3851,prsc:2|IN-4670-OUT;n:type:ShaderForge.SFN_OneMinus,id:1103,x:30701,y:33386,varname:node_1103,prsc:2|IN-3851-OUT;n:type:ShaderForge.SFN_Clamp01,id:3621,x:30917,y:33386,varname:node_3621,prsc:2|IN-1103-OUT;n:type:ShaderForge.SFN_OneMinus,id:2440,x:30275,y:33594,varname:node_2440,prsc:2|IN-1838-V;n:type:ShaderForge.SFN_Min,id:1978,x:30515,y:33594,varname:node_1978,prsc:2|A-1838-V,B-2440-OUT;n:type:ShaderForge.SFN_Multiply,id:6142,x:31119,y:33464,varname:node_6142,prsc:2|A-3621-OUT,B-1978-OUT;n:type:ShaderForge.SFN_Slider,id:6935,x:31131,y:33266,ptovrint:False,ptlb:Dissoltion,ptin:_Dissoltion,varname:node_6935,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0.4789392,max:1;n:type:ShaderForge.SFN_Subtract,id:8485,x:31413,y:33458,varname:node_8485,prsc:2|A-6935-OUT,B-4-OUT;n:type:ShaderForge.SFN_Slider,id:4,x:31173,y:33708,ptovrint:False,ptlb:With,ptin:_With,varname:node_4,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0,max:1;n:type:ShaderForge.SFN_Step,id:422,x:31642,y:33468,varname:node_422,prsc:2|A-8485-OUT,B-6142-OUT;n:type:ShaderForge.SFN_Step,id:9385,x:31592,y:33238,varname:node_9385,prsc:2|A-6935-OUT,B-6142-OUT;n:type:ShaderForge.SFN_Subtract,id:3274,x:31777,y:33294,varname:node_3274,prsc:2|A-422-OUT,B-9385-OUT;n:type:ShaderForge.SFN_Tex2d,id:5414,x:32464,y:32998,ptovrint:False,ptlb:ao,ptin:_ao,varname:node_5414,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False;proporder:5964-6665-7736-358-1813-6117-914-3693-9046-6935-4-5414;pass:END;sub:END;*/
  5. Shader "Shader Forge/pbrfog" {
  6. Properties {
  7. _BumpMap ("Normal Map", 2D) = "bump" {}
  8. _Color ("Color", Color) = (0.5019608,0.5019608,0.5019608,1)
  9. _MainTex ("Base Color", 2D) = "white" {}
  10. _Metallic ("Metallic", Range(0, 1)) = 0
  11. _Roughness ("Roughness", Range(0, 1)) = 0.8
  12. _Met ("Met", 2D) = "white" {}
  13. _Tile ("Tile", Range(0, 50)) = 10
  14. _Edge ("Edge", 2D) = "white" {}
  15. _EdgeColor ("EdgeColor", Color) = (0,0.8317747,1,1)
  16. _Dissoltion ("Dissoltion", Range(0, 1)) = 0.4789392
  17. _With ("With", Range(0, 1)) = 0
  18. _ao ("ao", 2D) = "white" {}
  19. [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  20. }
  21. SubShader {
  22. Tags {
  23. "Queue"="AlphaTest"
  24. "RenderType"="TransparentCutout"
  25. }
  26. Pass {
  27. Name "FORWARD"
  28. Tags {
  29. "LightMode"="ForwardBase"
  30. }
  31. CGPROGRAM
  32. #pragma vertex vert
  33. #pragma fragment frag
  34. #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
  35. #define _GLOSSYENV 1
  36. #pragma multi_compile_instancing
  37. #include "UnityCG.cginc"
  38. #include "AutoLight.cginc"
  39. #include "Lighting.cginc"
  40. #include "UnityPBSLighting.cginc"
  41. #include "UnityStandardBRDF.cginc"
  42. #pragma multi_compile_fwdbase_fullshadows
  43. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  44. #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  45. #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  46. #pragma multi_compile_fog
  47. #pragma target 3.0
  48. uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
  49. uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST;
  50. uniform sampler2D _Met; uniform float4 _Met_ST;
  51. uniform sampler2D _Edge; uniform float4 _Edge_ST;
  52. uniform sampler2D _ao; uniform float4 _ao_ST;
  53. UNITY_INSTANCING_BUFFER_START( Props )
  54. UNITY_DEFINE_INSTANCED_PROP( float4, _Color)
  55. UNITY_DEFINE_INSTANCED_PROP( float, _Metallic)
  56. UNITY_DEFINE_INSTANCED_PROP( float, _Roughness)
  57. UNITY_DEFINE_INSTANCED_PROP( float, _Tile)
  58. UNITY_DEFINE_INSTANCED_PROP( float4, _EdgeColor)
  59. UNITY_DEFINE_INSTANCED_PROP( float, _Dissoltion)
  60. UNITY_DEFINE_INSTANCED_PROP( float, _With)
  61. UNITY_INSTANCING_BUFFER_END( Props )
  62. struct VertexInput {
  63. float4 vertex : POSITION;
  64. float3 normal : NORMAL;
  65. float4 tangent : TANGENT;
  66. float2 texcoord0 : TEXCOORD0;
  67. float2 texcoord1 : TEXCOORD1;
  68. float2 texcoord2 : TEXCOORD2;
  69. };
  70. struct VertexOutput {
  71. float4 pos : SV_POSITION;
  72. UNITY_VERTEX_INPUT_INSTANCE_ID
  73. float2 uv0 : TEXCOORD0;
  74. float2 uv1 : TEXCOORD1;
  75. float2 uv2 : TEXCOORD2;
  76. float4 posWorld : TEXCOORD3;
  77. float3 normalDir : TEXCOORD4;
  78. float3 tangentDir : TEXCOORD5;
  79. float3 bitangentDir : TEXCOORD6;
  80. LIGHTING_COORDS(7,8)
  81. UNITY_FOG_COORDS(9)
  82. #if defined(LIGHTMAP_ON) || defined(UNITY_SHOULD_SAMPLE_SH)
  83. float4 ambientOrLightmapUV : TEXCOORD10;
  84. #endif
  85. };
  86. VertexOutput vert (VertexInput v) {
  87. VertexOutput o = (VertexOutput)0;
  88. UNITY_SETUP_INSTANCE_ID( v );
  89. UNITY_TRANSFER_INSTANCE_ID( v, o );
  90. o.uv0 = v.texcoord0;
  91. o.uv1 = v.texcoord1;
  92. o.uv2 = v.texcoord2;
  93. #ifdef LIGHTMAP_ON
  94. o.ambientOrLightmapUV.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  95. o.ambientOrLightmapUV.zw = 0;
  96. #elif UNITY_SHOULD_SAMPLE_SH
  97. #endif
  98. #ifdef DYNAMICLIGHTMAP_ON
  99. o.ambientOrLightmapUV.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  100. #endif
  101. o.normalDir = UnityObjectToWorldNormal(v.normal);
  102. o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
  103. o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  104. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  105. float3 lightColor = _LightColor0.rgb;
  106. o.pos = UnityObjectToClipPos( v.vertex );
  107. UNITY_TRANSFER_FOG(o,o.pos);
  108. TRANSFER_VERTEX_TO_FRAGMENT(o)
  109. return o;
  110. }
  111. float4 frag(VertexOutput i) : COLOR {
  112. UNITY_SETUP_INSTANCE_ID( i );
  113. i.normalDir = normalize(i.normalDir);
  114. float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
  115. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  116. float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
  117. float3 normalLocal = _BumpMap_var.rgb;
  118. float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
  119. float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
  120. float _Dissoltion_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Dissoltion );
  121. float _With_var = UNITY_ACCESS_INSTANCED_PROP( Props, _With );
  122. float _Tile_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Tile );
  123. float2 node_4612 = ((1.0/_Tile_var)*ceil((_Tile_var*i.uv0)));
  124. float node_6142 = (saturate((1.0 - length((node_4612*2.2+-1.2))))*min(i.uv0.g,(1.0 - i.uv0.g)));
  125. float node_422 = step((_Dissoltion_var-_With_var),node_6142);
  126. clip(node_422 - 0.5);
  127. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  128. float3 lightColor = _LightColor0.rgb;
  129. float3 halfDirection = normalize(viewDirection+lightDirection);
  130. ////// Lighting:
  131. float attenuation = LIGHT_ATTENUATION(i);
  132. float3 attenColor = attenuation * _LightColor0.xyz;
  133. float Pi = 3.141592654;
  134. float InvPi = 0.31830988618;
  135. ///////// Gloss:
  136. float _Roughness_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Roughness );
  137. float gloss = 1.0 - _Roughness_var; // Convert roughness to gloss
  138. float perceptualRoughness = _Roughness_var;
  139. float roughness = perceptualRoughness * perceptualRoughness;
  140. float specPow = exp2( gloss * 10.0 + 1.0 );
  141. /////// GI Data:
  142. UnityLight light;
  143. #ifdef LIGHTMAP_OFF
  144. light.color = lightColor;
  145. light.dir = lightDirection;
  146. light.ndotl = LambertTerm (normalDirection, light.dir);
  147. #else
  148. light.color = half3(0.f, 0.f, 0.f);
  149. light.ndotl = 0.0f;
  150. light.dir = half3(0.f, 0.f, 0.f);
  151. #endif
  152. UnityGIInput d;
  153. d.light = light;
  154. d.worldPos = i.posWorld.xyz;
  155. d.worldViewDir = viewDirection;
  156. d.atten = attenuation;
  157. #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
  158. d.ambient = 0;
  159. d.lightmapUV = i.ambientOrLightmapUV;
  160. #else
  161. d.ambient = i.ambientOrLightmapUV;
  162. #endif
  163. #if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
  164. d.boxMin[0] = unity_SpecCube0_BoxMin;
  165. d.boxMin[1] = unity_SpecCube1_BoxMin;
  166. #endif
  167. #if UNITY_SPECCUBE_BOX_PROJECTION
  168. d.boxMax[0] = unity_SpecCube0_BoxMax;
  169. d.boxMax[1] = unity_SpecCube1_BoxMax;
  170. d.probePosition[0] = unity_SpecCube0_ProbePosition;
  171. d.probePosition[1] = unity_SpecCube1_ProbePosition;
  172. #endif
  173. d.probeHDR[0] = unity_SpecCube0_HDR;
  174. d.probeHDR[1] = unity_SpecCube1_HDR;
  175. Unity_GlossyEnvironmentData ugls_en_data;
  176. ugls_en_data.roughness = 1.0 - gloss;
  177. ugls_en_data.reflUVW = viewReflectDirection;
  178. UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data );
  179. lightDirection = gi.light.dir;
  180. lightColor = gi.light.color;
  181. ////// Specular:
  182. float NdotL = saturate(dot( normalDirection, lightDirection ));
  183. float LdotH = saturate(dot(lightDirection, halfDirection));
  184. float4 _Met_var = tex2D(_Met,TRANSFORM_TEX(i.uv0, _Met));
  185. float _Metallic_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Metallic );
  186. float3 specularColor = (_Met_var.r*_Metallic_var);
  187. float specularMonochrome;
  188. float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
  189. float4 _Color_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Color );
  190. float3 diffuseColor = (_MainTex_var.rgb*_Color_var.rgb); // Need this for specular when using metallic
  191. diffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );
  192. specularMonochrome = 1.0-specularMonochrome;
  193. float NdotV = abs(dot( normalDirection, viewDirection ));
  194. float NdotH = saturate(dot( normalDirection, halfDirection ));
  195. float VdotH = saturate(dot( viewDirection, halfDirection ));
  196. float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );
  197. float normTerm = GGXTerm(NdotH, roughness);
  198. float specularPBL = (visTerm*normTerm) * UNITY_PI;
  199. #ifdef UNITY_COLORSPACE_GAMMA
  200. specularPBL = sqrt(max(1e-4h, specularPBL));
  201. #endif
  202. specularPBL = max(0, specularPBL * NdotL);
  203. #if defined(_SPECULARHIGHLIGHTS_OFF)
  204. specularPBL = 0.0;
  205. #endif
  206. half surfaceReduction;
  207. #ifdef UNITY_COLORSPACE_GAMMA
  208. surfaceReduction = 1.0-0.28*roughness*perceptualRoughness;
  209. #else
  210. surfaceReduction = 1.0/(roughness*roughness + 1.0);
  211. #endif
  212. specularPBL *= any(specularColor) ? 1.0 : 0.0;
  213. float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);
  214. half grazingTerm = saturate( gloss + specularMonochrome );
  215. float3 indirectSpecular = (gi.indirect.specular);
  216. indirectSpecular *= FresnelLerp (specularColor, grazingTerm, NdotV);
  217. indirectSpecular *= surfaceReduction;
  218. float3 specular = (directSpecular + indirectSpecular);
  219. /////// Diffuse:
  220. NdotL = max(0.0,dot( normalDirection, lightDirection ));
  221. half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
  222. float nlPow5 = Pow5(1-NdotL);
  223. float nvPow5 = Pow5(1-NdotV);
  224. float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;
  225. float3 indirectDiffuse = float3(0,0,0);
  226. indirectDiffuse += gi.indirect.diffuse;
  227. float4 _ao_var = tex2D(_ao,TRANSFORM_TEX(i.uv0, _ao));
  228. indirectDiffuse *= _ao_var.r; // Diffuse AO
  229. float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
  230. ////// Emissive:
  231. float4 _Edge_var = tex2D(_Edge,TRANSFORM_TEX(node_4612, _Edge));
  232. float4 _EdgeColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _EdgeColor );
  233. float3 emissive = (_Edge_var.rgb*_EdgeColor_var.rgb*(node_422-step(_Dissoltion_var,node_6142)));
  234. /// Final Color:
  235. float3 finalColor = diffuse + specular + emissive;
  236. fixed4 finalRGBA = fixed4(finalColor,1);
  237. UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
  238. return finalRGBA;
  239. }
  240. ENDCG
  241. }
  242. Pass {
  243. Name "FORWARD_DELTA"
  244. Tags {
  245. "LightMode"="ForwardAdd"
  246. }
  247. Blend One One
  248. CGPROGRAM
  249. #pragma vertex vert
  250. #pragma fragment frag
  251. #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
  252. #define _GLOSSYENV 1
  253. #pragma multi_compile_instancing
  254. #include "UnityCG.cginc"
  255. #include "AutoLight.cginc"
  256. #include "Lighting.cginc"
  257. #include "UnityPBSLighting.cginc"
  258. #include "UnityStandardBRDF.cginc"
  259. #pragma multi_compile_fwdadd_fullshadows
  260. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  261. #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  262. #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  263. #pragma multi_compile_fog
  264. #pragma target 3.0
  265. uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
  266. uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST;
  267. uniform sampler2D _Met; uniform float4 _Met_ST;
  268. uniform sampler2D _Edge; uniform float4 _Edge_ST;
  269. UNITY_INSTANCING_BUFFER_START( Props )
  270. UNITY_DEFINE_INSTANCED_PROP( float4, _Color)
  271. UNITY_DEFINE_INSTANCED_PROP( float, _Metallic)
  272. UNITY_DEFINE_INSTANCED_PROP( float, _Roughness)
  273. UNITY_DEFINE_INSTANCED_PROP( float, _Tile)
  274. UNITY_DEFINE_INSTANCED_PROP( float4, _EdgeColor)
  275. UNITY_DEFINE_INSTANCED_PROP( float, _Dissoltion)
  276. UNITY_DEFINE_INSTANCED_PROP( float, _With)
  277. UNITY_INSTANCING_BUFFER_END( Props )
  278. struct VertexInput {
  279. float4 vertex : POSITION;
  280. float3 normal : NORMAL;
  281. float4 tangent : TANGENT;
  282. float2 texcoord0 : TEXCOORD0;
  283. float2 texcoord1 : TEXCOORD1;
  284. float2 texcoord2 : TEXCOORD2;
  285. };
  286. struct VertexOutput {
  287. float4 pos : SV_POSITION;
  288. UNITY_VERTEX_INPUT_INSTANCE_ID
  289. float2 uv0 : TEXCOORD0;
  290. float2 uv1 : TEXCOORD1;
  291. float2 uv2 : TEXCOORD2;
  292. float4 posWorld : TEXCOORD3;
  293. float3 normalDir : TEXCOORD4;
  294. float3 tangentDir : TEXCOORD5;
  295. float3 bitangentDir : TEXCOORD6;
  296. LIGHTING_COORDS(7,8)
  297. UNITY_FOG_COORDS(9)
  298. };
  299. VertexOutput vert (VertexInput v) {
  300. VertexOutput o = (VertexOutput)0;
  301. UNITY_SETUP_INSTANCE_ID( v );
  302. UNITY_TRANSFER_INSTANCE_ID( v, o );
  303. o.uv0 = v.texcoord0;
  304. o.uv1 = v.texcoord1;
  305. o.uv2 = v.texcoord2;
  306. o.normalDir = UnityObjectToWorldNormal(v.normal);
  307. o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
  308. o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  309. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  310. float3 lightColor = _LightColor0.rgb;
  311. o.pos = UnityObjectToClipPos( v.vertex );
  312. UNITY_TRANSFER_FOG(o,o.pos);
  313. TRANSFER_VERTEX_TO_FRAGMENT(o)
  314. return o;
  315. }
  316. float4 frag(VertexOutput i) : COLOR {
  317. UNITY_SETUP_INSTANCE_ID( i );
  318. i.normalDir = normalize(i.normalDir);
  319. float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
  320. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  321. float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
  322. float3 normalLocal = _BumpMap_var.rgb;
  323. float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
  324. float _Dissoltion_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Dissoltion );
  325. float _With_var = UNITY_ACCESS_INSTANCED_PROP( Props, _With );
  326. float _Tile_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Tile );
  327. float2 node_4612 = ((1.0/_Tile_var)*ceil((_Tile_var*i.uv0)));
  328. float node_6142 = (saturate((1.0 - length((node_4612*2.2+-1.2))))*min(i.uv0.g,(1.0 - i.uv0.g)));
  329. float node_422 = step((_Dissoltion_var-_With_var),node_6142);
  330. clip(node_422 - 0.5);
  331. float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
  332. float3 lightColor = _LightColor0.rgb;
  333. float3 halfDirection = normalize(viewDirection+lightDirection);
  334. ////// Lighting:
  335. float attenuation = LIGHT_ATTENUATION(i);
  336. float3 attenColor = attenuation * _LightColor0.xyz;
  337. float Pi = 3.141592654;
  338. float InvPi = 0.31830988618;
  339. ///////// Gloss:
  340. float _Roughness_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Roughness );
  341. float gloss = 1.0 - _Roughness_var; // Convert roughness to gloss
  342. float perceptualRoughness = _Roughness_var;
  343. float roughness = perceptualRoughness * perceptualRoughness;
  344. float specPow = exp2( gloss * 10.0 + 1.0 );
  345. ////// Specular:
  346. float NdotL = saturate(dot( normalDirection, lightDirection ));
  347. float LdotH = saturate(dot(lightDirection, halfDirection));
  348. float4 _Met_var = tex2D(_Met,TRANSFORM_TEX(i.uv0, _Met));
  349. float _Metallic_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Metallic );
  350. float3 specularColor = (_Met_var.r*_Metallic_var);
  351. float specularMonochrome;
  352. float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
  353. float4 _Color_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Color );
  354. float3 diffuseColor = (_MainTex_var.rgb*_Color_var.rgb); // Need this for specular when using metallic
  355. diffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );
  356. specularMonochrome = 1.0-specularMonochrome;
  357. float NdotV = abs(dot( normalDirection, viewDirection ));
  358. float NdotH = saturate(dot( normalDirection, halfDirection ));
  359. float VdotH = saturate(dot( viewDirection, halfDirection ));
  360. float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );
  361. float normTerm = GGXTerm(NdotH, roughness);
  362. float specularPBL = (visTerm*normTerm) * UNITY_PI;
  363. #ifdef UNITY_COLORSPACE_GAMMA
  364. specularPBL = sqrt(max(1e-4h, specularPBL));
  365. #endif
  366. specularPBL = max(0, specularPBL * NdotL);
  367. #if defined(_SPECULARHIGHLIGHTS_OFF)
  368. specularPBL = 0.0;
  369. #endif
  370. specularPBL *= any(specularColor) ? 1.0 : 0.0;
  371. float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);
  372. float3 specular = directSpecular;
  373. /////// Diffuse:
  374. NdotL = max(0.0,dot( normalDirection, lightDirection ));
  375. half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
  376. float nlPow5 = Pow5(1-NdotL);
  377. float nvPow5 = Pow5(1-NdotV);
  378. float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;
  379. float3 diffuse = directDiffuse * diffuseColor;
  380. /// Final Color:
  381. float3 finalColor = diffuse + specular;
  382. fixed4 finalRGBA = fixed4(finalColor * 1,0);
  383. UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
  384. return finalRGBA;
  385. }
  386. ENDCG
  387. }
  388. Pass {
  389. Name "ShadowCaster"
  390. Tags {
  391. "LightMode"="ShadowCaster"
  392. }
  393. Offset 1, 1
  394. Cull Back
  395. CGPROGRAM
  396. #pragma vertex vert
  397. #pragma fragment frag
  398. #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
  399. #define _GLOSSYENV 1
  400. #pragma multi_compile_instancing
  401. #include "UnityCG.cginc"
  402. #include "Lighting.cginc"
  403. #include "UnityPBSLighting.cginc"
  404. #include "UnityStandardBRDF.cginc"
  405. #pragma fragmentoption ARB_precision_hint_fastest
  406. #pragma multi_compile_shadowcaster
  407. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  408. #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  409. #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  410. #pragma multi_compile_fog
  411. #pragma target 3.0
  412. UNITY_INSTANCING_BUFFER_START( Props )
  413. UNITY_DEFINE_INSTANCED_PROP( float, _Tile)
  414. UNITY_DEFINE_INSTANCED_PROP( float, _Dissoltion)
  415. UNITY_DEFINE_INSTANCED_PROP( float, _With)
  416. UNITY_INSTANCING_BUFFER_END( Props )
  417. struct VertexInput {
  418. float4 vertex : POSITION;
  419. float2 texcoord0 : TEXCOORD0;
  420. float2 texcoord1 : TEXCOORD1;
  421. float2 texcoord2 : TEXCOORD2;
  422. };
  423. struct VertexOutput {
  424. V2F_SHADOW_CASTER;
  425. UNITY_VERTEX_INPUT_INSTANCE_ID
  426. float2 uv0 : TEXCOORD1;
  427. float2 uv1 : TEXCOORD2;
  428. float2 uv2 : TEXCOORD3;
  429. float4 posWorld : TEXCOORD4;
  430. };
  431. VertexOutput vert (VertexInput v) {
  432. VertexOutput o = (VertexOutput)0;
  433. UNITY_SETUP_INSTANCE_ID( v );
  434. UNITY_TRANSFER_INSTANCE_ID( v, o );
  435. o.uv0 = v.texcoord0;
  436. o.uv1 = v.texcoord1;
  437. o.uv2 = v.texcoord2;
  438. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  439. o.pos = UnityObjectToClipPos( v.vertex );
  440. TRANSFER_SHADOW_CASTER(o)
  441. return o;
  442. }
  443. float4 frag(VertexOutput i) : COLOR {
  444. UNITY_SETUP_INSTANCE_ID( i );
  445. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  446. float _Dissoltion_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Dissoltion );
  447. float _With_var = UNITY_ACCESS_INSTANCED_PROP( Props, _With );
  448. float _Tile_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Tile );
  449. float2 node_4612 = ((1.0/_Tile_var)*ceil((_Tile_var*i.uv0)));
  450. float node_6142 = (saturate((1.0 - length((node_4612*2.2+-1.2))))*min(i.uv0.g,(1.0 - i.uv0.g)));
  451. float node_422 = step((_Dissoltion_var-_With_var),node_6142);
  452. clip(node_422 - 0.5);
  453. SHADOW_CASTER_FRAGMENT(i)
  454. }
  455. ENDCG
  456. }
  457. Pass {
  458. Name "Meta"
  459. Tags {
  460. "LightMode"="Meta"
  461. }
  462. Cull Off
  463. CGPROGRAM
  464. #pragma vertex vert
  465. #pragma fragment frag
  466. #define UNITY_PASS_META 1
  467. #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
  468. #define _GLOSSYENV 1
  469. #pragma multi_compile_instancing
  470. #include "UnityCG.cginc"
  471. #include "Lighting.cginc"
  472. #include "UnityPBSLighting.cginc"
  473. #include "UnityStandardBRDF.cginc"
  474. #include "UnityMetaPass.cginc"
  475. #pragma fragmentoption ARB_precision_hint_fastest
  476. #pragma multi_compile_shadowcaster
  477. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  478. #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  479. #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  480. #pragma multi_compile_fog
  481. #pragma target 3.0
  482. uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
  483. uniform sampler2D _Met; uniform float4 _Met_ST;
  484. uniform sampler2D _Edge; uniform float4 _Edge_ST;
  485. UNITY_INSTANCING_BUFFER_START( Props )
  486. UNITY_DEFINE_INSTANCED_PROP( float4, _Color)
  487. UNITY_DEFINE_INSTANCED_PROP( float, _Metallic)
  488. UNITY_DEFINE_INSTANCED_PROP( float, _Roughness)
  489. UNITY_DEFINE_INSTANCED_PROP( float, _Tile)
  490. UNITY_DEFINE_INSTANCED_PROP( float4, _EdgeColor)
  491. UNITY_DEFINE_INSTANCED_PROP( float, _Dissoltion)
  492. UNITY_DEFINE_INSTANCED_PROP( float, _With)
  493. UNITY_INSTANCING_BUFFER_END( Props )
  494. struct VertexInput {
  495. float4 vertex : POSITION;
  496. float2 texcoord0 : TEXCOORD0;
  497. float2 texcoord1 : TEXCOORD1;
  498. float2 texcoord2 : TEXCOORD2;
  499. };
  500. struct VertexOutput {
  501. float4 pos : SV_POSITION;
  502. UNITY_VERTEX_INPUT_INSTANCE_ID
  503. float2 uv0 : TEXCOORD0;
  504. float2 uv1 : TEXCOORD1;
  505. float2 uv2 : TEXCOORD2;
  506. float4 posWorld : TEXCOORD3;
  507. };
  508. VertexOutput vert (VertexInput v) {
  509. VertexOutput o = (VertexOutput)0;
  510. UNITY_SETUP_INSTANCE_ID( v );
  511. UNITY_TRANSFER_INSTANCE_ID( v, o );
  512. o.uv0 = v.texcoord0;
  513. o.uv1 = v.texcoord1;
  514. o.uv2 = v.texcoord2;
  515. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  516. o.pos = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST );
  517. return o;
  518. }
  519. float4 frag(VertexOutput i) : SV_Target {
  520. UNITY_SETUP_INSTANCE_ID( i );
  521. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  522. UnityMetaInput o;
  523. UNITY_INITIALIZE_OUTPUT( UnityMetaInput, o );
  524. float _Tile_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Tile );
  525. float2 node_4612 = ((1.0/_Tile_var)*ceil((_Tile_var*i.uv0)));
  526. float4 _Edge_var = tex2D(_Edge,TRANSFORM_TEX(node_4612, _Edge));
  527. float4 _EdgeColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _EdgeColor );
  528. float _Dissoltion_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Dissoltion );
  529. float _With_var = UNITY_ACCESS_INSTANCED_PROP( Props, _With );
  530. float node_6142 = (saturate((1.0 - length((node_4612*2.2+-1.2))))*min(i.uv0.g,(1.0 - i.uv0.g)));
  531. float node_422 = step((_Dissoltion_var-_With_var),node_6142);
  532. o.Emission = (_Edge_var.rgb*_EdgeColor_var.rgb*(node_422-step(_Dissoltion_var,node_6142)));
  533. float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
  534. float4 _Color_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Color );
  535. float3 diffColor = (_MainTex_var.rgb*_Color_var.rgb);
  536. float specularMonochrome;
  537. float3 specColor;
  538. float4 _Met_var = tex2D(_Met,TRANSFORM_TEX(i.uv0, _Met));
  539. float _Metallic_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Metallic );
  540. diffColor = DiffuseAndSpecularFromMetallic( diffColor, (_Met_var.r*_Metallic_var), specColor, specularMonochrome );
  541. float _Roughness_var = UNITY_ACCESS_INSTANCED_PROP( Props, _Roughness );
  542. float roughness = _Roughness_var;
  543. o.Albedo = diffColor + specColor * roughness * roughness * 0.5;
  544. return UnityMetaFragment( o );
  545. }
  546. ENDCG
  547. }
  548. }
  549. FallBack "Diffuse"
  550. CustomEditor "ShaderForgeMaterialInspector"
  551. }