Hello everyone!
Since I’m not the best at dealing with normals & UVs and I wasn’t able to find a solution on my own, I thought I’d ask you guys.
In short, I’ve got a system going which fractures glass into multiple shards from a point. It seems to be working great, other than the fact that on the front side two shards stick out because they appear to be shaded smoothly toward a darker tone in one corner. That is visible in this image:
And the back side has seemingly got every shard shaded smoothly…
The above is the result when resetting the normals with this code:
local normalIds = editableMesh:GetNormals()
for i = 1, #normalIds do
editableMesh:ResetNormal(normalIds[i])
end
If I don’t reset the normals, the result is this (on the front):
And like this on the back
Seems like all the faces are split into differently colored triangles on the back if I don’t reset the normals.
Though I doubt that the problem is in the code that makes the shards, I’ll provide it anyway.
The code to build every shard works the same, given that the vertices are already provided. An important thing to note for the algorithm to work tho, is that all the vertices are ordered clockwise and all of the polygons are convex.
Essentially, for every vertex starting from the third one, it makes a triangle with the i-th, i-1 -th and the 1st vertex, covering the entire polygon that it received as input that way. Then, it “extrudes” every vertex that it already made for the polygon to make the polygon a 3D shard and simultaneously makes triangles for the edges of the shard.
I hope the code is commented well enough.
-- For people trying to read this mess, for memory optimization purposes,
-- I need to use pre-made meshparts that have the exact amount of vertices and triangles in place already.
editableMesh = AssetService:CreateEditableMeshAsync((presetPolys["Poly"..tostring(#poly)]).MeshContent, { FixedSize = true } :: {[string]: any})
local pointsSum: Vector2 = Vector2.zero -- The sum of all the positions of the vertices
local vertIndices: {number} = editableMesh:GetVertices()
local faces: {number} = editableMesh:GetFaces()
local faceIdx: number = 1
-- Sum up all the vertices and add triangles
for i: number = 1, n do
local point: Vector2 = poly[i]
pointsSum += point
-- If we have assigned enough vertices, make triangles with the first, second and i-th vertex to fill the poly
if i > 2 then
editableMesh:SetFaceVertices(faces[faceIdx], {vertIndices[1], vertIndices[i-1], vertIndices[i]})
faceIdx += 1
end
end
local pointsArithmeticCenter: Vector2 = pointsSum/#poly -- The arithmetic center of all the vertex positions
-- Set the position of the shard to be at the arithmeic center of all the vertices
shardCF = relativeTo + relativeTo.RightVector * pointsArithmeticCenter.X + relativeTo.UpVector * pointsArithmeticCenter.Y
-- Set the positions of all the vertices, taking into account the new position of the glass shard
for i: number = 1, n do
local point: Vector2 = poly[i]
local sub: Vector2 = point - pointsArithmeticCenter
table.insert(vertices, sub)
editableMesh:SetPosition(vertIndices[i], Vector3.new(sub.X, sub.Y, relativeToData.Thickness/2))
end
-- Add the vertices and triangles for the second side of the glass shard and for the edges
for i: number = n+1, n*2 do
editableMesh:SetPosition(vertIndices[i], editableMesh:GetPosition(vertIndices[i-n]) - Vector3.new(0, 0, relativeToData.Thickness))
-- If we have assigned enough vertices, make triangles with the n+1, n+2 and the i-th vertex to fill the second side of the glass shard
if i > n+2 then
editableMesh:SetFaceVertices(faces[faceIdx], {vertIndices[i], vertIndices[i-1], vertIndices[n+1]})
faceIdx += 1
end
-- If we have enough vertices on both sides to start making triangles for the edges, then we do so
if i > n+1 then
editableMesh:SetFaceVertices(faces[faceIdx], {vertIndices[i-1], vertIndices[i], vertIndices[i-n]})
editableMesh:SetFaceVertices(faces[faceIdx+1], {vertIndices[i-n], vertIndices[i-n-1], vertIndices[i-1]})
faceIdx += 2
-- Since we couldn't make triangles for the first vertex when we assigned it, because the i-th one was missing, we make them now that we have the i-th one
if i == n*2 then
editableMesh:SetFaceVertices(faces[faceIdx], {vertIndices[n*2], vertIndices[n+1], vertIndices[1]})
editableMesh:SetFaceVertices(faces[faceIdx+1], {vertIndices[1], vertIndices[n], vertIndices[n*2]})
faceIdx += 2
end
end
end
I’m fairly certain, that the code is correct and winds all the triangles correctly so that they wouldn’t be facing the wrong way. Although, I have noticed that the two shards that have that one tinted vertex are the only ones with 7 vertices on both sides. All the other ones have less. Given that calling ResetNormal
on all the normals still kept those two like that, I’m not sure what to do really. And the back seems to be a whole nother story.