better nameplate

This commit is contained in:
Reid 2025-03-04 22:23:21 -08:00
parent 16f8f244ab
commit b9754e0d81
Signed by: reidlab
GPG key ID: 6C9EAA3364F962C8
4 changed files with 591 additions and 19 deletions

View file

@ -1,22 +1,110 @@
local name = "reidlab!"
local colors = { "#d87b5a", "#e0ab91" }
local offset = 0.05
local speed = 0.05
local g = require("scripts.libs.gradient")
colors[#colors + 1] = colors[1]
offset = offset / speed
---@class ColoredText
---@field text string
---@field color string
events.TICK:register(function ()
local newName = "["
for i = 1, #name, 1 do
local counter = (((world.getTime() + offset * i) * speed) % (#colors - 1)) + 1
local counterFloored = math.floor(counter)
local color = math.lerp(vectors.hexToRGB(colors[counterFloored]), vectors.hexToRGB(colors[counterFloored + 1]), counter - counterFloored)
newName = newName .. '{"text":"' .. name:sub(i,i) .. '","color":"#' .. vectors.rgbToHex(color) .. '"},'
avatar:setColor(color)
---@param name string
---@param gradient Gradient
---@param nameplates Nameplate | Nameplate[] where to use the animated name
---@param count number number of gradients to be present in the span
---@param phase_shift_rate number how fast to shift the gradient
---@returns fun():void # function to unregister
local function animate_gradient_name(name, gradient, nameplates, count, phase_shift_rate)
if type(nameplates) ~= "table" then
nameplates = {nameplates}
end
newName = newName:sub(1, #newName - 1) .. "]"
nameplate.ALL:setText(newName)
end)
local string_width = client.getTextWidth(name) / count;
local string_width_chars = {};
for i = 1, #name do
string_width_chars[i] = client.getTextWidth(name:sub(i, i))
end
---@type ColoredText[]
local result = {}
local phase_shift = 0;
local function render()
if client.isPaused() then
return
end
local acc = 0;
for i = 1, #name do
local offset = acc + string_width_chars[i];
local color = gradient:at((offset / string_width) + phase_shift);
acc = offset
result[i] = {
text = name:sub(i, i),
color = color:toHex(true, false)
}
end
phase_shift = phase_shift + phase_shift_rate
local json = toJson(result);
for _, nameplate in pairs(nameplates) do
nameplate:setText(json)
end
end
events.render:register(render)
return function ()
events.render:remove(render)
end
end
---@param name string
---@param gradient Gradient
---@param nameplates Nameplate | Nameplate[] where to use the static name
---@param count number number of gradients to be present in the span
---@param phase_shift number shift of the gradient
local function static_gradient_name(name, gradient, nameplates, count, phase_shift)
if type(nameplates) ~= "table" then
nameplates = {nameplates}
end
local string_width = client.getTextWidth(name) / count;
local string_width_chars = {};
for i = 1, #name do
string_width_chars[i] = client.getTextWidth(name:sub(i, i))
end
---@type ColoredText[]
local result = {}
local acc = 0;
for i = 1, #name do
local offset = acc + string_width_chars[i];
local color = gradient:at((offset / string_width) + phase_shift);
acc = offset
result[i] = {
text = name:sub(i, i),
color = color:toHex(true, false)
}
end
local json = toJson(result);
for _, nameplate in pairs(nameplates) do
nameplate:setText(json)
end
end
local name = "reidlab!"
local gradient_count = 1
local gradient = g.SimpleGradientBuilder.new()
:add({ "#d87b5a", "#e0ab91" })
:reflect(false)
:build()
animate_gradient_name(name, gradient, {
nameplate.ENTITY,
nameplate.LIST
}, gradient_count, 0.005)
static_gradient_name(name, gradient, {
nameplate.CHAT
}, gradient_count, 0)