Discuss Scratch

Nathancoolkid714
Scratcher
24 posts

3d help

Please help me with z clipping
heres the link 3d engine v0.2
dspace1015
Scratcher
100+ posts

3d help

In your function for drawing a line, you need to add a check for if the final z value is larger than the clipping plane
Then you need to add edge case tests if one point is in front vs one point is behind and “cut” the line so it only keeps the part in front.
define draw line at ....
... //the other blocks you had about rotation with set pos
if<(z1)>[clip]>then //clip is the clipping plane
if<(z2)>[clip]>then
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
else
set [%t v] to (((z1)-[clip])/((z1)-(z2))) // this means the first point is in front but the second point is behind, so we need to move the second point
set [x2 v] to (((x1)*((1)-(%t)))+((x2)*(%t)))
set [y2 v] to (((y1)*((1)-(%t)))+((y2)*(%t)))
set [z2 v] to (((z1)*((1)-(%t)))+((z2)*(%t)))//this should just put it at the clipping distance but on the same line it started on
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
end
else
if<(z2)>[clip]>then // this means the second point is in front but the first point is behind, so we need to move the first point
set [%t v] to (((z1)-[clip])/((z1)-(z2))) // same values as last time except we are setting the first points values
set [x1 v] to (((x1)*((1)-(%t)))+((x2)*(%t)))
set [y1 v] to (((y1)*((1)-(%t)))+((y2)*(%t)))
set [z1 v] to (((z1)*((1)-(%t)))+((z2)*(%t)))
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
else
... // do nothing because both points are behind camera
end
end
This is basically the approach that I used in my engine. Let me know if this doesn't work because I might have not implemented the equations correctly. the %t value just tells you how far along the line segment the cut needs to happen and the x1*(1-%t)+x2*(%t) tells you where the new value is. Let me know if you have any further questions.

Good luck with your engine!

Nathancoolkid714
Scratcher
24 posts

3d help

error 409

Last edited by Nathancoolkid714 (June 19, 2026 17:29:01)

Nathancoolkid714
Scratcher
24 posts

3d help

dspace1015 wrote:

In your function for drawing a line, you need to add a check for if the final z value is larger than the clipping plane
Then you need to add edge case tests if one point is in front vs one point is behind and “cut” the line so it only keeps the part in front.
define draw line at ....
... //the other blocks you had about rotation with set pos
if<(z1)>[clip]>then //clip is the clipping plane
if<(z2)>[clip]>then
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
else
set [%t v] to (((z1)-[clip])/((z1)-(z2))) // this means the first point is in front but the second point is behind, so we need to move the second point
set [x2 v] to (((x1)*((1)-(%t)))+((x2)*(%t)))
set [y2 v] to (((y1)*((1)-(%t)))+((y2)*(%t)))
set [z2 v] to (((z1)*((1)-(%t)))+((z2)*(%t)))//this should just put it at the clipping distance but on the same line it started on
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
end
else
if<(z2)>[clip]>then // this means the second point is in front but the first point is behind, so we need to move the first point
set [%t v] to (((z1)-[clip])/((z1)-(z2))) // same values as last time except we are setting the first points values
set [x1 v] to (((x1)*((1)-(%t)))+((x2)*(%t)))
set [y1 v] to (((y1)*((1)-(%t)))+((y2)*(%t)))
set [z1 v] to (((z1)*((1)-(%t)))+((z2)*(%t)))
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
else
... // do nothing because both points are behind camera
end
end
This is basically the approach that I used in my engine. Let me know if this doesn't work because I might have not implemented the equations correctly. the %t value just tells you how far along the line segment the cut needs to happen and the x1*(1-%t)+x2*(%t) tells you where the new value is. Let me know if you have any further questions.

Good luck with your engine!

thank you for your help now it makes sense.

Soon then you will see another engine that's insanely good.
also do I have to remove the old z cliping block?
__________________________________________________


from @Nathancoolkid714
dspace1015
Scratcher
100+ posts

3d help

Yes you should be able to remove the old z clipping block because this does the same thing.

One last thing I have to say to you is that you should change the clipping plane to be something greater than zero. If its zero than it causes glitches with the line drawing but if its too high than objects in front of you don't get drawn. I would play around with it so you can find the value that works best for your engine.
HunterRedmond
Scratcher
500+ posts

3d help

Check out this topic, maybe?
Nathancoolkid714
Scratcher
24 posts

3d help

dspace1015 wrote:

Yes you should be able to remove the old z clipping block because this does the same thing.

One last thing I have to say to you is that you should change the clipping plane to be something greater than zero. If its zero than it causes glitches with the line drawing but if its too high than objects in front of you don't get drawn. I would play around with it so you can find the value that works best for your engine.
um it does not work for some reeasion tell me why?
Nathancoolkid714
Scratcher
24 posts

3d help

Nathancoolkid714 wrote:

dspace1015 wrote:

In your function for drawing a line, you need to add a check for if the final z value is larger than the clipping plane
Then you need to add edge case tests if one point is in front vs one point is behind and “cut” the line so it only keeps the part in front.
define draw line at ....
... //the other blocks you had about rotation with set pos
if<(z1)>[clip]>then //clip is the clipping plane
if<(z2)>[clip]>then
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
else
set [%t v] to (((z1)-[clip])/((z1)-(z2))) // this means the first point is in front but the second point is behind, so we need to move the second point
set [x2 v] to (((x1)*((1)-(%t)))+((x2)*(%t)))
set [y2 v] to (((y1)*((1)-(%t)))+((y2)*(%t)))
set [z2 v] to (((z1)*((1)-(%t)))+((z2)*(%t)))//this should just put it at the clipping distance but on the same line it started on
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
end
else
if<(z2)>[clip]>then // this means the second point is in front but the first point is behind, so we need to move the first point
set [%t v] to (((z1)-[clip])/((z1)-(z2))) // same values as last time except we are setting the first points values
set [x1 v] to (((x1)*((1)-(%t)))+((x2)*(%t)))
set [y1 v] to (((y1)*((1)-(%t)))+((y2)*(%t)))
set [z1 v] to (((z1)*((1)-(%t)))+((z2)*(%t)))
go to X:(x1) Y:(y1) Z:(z1)::custom
pen down
go to X:(x2) Y:(y2) Z:(z2)::custom
pen up
else
... // do nothing because both points are behind camera
end
end
This is basically the approach that I used in my engine. Let me know if this doesn't work because I might have not implemented the equations correctly. the %t value just tells you how far along the line segment the cut needs to happen and the x1*(1-%t)+x2*(%t) tells you where the new value is. Let me know if you have any further questions.

Good luck with your engine!

thank you for your help now it makes sense.

Soon then you will see another engine that's insanely good.
also do I have to remove the old z cliping block?
__________________________________________________


from @Nathancoolkid714
I made it work! try my project you will be amazed
Nathancoolkid714
Scratcher
24 posts

3d help

can someone help me with tri fill if you can tell me!



by
Nathancoolkid714
THEIMPORTANT
Scratcher
1000+ posts

3d help

Nathancoolkid714 wrote:

can someone help me with tri fill if you can tell me!



by
Nathancoolkid714
https://scratch.mit.edu/projects/1330137952/editor/ (per-pixel if you want something complex in future and want a real-life-similar filler.)
https://scratch.mit.edu/projects/509029922/editor/ (if you want speed and don't care about transparency issues and want simple 3D.)

Powered by DjangoBB