LayaPBRSpecularFB.cginc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. fixed4 _Color;
  2. sampler2D _MainTex;
  3. float4 _MainTex_ST;
  4. sampler2D _SpecGlossMap;
  5. half _Glossiness;
  6. half _GlossMapScale;
  7. half _Cutoff;
  8. sampler2D _BumpMap;
  9. float _BumpScale;
  10. sampler2D _ParallaxMap;
  11. half _Parallax;
  12. sampler2D _OcclusionMap;
  13. half _OcclusionStrength;
  14. sampler2D _EmissionMap;
  15. fixed4 _EmissionColor;
  16. #include "AutoLight.cginc"
  17. #include "Lighting.cginc"
  18. #include "libs/LayaUtil.cginc"
  19. #include "libs/LayaPBRSpecularLighting.cginc"
  20. #include "UnityStandardUtils.cginc"
  21. #include "UnityCG.cginc"
  22. struct a2v {
  23. float4 vertex : POSITION;
  24. float3 normal : NORMAL;
  25. float4 tangent : TANGENT;
  26. float4 texcoord : TEXCOORD0;
  27. float4 texcoord1: TEXCOORD1;
  28. };
  29. struct v2f {
  30. float4 pos : SV_POSITION;
  31. float2 uv : TEXCOORD0;
  32. float2 uv2: TEXCOORD1;
  33. float3 worldPos : TEXCOORD2;
  34. float3 worldNormal : TEXCOORD3;
  35. float3 lightDir: TEXCOORD4;
  36. float3 viewDir : TEXCOORD5;
  37. SHADOW_COORDS(6)
  38. UNITY_FOG_COORDS(7)
  39. };
  40. v2f vert(a2v v) {
  41. v2f o;
  42. o.pos = UnityObjectToClipPos(v.vertex);
  43. o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  44. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  45. o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  46. fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
  47. fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
  48. fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
  49. float3x3 worldToTangent = float3x3(worldTangent, worldBinormal, worldNormal);
  50. o.lightDir = mul(worldToTangent, WorldSpaceLightDir(v.vertex));
  51. o.viewDir = mul(worldToTangent, WorldSpaceViewDir(v.vertex));
  52. o.worldNormal = worldNormal;
  53. // Pass shadow coordinates to pixel shader
  54. TRANSFER_SHADOW(o);
  55. UNITY_TRANSFER_FOG(o, o.pos);
  56. return o;
  57. }
  58. fixed4 frag(v2f i) : SV_Target {
  59. fixed3 lightDir = normalize(i.lightDir);
  60. fixed3 viewDir = normalize(i.viewDir);
  61. #ifdef ParallaxTexture
  62. half h = tex2D (_ParallaxMap, i.uv).g;
  63. float2 offset = LayaParallaxOffset (h, _Parallax, i.viewDir);
  64. i.uv += offset;
  65. #endif
  66. fixed4 diffuseColor = tex2D(_MainTex, i.uv) * _Color;
  67. #if EnableAlphaCutoff
  68. clip (diffuseColor.a - _Cutoff);
  69. #endif
  70. fixed4 lightMapColor = fixed4(0.0,0.0,0.0,1.0);
  71. #if defined(LIGHTMAP_ON)
  72. lightMapColor = fixed4(DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv2)), 1.0);
  73. #endif
  74. fixed4 occlusionColor = tex2D(_OcclusionMap, i.uv) * LayaOcclusion(i.uv);
  75. fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
  76. fixed3 worldRefl = reflect(-worldViewDir, normalize(i.worldNormal));
  77. fixed4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, worldRefl);
  78. fixed3 skyColor = DecodeHDR(skyData, unity_SpecCube0_HDR);
  79. LayaGI gi;
  80. gi.diffuse = (occlusionColor * UNITY_LIGHTMODEL_AMBIENT + lightMapColor).rgb;
  81. #ifdef EnableReflection
  82. gi.specular = skyColor;
  83. #else
  84. gi.specular = fixed4(0.0, 0.0, 0.0, 1.0);
  85. #endif
  86. fixed3 normal = LayaUnpackScaleNormal(tex2D(_BumpMap, i.uv), _BumpScale);
  87. fixed4 specularGloss = SpecularGloss(i.uv);
  88. fixed4 color = LayaPBRSpecularLighting(diffuseColor, specularGloss.xyz, specularGloss.w, normal, viewDir, lightDir, gi);
  89. #ifdef EnableEmission
  90. color += tex2D(_EmissionMap, i.uv) * _EmissionColor;
  91. #endif
  92. //fixed shadow = SHADOW_ATTENUATION(i);
  93. UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
  94. //color = fixed4(color.rgb * atten * shadow, color.a);
  95. color = fixed4(color.rgb * atten, color.a);
  96. UNITY_APPLY_FOG(i.fogCoord, color);
  97. color = clamp(color, 0, 1);
  98. return color;
  99. }