コード例 #1
0
    int CheckMeshDataCount(SenceHierarchyInfo.Object3DMesh inObj)
    {
        int finalCount = inObj.mesh.vertexCount * 3;

        if (inObj.exportUV)
        {
            finalCount += inObj.mesh.uv.Length * 2;
        }
        if (inObj.exportUV2)
        {
            finalCount += inObj.mesh.uv2.Length * 2;
        }
        return(finalCount);
    }
コード例 #2
0
    byte[] GenOneGameObjectBinBuffer(SenceHierarchyInfo.Object3DMesh obj3d, int LastBinCount)
    {
        string meshName;

        List <byte>  finalBinary          = new List <byte>();
        List <float> verticesList         = new List <float>();
        List <float> normalList           = new List <float>();
        List <float> uvList               = new List <float>();
        List <float> uv2List              = new List <float>();
        List <float> worldPosAndScaleList = new List <float>();
        List <float> tangentsList         = new List <float>();

        Vector3[] vertices;
        Vector3[] normals;
        Vector2[] uv;
        Vector2[] uv2;

        int[]    index;
        UInt16[] reorderIndex;

        meshName = obj3d.mesh.name;
        vertices = obj3d.mesh.vertices;
        normals  = obj3d.mesh.normals;

        uv  = obj3d.mesh.uv;
        uv2 = obj3d.mesh.uv2;

        index = obj3d.mesh.triangles;

        //index的类型是Uint16 取值最大范围为65535,所以vertices的数组长度最长为65535

        if (vertices.Length > 65535)
        {
            string log = meshName + "的模型的顶点数超过65535,请减面!";
            Debug.Log(log);
            Debug.LogWarning(log);
            Debug.LogError(log);
        }

        //单物体最大面数Uint16决定了只能为21845面
        reorderIndex = new UInt16[index.Length];

        //反转三角形渲染顺序
        for (int i = 0; i < index.Length / 3; i++)
        {
            reorderIndex[i * 3]     = (UInt16)index[i * 3 + 2];
            reorderIndex[i * 3 + 1] = (UInt16)index[i * 3 + 1];
            reorderIndex[i * 3 + 2] = (UInt16)index[i * 3];
        }

        ////////////

        byte[] binaryHold;

        finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 2));

        binaryHold           = GetUInt16ArrayBuffer(reorderIndex);
        obj3d.indexBufferPos = new int[] { finalBinary.Count + LastBinCount, binaryHold.Length / 2 };
        finalBinary.AddRange(binaryHold);


        for (int i = 0; i < vertices.Length; i++)
        {
            verticesList.Add(vertices[i].x);
            verticesList.Add(vertices[i].y);
            verticesList.Add(-vertices[i].z);
        }


        finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 4));
        binaryHold            = GetInt32ArrayBuffer(FloatArrayToInt32Array(verticesList.ToArray()));
        obj3d.vertexBufferPos = new int[] { finalBinary.Count + LastBinCount, binaryHold.Length / 4 };
        finalBinary.AddRange(binaryHold);

        if (obj3d.useRealtimeLight)
        {
            for (int i = 0; i < normals.Length; i++)
            {
                normals[i].Normalize();
                normalList.Add(normals[i].x);
                normalList.Add(normals[i].y);
                normalList.Add(-normals[i].z);
            }

//          finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 2));
            binaryHold            = FloatArrayToInt8Arrar(normalList.ToArray());
            obj3d.normalBufferPos = new int[] { finalBinary.Count + LastBinCount, binaryHold.Length / 1 };
            if (obj3d.normalBufferPos[1] == 0)
            {
                Debug.LogError(obj3d.mesh.name + " DontHave Normal");
            }
            finalBinary.AddRange(binaryHold);
        }

        if (obj3d.exportUV)
        {
            for (int i = 0; i < uv.Length; i++)
            {
                uvList.Add(uv[i].x);
                uvList.Add(uv[i].y);
            }

            finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 2));
            binaryHold        = GetInt16ArrayBuffer(FloatArrayToInt16Array(uvList.ToArray()));
            obj3d.uvBufferPos = new int[] { finalBinary.Count + LastBinCount, binaryHold.Length / 2 };
            if (obj3d.uvBufferPos[1] == 0)
            {
                Debug.LogError(obj3d.mesh.name + "DontHave UV");
            }
            finalBinary.AddRange(binaryHold);
        }

        if (obj3d.exportUV2)
        {
            //unity默认的Plane没有带uv2。因为经常使用的Plane,所有将uv1复制到uv2
            if (uv2.Length < 1 && obj3d.mesh.name == "Plane")
            {
                uv2List = uvList;
            }
            else
            {
                for (int i = 0; i < uv2.Length; i++)
                {
                    uv2List.Add(uv2[i].x);
                    uv2List.Add(uv2[i].y);
                }
            }

            finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 2));
            binaryHold         = GetInt16ArrayBuffer(FloatArrayToInt16Array(uv2List.ToArray()));
            obj3d.uv2BufferPos = new int[] { finalBinary.Count + LastBinCount, binaryHold.Length / 2 };
            if (obj3d.uv2BufferPos[1] == 0)
            {
                Debug.LogError(obj3d.mesh.name + " DontHave UV2");
            }
            finalBinary.AddRange(binaryHold);
        }


        if (obj3d.mesh.colors.Length > 0)
        {
            for (int i = 0; i < obj3d.mesh.colors.Length; i++)
            {
                worldPosAndScaleList.Add(obj3d.mesh.colors[i].r);
                worldPosAndScaleList.Add(obj3d.mesh.colors[i].g);
                worldPosAndScaleList.Add(-obj3d.mesh.colors[i].b);
                worldPosAndScaleList.Add(obj3d.mesh.colors[i].a);
            }

            finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 4));
            binaryHold = GetInt32ArrayBuffer(FloatArrayToInt32Array(worldPosAndScaleList.ToArray()));
            obj3d.worldPosAndScalePos = new int[] { finalBinary.Count + LastBinCount, binaryHold.Length / 4 };
            finalBinary.AddRange(binaryHold);
        }

        if (obj3d.hasWaterMat)
        {
            //           Debug.Log("-----------------------------------");
            //           Debug.Log(obj3d.mesh.tangents.Length);

            for (int i = 0; i < obj3d.mesh.tangents.Length; i++)
            {
                obj3d.mesh.tangents[i].Normalize();

                tangentsList.Add(obj3d.mesh.tangents[i].x);
                tangentsList.Add(obj3d.mesh.tangents[i].y);
                tangentsList.Add(-obj3d.mesh.tangents[i].z);
            }

            finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 4));
            binaryHold        = FloatArrayToInt8Arrar(tangentsList.ToArray());
            obj3d.tangentsPos = new int[] { finalBinary.Count + LastBinCount, binaryHold.Length / 1 };
            finalBinary.AddRange(binaryHold);
        }



        //这个补足四位是为了,后面压缩使用utf32进行
        finalBinary.AddRange(BuQuanByte(finalBinary.Count + LastBinCount, 4));

        return(finalBinary.ToArray());
    }