Calculating DepthScale of a Roblox Avatar like the Roblox backend API

Long ago, I was trying to figure out how to calculate the DepthScale. How to calculate DepthScale? For setting avatar properties not through the avatar editor on the website

Today, I finally found the formula, and that’s the only thing I will be sharing, not the source. This formula is going to be useful any Avatar Editing Component, e.g. for @ItsMuneeeb to apply it in a game like Catalog Avatar Creator - Roblox, as Depth is auto-calculated.
 

Why do I need a DepthScale Formula?

You don’t…

But by default, the Roblox Avatar Editor, does not allow you to modify the depth manually, it is calculated automatically in the backend by Roblox itself. If you want accurate previews or re-scaling adjustments, YOU WILL NEED this accurate formula.


DepthScale Formula

Depth is calculated based on the width.

A decrease Ratio is defined. This decrease Ratio takes the value from ScaleDepthWidthRatio which is set to 0.5.

This is used to calculate that what I will define as preDepth.
At the end, to get the actual depth, you’ll have to clamp it from it’s Min and Max from the https://avatar.roblox.com/v1/avatar-rules (but it’s not there, so you won’t need to min. max it)

 

At the end the formula looks like so: (note this is NOT code)

  • The 0.5 is the ScaleDepthWidthRatio used as the decrease Ratio.
  • The increment is used for decimal conversions for roundings and other stuff, it’s pretty smart actually.
  • ROUND_HALF_TO_EVEN, see Math.Round from C# (Regular Lua Round won’t work)
increment = 0.01

preDepth = 1 - (1 - width) * (0.5)
depth = increment  * ROUND_HALF_TO_EVEN( preDepth / increment )

 

And there you go!

I tried to do a desmos for this as well: Desmos | Graphing Calculator

Again, the end result depends on Round Half To Even, there are various of functions, the one on desmos is probably not the best.

 

Additionally

I am very happy to have finally discovered this, as I was seeking it once, and I am glad to share it.

If you were looking, or are using this for something specific, feel free to reply what you’re using this formula for, down below.

 

Feel free to ask any questions.

7 Likes

If anyone is need of the code here it is:

New code (uses RoundHalfToEven function)

local function RoundHalfToEven(Number)
	local FloorNumber = math.floor(Number)
	local DecimalPart = Number - FloorNumber

	if DecimalPart < 0.5 then
		return FloorNumber
	elseif DecimalPart > 0.5 then
		return math.ceil(Number)
	else
		return FloorNumber % 2 == 0 and FloorNumber or math.ceil(Number)
	end
end

local function CalculateDepthScale(Width)
	local ScaleDepthWidthRatio = 0.5
	local Increment = 0.01

	local PreDepth = 1 - (1 - Width) * ScaleDepthWidthRatio
	local Depth = Increment * RoundHalfToEven(PreDepth / Increment)

	return Depth
end

Old code (uses math.round)

local function CalculateDepthScale(Width)
	local ScaleDepthWidthRatio = 0.5
	local Increment = 0.01

	local PreDepth = 1 - (1 - Width) * ScaleDepthWidthRatio
	local Depth = Increment * math.round(PreDepth / Increment)

	return Depth
end

Usage
To use this function, simply call it with the width of the avatar as the parameter. For example:

local Width = 0.97 -- Replace with the width of your avatar
local DepthScale = CalculateDepthScale(Width)
print("DepthScale:", DepthScale)
1 Like

You’re missing something very very important here, that I tried to highlight.

math.round is wrong.

It needs to be a rounding function that uses the concept of “Round Half To Even” or the result is inaccurate.

Edit:

Don’t forget to replace math.round with the “Round Half To Even” you added.