• 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 jchristian · Jan 16, 2012 at 07:44 PM · shaderpositionvertex

How do you get the world position of a vertex in a shader?

I am trying to get the world position of a vertex into my fragment shader but the _Object2World translation doesn't appear to be working. Is there something I need to do in scripting such that the _Object2World matrix is updated?

Below is my simplified shader. We are using the 3.4.2f3 version of unity. My scene for this test contains:

plane pos: 0,0,100 rotation: 270,0,0 scale: 5,5,5

cube pos: 0,0,20 rotation: 0,0,0 scale: 1,1,1

camera pos: 0,0,0 rotation: 0,0,0 scale: 1,1,1

The result of this shader when applied to the scene is always red. I would expect to see green because the wpos.z should be in world coordinates and the vpos.z would still be in object coordinates. Note that I did see a similar question("How to get the global position of a vertex in a Cg vertex shader?") and have tried to make my vertex shader do exactly the same thing as the response to that question.

Help!

Shader "DepthEffect" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { ZTest Always Cull Off ZWrite Off Fog { Mode off }

         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #pragma fragmentoption ARB_precision_hint_fastest 
         #include "UnityCG.cginc"

         sampler2D _MainTex;
     
         struct v2f 
         {
             float4 pos  : POSITION;
             float2 uv   : TEXCOORD0;
             float3 wpos : TEXCOORD1;
             float3 vpos : TEXCOORD2;
         };
         
         v2f vert( appdata_img v )
         {
             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             float3 worldPos = mul (_Object2World, v.vertex).xyz;
             o.wpos = worldPos;
             o.vpos = v.vertex.xyz;
             o.uv =  v.texcoord.xy;
             return o;
         }
         
         float4 frag (v2f i) : COLOR
         {
             if (i.vpos.z == i.wpos.z)
                 return float4(1,0,0,1);
                 else
                 return float4(0,1,0,1);
         }
         ENDCG
     }
 } 
 FallBack "Diffuse"

}

Comment
imapler
shavais
yoyo
felixpk
mkusan
Propagant

People who like this

6 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 imapler · Apr 23, 2012 at 03:00 PM 0
Share

i have this very problem but when i look in the UnityCG.cginc they comput the worldspace pos several time using code like float3 worldPos = mul(_Object2World, v).xyz; which is exacly what you do

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by yoyo · Nov 30, 2013 at 03:02 AM

_Object2World worked fine for me. Looking at your code, I'm guessing the computed vpos.z and wpos.z are never exactly equal. You should instead check that they are close (subtract, square the difference, compare to a small number).

Comment
Seneral

People who like this

1 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

6 People are following this question.

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

Related Questions

How to get the screen position in an unlit shader. 1 Answer

Shader : issue with vertex positions when multiple models are on the scene 1 Answer

Shader vertex position depends on camera ? 1 Answer

Shader Vertex Position During Animation 1 Answer

How to get the position of a vertex in a particles-shader? 0 Answers


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