Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 Kourosh · Feb 17, 2011 at 11:33 AM · uvcoordinatesoffsetworld

Translate World transform data to UV offset

Imagine a box moving on top of a plane. Now i'm looking for a way to position a glow right at the box position inside the plane texture. I can not find a way to translate the box's world position into the UV offset.

Cheers,

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Alec-Slayden · Feb 17, 2011 at 02:12 PM

I should first suggest the possibility of using a light instead of UV tracking, which can be set to only affect certain objects, as a child of the object you're tracking.

That being said, I was curious about the UV as well and here's what I've found:

I fiddled around a bit, and I was able to produce a very simple UV tracking of an object with a plane and landscape brought in from cinema 4d. If your object has tidy and even UVs (like most planes) and you know its length and width (if not, a close approximation by moving a game object to its extents and calculating the size that way will do), you can get a basic UV track with fairly simple code. I used the following script on my plane / landscape object:

var target : Transform; // tracked object

var myWidth : float; // X size var myLength : float; // Z size

function Update () {

 var OffsetX = (target.position.x - transform.position.x) / myWidth;
 var OffsetZ = (target.position.z - transform.position.z) / myLength;

 renderer.material.SetTextureOffset ("_MainTex", Vector2(OffsetX,OffsetZ));



}

The math is based on the idea that an offset of 1 or -1 is where the texture has moved all the way around to where it first began. at 0.5, it's half way off the edge, and halfway onto the opposite side, and the same goes for -0.5

For the formulas we get the position of the target z or x, relative to the plane's. 10 units past the plane's position will return 10, etc.

We divide this by the plane's size (x or z), so that once the object has moved a distance beyond the axis equal to the size, the value is 1. This means if the object distance is half of the plane length, where the object would be on the edge if the plane's axis is centered, the resulting value is 0.5 (which as we recall is the offset where the texture would be at the edge too).

Again this is a very very basic setup, and fundamentally presumes a few things:

1) The plane UVs are tidy If the UVs are different sizes and distorted, there will be more globbing and distortion in the movement and shape of the texture. This may be desirable in some cases, but probably not yours

2) The plane is horizontal The above code presumes the plane is horizontal and the object is moving along the X and Z world coordinates. If your plane is going to be rotated, the code will have to be modified for local coordinates, and the objects will need proper nesting.

3) The plane axis is centered (x and z) The code uses the object's world x and z position relative to the plane's x and z position. an off-center axis for the plane might not actually cause any problems, but my kneejerk reaction is to believe it would. If your plane's axis isn't centered, you might have to adjust the code to include a 'dummy axis', which could be an empty game object as a child of the plane, which you reference for the plane's position instead of the plane itself.

You would probably also want to provide a fade point, where the texture disappears when the object reaches a certain position, unless there is no chance the object will leave the plane. This is because the formula will keep 'tracking' the object after it has left the plane, the offset causing it to act as though the object appeared on the other side, ad infinitum.

I made a unitypackage of the scene if you'd like me to email it.

I hope this was useful, somehow!

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 Jesse Anders · Feb 17, 2011 at 02:42 PM 0
Share

@The OP: Just for clarity's sake, the above basically implements what I described in my post (although without taking into account the transform of the plane or the possibility of the plane being centered in local space).

avatar image Alec-Slayden · Feb 17, 2011 at 03:09 PM 0
Share

Yeah it's barebones, just to show the math that came to $$anonymous$$d. Using local space for control is strongly advised.

avatar image Kourosh · Feb 17, 2011 at 09:59 PM 0
Share

Very helpful Alec, it helped me in calculating the relation between the 2 coordinate systems, at first I thought it's hard to achieve accuracy using this method. It simply worked. I also found the equation to consider the textureScale (tiling) in the calculation.

thanks!

avatar image
2

Answer by Jesse Anders · Feb 17, 2011 at 01:05 PM

I can not find a way to translate the box's world position into the UV offset.

Just to address this question, if you know the parameters of the quad (transform, extents, etc.) and the position of the box, and if you know the UV coordinates associated with the quad, you can compute the UV coordinates corresponding to the box by transforming the box position into the local space of the quad and computing the UV coordinates using a simple linear mapping.

If you need more detail than that, perhaps you could comment or edit your post and describe what information you have available to work with.

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 Kourosh · Feb 17, 2011 at 09:52 PM 0
Share

Thanks Jesse, that worked!

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

No one has followed this question yet.

Related Questions

Transform's position wherever the mouse is pointing. 2 Answers

Transform.Translate() coordinates confusion 2 Answers

Updating world coordinate according to world points 1 Answer

Remove annoying texture offset on custom mesh 0 Answers

Wrong uv coordinates in Unity? (but works fine with Blender) 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