I am writing a simple JavaScript engine from scratch using Canvas and box as primitives (ร la Minecraft), damn it. I implement as many optimizations as I can, and still I have a back face, culling and hidden occlusion of the face, but I canโt figure out how to make a truncation that selects the optimal path.
I tried two-dimensional sampling, but I have one specific problem that I can not solve: if one single vertex from a 4-point plane goes behind the camera, it is still drawn, but completely distorted (the x and y coordinates are reversed I think) - see. Image.
I'm starting to think that this does not have a real solution without using more complex mathematical and rendering sequences.


I tried to limit the x and y vertices to the coordinates inside the 2D screen if at least one of the 4 vertices is still inside the screen (see below), but this completely distorts the square face (although I think I can go with more fancy math and additional triangles).

I have some experience with OpenGL and it does things in a completely different way, so this is not even a problem.
Do I have a chance to fix this without pulling my hair?
Decision
The final solution was to check each of the 8 vertices with respect to the clipping plane in 3D before performing a 2D projection and then clipping the screen.
This is the second clipping step, the first of which is to check whether the box is completely beyond the clipping plane using the bounding sphere of radius sqrt(3/2)*boxSideLength .
Additional triangles (actually points in this case) were too complex and mathematically intense, this solution is not perfect, but pretty beautiful.
source share