oframe / ogl

Minimal WebGL Library
https://oframe.github.io/ogl/examples
3.77k stars 213 forks source link

Projecting mouse coordinates onto an infinite plane #203

Closed caracal7 closed 9 months ago

caracal7 commented 9 months ago

How to project the current mouse coordinates onto an infinite plane in space at arbitrary coordinates and angles and get the coordinates in 3D?

This is more related to 3d math in general than to the library, but it is a very useful feature for applications that allow the user to move objects in 3D.

It would be nice to have it at the library level or as an example.

P.S. Thanks for the great library!

caracal7 commented 9 months ago

Looked deep into the source code and found the answer. :)

  const mouse = new Vec2();
  const raycast = new Raycast();
  raycast.castMouse(camera, mouse);
  const answer = raycast.intersectPlane({
    origin: [0,0,0],
    normal: [0,1,0],
  });
  console.log(answer)