• 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
2
Question by ModLunar · Dec 18, 2017 at 04:33 AM · shadersgraphics

Shaders: What is clip space?

If you're a shader writer, writing vertex and fragment programs with Unity, you'll be familiar with t$$anonymous$$s: we use t$$anonymous$$s line a lot in the vertex program:

o.vertex = UnityObjectToClipPos(v.vertex);

In theory I understand it -- a transformation of the vertex from its local space finally into the clip space for the camera using the MVP matrix. But what are the conventions of clip space? Is the bottom left of the rendering screen (0, 0) in (x, y). increasing to (1, 1) at the top right corner? And what are the z and w values in clip space, and what range do each of those go to?

Comment
Add comment · Show 2
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 indevious · Dec 18, 2017 at 06:34 PM 0
Share

Check out the Clip Coordinates section in the (old) manual.

avatar image ModLunar indevious · Dec 18, 2017 at 07:10 PM 0
Share

Hey, thanks yeah they actually still have that in the manual now, at https://docs.unity3d.com/Manual/SL-PlatformDifferences.html.

Unfortunately though it doesn't tell me what the x, y, or w components mean in clip space. Do you happen to know? (That page does help me a lot with the z component though!)

1 Reply

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

Answer by insominx · Dec 18, 2017 at 06:52 PM

To start, clip space is often conflated with NDC (normalized device coordinates) and there is a subtle difference: NDC are formed by dividing the clip space coordinates by w (also known as perspective divide). NDC boundaries are "normalized" and therefore always consistently bound.

The conversion from clip space to NDC happens after the vertex shader is run and is done automatically for you. Whatever you put in your w component will be used to divide your xyz components. You output your clip space position as (x, y, z, w) in the vertex shader and the grap$$anonymous$$cs API converts it to (x/w, y/w, z/w, 1).

Regardless of platform, the NDC for left/right/top/bottom bounds will be the same: the range of the x-coordinate from [l, r] to [-1, 1], the y-coordinate from [b, t] to [-1, 1]

Unfortunately, the depth coordinate will differ based on whether you are in an OpenGL-like platform or a Direct3D-like one.

Direct3D-like: The clip space depth goes from 0.0 at the near plane to +1.0 at the far plane. T$$anonymous$$s applies to Direct3D, Metal and consoles.

OpenGL-like: The clip space depth goes from –1.0 at the near plane to +1.0 at the far plane. T$$anonymous$$s applies to OpenGL and OpenGL ES.

Also note: from wit$$anonymous$$n the shader you can use the UNITY_NEAR_CLIP_VALUE built-in macro to get the near plane value based on the platform.

Comment
Add comment · Show 2 · 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 ModLunar · Dec 18, 2017 at 10:38 PM 1
Share

Ohh wow, thank you for explaining this!

So this whole time I've been calling it the clip space when I access i.vertex in the fragment shader, but that's actually NDC by that time? Then now I'm wondering, if that's NDC -- (-1, -1) bottom left to (1, 1) at the top right in terms of (x, y), and that deal with the depth coordinate on Direct3D vs. OpenGL-like platforms, then is there a region in clip space in terms of the (x, y, z, w) coordinates that means something in clip space? (before the perspective divide)

Or do we usually not really care about that? Sorry if this question is silly or vague lol xD

avatar image insominx ModLunar · Dec 19, 2017 at 12:57 AM 1
Share

Clip space would be a scaled version of NDC which is generally not helpful. You can easily just calculate the NDC in the vertex shader if you want it, just divide by w yourself.

 float3 ndcPosition = o.vertex.xyz / o.vertex.w

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

90 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

Related Questions

How to use Graphics.DrawProcedural to render a quad over a scene? 0 Answers

How to change textures over time? 0 Answers

How can I pan a texture to specific location on a sheet? 1 Answer

How to get a plane to blend in with intersections,How to get blend a edge of a plane to wall 0 Answers

Send vertice shader changes to surface 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