I made a wave system, and I’m attempting to detect if a vertex is on screen, if not, it will not calculate it’s position. I’m using WorldToScreenPoint, and it doesn’t work as intended.
Video:
Entire local script code:
local AssetService = game:GetService("AssetService")
local RunService = game:GetService("RunService")
local Mesh = workspace:WaitForChild("WaveMesh")
local Camera = workspace.CurrentCamera
-- Create an editable mesh from the mesh content.
local EditableMesh = AssetService:CreateEditableMeshAsync(Mesh.MeshContent)
local Vertices = EditableMesh:GetVertices()
local Normals = EditableMesh:GetNormals()
local vertexNormals = {}
local VerticesPositions = {}
local OriginalPositions = {}
function getVertPos(pos, steepness, t, w, direction : Vector2, tangent, binormal)
local x = pos.X
local y = pos.Y
local z = pos.Z
local k = 2 * math.pi / w
local c = math.sqrt(9.8 / k)
local d = direction.Unit
local f = k * (d:Dot(Vector2.new(x, z)) - c * t)
local a = steepness / k
local newX = d.X * (a * math.cos(f))
local newY = a * math.sin(f)
local newZ = d.Y * (a * math.cos(f))
local vertPos = Vector3.new(x + newX, newY, z + newZ)
tangent += Vector3.new(
1 - d.X * d.X * (steepness * math.sin(f)),
d.X * (steepness * math.cos(f)),
-d.X * d.Y * (steepness * math.sin(f))
)
binormal += Vector3.new(
-d.X * d.Y * (steepness * math.sin(f)),
d.Y * (steepness * math.cos(f)),
1 - d.Y * d.Y * (steepness * math.sin(f))
)
return vertPos, tangent, binormal
end
for i, vertex_id in pairs(Vertices) do
local position = EditableMesh:GetPosition(vertex_id)
local normal_id = Normals[i]
vertexNormals[vertex_id] = normal_id
VerticesPositions[vertex_id] = position
OriginalPositions[vertex_id] = position
end
function updateVerticesPositions()
for vertex_id, vertexPosition in pairs(VerticesPositions) do
local originalPos = OriginalPositions[vertex_id]
local _, onScreen = Camera:WorldToScreenPoint(originalPos)
if onScreen then
local normal_id = vertexNormals[vertex_id]
local w1 = 15
local steepness = 0.75
local w2 = 10
local steepness2 = 0.5
local w3 = 5
local tangent = Vector3.new(2, 0, 0)
local binormal = Vector3.new(0, 0, 2)
local vertPos = originalPos
local vertPos1, tangent, binormal = getVertPos(originalPos, steepness, os.clock(), w1, Vector2.new(1, 1), tangent, binormal)
local vertPos2, tangent2, binormal2 = getVertPos(originalPos, steepness2, os.clock(), w2, Vector2.new(1, 0.5), tangent, binormal)
local vertPos3, tangent, binormal = getVertPos(originalPos, steepness / 2, os.clock(), w3, Vector2.new(1, 1.5), tangent, binormal)
vertPos += Vector3.new(vertPos1.X, vertPos1.Y, vertPos1.Z)
vertPos += Vector3.new(vertPos2.X, vertPos2.Y, vertPos2.Z )
vertPos += Vector3.new(vertPos3.X, vertPos3.Y, vertPos3.Z)
vertPos = Vector3.new(vertPos.X / 3, vertPos.Y, vertPos.Z / 3)
local normal = binormal2:Cross(tangent2).Unit
EditableMesh:SetNormal(normal_id, normal)
VerticesPositions[vertex_id] = vertPos
EditableMesh:SetPosition(vertex_id, vertexPosition)
end
end
end
RunService:BindToRenderStep("Waves", 1, function()
updateVerticesPositions()
-- Create a new mesh part using the new Content.fromObject(x : EditableMesh) function.
local newMesh = AssetService:CreateMeshPartAsync(Content.fromObject(EditableMesh))
-- Apply the newMesh to the original Mesh
Mesh:ApplyMesh(newMesh)
end)