Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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 /
  • Help Room /
avatar image
0
Question by comomomo · May 18 at 07:45 AM · shadermaterialgraphicsmathrect

How would you go about panning and zooming like Google Maps using SetTextureOffset & SetTextureScale ?

Hi, I might be approaching this the wrong way but I've been really stuck on this. It totally caught me off guard because it seems like a simple problem:

Essentially I'm trying to implement controls like Google Maps to my project where the user would be able to move an image underneath their cursor by clicking and dragging. In addition the user would be able to zoom in on the image by scrolling. The image would then scale from the position of the mouse without the thing underneath it moving (that's 100% the hardest part). All of this should work using only texture offsets to a material rather than using a camera or transforms. (using _MainTex_ST via SetTextureScale & SetTextureOffset).

Does anyone have ideas for how to solve this?




So far I've only been able to get approximations of the effect. You can test them here: https://github.com/thnewlands/PanningZoomingMapWithUVs

Approximation 1: I've gotten it to sort of work by using a custom shader (which is ultimately not how I want to solve the problem). But the camera wiggles around when it zooms in. alt text

This code in a vertex shader will scale the UVs around the pivot position. In this case: _Pivot = the mouse position in view space, _Zoom = a float2 I increase w/ scroll, _Offset = a float2 I add a (lastMousePos - mousePos) to in a loop.

 o.uv -= _Pivot;
 o.uv *= _Zoom;
 o.uv += _Pivot;
 o.uv += _Offset;



Approximation 2: I tried initially to solve this problem using Rects but found the strategy to be super cumbersome. It ended up introducing a ton of error when I would zoom in and out. It did show me that this should be possible without using any specialized shaders though.

 canvasOffset.size += scaleDelta; //This scales from the top left corner -- the corner now needs to be offset
 canvasOffset.position -= pivot * scaleDelta;// Scale from mouse position
 canvasOffset.position += positionDelta;
 panningMaterial.SetTextureOffset("_MainTex", canvasOffset.position);
 panningMaterial.SetTextureScale("_MainTex", canvasOffset.size);




I also have this posted on reddit here: https://www.reddit.com/r/Unity3D/comments/usj8lk/how_would_you_go_about_panning_and_zooming_like/


Here are a list of resources I've looked closely at to try to answer this question:

  • https://gamedev.net/forums/topic/594055-zooming-onto-an-arbitrary-point/

  • https://github.com/AzizSayfutdinov/VirtualOverlapRecognition/blob/1aa03674c7083d62c2fe7a1af00c03151ea6bc5b/unity/oldProject/VirtualOverlapRecognition/Assets/MRTK/Examples/Demos/EyeTracking/DemoScrollPanZoom/Scripts/BaseClasses/PanZoomBaseTexture.cs#L162

  • https://www.reddit.com/r/GraphicsProgramming/comments/qkf19z/trying_to_set_up_pinch_panzoomrotate_in_2d_on_a/

  • https://math.stackexchange.com/questions/5805/scaling-at-an-arbitrary-point-and-figuring-out-the-distance-from-origin

  • https://github.com/KDE/krita/blob/0b24bfa0a7c77019dfadd9e76ecbcca0f1ccbc14/libs/widgets/KoZoomController.cpp#L194

wiggle.gif (306.8 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 comomomo · May 18 at 10:08 PM

I think I found the solution!! My rect approach turned out to be the correct one. You can see it in action in the github repository here: https://github.com/thnewlands/PanningZoomingMapWithUVs



 canvasOffset.size += scaleDelta * Vector2.one;//This scales from the top left corner -- the corner now needs to be offset
 canvasOffset.position -= mousePos * scaleDelta;// Scale from mouse position
 canvasOffset.position += positionDelta * canvasOffset.size;
 panningMaterial.SetTextureOffset("_MainTex", canvasOffset.position);
 panningMaterial.SetTextureScale("_MainTex", canvasOffset.size);
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

284 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Is there any way to make a custom written shader with hdrp? 0 Answers

How to change the render mode of a material in runtime? 0 Answers

Make realtime shader movement appear at intervals but update as if in realtime? 0 Answers

Is it possible to blend 2 different materials via the shader graph? 0 Answers

Outline Shader working in editor but not in-game 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