• 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 /
avatar image
Question by ben_unity878 · Oct 13, 2022 at 09:55 PM · shaderrenderingnormalsuv2

Normals in TEXCOORDS2 messed up, unless object inverted

I'm working on an outline shader what will be used as a URP render feature.

I'm doing this with the normal extrude method, where for the outline pass, all the vertices are shifted along their world normal. This works as expected, but I want to use custom, smoothed normals stored in a uv channel.

To sanity test the method, I am just passing the unmodified normals into TEXCOORD2. Code for this is:

 void Start()
 {
   var mf = GetComponent<MeshFilter>();
   var mesh = mf.mesh;
   mesh.SetUVs(2, mesh.normals);
 }

and shader is:

 Shader "Outlines/BackFaceOutlines" {
   Properties{
       _Thickness("Thickness", Float) = 1 // The amount to extrude the outline mesh
       _Color("Color", Color) = (1, 1, 1, 1) // The outline color
       // If enabled, this shader will use "smoothed" normals stored in TEXCOORD1 to extrude along
       [Toggle(USE_PRECALCULATED_OUTLINE_NORMALS)]_PrecalculateNormals("Use UV1 normals", Float) = 0
   }
 
   SubShader{
     Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
 
     Pass {
       Name "Outline"
       // Cull front faces
       Cull Front
       
       HLSLPROGRAM
       // Standard URP requirements
       #pragma prefer_hlslcc gles
       #pragma exclude_renderers d3d11_9x
 
       // Register our material keywords
       #pragma shader_feature USE_PRECALCULATED_OUTLINE_NORMALS
 
       // Register our functions
       #pragma vertex Vertex
       #pragma fragment Fragment
 
       // Include helper functions from URP
       #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
 
       // Data from the meshes
       struct Attributes {
         float4 positionOS       : POSITION; // Position in object space
         float3 normalOS         : NORMAL; // Normal vector in object space
       #ifdef USE_PRECALCULATED_OUTLINE_NORMALS
         float3 smoothNormalOS   : TEXCOORD2; // Calculated "smooth" normals to extrude along in object space
       #endif
       };
 
       // Output from the vertex function and input to the fragment function
       struct VertexOutput {
         float4 positionCS   : SV_POSITION; // Position in clip space
       };
 
       // Properties
       float _Thickness;
       float4 _Color;
 
       VertexOutput Vertex(Attributes input) {
         VertexOutput output;
 
         float3 normalOS;
       #ifdef USE_PRECALCULATED_OUTLINE_NORMALS
         normalOS = input.smoothNormalOS;
       #else
         normalOS = input.normalOS;
       #endif
 
         float4 worldPos = mul(unity_ObjectToWorld, input.positionOS);
         float4 worldNorm = mul(unity_ObjectToWorld, float4(normalOS, 0));
 
         worldPos += normalize(worldNorm) * _Thickness;
 
         // Convert this position to world and clip space
         output.positionCS = mul(UNITY_MATRIX_VP, worldPos);
 
         return output;
       }
 
       float4 Fragment(VertexOutput input) : SV_Target {
         return _Color;
       }
 
       ENDHLSL
     }
   }
 }

This is based on this example, btw

Using the normal channel, everything works as expected: alt text

But using TEXCOORD2: alt text

Now this very much confuses me, as surely the values should be the same; I'm copying the unmodified normal array to the uv array. All the handling of the values in the shader is the same, so somehow the values must be getting mangled in the uv array? Is something else targeting this channel?

Most confusing though, when the object is inverted (an odd number of axis' scale is negative), it works perfectly again. I'm out of attachments for this post, but trust me, it's outlining as expected

Any ideas on what is going on? How to fix this?

unnamed.png (100.3 kB)
2022-10-13-14-46-51-haven-day-android-unity-202030.png (132.0 kB)
Comment

People who like this

0 Show 1
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 ben_unity878 · Oct 13, 2022 at 09:57 PM 0
Share

Inverted, working example alt text

URP Renderer: alt text

2022-10-13-14-50-18-haven-day-android-unity-202030.png (151.5 kB)
2022-10-13-14-55-56-haven-microsoft-visual-studio.png (68.9 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by ben_unity878 · Oct 14, 2022 at 03:32 PM

After about a full day of fighting this, I found the solution!! And it was a single checkbox o.o Turning off Dynamic Batching in the URP settings does the trick. Something in there was hooking into the outline draw calls, and batching where it shouldn't. Seems like a Unity bug, or maybe something I don't understand

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

232 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 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

Surface Shader World Normal 0 Answers

Seam on Procedural Mesh 1 Answer

Is a shader with selective passes faster than a single-pass which uses lerp to enable/disable shader effects? 0 Answers

Offset the result of camera scissor rect 0 Answers

Shade Based on Normal Map World Position Angle (Updated) 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