[Hard] Need help with getting idea of how I can process invisible points correctly (Writing custom rasterizer)

Hi guys. I have decided to make my own rasterizer. Cuz Roblox is simple, I have decided to start from it.

Let’s firstly look at this scene:
image
Let’s try to render this part. Not baseplate.
So we will have 2 main objects: Camera and Part.
Part has 12 triangles and 8 vertices. Problem is making correct caluclations.
Just getting position of point on screen isn’t hard with some math (code below), but problem is processing points which are:

  1. Part of visible triangle
  2. Off-screen
  3. Behind camera.
local ScreenDistance = 0.001
local ObserveRange = 70*2
local Scale = ScreenSize.X / (2 * ScreenDistance * math.tan(ObserveRange / 2))
local function Vector3OnScreen(Position)
	local Relative = Camera.CFrame:Inverse() * Position
	Relative = Vector3.new(Relative.X, Relative.Y, -Relative.Z)
	--[[if Relative.Z < ScreenDistance then
		return nil
	end]]
	local Delta = ScreenDistance / math.abs(Relative.Z) * Scale
	local Projection = Vector2.new(Relative.X, Relative.Y) * Delta
	local Screen = Projection + Vector2.new(ScreenSize.X / 2, -ScreenSize.Y / 2)
	local ScreenCoords = Vector2.new(math.floor(Screen.X+0.5), math.floor(-Screen.Y+0.5))
	--if ScreenCoords.X >= 0 and ScreenCoords.X < ScreenSize.X and ScreenCoords.Y >= 0 and ScreenCoords.Y < ScreenSize.Y then
		return ScreenCoords, Relative.Z
	--end
	--return nil
end

This code calculates the 2D position of object on screen.
But firstly, let’s talk WHY I commented some lines. And what they should do.
1st block of code used to filter points which are behind camera.
2nd block used to filter points off-screen

But this method which I found on internet is just completely wrong! How otherwise you will be able to process triangles, one point or two (or even three) of which are not on screen?
Like, let’s look at our example:
image
image
Just one point off, you can say. But ignoring it results in not rendering part completely. Not triangle = render of edge possible only.
Same problem with points which are just offscreen.

Alright, I decided to not ignore them cuz it’s impossible to workaround that (As far as I know) What’s my trouble?
Let’s now move camera a little down:
image
And what code should render? It should do this:


But renderer will do this instead:

Now let’s try understand why this occurs?
Let’s turn back by 180 degrees:

Now let’s combine this screenshots together:

(I were a bit unprecize with drawings, so sorry for that)
So that’s our problem and issue. Point got on screen but backfased.
IDK how to solve this and need your help guys.

I also has another problem, texture related, but that’s not top priority. Honestly.

Some screenshots of render problems now:
image
Good, all points are visible!
image
It’s still on front, but off-screen. Look how triangles of part got stretched!
image
Issue is slightly noticeable now - backpoint is on screen now, and makes it so part’s triangles are squished now. Looks like ZIndex issue, but not really.
image
This screenshot even further shows issue - part got not just squished but instead it got non-convex shape.