[Feature Request] Light levels should affect weapon/hand rendering

Discuss coding questions, pull requests, and implementation details.
Post Reply
User avatar
DunnyOfPenwick
Posts: 275
Joined: Wed Apr 14, 2021 1:58 am
Location: Southeast US

[Feature Request] Light levels should affect weapon/hand rendering

Post by DunnyOfPenwick »

Was fiddling with grappling hook stuff and...
...it always bugged me that weapons always appear the same regardless of local lighting levels.

FPSWeapon OnGUI() code now uses DaggerfallUI.DrawTextureWithTexCoords() which allows color blending.
I wrote some short testing code...

Code: Select all

//in FPSWeapon.OnGUI...
                DaggerfallUI.DrawTextureWithTexCoords(
                    weaponPosition,
                    curCustomTexture ? curCustomTexture : weaponAtlas,
                    curAnimRect,
                    true,
                    GetLightLevel());

Code: Select all

        public static Color GetLightLevel()
        {
            Color totalLight = RenderSettings.ambientLight;

            GameObject obj = GameManager.Instance.PlayerObject;
            Vector3 position = obj.transform.position + obj.transform.forward * 0.5f;

            //Performance issue.
            //Should be using cached values from a call to PlayerGPS.GetNearbyObjects() or similar.
            Light[] lights = GameObject.FindObjectsOfType<Light>();

            foreach (Light light in lights)
            {
                float distance = Vector3.Distance(light.transform.position, position);
                if (distance <= 1)
                    totalLight += light.color * light.intensity;
                else if (distance < light.range)
                    totalLight += light.color * light.intensity / (distance * distance);
            }

            //How to detect if standing in building shadows when outside?

            totalLight.r = Mathf.Clamp(totalLight.r, 0f, 1f);
            totalLight.g = Mathf.Clamp(totalLight.g, 0f, 1f);
            totalLight.b = Mathf.Clamp(totalLight.b, 0f, 1f);
            totalLight.a = 1.0f; //not messing with alpha, unless ghost player (hmm...)

            return totalLight;
        }
It seems to work pretty well, though at night near a streetlamp it seems too dim. Maybe I shouldn't have used squared distance.

Other than the FindObjectsOfType() performance issue (easily fixed), it should be easy to shade weapons and other hand related stuff.

User avatar
Interkarma
Posts: 7236
Joined: Sun Mar 22, 2015 1:51 am

Re: [Feature Request] Light levels should affect weapon/hand rendering

Post by Interkarma »

FindObjectsOfType is very expensive. It's the sort of thing I'm only comfortable running a few times per second, and might be involved with a random crash in Unity runtime. Running this in a OnGUI or Update is very undesirable without some heavy throttling.

I've already scheduled refinements to remove almost all uses of FindObjectsOfType outside of a very controlled service method. I wouldn't want to add any more uses, so this method isn't one I'd explore.

There are other ways to fake real-time lighting on weapon rendering though. I've already done a little work along these lines as I was considering using 2D weapons in my next game. One technique is the one used by Dark Mod:
Basically there is an invsible octahedron where the player is. Every time the player litness needs to be calculated a camera looks at that octahedron (from the top and bottom), checks the pixel brightness of that octahedron and uses that to determine how well lit the player is.
With an orthographic projection of the octahedron and sampling brightness from all quadrants, you could even lerp brightness levels across the surface of weapon using a custom shader. It's a bit more setup, but also one that could be added via a mod with no changes to core game (or minor support changes) and provide a nice real-time light effect.

As for base DFU though, I'm essentially done with these kinds of changes. My focus now is entirely bugfix/refinement, localization, and very low impact changes to support mods.

User avatar
DunnyOfPenwick
Posts: 275
Joined: Wed Apr 14, 2021 1:58 am
Location: Southeast US

Re: [Feature Request] Light levels should affect weapon/hand rendering

Post by DunnyOfPenwick »

So we should just wait until v1.0 is done for any new enhancements like this.

For future reference, use a temporary RenderTexture as a light sensor...

<edit> I meant that as a note to self...

Post Reply