Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Madeck · Oct 29, 2014 at 06:52 PM · shaderpositionglslworld spaceabsolute

Get absolute position of vertex in shader

I am trying to clip my object based on an absolute plane or distance, however I am unable to get a value that is not directly tied to the camera position and rotation.

here’s basically what I want to achieve, but it should remain the same when camera moves around it.

clipping example

To see what happens when the camera rotates, here’s an animated version:

clipping example animatedt

I thought this would be a rather simple thing to do, but shaders are still strange for me.

Here’s some of my experiments with non-working code:

 void vert (inout appdata_full v, out Input o) {
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 }
 
 void surf (Input IN, inout SurfaceOutput o) {
     half4 c = tex2D (_MainTex, IN.uv_MainTex);
     o.Albedo = c.rgb;
     o.Alpha = c.a;
 
     float3 origin = mul(_Object2World, float4(0.0,0.0,0.0,1.0)).xyz;
     float3 cameraDir = mul((float3x3)UNITY_MATRIX_V,float3(0,0,1));
     float3 worldPos = mul(UNITY_MATRIX_T_MV, IN.pos);

     // HERE IS WHAT I CANNOT FIGURE OUT
     // I have tried combining several things such as UNITY_MATRIX_IT_MV and such, to no avail
     //o.Alpha = distance(_WorldSpaceCameraPos + cameraDir -IN.pos - origin, float3(0,0,0)) > 240;
     //o.Alpha = distance(worldPos, float3(0,0,0)) < 120;
     o.Alpha = distance(IN.pos, _WorldSpaceCameraPos) < 240;
 
     o.Albedo = float3(0.5,0.5,0.5);
 
     clip( o.Alpha - 0.5);
 
     o.Albedo.r = IN.normal.x + _Object2World;
     o.Albedo.g = IN.normal.y+sin(IN.normal.z);
     o.Albedo.b = 1-distance(IN.pos.xyz, _WorldSpaceCameraPos)/240;
 }

From what I understand, I would have to revert the projection or view matrix, but I don’t understand how to do that.

Thanks in advance!

Comment
Add comment
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

1 Reply

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

Answer by Madeck · Feb 24, 2015 at 08:29 PM

Figured it out. The definition of worldPos is absolute:

 struct Input {
     float2 uv_MainTex;
 
     // This does the magic
     float3 worldPos;
 };
 
 void surf (Input IN, inout SurfaceOutput o) {
      o.Albedo = IN.worldPos; // this is an absolute position.
  }
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Fog shader 2 Answers

Absolute Window Position of Unity Application 0 Answers

converting HLSL to GLSL. 0 Answers

How to access the lights properties from within a shader? 1 Answer

Shader error: Shader program of type 'vp' already exists at line .... 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