getRandomPaint
Definition
-- @/lua/ge/ge_utils.lua:983
-- gets a random paint, with a bias for real world paints
function getRandomPaint(vehId, commonPaintProb)
local obj = getObjectByID(vehId or 0)
local model = obj and obj.jbeam or 'pessima' -- if vehId or vehicle is nil, uses this vehicle paint data as a fallback
local config = obj and tostring(obj.partConfig)
if not obj then
log('W', 'getRandomPaint', 'Vehicle not found, now using default paint data')
end
local paintName, paint
commonPaintProb = commonPaintProb or 0
local commonPaints = {
{0.83, 0.83, 0.83, 1}, -- white
{0, 0, 0, 1}, -- black
{0.33, 0.33, 0.33, 1}, -- grey
{0.65, 0.65, 0.65, 1}, -- silver
{0, 0.1, 0.42, 1}, -- blue
{0.58, 0.12, 0.12, 1} -- red
}
local modelData = core_vehicles.getModel(model).model
local modelDataPaints = modelData and modelData.paints or {}
local paintNames = tableKeys(modelDataPaints)
if math.random() <= commonPaintProb then -- if true, selects a color that is typically found in the real world (commonPaints)
local n = square(math.random()) * 6 -- selects a common paint, with diminishing likelihood
if n > 0.2 then -- arbitrary cutoff value for common paints (if lower, selects the assigned default paint of the vehicle)
local bestVal = math.huge
local bestPaintName
local c = commonPaints[math.ceil(n)]
local c1 = {c[1] * 255, c[2] * 255, c[3] * 255}
for k, v in pairs(modelDataPaints) do
local bc = v.baseColor
if bc[1] == c[1] and bc[2] == c[2] and bc[3] == c[3] then -- exact match (ends the loop early)
bestPaintName = k
break
else
local c2 = {bc[1] * 255, bc[2] * 255, bc[3] * 255}
local val = colorDifference(c1, c2)
if val < bestVal then
bestVal = val
bestPaintName = k
end
end
end
if bestPaintName then
paintName = bestPaintName
end
else
if config then
local _, configKey = path.splitWithoutExt(config)
local configData = core_vehicles.getModel(model).configs[configKey or 0]
paintName = configData and configData.defaultPaintName1 or modelData.defaultPaintName1
else
paintName = 'Pearl White' -- fallback (assuming that it exists)
end
end
else -- selects any color from the paints table
paintName = paintNames[math.random(#paintNames)]
end
paint = modelDataPaints[paintName] or createVehiclePaint()
return paintName, paint
end
Callers