Code Monkey home page Code Monkey logo

scenemagnifier's Introduction

Off-axis projection demo: Scene Magnifier

banner

A tiny Unity demo showcasing how a custom off-axis projection matrix can be used to build a "scene magnifier". You can scroll to zoom, LMB drag to pan the view, just like an image viewer, but with loseless image and infinite zoom level (like a vector graphics). Different from adjusting camera rotation and FOV angle, using a off-axis projection matrix to zoom in will not change the "perspective relationship" of the original shot (like on the top-left image).

This method can be extented to build a display array (like on a stage), with each screen being rendered by different computer using the same scene, but with different projection matrix (this custom projection matrix can also be used to cull objects!), to average the computational load.

This method can also be used to make a super-resolution screenshot tool, by dividing the screen into several smaller grids and use the custom projection matrices to capture them individually (Nvidia Ansel already did that!). This approach avoids hitting GPU texture size limit, which is 16384x16384 on dx11 and dx12, when allocating huge render targets.


The core code is so simple and self-explanatory, I can just paste them here:

// range from (-1, -1) to (1, 1) inclusive, with (-1, -1) being screen's lower-left, and (1, 1) being screen's upper-right
void SetMagnifyRegion(float xmin, float ymin, float xmax, float ymax)
{
    // Retrive camera properties
    float n = camera.nearClipPlane;
    float f = camera.farClipPlane;
    float fovy = camera.fieldOfView; // in degrees, uses verticle fov (fovy)
    float aspect = camera.aspect; // width / height

    // Calculate l, r, b, t
    float ogT = Mathf.Tan(fovy / 2f * Mathf.Deg2Rad) * n;
    float ogR = ogT * aspect;

    float l = ogR * xmin;
    float r = ogR * xmax;
    float b = ogT * ymin;
    float t = ogT * ymax;

    Matrix4x4 persp = Matrix4x4.Frustum(l, r, b, t, n, f);
    camera.projectionMatrix = persp;
    camera.cullingMatrix = persp * camera.worldToCameraMatrix;
}

scenemagnifier's People

Contributors

gavinkg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

hengle

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.