예제 #1
0
    private void WriteToFile(out string[] strs, List <Texture> list, int typeIndex, string path)
    {
        strs = new string[list.Count];
        for (int i = 0; i < list.Count; i++)
        {
            strs[i] = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(list[i]));
            ComputeBuffer floatBuffer = new ComputeBuffer(list[i].width * list[i].height, sizeof(float4));

            Texture tex  = list[i];
            int     pass = typeIndex == 0 ? 0 : 1;// 正常纹理和法线纹理
            TextureProcess.SetBuffer(pass, "_TextureDatas", floatBuffer);
            TextureProcess.SetTexture(pass, "_TargetTexture", tex);
            TextureProcess.SetInt("_Width", tex.width);
            TextureProcess.SetInt("_Height", tex.height);
            TextureProcess.SetInt("_Offset", 0);
            TextureProcess.Dispatch(pass, tex.width / 8, tex.height / 8, 1);

            float4[] resultDatas = new float4[floatBuffer.count];
            floatBuffer.GetData(resultDatas);

            string savePath = path + ClusterComponents.PATH_TEX_BYTES;
            ClusterUtils.CheckPath(savePath);
            savePath += strs[i] + ".bytes";

            switch (typeIndex)
            {
            case 0:
                byte[]   albedoColors    = new byte[resultDatas.Length * sizeof(Color32)];
                Color32 *albedoColorsPtr = (Color32 *)(UnsafeUtility.AddressOf(ref albedoColors[0]));
                for (int j = 0; j < resultDatas.Length; ++j)
                {
                    float4 v = resultDatas[j];
                    albedoColorsPtr[j] = new Color32((byte)(v.x * 255), (byte)(v.y * 255), (byte)(v.z * 255), (byte)(v.w * 255));
                }
                break;

            case 1:
                byte[] normalColors    = new byte[resultDatas.Length * sizeof(half2)];
                half2 *normalColorsPtr = (half2 *)(UnsafeUtility.AddressOf(ref normalColors[0]));
                for (int j = 0; j < resultDatas.Length; ++j)
                {
                    float4 v = resultDatas[j];
                    normalColorsPtr[j] = (half2)v.xy;
                }
                break;
            }
            ClusterUtils.WriteBytes(savePath, resultDatas);
            floatBuffer.Dispose();
        }
    }
예제 #2
0
    private void Do()
    {
        Scene scene = SceneManager.GetActiveScene();

        sceneName = scene.name.Replace("workscene_", "");

        LODGroup[] groups = GetComponentsInChildren <LODGroup>();
        Dictionary <MeshRenderer, bool> lowLevelDict = new Dictionary <MeshRenderer, bool>();

        foreach (var i in groups)
        {
            LOD[] lods = i.GetLODs();
            for (int j = 1; j < lods.Length; ++j)
            {
                foreach (var k in lods[j].renderers)
                {
                    if (k.GetType() == typeof(MeshRenderer))
                    {
                        lowLevelDict.Add(k as MeshRenderer, true);
                    }
                }
            }
        }

        resPath = PATH_FOLDER + sceneName + "/";

        CombinedModel model        = ProcessCluster(GetComponentsInChildren <MeshRenderer>(false), lowLevelDict);
        int           clusterCount = GetCluster(model.allPoints, model.bound, out NativeList <Cluster> boxes, out NativeList <Point> points, voxelCount);

        ClusterUtils.CheckPath(PATH_FOLDER);
        ClusterUtils.CheckPath(resPath.ToString());
        ClusterUtils.WriteBytes(resPath + PATH_VERTEX + sceneName, points);
        ClusterUtils.WriteBytes(resPath + PATH_CLUSTER + sceneName, boxes);

        AddSceneStreaming(model.vm, clusterCount);

        SaveAsGameScene();
    }