- Home /
Scale guiTexture with respect to camera distance
Id like my GUI Texture that is clamped to the 3d space of a gameobject, to scale with respect to the distance of the camera and that gameObject.
I cant seem to get my math right however. Farther distances should scale it smaller, and vice versa.
Any ideas?
if you want to just fake perspective effect, why don't you just use a plane that always looks at camera?
Thats my current implementation, but it decreases my frame rate dramatically when there are a lot of these in the scene at once, I was experimenting with the guitextures ins$$anonymous$$d and for whatever reason I dont get that framerate drop using them ins$$anonymous$$d.
As an alternate solution, look at implementing a texture atlas for the textures on your plane.
Answer by gharbill · May 07, 2013 at 03:21 PM
Ok so in that case this seems to work:
// Put this Script on your GUITexture
// assign myCamera and myObject and you're good to go
var myCamera : Transform;
var myObject: Transform;
private var viewPos: Vector3;
private var ratio : float;
private var distance : float;
var distanceFactor : float = 1000; //factor that needs to be configured
//to achieve desired result
//with field of view = 60, value of 1000
//seems to be a good number.
function Start () {
ratio = guiTexture.pixelInset.height/guiTexture.pixelInset.width; // fixes the ratio of the texture
}
function Update () {
distance = (myCamera.position - myObject.position).magnitude;
viewPos = myCamera.camera.WorldToScreenPoint(myObject.position);
guiTexture.pixelInset.width = (1/distance)*distanceFactor;
guiTexture.pixelInset.height = ratio * guiTexture.pixelInset.width;
//to keep the pivot of texture in center
guiTexture.pixelInset.x = -(guiTexture.pixelInset.width /2) + viewPos.x;
guiTexture.pixelInset.y = -(guiTexture.pixelInset.height /2) + viewPos.y;
}
sure you have to manipulate it to make it work better hope it helps...
I edited the code, now it will follow the object but the Texture is also drawn if you have 180 degree angle from it.
Just remember to zero out position of your GUITexture ;)
Your answer
Follow this Question
Related Questions
Setting a cube to be exactly size of intersecting camera view plane 1 Answer
How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer
Transform.LookAt - wrong vector 4 Answers
Some GameObjects can't touch !!! 0 Answers
Why is my Camera rotation tilted after parenting it to head? 1 Answer