Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Siccity · Jun 07, 2018 at 09:18 AM · shadershadersambient occlusion

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"
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Invertex · Jun 07, 2018 at 09:40 AM 0
Share

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;
avatar image Siccity Invertex · Jun 07, 2018 at 09:43 AM 0
Share

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?

avatar image Invertex Siccity · Jun 07, 2018 at 09:46 AM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Siccity · Jun 11, 2018 at 07:28 AM 0
Share

That seems to work just fine. Thanks! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

196 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges