Search Unity

[Req] An asset that handles 3D Perspective Tilt?

Discussion in 'Assets and Asset Store' started by outtoplay, Jun 29, 2015.

  1. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Something similar to to how the IOS 8 screen has depth and tilts.


    Been looking for something in the Asset store that might handle this type of camera effect for scenes (3d or 2D or either). Thanks.
     
  2. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Hi. This should be quite achievable - there is a little work to be done to make the perspective adjustments look correct, and whether you need to track the user (probably not necessary, you can use default positions). Would even be nice to have a 3D background and this would give a sense of a "deep" phone. Will examine it this week.
     
    outtoplay likes this.
  3. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    That would be cool. I've seen it recently employed in some comic book creation app, the name escapes me. Here is a older example of a game on a Nintendo handheld. I think the camera feeds off the gyroscope.

     
  4. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Yes. I co-authored a patent that does a similar thing but uses head motion to determine a perspective(
    Visual presentation system - AU2010295239). In this case its more about just using the screen space directionality (will work fine). I'll look at building some tests this week.

    Just for some extra info if others want to try it out. Its essentially the modification of the projection matrix from being a symetric matrix to an asymetric one. This is because the project volume effectively gets "squashed". Hopefully we can modify Unity's cameras to be able to do this.
     
    outtoplay likes this.
  5. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Here's a sample - its not perfect but is a start for people to use. Save as "DeepView.cs" and attach to your camera.

    // Set an off-center projection, where perspective's vanishing
    // point is not necessarily in the center of the screen.
    //
    // left/right/top/bottom define near plane size, i.e.
    // how offset are corners of camera's near plane.
    // Tweak the values and you can see camera's frustum change.

    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof(Camera))]
    public class DeepView : MonoBehaviour {

    // Original dimenstions for the output of the view area
    private float left = -2F;
    private float right = 2F;
    private float top = 2F;
    private float bottom = -2F;

    // Use the modifier to accentuate the movement of the posx and posy.
    public float modifier = 1.0f;
    // Whether to apply an angle (based on orientation of phone)
    public bool applyAngle = false;

    // Camera that is being used for the rendering
    private Camera cam;
    // Original position and offset. This may need to be updated.
    private Vector3 opos;
    private Vector3 odir;
    private Vector3 oright;
    private Vector3 oup;

    // Position of the mouse offset - this could also be orientation
    private static float posx = 0.0f;
    private static float posy = 0.0f;

    // Initialise the data
    void Start() {
    cam = this.gameObject.GetComponent<Camera> ();
    opos = transform.position;
    odir = transform.forward;
    oright = transform.right;
    oup = transform.up;

    left = cam.rect.width * -0.5f * cam.aspect;
    right = cam.rect.width * 0.5f * cam.aspect;
    top = cam.rect.height * 0.5f;
    bottom = cam.rect.height * -0.5f;
    }

    void LateUpdate() {

    // Attach the movement to the mouse movement
    posx = (right - left) * Input.mousePosition.x / (float)Screen.width + left;
    posy = (top - bottom) * Input.mousePosition.y / (float)Screen.height + bottom;

    // Move the position of the camera itself
    transform.position = opos + oright * posx * modifier + oup * posy * modifier;

    // This is a fixed view point direction - can be changed to target a distant object
    if (applyAngle == false)
    transform.LookAt(transform.position + odir * cam.nearClipPlane);
    else {
    transform.LookAt(opos + odir * cam.nearClipPlane);
    }

    Matrix4x4 m = PerspectiveOffCenter((left + posx * modifier),
    (right + posx * modifier),
    (bottom + posy * modifier),
    (top + posy * modifier),
    cam.nearClipPlane, cam.farClipPlane);
    cam.projectionMatrix = m;
    //cam.transform.position = opos + cam.transform.right * posx + cam.transform.up * posy;
    }

    // Taken from the Unity Off Center Perspective code here:
    // http://docs.unity3d.com/ScriptReference/Camera-projectionMatrix.html
    static Matrix4x4 PerspectiveOffCenter(float left, float right, float bottom, float top, float near, float far) {
    float x = 2.0F * near / (right - left);
    float y = 2.0F * near / (top - bottom);
    float a = 0.0f; //(right + left) / (right - left);
    float b = 0.0f; //(top + bottom) / (top - bottom);
    float c = -(far + near) / (far - near);
    float d = -(2.0F * far * near) / (far - near);
    float e = -1.0F;
    Matrix4x4 m = new Matrix4x4();
    m[0, 0] = x;
    m[0, 1] = 0;
    m[0, 2] = a;
    m[0, 3] = 0;
    m[1, 0] = 0;
    m[1, 1] = y;
    m[1, 2] = b;
    m[1, 3] = 0;
    m[2, 0] = 0;
    m[2, 1] = 0;
    m[2, 2] = c;
    m[2, 3] = d;
    m[3, 0] = 0;
    m[3, 1] = 0;
    m[3, 2] = e;
    m[3, 3] = 0;
    return m;
    }
    }
     
  6. schmosef

    schmosef

    Joined:
    Mar 6, 2012
    Posts:
    852
    True Parallax

    I bought this a while ago. It works on iOS, Android and Windows Store.

    The actual script driving it is very simple but it was cheaper to buy than spend an hour of my time to develop and test.
     
  7. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    The above component will work on any platform - just hook the Input to whatever you want (visual tilt, last pressed positions, face positions, etc). As mentioned this is mostly from the Unity Camera documents with some minor changes to allow for specific types of camera orientation (either rotational, or oblique).
     
  8. schmosef

    schmosef

    Joined:
    Mar 6, 2012
    Posts:
    852
    Thanks for the deepview.cs code. I'm going to check it out.
     
  9. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    I'll put some videos up on how to use it too - will work best with similar backgrounds as shown above. I did one with an open background (3d) and it looks ok, but you need to accentuate the movement to make it a little more interesting :)
    I'll also build a sample specifically for mobile (using the orientation sensor) so people can see how to use it.
     
  10. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Thanks David. Stoked to try this. Will post the results! Have a great day. :)
     
  11. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Sample in editor
     
    schmosef likes this.
  12. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Another Sample
     
  13. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Looks terrific! Great work, David.