Shader refuses two UV offsets
I have a relatively simple shader. I want MainTex and OcclusionMap to use separate ScaleOffsets (set in the inspector), but for some reason, OcclusionMap uv is forced to 0,0 unless it uses the same UV as MainTex.
How i want it to be:
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; // Works
half ao = tex2D(_OcclusionMap, IN.uv_OcclusionMap); // Doesn't work
It's not an issue with uv_OcclusionMap, because if i swap them around, uv_MainTex stops working instead:
fixed4 c = tex2D (_MainTex, IN.uv_OcclusionMap) * _Color; // Works
half ao = tex2D(_OcclusionMap, IN.uv_MainTex); // Doesn't work
If they use the same uv, it works just fine:
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; // Works
half ao = tex2D(_OcclusionMap, IN.uv_MainTex); // Works
Doesn't matter which one they use:
fixed4 c = tex2D (_MainTex, IN.uv_OcclusionMap) * _Color; // Works
half ao = tex2D(_OcclusionMap, IN.uv_OcclusionMap); // Works
Full script:
Shader "Custom/OcclussionOnUv2" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_OcclusionMap("Occlusion", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _OcclusionMap;
struct Input {
float2 uv_MainTex;
float2 uv_OcclusionMap;
};
half _Glossiness, _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
half ao = tex2D(_OcclusionMap, IN.uv_OcclusionMap);
// Albedo comes from a texture tinted by color
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
// Occlusion comes from the second uv channel
o.Occlusion = ao;
}
ENDCG
}
FallBack "Diffuse"
}
Your shader works fine for me on 2018.1.0f2, both maps tile individually. You may need to update to a newer patch release, or maybe you have a duplicate shader file that has the same shader name as this one causing a conflict.
Also, when sampling a map into a single value variable, choose which channel from the texture map you want to avoid any potential compiler quirks giving you unexpected results. In this example, I'd do:
half ao = tex2D(_Occlusion$$anonymous$$ap, IN.uv_Occlusion$$anonymous$$ap).r;
Updating is not an option for me, as I need to support WebPlayer (IE). I tried the shader on 2017.2.0f3 without luck, but I need it to work on 5.3.8f2.
I have a hard time believing that something as see$$anonymous$$gly simple as this hasn't been possible up until 2018?
It's not that hasn't been possible until 2018, it should work perfectly fine in Unity 5, so something else is going on in your project, perhaps a shader conflict like I mentioned.
If you click on the actual shader file, does it show any shader errors/warning in the inspector?
Answer by Invertex · Jun 09, 2018 at 01:40 PM
In that Unity version, there's some weird issue with it not handling the o.Occlusion output correctly when in Forward rendering mode, but the shader works in Deferred. Sampling the texture clearly is fine, since if you assign it to Albedo it looks normal.
Here's a sort of hacky workaround I've come up with, go to your Unity install folder, go into Editor\Data\CGIncludes\ folder. Open up "UnityPBSLighting.cginc" and scroll down to "LightingStandard" on line 103, and then paste this in the first line of that function.
gi.indirect.specular *= s.Occlusion;
Save it, then right-click reimport your shader in Unity. The occlusion should now work correctly. Let me know if it has any weird visual issues, as this isn't how it's normally supposed to be modified.
Your answer
Follow this Question
Related Questions
How to Change the Shader Color? 0 Answers
Getting Color Generated from Shader to Script to Shader 0 Answers
Volumetric Light with VR in URP have a weird clipping... Any ideas? 0 Answers
How do i get a shader to affect every object with the same material? 0 Answers
why outline is drawed only on front side of skinned mesh render 1 Answer