public void Init(MCRScene scene)
        {
            clusterStride = Marshal.SizeOf <ClusterInfo>();
            vertexStride  = Marshal.SizeOf <VertexInfo>();

            clusterList   = new Unity.Collections.NativeArray <ClusterInfo>(ClusterCount, Unity.Collections.Allocator.Persistent);
            vertexList    = new Unity.Collections.NativeArray <VertexInfo>(VertexCount, Unity.Collections.Allocator.Persistent);
            instanceCount = new uint[1];

            indirectArgs = new List <uint> {
                0, 0, 0, 0, 0
            };
            vertexBuffer            = ComputeBufferPool.GetTempBuffer(VertexCount, vertexStride);
            clusterBuffer           = ComputeBufferPool.GetTempBuffer(ClusterCount, clusterStride);
            cullResultBuffer        = ComputeBufferPool.GetTempBuffer(ClusterCount, 4);
            cullInstanceCountBuffer = ComputeBufferPool.GetTempBuffer(1, 4);
            cullInstanceCountBuffer.SetData(instanceCount);
            inDirectBuffer = ComputeBufferPool.GetIndirectBuffer();

            ClusterInfoAssetsPath = MCRConstant.BakeAssetSavePath + "/" + ClusterInfoAssetsPath;
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                //ClusterInfoAssetsPath = MCRConstant.BakeAssetSavePath + "/" + ClusterInfoAssetsPath;
            }
            else
            {
                scene.StartCoroutine(LoadingBytes());
            }


            bLoadFinish = false;
            bDestroyed  = false;
        }
 private static void RenderScene(MCRScene scene)
 {
     if (scene)
     {
         scene.BeforeRender();
         scene.Render();
         scene.EndRender();
     }
 }
        public static void AddToRenderList(MCRScene scene)
        {
            if (renderList.Contains(scene))
            {
                return;
            }

            renderList.Add(scene);
        }
 public static void RemoveFromRenderList(MCRScene scene)
 {
     renderList.Remove(scene);
 }
Ejemplo n.º 5
0
        void Bake()
        {
            GameObject parentRoot = GameObject.Find(MCRParentRoot);


            if (!parentRoot)
            {
                return;
            }

            MeshFilter[] allFilters = parentRoot.GetComponentsInChildren <MeshFilter>();

            if (allFilters.Length < 0)
            {
                return;
            }

            MCRScene mcrScene = parentRoot.GetComponent <MCRScene>();

            if (!mcrScene)
            {
                mcrScene = parentRoot.AddComponent <MCRScene>();
            }

            string currentSceneName    = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
            string fullSavedAssetsPath = MCRConstant.BakeAssetSavePath + "/" + currentSceneName + "_MCRBakeAsset" + ".mcr";

            List <ClusterInfo> allClusterList = new List <ClusterInfo>();
            List <VertexInfo>  allVertexList  = new List <VertexInfo>();

            //提取cluster
            foreach (MeshFilter mf in allFilters)
            {
                if (!mf.sharedMesh || !mf.gameObject.activeInHierarchy)
                {
                    continue;
                }

                MeshRenderer render = mf.GetComponent <MeshRenderer>();
                if (!render)
                {
                    continue;
                }

                if (render.sharedMaterials.Length != mf.sharedMesh.subMeshCount)
                {
                    Debug.LogError(mf.gameObject.name + "材质数量与submesh不匹配" + ",submeshcout:" + mf.sharedMesh.subMeshCount + ",材质数量:" + render.sharedMaterials.Length);
                    continue;
                }

                //if(mf.sharedMesh.subMeshCount > 1)
                //{
                //    Debug.LogError("不支持多submesh的物体:" + mf.gameObject.name + ",submeshcout:" + mf.sharedMesh.subMeshCount);
                //    continue;
                //}

                List <Vector3> vertex    = new List <Vector3>();
                List <int>     triangles = new List <int>();
                List <Vector2> uv        = new List <Vector2>();
                List <Vector3> normal    = new List <Vector3>();
                List <Color>   color     = new List <Color>();

                mf.sharedMesh.GetVertices(vertex);
                mf.sharedMesh.GetUVs(0, uv);
                mf.sharedMesh.GetColors(color);
                mf.sharedMesh.GetNormals(normal);

                //处理submesh
                for (int k = 0; k < mf.sharedMesh.subMeshCount; k++)
                {
                    triangles.Clear();
                    mf.sharedMesh.GetTriangles(triangles, k);

                    //读取三角形顶点信息
                    for (int i = 0; i < triangles.Count; i += 3)
                    {
                        VertexInfo vertex1 = new VertexInfo();
                        int        index0  = triangles[i];
                        vertex1.worldPos    = mf.transform.localToWorldMatrix.MultiplyPoint(vertex[index0]);
                        vertex1.worldNormal = mf.transform.localToWorldMatrix.MultiplyVector(normal[index0]);
                        vertex1.UV0         = uv[index0];
                        //看看有没有顶点色
                        if (color.Count > 0)
                        {
                            vertex1.Color = new float4(color[index0].r, color[index0].g, color[index0].b, color[index0].a);
                        }

                        allVertexList.Add(vertex1);

                        VertexInfo vertex2 = new VertexInfo();
                        int        index1  = triangles[i + 1];
                        vertex2.worldPos    = mf.transform.localToWorldMatrix.MultiplyPoint(vertex[index1]);
                        vertex2.worldNormal = mf.transform.localToWorldMatrix.MultiplyVector(normal[index1]);
                        vertex2.UV0         = uv[index1];
                        //看看有没有顶点色
                        if (color.Count > 0)
                        {
                            vertex2.Color = new float4(color[index1].r, color[index1].g, color[index1].b, color[index1].a);
                        }

                        allVertexList.Add(vertex2);

                        VertexInfo vertex3 = new VertexInfo();
                        int        index2  = triangles[i + 2];
                        vertex3.worldPos    = mf.transform.localToWorldMatrix.MultiplyPoint(vertex[index2]);
                        vertex3.worldNormal = mf.transform.localToWorldMatrix.MultiplyVector(normal[index2]);
                        vertex3.UV0         = uv[index2];

                        //看看有没有顶点色
                        if (color.Count > 0)
                        {
                            vertex3.Color = new float4(color[index2].r, color[index2].g, color[index2].b, color[index2].a);
                        }

                        allVertexList.Add(vertex3);

                        //完成了一个Cluster所需要的顶点数
                        if ((allVertexList.Count % MCRConstant.CLUSTER_VERTEX_COUNT) == 0 && allVertexList.Count > 0)
                        {
                            allClusterList.Add(BuildCluster(allVertexList));
                        }
                    }
                }
            }

            int packCout = allVertexList.Count - (MCRConstant.CLUSTER_VERTEX_COUNT * allClusterList.Count);

            //完了之后看看顶点数是否刚好够,不够的话,补上去
            if (packCout > 0)
            {
                int dt = MCRConstant.CLUSTER_VERTEX_COUNT - packCout;
                for (int i = 0; i < dt; i++)
                {
                    VertexInfo info = allVertexList[allVertexList.Count - 1];
                    allVertexList.Add(info);
                }

                allClusterList.Add(BuildCluster(allVertexList));
            }

            AddRedundancyCluster(allClusterList);

            //保存cluster
            SaveClustetInfo(allClusterList, allVertexList, fullSavedAssetsPath);
            mcrScene.context.ClusterInfoAssetsPath = fullSavedAssetsPath.Replace(MCRConstant.BakeAssetSavePath + "/", string.Empty);
            mcrScene.context.ClusterCount          = allClusterList.Count;
            mcrScene.context.VertexCount           = allVertexList.Count;
        }