Skip to main content

Saving Character to Code string

Key Problem

UploadUser Inneed Progressto be able to use a string of code to save and load their customized/created avatar. The code length needs to be as minimum as possible.

Definition of Done

User able to use save and load their customized/created avatar using a string of code with the length as minimum as possible. The generated code must be limited to alphanumerical characters.

Key Solution

Map the saved data into BitArray, then convert the BitArray to alphanumeric characters.

To minimize bitarray length, float (32 bits) variables, including color channels and blendshapes, are converted into 7 bits resolution.

Item selections are limited to 8 item per type and takes only 3 bits.

Data to save 

		parameters:
		- color is using 128 resolution per channel
    - blend shapes is using 128 resolution
		- items are limited to 8 type

		versioncode : 128   (7) //used to determine changes in code parsing
    gender      : bool  (1)
    skincolor   : 128x3 (21)
    hair        : 8     (3)
    haircolor   : 128x3 (21)
    haircolor2  : 128x3 (21)
    top         : 8     (3)
    topcolor    : 128x3 (21)
    bottom      : 8     (3)
    bottomcolor : 128x3 (21)
    accessory   : 8     (3)
    acccolor    : 128x3 (21)
    bodyblends  : 128x5 (35) //{"Belly", "Chest", "Waist", "Thigh", "Butt"};
    face        : 8     (3)
    nose        : 8     (3)
    eye         : 8     (3)
    eyecolor    : 128x3 (21)
    lips        : 8     (3)
    lipscolor   : 128x3 (21)

    totalBits   : 228 bit

Available bits for each alphanumeric character in code

    0-9 :   10
    A-Z :   26
    total = 36
    nearestPOT (floored  = 32
    bitsPerChar          = 5

Minimum code length = 228 : 5 = 46 (ceiled from 45.6)

Snippet to convert bitArray to Alpha Numeric Character

		static char GetCharFromBitArray(BitArray bits, int start, int length)
    {
        BitArray bitArray = new BitArray(length);
        int[]    intArray = new int[1];
        bits.CopyTo(bitArray, 0, start);
        bitArray.CopyTo(intArray, 0);
        int i   = intArray[0];
        char c  = '0';
        int  i0 = (int) Encoding.ASCII.GetBytes("0")[0];
        int  iA = (int) Encoding.ASCII.GetBytes("A")[0];

        //0 - 10
        if(i < 10)
            c = (char)(i + i0);
        //11 - 35
        else if(i < 36) 
            c = (char)(i + iA - 10);
        return c;
    }

Limitation

To minimize too long character, the recorded blendshape keys are hardcoded in the script instead of included in the data code. This will make the saved code format less flexible since changes in blendshape names in asset will require the blendshape keys to be changed.

This limitation doesn’t apply to the saved data using json format where the blendshape names are included in the saved data.