Raycasting in Shadowmap

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?..

1 Like

If your game was working fine before enabling shadowmap, try disabling the CastShadow property on the bullets. I imagine trying to create an accurate shadow for multiple fast moving objects is causing unnecessary strain.

I’d recommend trying PartCache rather than creating new parts. It helps a lot with performance.

But @MayorGnarwhal’s solution could be especially helpful in this case aswell.

Everything has Cast Shadow off already, CanCollide false, and Anchored true, no collisions / physics were calculated.

I’ll try PartCache if it’s possible.

I dont think its the cloning because my laptop is not that great and I made a simple gun that runs pretty smooth. I split up my rays using heartbeat though and I use the delta time to make them travel at a set speed, and then i use a Body velocity to make the bullets move

Are you trying it in a map with around 3k parts? Also in Max Graphics in main game instead of studio.
Yes, we have the similar approach except I use CFraming. When the bullet part is initially created, there’s a lag spike.

well 3k parts is alot, I dont think other arsenal maps have so much from the looks of it. Could you send a link so i can see the game, and no i dont use max graphics, if your pc is not very good why are using max?

Sadly Roblox is down right now lol, can’t do anything. I’ll send it to you when it’s back up.

and it says that partcache is only for instance.new and not for cloning.