LayaPBRStandardFB.cginc 3.3 KB

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