- Home /
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,
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!
@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).
Yeah it's barebones, just to show the math that came to $$anonymous$$d. Using local space for control is strongly advised.
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!
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104845im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Material.mainTextureOffset,...how do I use this? 1 Answer
Fix a GUI's position relative to a 3D object 1 Answer
Getting rid of Scrolling Uv Jitters 0 Answers
Assigning UV Map to model at runtime 0 Answers
Diagonal Raycast - Origin problem 1 Answer