Getting ViewportFrame Light Direction to match World Light Direction

Hi,

I couldn’t find any topics, or Videos related to it by searching, so I posted here.

So right now I’m currently Playing Around with ViewportFrames, and I’m trying to match its LightDirection with the way the Light is facing in the World, I have tried to use game.Lighting:GetSunDirection() to help with getting that direction, but I dont really know if thats the actual way to do it, or if its even Possible.

But I dont want it to just face where its pointing, I trying to get it to rotate based on the Direction the Player’s Camera is facing, which is where I’m running into problems.

A Couple of things I have Tried was to try subtracting, and adding the Camera’s LookVector with the Sun Direction, I also used Position but every Result I got was it was off by a couple of Degree’s, or doesnt work at all (the lighting ends up glitching out)

I played around with the values a bit, and this is what I currently have, which I’m pretty sure I’m doing it completely wrong:

local SunDir  = Lighting:GetSunDirection() -- Sun Direction
local CamDir  = Camera.CFrame.LookVector -- Camera LookVector

local Dir     = (camDir - SunDir) -- I'm pretty sure I'm missing something here
	
ViewportFrame.LightDirection = Direction -- Applies "Light Direction"

I feel like I’m missing something important, or missing something entirely, this is my first time trying to do it so I might be missing something.

There are two CFrames to consider that will affect this, the player’s camera CFrame, which is what the UI is glued to, and the viewport camera. The viewport of the ViewportFrame must also be the same size and shape as the player’s full viewport, otherwise the projection will not be exactly the same. The Lighting light direction is in WorldSpace, so you want the following to be true in WorldSpace:

PlayerCameraCFrame * ViewportCameraCFrame * ViewportLightDirection = WorldLightDirection

So you should be able to rearrange it like this:

ViewportLightDirection = ViewportCameraCFrame:Inverse() * PlayerCameraCFrame:Inverse() *WorldLightDirection

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.