How to draw only on 3 sides?

So I have a simple triangle renderer, but it always draws all sides of the object and I need to draw them only on 3 sides that player sees

, how can get this three sides and draw triangles only on them?


script:

local Map = workspace:WaitForChild("Map"):GetDescendants()

local Camera = workspace.CurrentCamera

local CanvasModule = require(script.Parent.CanvasDraw)

local Canvas = CanvasModule.new(script.Parent.Parent.Frame, Vector2.new(150,150))

local brickTexture = CanvasModule.GetImageData(game.ReplicatedStorage.Textures.RedCube) 

local a = 0
local b = -8


for _, Object in pairs(Map) do
	if Object:IsA("BasePart") then
		b += 8
		for i = 1, 8 do
			a += 1
			
			local folder = script.Parent.Parent.Frames
			local frame = Instance.new("Frame")
			frame.Parent = folder
			frame.AnchorPoint = Vector2.new(0.5, 0.5)
			frame.BackgroundColor3 = Color3.new(0,1,0)
			frame.Name = "Frame".. a


			coroutine.wrap(function()
				while true do
					local vertices  = {
						[1] = Object.CFrame * CFrame.new(Object.Size.X / 2, Object.Size.Y / 2, Object.Size.Z / 2),
						[2] = Object.CFrame * CFrame.new(Object.Size.X / 2, Object.Size.Y / 2, Object.Size.Z / -2),
						[3] = Object.CFrame * CFrame.new(Object.Size.X / 2, Object.Size.Y / -2, Object.Size.Z / 2),
						[4] = Object.CFrame * CFrame.new(Object.Size.X / 2, Object.Size.Y / -2, Object.Size.Z / -2),
						[5] = Object.CFrame * CFrame.new(Object.Size.X / -2, Object.Size.Y/ 2, Object.Size.Z / 2),
						[6] = Object.CFrame * CFrame.new(Object.Size.X / -2, Object.Size.Y / -2, Object.Size.Z / 2),
						[7] = Object.CFrame * CFrame.new(Object.Size.X / -2, Object.Size.Y / -2, Object.Size.Z / -2),
						[8] = Object.CFrame * CFrame.new(Object.Size.X / -2, Object.Size.Y / 2, Object.Size.Z / -2)
					}

					local ViewportSize = Camera.ViewportSize
					local AspectRatio = ViewportSize.X/ViewportSize.Y

					local VerticalFoV = math.rad(Camera.FieldOfView)

					local ScreenSizeY = 2*math.tan(VerticalFoV/2)
					local ScreenSizeX = AspectRatio*ScreenSizeY

					local RelativePosition1 = Camera.CFrame:Inverse() * vertices[i]

					local ScreenPositionX1 = RelativePosition1.X/-RelativePosition1.Z
					local ScreenPositionY1 = RelativePosition1.Y/-RelativePosition1.Z

					frame.Position = UDim2.fromScale(
						1/2 + ScreenPositionX1/ScreenSizeX,
						1/2 - ScreenPositionY1/ScreenSizeY
					)

					local function frame(name)
						return Vector2.new(folder:WaitForChild(name).Position.X.Scale, folder:WaitForChild(name).Position.Y.Scale) * 150
					end
					
					local function drawTriangle(num1 : number, num2 : number, num3 : number, Color : Color3)
						Canvas:DrawTriangle(frame("Frame" .. num1), frame("Frame" .. num2), frame("Frame" .. num3), Color, true)
					end
					
					local function drawTexturedTriangle(num1 : number, num2 : number, num3 : number, ImagePos1, ImagePos2, ImagePos3, ImageData, Brighness)
						Canvas:DrawTexturedTriangle(frame("Frame" .. num1), frame("Frame" .. num2), frame("Frame" .. num3), ImagePos1, ImagePos2, ImagePos3, ImageData, Brighness)
					end
				
					findNearestTarget()
					drawTriangle(1,2,3, Color3.new(1,1,0))
					drawTriangle(2,3,4, Color3.new(1,1,0))
					drawTriangle(2,4,8, Color3.new(0,1,0))
					drawTriangle(8,4,7, Color3.new(0,1,0))
					drawTriangle(5,7,8, Color3.new(0,0,1))
					drawTriangle(5,6,7, Color3.new(0,0,1))
					drawTriangle(1,6,5, Color3.new(0,1,1))
					drawTriangle(1,6,3, Color3.new(0,1,1))
					drawTriangle(1,5,8, Color3.new(1, 0, 0))
					drawTriangle(1,8,2, Color3.new(1, 0, 0))
					task.wait()
					Canvas:Clear()
				end
			end) ()
		end
	end
end
2 Likes

Maybe fire a ray towards the part from the camera. If it hits something else then you can’t see it as something is blocking it otherwise you can and you can view the normal it hit so if it does hit the part then you can check the normal against the face normal

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.