开发者

Is there way to get 3D coords on some plane for 2D mouse coords in Flash with Away3D?

Waht is the right way to get 3D coords from 2D mouse coords using Away3D. (version 3.6.0) It's ambigoius problem in general, so there is restriction that 3D point belongs to some fixed plane.

There are some examples with camera.unproje开发者_运维问答ct and plane.getIntersectionLineNumbers methods, but they don't work if camera is rotated or plane is not trivial.


I'm pretty sure Away3D has this implemented in some nice functions, but here's how I'd do it. I'll assume you mean an orthographic projection from the camera "film" to the world space (no idea how that is implemented).

You're projecting a ray from the camera view plane, so you're basically looking to find the intersection of the ray and the plane. The ray begins at the camera's "eye" and passes through the view plane of the camera.

So the 3D point of intersection between the two would then be:

Point getIntersection(Ray ray) {
  float k, t;
  Point point;
  Vector toPoint;

  k = ray.direction.dot(this.normal);

  if (k != 0.0) {
    t = (this.position - ray.origin).dot(this.normal) / k;
  } else {
    return false;
  }

  if (t < 0.0000001) {
    return false;
  }

  return ray.origin + ray.direction * t;
}

I used similar code in my raytracer, so it should work.


You'll have to first obtain a "ray" that the mouse click in effect produces into the 3D space. Then you find the point you want by calculating the intersection of that ray and your surface (some mathematics here). In my case, I had to get the point on a sphere where a user clicked.

To get the "ray":

var vector:Vector3D = new Vector3D(mX/camera.zoom, -mY/camera.zoom, camera.focus);
vector = camera.transform.matrix3D.deltaTransformVector(vector);

Note the "camera" object above.

If you want to see my implementation with the sphere, check out https://github.com/SabinT/Earth3D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜