Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 fritzlloydw · Aug 02, 2021 at 03:23 PM · shader programmingshaderlabimage effectscoordinate-system

How to get vertex world positions in fragment shader

I am trying to recreate this shader:

https://youtu.be/4QOcCGI6xOU?t=280

However, I am having trouble as early as 4:40, when I am trying to draw a box to my view with an ImageEffectShader.

My shader looks like this: [code=CSharp] v2f vert (appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.pos); o.uv = v.uv; return o; }

         float2 rayBoxDist (float3 boundsMin, float3 boundsMax, float3 rayOrigin, float3 rayDir)
         {
             float3 t0 = (boundsMin - rayOrigin) / rayDir;
             float3 t1 = (boundsMax - rayOrigin) / rayDir;
             float3 tmin = min(t0, t1);
             float3 tmax = max(t0, t1);

             float distA = max(max(tmin.x, tmin.y), tmin.z);
             float distB = min(tmax.x, min(tmax.x, tmax.y));

             float distToBox = max(0, distA);
             float distInsideBox = max(0, distB - distToBox);
             return float2(distToBox, distInsideBox);
         }

         float3 BoundsMin;
         float3 BoundsMax;
         float3 CameraPos;

         sampler2D _MainTex;

         fixed4 frag (v2f i) : SV_Target
         {
             fixed4 col = tex2D(_MainTex, i.uv);


             float3 rayOrigin = CameraPos;

             float3 worldPos = mul(unity_ObjectToWorld, float4(i.pos.xyz, 1)).xyz;
             float3 rayDir = normalize(worldPos - CameraPos);

             float2 rayBoxInfo = rayBoxDist(BoundsMin, BoundsMax, rayOrigin, rayDir);

             float distToBox = rayBoxInfo.x;
             float distInsideBox = rayBoxInfo.y;

             bool rayBoxHit = distInsideBox > 0;
             if (rayBoxHit) {
                 return col;
             }

             return float4(0,0,0,1);
         }[/code]

The shader renders an all-black image, indicating that no ray has hit the box.

I have already confirmed that rayBoxDist works with this line of code: [code=CSharp]float2 rayBoxInfo = rayBoxDist(float3(0,0,0), float3(2,2,2), float3(1,1.9,1), 1 / float3(-1,0,0));[/code] Which returns the expected rayBoxInfo.

I assume that I am somehow confusing different coordinate systems, but I don't know where my error is.

If I get the world space position of a vertex in the shader, I should be able to infer the view direction, so how can I get the position of a vertex in world space?

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
1
Best Answer

Answer by Bunny83 · Aug 02, 2021 at 04:25 PM

Your vertex shader already transforms your object space position into clip space. So your fragment shader does not get the object space vector so transforming the clip space vector again through the object to world matrix makes no sense ^^. You need to also pass along either the object space vertex position or better, calculate the worldspace position of the vertex in the vertex shader and pass the worldspace position along. For this you need to add the worldPos to your "v2f" struct so you can carry it over from your vertex to fragment shader.


However since this ia an image effect shader, the object or worldspace position of the fullscreen quad that is actually rendered with this shader it pretty much irrelevant.


There are so many differences between the shader code in the video and your own. So it's hard to follow what's actually happening. In the original code he calculates the actual view / ray direction from the uv coordinates on the fullscreen quad which he "unprojects" into camera space by using the inverse projection matrix and then convert the camera space direction vector into a worldspace direction through the camera to world matrix. You're doing something completely different here.

Comment
Add comment · Show 1 · 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 Bunny83 · Aug 02, 2021 at 04:36 PM 0
Share

Note over here I posted a raycast shader that sampled from a flat skybox texture. While the sampling of the texture is irrelevant, I essentially used a similar approach to get the world space view vector which is required to do the raycasting.

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

128 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

Related Questions

Can ShaderLab be configured to use EXT_shader_texture_lod instead of ARB_shader_texture_lod on iOS? 1 Answer

Flat Shading / No Vertex Interpolation 2 Answers

Shader Graph: Objects have no shadows opposite the Main Light 1 Answer

Shader inverts uv map only once, how to fix it? 0 Answers

Custom Shader in URP 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