Giter Site home page Giter Site logo

alexandergugel / from-3d-to-2d Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hughsk/from-3d-to-2d

0.0 3.0 0.0 78 KB

Take a transformation matrix like you're used to constructing with WebGL, and project arbitrary 3D points onto your 2D screen.

License: Other

JavaScript 100.00%

from-3d-to-2d's Introduction

from-3d-to-2d stable

Take a transformation matrix like you're used to constructing with WebGL, and project arbitrary 3D points onto your 2D screen.

You can use this to take a point in your WebGL scene and get its onscreen position on the canvas, e.g. to add DOM element overlays, or bootstrapping your own canvas/SVG.

Usage

NPM

transform(out, position, matrix)

Given a 3-element array position and a 16-element array matrix, update the out array to contain your new 2D points โ€“ each of them ranging from 0 to

  1. For example:
var mat4       = require('gl-matrix').mat4
var camera     = require('orbit-camera')()
var transform  = require('from-3d-to-2d')

var projection = new Float32Array(16)
var view       = new Float32Array(16)
var pvMatrix   = new Float32Array(16)
var point      = new Float32Array(2)

function render() {
  // get your camera/view matrix from something like orbit-camera
  camera.view(view)

  // perspective projection
  mat4.perspective(projection
    , Math.PI / 4                  // field of view
    , canvas.width / canvas.height // aspect ratio
    , 0.00001
    , 10000
  )

  // Combine your matrix transformations into
  // a single transform by multiplying them together
  mat4.mul(pvMatrix, projection, view)

  // get your 2d point from your 3d point:
  transform(point, [0, 0, 0], pvMatrix)
  var x = point[0]
  var y = point[1]
}

See Also

License

MIT. See LICENSE.md for details.

from-3d-to-2d's People

Contributors

hughsk avatar

Watchers

 avatar  avatar  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.