Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Flashlight / Third Person Flashlight #1486

Open
sirzento opened this issue Mar 17, 2024 · 5 comments
Open

Better Flashlight / Third Person Flashlight #1486

sirzento opened this issue Mar 17, 2024 · 5 comments
Labels
type/enhancement Enhancement or simple change to existing functionality

Comments

@sirzento
Copy link

Is your feature request related to a problem? Please describe.

Adding a third person flashlight would be greate because I have seen it so many times that someone tryes to hide in the dark and thinks that they are pretty hidden but for another person who has the flashlight on the whole room might be lit up but the person hiding doesn't see this at all. If the flashlight would also light up the room for everyone like the person with the flashlight sees it, the person hiding will see that he isn't in the dark anymore. (And shadows casted from a flashlight do also look pretty cool)

Describe the solution you'd like

A flashlight that lights up the world not only client side

Describe alternatives you've considered

Looking at videos and the workshop I found the Dynamic Flashlight mod that pretty much does all that. The problem is that this mod had a bug with turning off the flashlight but a user in the comments did fix this but there where still other bugs around like:

  • The light has a fixed starting point of 40 units. This has the problem that shadows where casted above the object and that does look like a bug to the player. You can also see that in the video I have linked that the player says that this is strange looking and a bug. When crouching, the height will still be at 40 units.
  • The light doesn't start right at the body but a few units away from it. That causes shadows to be casted left and right of the object. Thats just like a real flashlight would behave but it looks quite odd if you are close to an object and there are shadows left and right from it.
  • You can turn on the flashlight as spactator
  • You can't see your own shadow if another player shines his flashlight at you. Other players do see this shadow.

Additional context

I tryed to fix most of the bugs this mod has.

  • I did use :EyePos() to get the right hight for the light so the light always starts from the players eyes to light everything up form his perspective. This also fixes the problem that the light stays at the same hight when crouching.
  • I removed the extra distance that was added between the flashlight and the player. Since this means that the flashlight is now inside the player, I set an :SetNearZ() value to let the light ignore everything to close to the light so it can shine through his head.
  • I added a check that prevents spectators from turning on the flashlight

So overall, the flashlight works flawless in first person but the third person view still has some problems

Open problems

  • I didn't have a good NearZ value. When running, the playermodel seems to be a bit faster than the light source so if the player runns at high speed, his head can still poke into the range where shadows are casted. You can prevent this with an different NearZ value but there is another problem. When the player with the flashlight has a weapon that is infront of his view, it will cast a shadow. Other players see for example a big magneto stick shadow. In first person this is invisible for you since the stick perfectly blocks the light but I don't know how this behaves with other weapons that could be in the sight like for example a rocket launcher model. Changing the NearZ value can prevent this but this needs to be tuned.
  • You still can't see your own shadow if another player shines a light at you. This is because you own player model doesn't get rendered for your client and with no model there is no shadow. You can still the the shadow from other people since you also can see the model of them. This problem isn't that bad and can be ignored I guess.

Other possible upgrades

  • You can change the sound the flashlight does simply by changing it at the bottom of the code. Currently it does use the HL2 flashlight sound.
  • IF VManip get's added as optional mod, you can also add a nice animation when turning on the flashlight. I can't find the exact animation in the workshop right now but you can see it here in this video. You can also see the cool interaction with the door or the pickup animation.

Code

It's very much the same original code, just with my fixes.

if CLIENT then
    local cache = {}
    local function UpdateCache(entity, state)
        if not entity:IsPlayer() then return end

        if state then
            table.insert(cache, entity)
        else
            for i = 1, #cache do
                if cache[i] == entity then
                    table.remove(cache, i)
                end
            end
        end
    end

    hook.Add("NotifyShouldTransmit", "DynamicFlashlight.PVS_Cache", function(entity, state)
        UpdateCache(entity, state)
    end)

    hook.Add("EntityRemoved", "DynamicFlashlight.PVS_Cache", function(entity)
        UpdateCache(entity, false)
    end)

    hook.Add("Think", "DynamicFlashlight.Rendering", function()
        for i = 1, #cache do
            local target = cache[i]

            if target:GetNWBool("DynamicFlashlight") then
                if target.DynamicFlashlight then
                    local position = target:EyePos()
                    local viewPos = target:GetHeightVector().z

                    target.DynamicFlashlight:SetPos(Vector(position[1], position[2], position[3]) + target:GetForward())
                    target.DynamicFlashlight:SetAngles(target:EyeAngles())
                    target.DynamicFlashlight:Update()
                else
                    target.DynamicFlashlight = ProjectedTexture()
                    target.DynamicFlashlight:SetTexture("effects/flashlight001")
                    target.DynamicFlashlight:SetFarZ(900)
                    target.DynamicFlashlight:SetNearZ(20)
                    target.DynamicFlashlight:SetFOV(70)
                end
            else
                if target.DynamicFlashlight then
                    target.DynamicFlashlight:Remove()
                    target.DynamicFlashlight = nil
                end
            end
        end
    end)
else
    hook.Add("PlayerSwitchFlashlight", "DynamicFlashlight.Switch", function(ply, state)
        local isAlive = ply:Alive() and not ply:IsSpec()

        if isAlive then
            ply:EmitSound("HL2Player.FlashLightOn")
        end

        ply:SetNWBool("DynamicFlashlight", isAlive and (not ply:GetNWBool("DynamicFlashlight")) or false)

        return false
    end)
end
@TimGoll TimGoll added the type/enhancement Enhancement or simple change to existing functionality label Mar 17, 2024
@TimGoll
Copy link
Member

TimGoll commented Mar 17, 2024

You still can't see your own shadow if another player shines a light at you. This is because you own player model doesn't get rendered for your client and with no model there is no shadow. You can still the the shadow from other people since you also can see the model of them. This problem isn't that bad and can be ignored I guess.

I wonder how this interacts with GMod Legs

@TimGoll
Copy link
Member

TimGoll commented Mar 17, 2024

Maybe it is also possible to add a player model for the local player, that only interacts with the light, but is not rendered

@TimGoll
Copy link
Member

TimGoll commented Mar 17, 2024

Also I'm wondering if we could create a basic custom animation library that is integrated into TTT2. Similar how we did with it with the view and world models. Adding more animations and sounds is something I really want to focus on to give the game a more polished feel.

Our version of that library should support world model animations though, as it would be nice to see these animations on any player.

We'd probably also need a flashlight model that is attached to the shoulder or so

@sirzento
Copy link
Author

I wonder how this interacts with GMod Legs

I have the feeling that you will just have a shadow from your lower body

Also I'm wondering if we could create a basic custom animation library that is integrated into TTT2. Similar how we did with it with the view and world models. Adding more animations and sounds is something I really want to focus on to give the game a more polished feel.

Our version of that library should support world model animations though, as it would be nice to see these animations on any player.

Would be nice. VManip already shows us what animation you guys could add. I like that shoulder tap to turn on the flashlight, the "grab to pick up" and "stretch hand out to interact" is also very nice and immersive. That one addon also adds things like "putting hand infront of the face" when the player did get explosion damage.

We'd probably also need a flashlight model that is attached to the shoulder or so

Isn't that pretty hard since there are any servers that are using custom models? I don't mind seeing no flashlight on the model and if it's turned on you can't see it anyways because of the light.

@TimGoll
Copy link
Member

TimGoll commented Mar 17, 2024

Isn't that pretty hard since there are any servers that are using custom models? I don't mind seeing no flashlight on the model and if it's turned on you can't see it anyways because of the light.

There are attachment points on models, like the hands where the weapons are rendered. It is basically an extension of the custom world model stuff I already did

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement Enhancement or simple change to existing functionality
Projects
None yet
Development

No branches or pull requests

2 participants