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 Townsie · May 03, 2013 at 07:20 PM · camerashaderraycast

Rotate a ray to camera direction, in a CG shader

I've got a shader which draws a ray-casted sphere. Here's a snippet:

 // A basic ray.
 struct ray {
     float3 o; // Point of origin
     fixed3 d; // Direction
 };
 
 // Raytrace sphere
 float3 calcPixelColor(float2 pixel, float2 resolution) {
     const float fov = 2.5;
     
     // Pixel in screen space
     float2 p = (-resolution + 2.0 * pixel) / resolution.y;
     
     // Create a ray
     ray r;
     r.o = _WorldSpaceCameraPos;
     
     float3 ww = float3(0.0, 0.0, 1.0);
     fixed3 uu = normalize(cross(fixed3(0.0, -1.0, 0.0), ww));
     fixed3 vv = normalize(cross(ww, uu));
     
     // Calculate ray direction
     fixed3 er = normalize(float3(p.xy, fov));
     r.d = normalize(er.x * uu + er.y * vv + er.z * ww);
     
     // The color of a sphere, or background
     float3 col = intersectSphere(r);
     
     return col;
 }

It emulates a camera pointed into the scene: [0, 0, 1].

But it gives me kind of a weird result.

If I set up my virtual camera in the shader at [0,0,-5], looking down the z-axis like this:

 r.o = float3(0, 0, -5);
 fixed3 ww = fixed3(0, 0, 1);

My shader renders a proper sphere:

alt text

And if I feed in the Unity camera's position, like so:

 r.o = _WorldSpaceCameraPos;

Everything is fine. But I can't seem to apply the camera's orientation to the direction vector ww. I try this:

 fixed3 ww = normalize(float3(0, 0, 1) * UNITY_MATRIX_IT_MV);

But all I get is a blank blue background unless I move the camera really close to [0,0,0]. Then I see part of the sphere, but if I rotate the camera, nothing happens.

screen shot 2013-05-05 at 5.39.21 pm.png (61.0 kB)
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

Answer by whydoidoit · May 03, 2013 at 10:08 PM

You use UNITY_MATRIX_IT_MV to rotate a direction rather than a point into view space.

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 Townsie · May 05, 2013 at 03:53 PM 0
Share

I've updated my question, after trying your suggestion to using the inverse transpose matrix. But I can't seem to get it right.

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

13 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

Related Questions

Ojbect with Unity's default Toon Light/outlined Shader drawing black when seen from inside? 0 Answers

3rd Person Camera collision is jerky when zooming out 1 Answer

Shader based camera compositing. 2 Answers

Dividng clip space by "w" warps the output? 1 Answer

How to achieve diablo style walls? 4 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