Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 EmmetOT · Dec 12, 2019 at 10:17 AM · shadershadersmaterialsshader programmingsphere

Shader for distorting a texture as if it were on a sphere

I'm trying to add 3D spheres with simple textures to my UI. Currently I'm doing this using cameras and render textures but this is not very performant. I'm thinking there must be a way to take a flat texture and a set of euler angles and produce a distortion which makes the texture appear as though it's on a rotated sphere.

Note that in the attached image, my examples are on an actual sphere. However in the UI it'd probably just be projected onto a square sprite which I then add a circular mask to.

I'm already somewhat capable with simple shaders but I'm not sure what to look into to start with this one. I'd appreciate if someone could point me in the right direction. Thanks.

alt text

eyedistort.png (60.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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Dec 13, 2019 at 03:59 AM

Well, if you actually talk about just displaying a single quad in screen space with a special shader that actually shows a sphere with a texture mapped onto it, this is possible but not that trivial. You essentially do a manual ray-tracing in the fragment shader. You just need to specify all relevant information from outside. Also I barely use the gameobject based UI system so I'm not sure if you can actually use a custom material / shader for your element. However instead of normal UI element you could also use a normal Quad. I currently do not have the time to modify my raytrace shader I've written some time ago. I used it to render a to-scale earth sphere.


Since using an actual sphere mesh is always imprecise I created this raytrace shader. Together with the shader I used this script to draw a fullscreen overlay quad procedurally which essentially overlayed the raytraced earth sphere in the main camera view. It also updates the relevant shader variables like the position of the earth sphere and the rotation. Of course you wouldn't need most of that if you have a fix view and position of your virtual sphere. Note that I generate the usual UV-sphere texture coordinates in the shader. So the usual cylindrical mapping is used. Which means the top and bottom of the texture will be mapped to the north / south pole and have the greatest distortion in the mapping.

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 Bunny83 · Dec 13, 2019 at 04:00 AM 0
Share

$$anonymous$$aybe if I find the time in the next days I can try to simplify the shader / script to suit your needs.

avatar image EmmetOT · Dec 13, 2019 at 10:07 AM 0
Share

This is pretty fascinating. I have to say I thought ray tracing might be a bit overkill for what I'm looking for. I don't actually need the sphere to have shadowing or anything, I just need to sample a texture in such a way that it becomes distorted as though it were the surface of a sphere facing the camera. (Sort of an 'inverse mercator projection' or something.)

This is very cool, though! I'm curious is there a way to rotate the resulting sphere on its pivot? If yes, and this works in UI, then it may be exactly what I need.

EDIT: I was able to get it rotating by multiplying the local matrix passed in by a rotation matrix. Pretty straightforward.

EDIT 2: So if I add this to a UI image, -something- does show up, a tiny sliver of the sphere which appears and disappears see$$anonymous$$gly at random. Worked very nicely in scene view on a quad, though.

avatar image
0

Answer by TomazTreven · Dec 12, 2019 at 11:38 AM

For moving texture around in shader you can manipulate uv's in shader, for morphing quad into sphere you can change position of vertices in vertex shader.. but such huge modification is probably not going to work well.. maybe it can be checked what exactly is causing performance issues in current solution

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 EmmetOT · Dec 12, 2019 at 02:42 PM 0
Share

I don't want to move any vertices or just move a texture around, I want to make a flat 2D texture appear like it's a rotating sphere, while still just being a flat image on a texture.

"maybe it can be checked what exactly is causing performance issues in current solution "

Doing that is what led me here. :)

avatar image
0

Answer by WheresMommy · Dec 12, 2019 at 01:01 PM

Why not take the offset of yozr texture and scale and rotate always pointing to center?

Comment
Add comment · Show 3 · 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 EmmetOT · Dec 12, 2019 at 02:40 PM 0
Share

That wouldn't produce any distortion. I want it to appear as though it's a rotating sphere.

avatar image WheresMommy · Dec 12, 2019 at 02:56 PM 0
Share

It will distort with the scale. So moving up and down would distort on y axis, left right on x

avatar image EmmetOT WheresMommy · Dec 12, 2019 at 03:01 PM 0
Share

Scaling the y axis will squish the iris uniformly, like an oval, but if you look at the second example, the correct distortion would squash the bottom half more and appear to bring the pupil and iris closer together along the bottom.

In addition, I'm only using an eye as an example. I would like this to work for any texture.

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

197 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 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

See backside of a transparent emissive shader? 0 Answers

Turning standard materials from opaque to transparent and back in runtime 1 Answer

How to set shader vertex normals to face up 1 Answer

EnableKeyword("_EMISSION") not working 2019.3.13f1 0 Answers

Silhouette overlay 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