Giter VIP home page Giter VIP logo

screen-to-world's Introduction

screen-to-world

The code in this repo shows how you, using a screen coordinate (mouse location) as input, can read out a depth buffer value from a 3d scene rendered in WebGL/three.js and then use the coordinate and depth information to calculate a 3d world coordinate for that point.

method

To get hold of depth buffer values, we do this:

  • render the scene into a render target (a buffer) that stores the depth buffer values
  • render a plane, using the depth texture as a color texture on a flat plane, into a buffer
  • read out pixel values from that buffer -> this is our depth buffer values

WebGL does not support reading out depth texture values directly. That is the reason for the extra render stages.

To transform from depth buffer values to world coordinate values (essentially: running the 3D pipeline in reverse), we do this:

  • convert the screen coordinates and depth buffer values into NDC coordinates
  • convert the NDC coordinates into view space coordinates (transforming by inverse projection matrix)
  • convert from 4d homogeneous view space cordinates to normal 3d coordinates
  • convert the view space coordinates into world coordinates (transforming by inverse view matrix)

The code has been optimized to give high accuracy in the calculations - but the resulting accuracy will depend on the z buffer resolution available for the hardware and the intermediate render buffers.

sources

screen-to-world's People

Contributors

kristoffer-dyrkorn avatar

Stargazers

某个大叔 avatar

Watchers

 avatar

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.