• 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 Tudor · Sep 22, 2015 at 04:22 PM · unity 5shadercgopenglmatrix4x4

Difference between pos.z and pos.w?

I'd like some clarification on what's stored in the builtin float4's at different stages.

What value does the .w parameter of a vertex Position hold in the stages prior to the Projection stage? Is it 1? (is it at all useful in the vertex program of a unity CG shader?)

From theory I know that if you want to do a perspective divide on your view frustum in your clip / projection stage, you need to divide the current position by the inverse of its distance from camera. Which in (viewspace and) clipspace should be 1/ the .z component (and same as 1/ depth), no?

But in opengl books and articles I see it's always divided by .w, not by .z.

Is .w actually == 1/.z and is it only there for homogenous space coordinates reasons (and also to save you having to do an extra 1/ division?)? I'm asking because you have to turn .z into 1/.z (and then back) anyway in order to keep it linear during interpolation (and OpenGL does that automatically between vert and frag), so I'm a bit confused.

As well I see in unity's shader variables that _ProjectionParams.w == 1/FarPlane and ProjectionParams.z == FarPlane.

Does ProjectionParamscontain the camera's near and far clip plane positions in worldspace if you access it in the vert program, vs it being in normalized device coordinates in frag?

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 _Gkxd · Sep 22, 2015 at 07:37 PM 1
Share

3D points are stored as 4D vectors so that homogeneous transforms can be used. The 4-vector (x,y,z,1) represents the point (x,y,z).

Points are passed into the vertex shaders as (x,y,z,1), which is then multiplied at some point by the perspective projection matrix:

alt text

The resulting point then goes through the perspective divide, and so on.

ProjectionParams is just a structure that allows you to obtain certain properties about the current view. Think of it as four separate variables that are stored together instead of a point in 3D space.

projection-matrix.png (10.9 kB)

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.

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

31 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

Related Questions

Unity5 CG Shader get model position. 1 Answer

Unity 5 toon shaders don't work in Windows standalone build 3 Answers

CG compiled to GLSL: hyperbolic functions not found. 0 Answers

Unity5 v Unity4 Shader array errors 1 Answer

Efficient code for shortest distance inside a fragment shader 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