I'm not sure what to search, so I haven't been able to find what I need.
Say I have a 3D triangle with points [0, 1, 1], [1, 0.5, 0.5], [0, 0, 0]
. I discard the Z component to create a 2D triangle with points [0, 1], [1, 0.5], [0, 0]
. (I think this is an orthographic projection?) Through an unimportant process I find some 2D point that lies within the 2D triangle, say [0.5, 0.5]
.
How do I take that 2D point and find what its Z value should be to have it lie on the plane formed by the original 3D triangle?
Answers (or dupe links!) that describe maths through code rather than mathematical symbols would be greatly appreciated; I struggle to read the types of answers you get on Math.SE.
You can use barycentric coordinates...
So you got 2D triangle q0,q1,q2
and corresponding 3D triangle p0,p1,p2
and want to convert 2D point q
into 3D point p
compute barycentric coordinates u,v
of q
within q0,q1,q2
convert u,v
to cartessian using triangle p0,p1,p2
So when put together:
| u | | (q1.x - q0.x) , (q2.x - q0.x) , q0.x | | q.x |
| v | = inverse | (q1.y - q0.y) , (q2.y - q0.y) , q0.y | * | q.y |
| 1 | | 0 , 0 , 1 | | 1 |
p.x = p0.x + (p1.x - p0.x) * u + (p2.x - p0.x) * v
p.y = p0.y + (p1.y - p0.y) * u + (p2.y - p0.y) * v
p.z = p0.z + (p1.z - p0.z) * u + (p2.z - p0.z) * v