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 Benproductions1 · Feb 01, 2013 at 12:30 PM · raycastmaterialdirectionskybox

Skyboxes, GetPixel and Raycast Directions

Hi there,

I'm currently making a raytraced renderer using recursive Raycasts and up till now, I have just been using the color black for a trace, that doesn't collide with anything.

So I was wondering how you reproduce unity's display of skyboxes, by just having a Vector3 direction and the skybox material.

Thank you, Benproductions1

If any more information is needed, just ask :)

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

Answer by Wolfram · Feb 01, 2013 at 02:51 PM

This is not tested, but I'd do it like this:

First you need to figure out which of your 6 skybox textures to sample. This is determined by the dominant axis of your direction:

 if(Mathf.Abs(dir.x)>Mathf.Abs(dir.y)){
     if(Mathf.Abs(dir.x)>Mathf.Abs(dir.z)){
         // x is dominant axis.
         if(dir.x<0){
             // -X is dominant axis -> left face
         }else{
             // +X is dominant axis -> right face
         }
     }else{
         // z is dominant axis.
         ... // similar
     }
 }else if(Mathf.Abs(dir.y)>Mathf.Abs(dir.z)){
     // y is dominant axis.
     ... // similar
 }else{
     // z is dominant axis.
     ... // similar
 }

If it's a cubemap, then you supply your dominant axis to the GetPixel() call. Otherwise, get your textures from the shader (mat.GetTexture("_FrontTex") etc.)

Once you have the face, you need to figure out which WC axes correspond to which texture axes. For example, if -X is your dominant direction, you "look" along -X, so +Y points up and +Z points right. Therefore +Z corresponds to texture.u, and -Y corresponds to texture.v (texture origin is always upper left).

Then you need to compute the orthogonal normalized texture coordinates. Start with a normalized [-1..1] space:

 texture.u=dir.z/(-dir.x); // for the -X example
 texture.v=-dir.y/(-dir.x); // for the -X example

You will now need to transform this to [0..1] space:

 texture.u=(texture.u+1)/2;
 texture.v=(texture.v+1)/2;

And finally to [0..texture.width|height] space:

 texture.u*=texture.width;
 texture.v*=texture.height;

These are your pixel coordinates, which you can feed to GetPixel().

Additional notes:

  • for Texture2D you can use GetPixelBilinear() to find interpolated pixel colors, AFAIK you can only point-sample Cubemaps at integer coordinates.

  • you will need to take care of correct orientation for the up/down textures, the axes might be rotated by 180 degrees or something.

Comment
Add comment · Show 9 · 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 Benproductions1 · Feb 01, 2013 at 03:03 PM 0
Share

Thank you! $$anonymous$$aking RayTracing is quite difficult :) Even though it might not explicitly work, it's definitely the right direction I'll come back once it's implemented and accept your answer

avatar image Benproductions1 · Feb 01, 2013 at 03:44 PM 0
Share

So I made a quick implementation, and I am definitely drawing the skybox per pixel, but with the wrong texture coords... return (skybox.GetTexture("_LeftTex") as Texture2D).GetPixelBilinear(direction.z/-direction.x, direction.y/-direction.x); Is the calculation I'm doing for the Left texture, similarly with the others. Did I understand your method wrong?

avatar image Wolfram · Feb 01, 2013 at 03:49 PM 0
Share

It seems you missed a "-" for the y-coordinate, but otherwise it should work.

For the other faces you'll have to flip axes around. For example, (-Z/X,-Y/X) for +X, (-X/-Z,-Y/-Z) for -Z, (X/Y,Z/Y) for +Y, and so on.

avatar image Benproductions1 · Feb 01, 2013 at 03:57 PM 0
Share

It seems as though me missing the $$anonymous$$us was correct... Adding in the "+1)/2" fixes it completely, giving the desired result...

 return (skybox.GetTexture("_LeftTex") as Texture2D).GetPixelBilinear((direction.z/-direction.x+1)/2, (direction.y/-direction.x+1)/2);

Thank you very much :)

avatar image Benproductions1 · Feb 01, 2013 at 04:27 PM 1
Share

For future reference, the axis flips are as follows: -X(-Z, +Y), +X(+Z, +Y), -Z(+X, +Y), +Z(-X, +Y), -Y(-X, Z), +Y(-X, -Z)

Show more comments

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

10 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

Related Questions

Add force to mouse position? 0 Answers

How can I raycast the direction my 2D character is facing? 1 Answer

Optimize script or Other way get material name (js) 2 Answers

Need help with third-person camera stuttering involving raycasts and clamp 0 Answers

change skybox via script help ? 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