half4 SpecularGloss(float2 uv) { half4 sg; #ifdef _SPECGLOSSMAP #if defined(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A) sg.rgb = tex2D(_SpecGlossMap, uv).rgb; sg.a = tex2D(_MainTex, uv).a; #else sg = tex2D(_SpecGlossMap, uv); #endif sg.a *= _GlossMapScale; #else sg.rgb = _SpecColor.rgb; #ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A sg.a = tex2D(_MainTex, uv).a * _GlossMapScale; #else sg.a = _Glossiness; #endif #endif return sg; } half2 MetallicGloss(float2 uv) { half2 mg; #ifdef _METALLICGLOSSMAP #ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A mg.r = tex2D(_MetallicGlossMap, uv).r; mg.g = tex2D(_MainTex, uv).a; #else mg = tex2D(_MetallicGlossMap, uv).ra; #endif mg.g *= _GlossMapScale; #else mg.r = _Metallic; #ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A mg.g = tex2D(_MainTex, uv).a * _GlossMapScale; #else mg.g = _Glossiness; #endif #endif return mg; } half2 MetallicRough(float2 uv) { half2 mg; #ifdef _METALLICGLOSSMAP mg.r = tex2D(_MetallicGlossMap, uv).r; #else mg.r = _Metallic; #endif #ifdef _SPECGLOSSMAP mg.g = 1.0f - tex2D(_SpecGlossMap, uv).r; #else mg.g = 1.0f - _Glossiness; #endif return mg; }
inline FragmentCommonData SpecularSetup (float4 i_tex) { half4 specGloss = SpecularGloss(i_tex.xy); half3 specColor = specGloss.rgb; half smoothness = specGloss.a; half oneMinusReflectivity; half3 diffColor = EnergyConservationBetweenDiffuseAndSpecular (Albedo(i_tex), specColor, /*out*/ oneMinusReflectivity); FragmentCommonData o = (FragmentCommonData)0; o.diffColor = diffColor; o.specColor = specColor; o.oneMinusReflectivity = oneMinusReflectivity; o.smoothness = smoothness; return o; } inline FragmentCommonData RoughnessSetup(float4 i_tex) { half2 metallicGloss = MetallicRough(i_tex.xy); half metallic = metallicGloss.x; half smoothness = metallicGloss.y; // this is 1 minus the square root of real roughness m. half oneMinusReflectivity; half3 specColor; half3 diffColor = DiffuseAndSpecularFromMetallic(Albedo(i_tex), metallic, /*out*/ specColor, /*out*/ oneMinusReflectivity); FragmentCommonData o = (FragmentCommonData)0; o.diffColor = diffColor; o.specColor = specColor; o.oneMinusReflectivity = oneMinusReflectivity; o.smoothness = smoothness; return o; } inline FragmentCommonData MetallicSetup (float4 i_tex) { half2 metallicGloss = MetallicGloss(i_tex.xy); half metallic = metallicGloss.x; half smoothness = metallicGloss.y; // this is 1 minus the square root of real roughness m. half oneMinusReflectivity; half3 specColor; half3 diffColor = DiffuseAndSpecularFromMetallic (Albedo(i_tex), metallic, /*out*/ specColor, /*out*/ oneMinusReflectivity); FragmentCommonData o = (FragmentCommonData)0; o.diffColor = diffColor; o.specColor = specColor; o.oneMinusReflectivity = oneMinusReflectivity; o.smoothness = smoothness; return o; }