public static byte[] compressVectorArray(Vector3[] data, CPVectorCompressionMode compressionMode)
        {
            float recPrecision = getModeResize(compressionMode);
            float precision    = 1.0f / recPrecision;

            int bitSize = getModeBitsize(compressionMode);

            /* 7 bytes
             * 3 for size
             * 1 for rounding ((size*3 * bitSize) >> 3) */
            int size      = data.Length;
            int bytesSize = ((size * 3 * bitSize) >> 3) + 4;

            BitOutputStream bitOutputStream = new BitOutputStream(bytesSize);

            bitOutputStream.WriteBits(24, size);
            for (int i = 0; i < size; i++)
            {
                int value = vectorFloatToInt(data[i].x, recPrecision, precision);
                bitOutputStream.WriteBits(bitSize, value);
                value = vectorFloatToInt(data[i].y, recPrecision, precision);
                bitOutputStream.WriteBits(bitSize, value);
                value = vectorFloatToInt(data[i].z, recPrecision, precision);
                bitOutputStream.WriteBits(bitSize, value);
            }

            return(bitOutputStream.GetData());
        }
示例#2
0
        public static byte[] compressUVArray(Vector3[] data, CPUVCompressionMode compressionMode)
        {
            float precision    = getPrecision(compressionMode);
            float recPrecision = 1.0f / precision;

            //Debug.Log("Compressing UV Data precision:" + precision+ " compressionMode:"+ compressionMode);

            int size      = data.Length;
            int maxIndexX = size > 0 ? CPFloatArrayData.floatToInt(data[0].x, recPrecision, precision) : 0;
            int minIndexX = maxIndexX;
            int maxIndexY = size > 0 ? CPFloatArrayData.floatToInt(data[0].y, recPrecision, precision) : 0;
            int minIndexY = maxIndexY;

            for (int i = 0; i < size; i++)
            {
                int valueX = CPFloatArrayData.floatToInt(data[i].x, recPrecision, precision);
                maxIndexX = valueX < maxIndexX ? maxIndexX : valueX;
                minIndexX = valueX > minIndexX ? minIndexX : valueX;
                int valueY = CPFloatArrayData.floatToInt(data[i].y, recPrecision, precision);
                maxIndexY = valueY < maxIndexY ? maxIndexY : valueY;
                minIndexY = valueY > minIndexY ? minIndexY : valueY;
            }

            int bitSizeX = CPShortArrayData.getBitSize(maxIndexX - minIndexX);
            int bitSizeY = CPShortArrayData.getBitSize(maxIndexY - minIndexY);

            int bitSize = bitSizeX > bitSizeY ? bitSizeX : bitSizeY;

            /* 7 bytes
             * 3 for each minIndex
             * 1 for bitSize
             * 3 for size
             * 1 for rounding ((size*3 * bitSize) >> 3)
             */
            int bytesSize = ((size * 3 * bitSize) >> 3) + 11;

            BitOutputStream bitOutputStream = new BitOutputStream(bytesSize);

            bitOutputStream.WriteBits(24, minIndexX);
            bitOutputStream.WriteBits(24, minIndexY);
            bitOutputStream.WriteBits(8, bitSize);
            bitOutputStream.WriteBits(24, size);
            for (int i = 0; i < size; i++)
            {
                int value = CPFloatArrayData.floatToInt(data[i].x, recPrecision, precision);
                bitOutputStream.WriteBits(bitSize, value - minIndexX);
                value = CPFloatArrayData.floatToInt(data[i].y, recPrecision, precision);
                bitOutputStream.WriteBits(bitSize, value - minIndexY);
            }
            return(bitOutputStream.GetData());
        }
示例#3
0
        public static byte[] CompressShortsArray(short[] data)
        {
            int size     = data.Length;
            int maxIndex = size > 0 ? (data[0] + MIDDLE_VALUE) : 0;
            int minIndex = maxIndex;

            for (int i = 0; i < size; i++)
            {
                int value = data[i] + MIDDLE_VALUE;
                maxIndex = value < maxIndex ? maxIndex : value;
                minIndex = value > minIndex ? minIndex : value;
            }

            int bitSize = getBitSize(maxIndex - minIndex);

            /* 7 bytes
             * 2 for minIndex
             * 1 for bitSize
             * 3 for size
             * 1 for rounding ((size * bitSize) >> 3)
             */
            int bytesSize = ((size * bitSize) >> 3) + 7;

            int             delta           = MIDDLE_VALUE - minIndex;
            BitOutputStream bitOutputStream = new BitOutputStream(bytesSize);

            bitOutputStream.WriteBits(16, minIndex);
            bitOutputStream.WriteBits(8, bitSize);
            bitOutputStream.WriteBits(24, size);
            for (int i = 0; i < size; i++)
            {
                bitOutputStream.WriteBits(bitSize, data[i] + delta);
            }

            return(bitOutputStream.GetData());
        }