Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by AgapitoTori · Jul 24, 2019 at 11:28 PM · shadershadersshader programmingcgmatrix

UnityObjectToClipPos is inverted?

I upgraded untiy and an old shader and one of the core cg functions used by unity got its values inverted. To illustrate I assigned one of the values to the red channel on a test shader:


alt text


The code is the same on both versions, the offending line/function is:

 o.vertex = UnityObjectToClipPos(v.vertex);


This is apparently expected behaviour in newer versions, but it breaks the shader. A solution would be to somehow invert the return values of UnityObjectToClipPos, but I have no idea on how to do it, I haven't found a similar function for it and the documentation seems sparse, any help deeply appreciated!



UPDATE: I managed to solve the original issue by just finding a newer version of the shader I was using. My situation was unlikely but here are the details if anybody has the same issue in the future.

The shader is the FXWater4Simple from the standard assets, I was using the 5.4 and didn't think they would actually update it. Now I have the 2018.1.9f2 version of the shader (last time they updated the standard assets apparently) and everything seems fine.

In regards to the original question, there was another line in the code which was important:

 o.pos = UnityObjectToClipPos(v.vertex);
 
 o.screenPos = ComputeScreenPos(o.pos);

which now is:

 o.pos = UnityObjectToClipPos(v.vertex);
 
 o.screenPos = ComputeNonStereoScreenPos(o.pos);


That's the only change I've noticed in the code. The problem is still a mystery to me because now I can't seem to replicate the issue by reverting the code, which spells doom and makes me believe maybe the problem is something else and it will eventually come back.

1.png (27.8 kB)
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 Namey5 · Jul 26, 2019 at 12:05 PM 1
Share

UnityObjectToClipPos transforms mesh vertices from object space to clip space such that they can be displayed on screen. To invert the line you've provided would flip the entire mesh. The reason it may be inverted is because some platforms (and some rendering systems) have different definitions for clip space; on some the z-axis goes from [-1,1] and on others it goes from [0,1]. Likewise, the y-axis is also flipped in some cases. Could you post where and how you actually use clip space coordinates? If you really need it to be consistent, you can do the raw multiplication yourself;

 o.vertex = mul (UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, float4 (o.vertex.xyz, 1.0));
avatar image AgapitoTori Namey5 · Jul 26, 2019 at 03:22 PM 0
Share

Thanks for the explanation, the original shader I was using is the FXWater4Simple from the standard assets, I just managed to find a solution to the problem, though my original question is still a mystery to me, I've updated the original post with details if you're interested. Otherwise here's the test shader I was using on the screenshots:

 Shader "Test/$$anonymous$$VPTest"
 {
     Properties
     {
         _$$anonymous$$ainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
     //Tags { "Queue" = "Transparent"}
     //Blend SrcAlpha One$$anonymous$$inusSrcAlpha
         // No culling or depth
         Cull Off ZWrite Off ZTest Always
 
         Pass
         {
             CGPROGRA$$anonymous$$
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
                 float4 screenPos : TEXCOORD1;
             };
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
 
                 o.uv = v.uv;
                 return o;
             }
 
             sampler2D _$$anonymous$$ainTex;
 
             fixed4 frag (v2f i) : SV_Target
             {
                 fixed4 col = tex2D(_$$anonymous$$ainTex,i.uv) ;
                 col = float4(i.vertex.y,1.0f,1.0f,1.0f)/500 ;
 
                 return col;
             }
             ENDCG
         }
     }
 }
 
avatar image Bunny83 · Jul 26, 2019 at 02:16 PM 1
Share

I'm not sure if this issue still exists, but if this is about image effect shaders you may want to have a look at my answer over here and read the comments below the answer. Unity has some strange flipping going on in certain edge cases.


Your second image looks actually right since clipspace, normalized device coordinates and screenspace usually goes from bottom to top. That's true for almost all rendering contexts. Usually the only exception is GUI which most of the time has it's origin at the top left and goes downwards.

avatar image AgapitoTori Bunny83 · Jul 26, 2019 at 03:29 PM 0
Share

Hey thank you, I just managed to sort the original issue out though, updated the post with details. The original is a water shader, which I'm not sure how similar they are to image effects in regards to working with cameras.

0 Replies

· Add your reply
  • Sort: 

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

184 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

Related Questions

Dissolve Shader Problem 1 Answer

Triplanar shaders that use the position and rotation of the attached object (or local Triplanar shaders) 0 Answers

Flat Shading / No Vertex Interpolation 2 Answers

Unlit Transparent With Color Support 1 Answer

create a texture with shader code? ( without using a texture ) 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