Search Unity

Scale object to match specific depth from camera

Discussion in 'Scripting' started by samloeschen, Mar 7, 2013.

  1. samloeschen

    samloeschen

    Joined:
    Mar 22, 2010
    Posts:
    268
    Hey there. So, I'm working on a faked glow effect for my game, using massive textured alpha faded planes behind objects that I want to glow instead of using one of the camera filters. My game is a top-down space shooter, so this works out pretty nicely. Each object in space big enough to have a glow gets a childed object which is a large plane with a circular gradient on it. The problem that arises is that since each glow plane is behind their parent object, the FOV causes the center, most opaque part of the gradient to be offset from it's parent object due to parallax.

    I've been working on a script that scales the glow planes to the size that they would be at the depth of their parent object, and I've been basing it on a Valve developer wiki page that talks about how to find the relationship between the FOV and the size of an object at different distances.

    This is the equation from that page:
    ObjectScreenWidth = ScreenResolution * ObjectWidth / (ObjectDistance * (2 tan (FOV/2)))

    I'm not that great at trig, so I think I'm just supremely screwing up the math. The logic seems right..

    Here is my code:

    Code (csharp):
    1. var glowScale : float;
    2. var glowColor : Color;
    3. var glowDepth : float;
    4. var glowInstance : GameObject;
    5. var glowObjectPrefab : GameObject;
    6. private var cam : Camera;
    7. private var hFov : float;
    8. private var scaleRatio : float;
    9.  
    10.  
    11. function Start () {
    12.     //get camera
    13.     cam = Camera.main;
    14.     //get horizontal field of view
    15.     var radAng : float = cam.fieldOfView * Mathf.Deg2Rad;
    16.     var radHFOV : float = 2 * Mathf.Atan(Mathf.Tan(radAng / 2) * cam.aspect);
    17.     hFov = radHFOV * Mathf.Rad2Deg;
    18.     //get object scale ratio for scale from camera by distance
    19.     scaleRatio = 1/(2*(Mathf.Tan(hFov/2)));
    20.    
    21.     //set up plane
    22.     glowInstance = GameObject.Instantiate(glowObjectPrefab, new Vector3(transform.position.x, -glowDepth, transform.position.z), Quaternion.identity);
    23.     glowInstance.transform.localScale = Vector3.one * glowScale;
    24.     glowInstance.renderer.material.color = glowColor;
    25.  
    26. }
    27.  
    28. function Update () {
    29.  
    30.     //update glow scaling
    31.     var objDist : float = (cam.transform.position - transform.position).magnitude;
    32.     var glowDist : float = (cam.transform.position - glowInstance.transform.position).magnitude;
    33.     var objFrameScale : float = objDist / scaleRatio;
    34.     var glowFrameScale : float = glowDist / scaleRatio;
    35.     var scaleDiff : float = objFrameScale - glowFrameScale;
    36.     glowInstance.transform.localScale = Vector3.one * (glowScale * scaleDiff);
    37.    
    38.    
    39. }
    40.  
    As of now, scaleRatio ends up as a negative number (which according to the equation on the Valve page, it shouldn't be) and I'm getting ridiculous scales for the size of the plane.

    Thanks so much in advance for any input you can give me on what I'm doing wrong here...really scratching my head
     
    Last edited: Mar 7, 2013