I am trying to make my shooter game compatible with ShadowMap lighting. However I ran into few performance problems.
Before I start, I wanna clarify that I don’t have a top tier PC and this was done in a map with around 4k parts, with ShadowMap lighting, of course. I believe that if my PC aren’t even able to handle this, neither some my players will.
Also everything works fine with no frame drops/spikes in Voxel lighting.
Whenever I’m calling Clone or Instance.new, there will be a sight frame spike which can be viewed in the Micro Profiler. For an example, I clone a bullet shell from ReplicatedStorage or create a bullet hole part and clone an Effect on it or cloning a bullet part with a trail attached on it, there will be a frame spike.
https://gyazo.com/4b52fd13bf69aff7c9407e219fd1744a
Here are some code examples that caused such behavior, I think they are just some really generic instance.new/clone stuff.
local FlingShell = bullet:Clone() -- lag spike already, no matter parenting or not
PhysicsService:SetPartCollisionGroup(FlingShell, "Players")
FlingShell.CFrame = tool.BoltEject.CFrame
FlingShell.Orientation = Vec3new(math.random(0,180),math.random(0,180),math.random(0,180))
FlingShell.Velocity = tool.BoltEject.CFrame.LookVector * 10
FlingShell.Parent = workspace
FlingShell.Anchored = false
delay(5,function()
FlingShell:Destroy()
FlingShell = nil
end)
function CreateBulletHolePart(hit, pos, norm)
local BulletHolePart = Instance.new("Part")
BulletHolePart.FormFactor = Enum.FormFactor.Custom
BulletHolePart.Name = "BulletHole"
BulletHolePart.Size = Vec3new(0.3,0.3,0.05)
BulletHolePart.Anchored = true
BulletHolePart.Transparency = 1
BulletHolePart.CFrame = CFnew(pos, pos + norm)
BulletHolePart.CanCollide = false
local BulletHoleDecal = Instance.new("Decal")
BulletHoleDecal.Face = "Front"
BulletHoleDecal.Texture = "http://www.roblox.com/asset/?id=4813872065"
local HitEffect = rs.HitEffect:Clone()
HitEffect.Enabled = true
HitEffect.Parent = BulletHolePart
local HitEffect2 = rs.SmokeEffect:Clone()
HitEffect2.Enabled = true
HitEffect2.Parent = BulletHolePart
debris:AddItem(HitEffect, 0.15)
...
I’m not sure is RayCasting the reason behind but I do raycasting per step, which means 60 times per second until a part hit is detected. It is a really short ray and I set the bullet part’s CFrame which is anchored, and CanCollide false. When the part was initially created, there will be a spike (which is the problem mentioned above), I was using FindPartOnRayWithIgnoreList with a table with ignored parts (maybe a thousand so) and I changed to FindPartOnRay with no ignore parts for testing purpose, nothing much changed.
I’m wondering how does other games can perform it so perfectly without a single lag spike? Take Arsenal as an example, they used ShadowMap but I have literally no problems on it. They also have bullet trails, raycasting per step, and particles like I do, and I believe the map size is also similar,
Any suggested approaches to fix this?..