Problem with surface shader using uv2
Hey I'm writing a surface shader that needs to use UV2 on a model, however the texture doesn't appear to be using the uvs at all when I apply them to the model.
Shader "Custom/shaderEnvironment" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
[Toggle(TOGGLE)] _DetailToggle ("Enable Primary Detail", Float) = 0
_DetailTex ("Primary Detail (RGB)", 2D) = "white" {}
[Toggle(TOGGLE2)] _Detail2Toggle ("Enable Secondary Detail", Float) = 0
_Detail2Tex ("Secondary Detail (RGB)", 2D) = "white" {}
[Toggle(TOGGLE3)] _Detail3Toggle ("Enable Micro Detail", Float) = 0
_Detail3Tex ("Micro Detail (RGB)", 2D) = "white" {}
_Lightmaps ("Lightmaps", 2D) = "black" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
#pragma shader_feature TOGGLE
#pragma shader_feature TOGGLE2
#pragma shader_feature TOGGLE3
sampler2D _MainTex, _DetailTex, _Detail2Tex, _Detail3Tex, _Lightmaps;
struct Input {
float2 uv_MainTex;
float2 uv2_LightmapTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
UNITY_INSTANCING_CBUFFER_START(Props)
UNITY_INSTANCING_CBUFFER_END
half4 detailDouble(half4 albedo, half4 detail)
{
#ifdef TOGGLE
return ((albedo.a)*(albedo*detail)*2)+(albedo*(1-albedo.a));
#else
return albedo;
#endif
}
half4 detail2Double(half4 albedo, half4 detail)
{
#ifdef TOGGLE2
return ((1-albedo.a)*((albedo*detail)*2))+(albedo*albedo.a);
#else
return albedo;
#endif
}
half4 detail3Double(half4 albedo, half4 detail)
{
#ifdef TOGGLE3
return (albedo*detail)*2;
#else
return albedo;
#endif
}
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
fixed4 d = tex2D (_DetailTex, IN.uv_MainTex*100);
fixed4 d2 = tex2D (_Detail2Tex, IN.uv_MainTex*30);
fixed4 d3 = tex2D (_Detail3Tex, IN.uv_MainTex*12);
fixed4 lm = tex2D (_Lightmaps, IN.uv2_LightmapTex);
c = detailDouble(c,d);
c = detail2Double(c,d2);
c = detail3Double(c, d3);
o.Emission = lm.rgb;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Does anyone know what the issue is? Thank you.
Answer by Pangamini · Jan 28, 2018 at 01:02 PM
Well that's pretty simple, you are not using the uv2 information anywhere. IN.uv_MainTex contains the main uv transformed by the texture scale/offset. You need to write your own vertex shader, read the actual uv2 channel information from the vertex and pass that into the surface shader.
Also, try to avoid calculating UVs in the pixel shader (surface shader is just a part of it), I am referring to IN.uv_MainTex*30
. It's far more erfficient to have the final UV calculated in vertex and having it interpolated, both because you avoid the multiplication per-pixel, but more important, the GPU can send those UVs to texture sampling units before the pixel shader even starts (at least that's what I believe, though I can't find any reference to back this statement)