Skip to content

Commit

Permalink
add ability to customise base lighting in realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
MattJeanes committed May 13, 2024
1 parent 209272a commit 9261c34
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 8 deletions.
7 changes: 2 additions & 5 deletions lua/entities/gmod_tardis_interior/modules/cl_render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ local function predraw_o(self, part)

render.SuppressEngineLighting(true)

local br = power and lo.basebrightness or lo.nopowerbrightness
local col = power and lo.basebrightnessRGB or lo.nopowerbrightnessRGB
local col = self:GetData("interior_base_light_color", TARDIS.color_white_vector)

local parts_table = power and lo.parts or lo.parts_nopower

Expand All @@ -21,10 +20,8 @@ local function predraw_o(self, part)
else
render.ResetModelLighting(part_br, part_br, part_br)
end
elseif col then
render.ResetModelLighting(col[1], col[2], col[3])
else
render.ResetModelLighting(br, br, br)
render.ResetModelLighting(col[1], col[2], col[3])
end

--render.SetLightingMode(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function ENT:SetData(k,v,network)
end

function ENT:GetData(k,default)
return IsValid(self.exterior) and self.exterior:GetData(k, default)
return IsValid(self.exterior) and self.exterior:GetData(k, default) or default
end

function ENT:ClearData()
Expand Down
77 changes: 77 additions & 0 deletions lua/entities/gmod_tardis_interior/modules/sh_interior_lights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -624,4 +624,81 @@ if CLIENT then
end
self.lights_lastpos = pos
end)
end

-- Base light

if SERVER then
function ENT:SetCustomBaseLightEnabled(enabled, color, brightness)
self:SetData("interior_custom_base_light_enabled", enabled or false, true)
end

function ENT:GetCustomBaseLightEnabled()
return self:GetData("interior_custom_base_light_enabled", false)
end

function ENT:ToggleCustomBaseLightEnabled()
self:SetCustomBaseLightEnabled(not self:GetCustomBaseLightEnabled())
end

function ENT:SetCustomBaseLightColor(color)
self:SetData("interior_custom_base_light_color", color, true)
end

function ENT:GetCustomBaseLightColor()
return self:GetData("interior_custom_base_light_color")
end

function ENT:SetCustomBaseLightBrightness(brightness)
self:SetData("interior_custom_base_light_brightness", brightness, true)
end

function ENT:GetCustomBaseLightBrightness()
return self:GetData("interior_custom_base_light_brightness")
end
else
ENT:AddHook("Think", "baselight", function(self)
local lo = self.metadata.Interior.LightOverride
if not lo then return end

local power = self:GetPower()

local normalbr = power and lo.basebrightness or lo.nopowerbrightness
local normalcolvec = power and lo.basebrightnessRGB or lo.nopowerbrightnessRGB or TARDIS.color_white_vector

local customcol = self:GetData("interior_custom_base_light_color")
local custombr = self:GetData("interior_custom_base_light_brightness", normalbr)

local currentcolvec = self:GetData("interior_base_light_color")
local targetcolvec
if self:GetData("interior_custom_base_light_enabled") and customcol then
targetcolvec = customcol:ToVector() * (custombr or normalbr)
else
targetcolvec = normalcolvec * normalbr
end

if currentcolvec == targetcolvec then
return
elseif not currentcolvec then
self:SetData("interior_base_light_color", targetcolvec)
return
end

local savedtargetcolvec = self:GetData("interior_base_light_target_color_vec")

if savedtargetcolvec ~= targetcolvec then
self:SetData("interior_base_light_target_color_vec", targetcolvec)
self:SetData("interior_base_light_previous_color_vec", currentcolvec)
self:SetData("interior_base_light_transition_fraction", 0)
end

local prevcolvec = self:GetData("interior_base_light_previous_color_vec", currentcolvec)
local fraction = self:GetData("interior_base_light_transition_fraction", 0)
fraction = math.min(fraction + (FrameTime() * self.metadata.Interior.LightOverride.transitionspeed), 1)

local colvec = LerpVector(fraction, prevcolvec, targetcolvec)

self:SetData("interior_base_light_color", colvec)
self:SetData("interior_base_light_transition_fraction", fraction)
end)
end
2 changes: 1 addition & 1 deletion lua/entities/gmod_tardis_part/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function ENT:SetData(k,v,network)
end

function ENT:GetData(k,default)
return IsValid(self.exterior) and self.exterior:GetData(k, default)
return IsValid(self.exterior) and self.exterior:GetData(k, default) or default
end

hook.Add("BodygroupChanged", "tardis_parts", function(ent,bodygroup,value)
Expand Down
15 changes: 15 additions & 0 deletions lua/matproxy/matproxy_tardis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,18 @@ matproxy.Add({
init = matproxy_tardis_power_init,
bind = matproxy_tardis_power_bind,
})

matproxy.Add({
name = "TARDIS_InteriorBaseLight",

init = function( self, mat, values )
self.ResultTo = values.resultvar
end,

bind = function( self, mat, ent )
if not IsValid(ent) or not ent.TardisPart then return end

local col = ent:GetData("interior_base_light_color", TARDIS.color_white_vector)
mat:SetVector(self.ResultTo, col)
end
})
3 changes: 2 additions & 1 deletion lua/tardis/interiors/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ T.Interior = {
},
LightOverride = {
basebrightness = 0.3, --Base interior brightness when power is on.
nopowerbrightness = 0.05 --Interior brightness with no power. Should always be darker than basebrightness.
nopowerbrightness = 0.05, --Interior brightness with no power. Should always be darker than basebrightness.
transitionspeed = 1, --Speed of the light transition.
},
ScreenDistance = 500,
ScreensEnabled = true
Expand Down
2 changes: 2 additions & 0 deletions lua/tardis/sh_helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function TARDIS:CheckPP(ply, ent)
return hook.Call("CanTool", GAMEMODE, ply, pp_trace, "")
end

TARDIS.color_white_vector = Vector(1,1,1)

--[[
local meta=FindMetaTable("Player")
meta.OldSetEyeAngles=meta.OldSetEyeAngles or meta.SetEyeAngles
Expand Down
6 changes: 6 additions & 0 deletions lua/tardis/sh_metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ function TARDIS:CreateInteriorMetadata(id, ent)
metadata.Interior.TextureSets = TARDIS:GetMergedTextureSets(metadata.Interior.TextureSets)
metadata.Exterior.TextureSets = TARDIS:GetMergedTextureSets(metadata.Exterior.TextureSets)

local lightOverridebaseBrightnessRGB = metadata.Interior.LightOverride.basebrightnessRGB
if lightOverridebaseBrightnessRGB and type(lightOverridebaseBrightnessRGB) == "table" then
metadata.Interior.LightOverride.basebrightnessRGB = Vector(lightOverridebaseBrightnessRGB[1], lightOverridebaseBrightnessRGB[2], lightOverridebaseBrightnessRGB[3])
print("[TARDIS] WARNING: Interior '"..id.."' metadata: Exterior.LightOverride.basebrightnessRGB should be a Vector not a table\n")
end

return metadata
end

Expand Down

0 comments on commit 9261c34

Please sign in to comment.