BitHelper.ToVector4 C# (CSharp) Method

ToVector4() public static method

public static ToVector4 ( byte buf, int pos ) : Vector4
buf byte
pos int
return Vector4
    public static Vector4 ToVector4(byte[] buf, int pos)
    {
        Vector4 vec;
        vec.x = BitConverter.ToSingle(buf, pos + 0);
        vec.y = BitConverter.ToSingle(buf, pos + 4);
        vec.z = BitConverter.ToSingle(buf, pos + 8);
        vec.w = BitConverter.ToSingle(buf, pos + 12);
        return vec;
    }
    public static byte[] getBytes(Quaternion pos)

Usage Example

コード例 #1
0
    public static Mesh ReadMesh(byte[] data, int _seek)
    {
        Mesh m    = new Mesh();
        int  seek = _seek;
        int  offset;

        m.name   = BitHelper.ReadString(data, seek, out offset);
        m.name   = "Instance";
        seek    += offset;
        m.bounds = BitHelper.ToBounds(data, seek);
        seek    += 24;
        UInt32 vcount = BitConverter.ToUInt32(data, seek);

        seek += 4;
        while (true)
        {
            byte tag = data[seek];
            Debug.Log("ReadMesh tag=" + tag);
            seek += 1;
            if (tag == 255)
            {
                break;
            }
            if (tag == 1)//pos
            {
                Vector3[] poss = new Vector3[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    poss[i] = BitHelper.ToVector3(data, seek);
                    seek   += 12;
                    //if (i == 0)
                    //{
                    //    Debug.Log("vertices0:" + poss[i]);
                    //}
                }
                m.vertices = poss;
            }
            if (tag == 2)//color
            {
                Color32[] colors = new Color32[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    colors[i] = BitHelper.ToColor32(data, seek);
                    seek     += 4;
                    //if (i == 0)
                    //{
                    //    Debug.Log("colors320:" + colors[i]);
                    //}
                }
                m.colors32 = colors;
            }
            if (tag == 3)//normal
            {
                Vector3[] normals = new Vector3[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    normals[i] = BitHelper.ToVector3(data, seek);
                    seek      += 12;
                    //if (i == 0)
                    //{
                    //    Debug.Log("normals0:" + normals[i]);
                    //}
                }
                m.normals = normals;
            }
            if (tag == 4)//uv
            {
                Vector2[] uvs = new Vector2[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    uvs[i] = BitHelper.ToVector2(data, seek);
                    seek  += 8;
                    //if (i == 0)
                    //{
                    //    Debug.Log("uvs0:" + uvs[i]);
                    //}
                }
                m.uv = uvs;
            }
            if (tag == 5)//uv2
            {
                Vector2[] uvs = new Vector2[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    uvs[i] = BitHelper.ToVector2(data, seek);
                    seek  += 8;
                }
                m.uv2 = uvs;
            }
            if (tag == 6)//uv3
            {
                Vector2[] uvs = new Vector2[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    uvs[i] = BitHelper.ToVector2(data, seek);
                    seek  += 8;
                }
                m.uv3 = uvs;
            }
            if (tag == 7)
            {
                Vector4[] tangents = new Vector4[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    tangents[i] = BitHelper.ToVector4(data, seek);
                    seek       += 16;
                }
                m.tangents = tangents;
            }
            if (tag == 8)//uv4
            {
                Vector2[] uvs = new Vector2[vcount];
                for (int i = 0; i < vcount; i++)
                {
                    uvs[i] = BitHelper.ToVector2(data, seek);
                    seek  += 8;
                }
                m.uv4 = uvs;
            }
            if (tag == 16)
            {
                int len = data[seek]; seek++;
                //s.WriteByte((byte)mesh.bindposes.Length);//length diff
                Matrix4x4[] bindpose = new Matrix4x4[len];

                for (int i = 0; i < len; i++)
                {
                    Vector3 pos = BitHelper.ToVector3(data, seek);
                    seek += 12;
                    Vector3 scale = BitHelper.ToVector3(data, seek);
                    seek += 12;
                    Quaternion quat = BitHelper.ToQuaternion(data, seek);
                    seek       += 16;
                    bindpose[i] = Matrix4x4.TRS(pos, quat, scale);
                }
                m.bindposes = bindpose;
            }
            if (tag == 17)
            {
                BoneWeight[] weights = new BoneWeight[vcount];

                for (int i = 0; i < vcount; i++)
                {
                    weights[i].boneIndex0 = BitConverter.ToInt32(data, seek); seek += 4;
                    weights[i].boneIndex1 = BitConverter.ToInt32(data, seek); seek += 4;
                    weights[i].boneIndex2 = BitConverter.ToInt32(data, seek); seek += 4;
                    weights[i].boneIndex3 = BitConverter.ToInt32(data, seek); seek += 4;
                    weights[i].weight0    = BitConverter.ToSingle(data, seek); seek += 4;
                    weights[i].weight1    = BitConverter.ToSingle(data, seek); seek += 4;
                    weights[i].weight2    = BitConverter.ToSingle(data, seek); seek += 4;
                    weights[i].weight3    = BitConverter.ToSingle(data, seek); seek += 4;
                }
                m.boneWeights = weights;
            }
        }
        int subcount = data[seek];

        seek += 1;
        Debug.Log("sub=" + subcount);
        for (int i = 0; i < subcount; i++)
        {
            int tv = BitConverter.ToInt32(data, seek);
            seek += 4;


            UInt32 length = BitConverter.ToUInt32(data, seek);

            Debug.Log("indlength:" + length);

            seek += 4;
            int[] indices = new int[length];
            for (int j = 0; j < length; j++)
            {
                indices[j] = BitConverter.ToInt32(data, seek);
                seek      += 4;
            }
            MeshTopology ttv = (MeshTopology)tv;
            m.SetIndices(indices, (MeshTopology)tv, i);
        }
        return(m);
    }