LayaStandardcore.cginc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 
  2. #ifndef LAYA_GC_INCLUDE
  3. #define LAYA_GC_INCLUDE
  4. #if (NORMALTEXTURE || DIRLIGHTMAP_COMBINED || PARALLAXTEXTURE)
  5. #define _TANGENT_TO_WORLD 1
  6. #endif
  7. #if (_DETAIL_MULX2 || _DETAIL_MUL || _DETAIL_ADD || _DETAIL_LERP)
  8. #define _DETAIL 1
  9. #endif
  10. half4 _Color;
  11. half _Cutoff;
  12. sampler2D albedoTexture;
  13. float4 tilingOffset;
  14. // sampler2D _DetailAlbedoMap;
  15. // float4 _DetailAlbedoMap_ST;
  16. sampler2D normalTexture;
  17. half normalTextureScale;
  18. // sampler2D _DetailMask;
  19. // sampler2D _DetailNormalMap;
  20. // half _DetailNormalMapScale;
  21. sampler2D specularTexture;
  22. float4 specularColor;
  23. sampler2D metallicGlossTexture;
  24. half metallic;
  25. float smoothness;
  26. float smoothnessTextureScale;
  27. sampler2D occlusionTexture;
  28. half occlusionTextureStrength;
  29. sampler2D parallaxTexture;
  30. half parallaxTextureScale;
  31. half4 _EmissionColor;
  32. sampler2D _EmissionMap;
  33. #include "AutoLight.cginc"
  34. #include "Lighting.cginc"
  35. #include "UnityCG.cginc"
  36. #include "libs/LayaPBRStruct.cginc"
  37. #include "libs/LayaPBRUtil.cginc"
  38. #include "libs/LayaBRDF.cginc"
  39. #include "UnityShaderVariables.cginc"
  40. #include "UnityStandardBRDF.cginc"
  41. #if !defined (UNITY_BRDF_PBS)
  42. #if SHADER_TARGET < 30 || defined(SHADER_TARGET_SURFACE_ANALYSIS)
  43. #define UNITY_BRDF_PBS BRDF3_Unity_PBS
  44. #elif defined(UNITY_PBS_USE_BRDF3)
  45. #define UNITY_BRDF_PBS BRDF3_Unity_PBS
  46. #elif defined(UNITY_PBS_USE_BRDF2)
  47. #define UNITY_BRDF_PBS BRDF2_Unity_PBS
  48. #elif defined(UNITY_PBS_USE_BRDF1)
  49. #define UNITY_BRDF_PBS BRDF1_Unity_PBS
  50. #else
  51. #error something broke in auto-choosing BRDF
  52. #endif
  53. #endif
  54. VertexOutputForwardBase vertForwardBase(VertexInput v)
  55. {
  56. VertexOutputForwardBase o;
  57. o = (VertexOutputForwardBase)0;
  58. float4 posWorld = mul(unity_ObjectToWorld, v.vertex);
  59. o.tangentToWorldAndPackedData[0].w = posWorld.x;
  60. o.tangentToWorldAndPackedData[1].w = posWorld.y;
  61. o.tangentToWorldAndPackedData[2].w = posWorld.z;
  62. o.posWorld = posWorld.xyz;
  63. o.pos = UnityObjectToClipPos(v.vertex);
  64. o.eyeVec.xyz = LayaNormalizePerVertexNormal(posWorld.xyz - _WorldSpaceCameraPos);
  65. o.tex = layaTexCoords(v);
  66. float3 normalWorld = UnityObjectToWorldNormal(v.normal);
  67. #ifdef _TANGENT_TO_WORLD
  68. float4 tangentWorld = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w);
  69. float3x3 tangentToWorld = CreateTangentToWorldPerVertex(normalWorld, tangentWorld.xyz, tangentWorld.w);
  70. o.tangentToWorldAndPackedData[0].xyz = tangentToWorld[0];
  71. o.tangentToWorldAndPackedData[1].xyz = tangentToWorld[1];
  72. o.tangentToWorldAndPackedData[2].xyz = tangentToWorld[2];
  73. #else
  74. o.tangentToWorldAndPackedData[0].xyz = 0;
  75. o.tangentToWorldAndPackedData[1].xyz = 0;
  76. o.tangentToWorldAndPackedData[2].xyz = normalWorld;
  77. #endif
  78. UNITY_TRANSFER_LIGHTING(o, v.uv1);
  79. o.ambientOrLightmapUV = LayaVertexGIForward(v, posWorld, normalWorld);
  80. #ifdef PARALLAXTEXTURE
  81. TANGENT_SPACE_ROTATION;
  82. half3 viewDirForParallax = mul(rotation, ObjSpaceViewDir(v.vertex));
  83. o.tangentToWorldAndPackedData[0].w = viewDirForParallax.x;
  84. o.tangentToWorldAndPackedData[1].w = viewDirForParallax.y;
  85. o.tangentToWorldAndPackedData[2].w = viewDirForParallax.z;
  86. #endif
  87. UNITY_TRANSFER_FOG_COMBINED_WITH_EYE_VEC(o, o.pos);
  88. return o;
  89. }
  90. fixed4 fragForwardBaseInternal(VertexOutputForwardBase i) : SV_Target
  91. {
  92. LayaFragmentCommonData s = LayaFragmentSetup(i.tex, i.eyeVec.xyz, in_ViewDir4Parallax(i), i.tangentToWorldAndPackedData,i.posWorld);
  93. LayaLight mainLight;
  94. mainLight.color = _LightColor0.rgb;
  95. mainLight.dir = _WorldSpaceLightPos0.xyz;
  96. UNITY_LIGHT_ATTENUATION(atten, i, s.posWorld);
  97. half occlusion = LayaOcclusion(i.tex.xy);
  98. LayaGI gi = LayaFragmentGI(s, occlusion, i.ambientOrLightmapUV, atten, mainLight);
  99. half4 c;
  100. c = BRDF1_Laya_PBS(s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, s.normalWorld, -s.eyeVec, gi.light, gi.indirect);
  101. c.rgb += LayaEmission(i.tex.xy);
  102. float4 col;
  103. #if UNITY_TANGENT_ORTHONORMALIZE
  104. col = float4(1.0, 0.0, 0.0, 1.0);
  105. #else
  106. col = float4(0.0,1.0, 0.0, 1.0);
  107. #endif
  108. UNITY_EXTRACT_FOG_FROM_EYE_VEC(i);
  109. UNITY_APPLY_FOG(_unity_fogCoord, c.rgb);
  110. return LayaOutputForward(c, s.alpha);
  111. }
  112. #endif
  113. VertexOutputForwardAdd vertForwardAdd(VertexInput v)
  114. {
  115. VertexOutputForwardAdd o;
  116. o = (VertexOutputForwardAdd)0;
  117. float4 posWorld = mul(unity_ObjectToWorld, v.vertex);
  118. o.pos = UnityObjectToClipPos(v.vertex);
  119. o.tex = layaTexCoords(v);
  120. o.posWorld = posWorld.xyz;
  121. o.eyeVec.xyz = LayaNormalizePerVertexNormal(posWorld.xyz - _WorldSpaceCameraPos);
  122. float3 normalWorld = UnityObjectToWorldNormal(v.normal);
  123. #ifdef _TANGENT_TO_WORLD
  124. float4 tangentWorld = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w);
  125. float3x3 tangentToWorld = CreateTangentToWorldPerVertex(normalWorld, tangentWorld.xyz, tangentWorld.w);
  126. o.tangentToWorldAndLightDir[0].xyz = tangentToWorld[0];
  127. o.tangentToWorldAndLightDir[1].xyz = tangentToWorld[1];
  128. o.tangentToWorldAndLightDir[2].xyz = tangentToWorld[2];
  129. #else
  130. o.tangentToWorldAndLightDir[0].xyz = 0;
  131. o.tangentToWorldAndLightDir[1].xyz = 0;
  132. o.tangentToWorldAndLightDir[2].xyz = normalWorld;
  133. #endif
  134. UNITY_TRANSFER_LIGHTING(o, v.uv1);
  135. float3 lightDir = _WorldSpaceLightPos0.xyz - posWorld.xyz * _WorldSpaceLightPos0.w;
  136. #ifndef USING_DIRECTIONAL_LIGHT
  137. lightDir = LayaNormalizePerVertexNormal(lightDir);
  138. #endif
  139. o.tangentToWorldAndLightDir[0].w = lightDir.x;
  140. o.tangentToWorldAndLightDir[1].w = lightDir.y;
  141. o.tangentToWorldAndLightDir[2].w = lightDir.z;
  142. #ifdef PARALLAXTEXTURE
  143. TANGENT_SPACE_ROTATION;
  144. half3 viewDirForParallax = mul(rotation, ObjSpaceViewDir(v.vertex));
  145. #endif
  146. UNITY_TRANSFER_FOG_COMBINED_WITH_EYE_VEC(o, o.pos);
  147. return o;
  148. }
  149. half4 fragForwardAddInternal(VertexOutputForwardAdd i) : SV_Target
  150. {
  151. LayaFragmentCommonData s = LayaFragmentSetup(i.tex, i.eyeVec.xyz, in_ViewDir4Parallax_Fwdadd(i), i.tangentToWorldAndLightDir,i.posWorld);
  152. LayaLight light;
  153. light.color = _LightColor0.rgb;
  154. light.dir = half3(i.tangentToWorldAndLightDir[0].w, i.tangentToWorldAndLightDir[1].w, i.tangentToWorldAndLightDir[2].w);
  155. UNITY_LIGHT_ATTENUATION(atten, i, s.posWorld)
  156. light.color *= atten;
  157. light.dir = normalize(light.dir);
  158. LayaIndirect noIndirect;
  159. noIndirect.diffuse = 0;
  160. noIndirect.specular = 0;
  161. half4 c = BRDF1_Laya_PBS(s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, s.normalWorld, -s.eyeVec, light, noIndirect);
  162. UNITY_EXTRACT_FOG_FROM_EYE_VEC(i);
  163. UNITY_APPLY_FOG_COLOR(i.fogCoord, c.rgb, half4(0, 0, 0, 0));
  164. return LayaOutputForward(c, s.alpha);
  165. }