Exemplo n.º 1
0
        internal static void FinalizeVisualMesh(Transform meshGroupHolder,
                                                Mesh mesh,
                                                Material material,
                                                CSGBuildSettings buildSettings,
                                                CSGBuildContext.BuildContext buildContext,
                                                List <Vector3> positionsList,
                                                List <Vector3> normalsList,
                                                List <Vector2> uvList,
                                                List <Color> colorsList,
                                                List <int> trianglesList,
                                                MaterialMeshDictionary materialMeshDictionary)
        {
            Vector3[] positionsArray = new Vector3[positionsList.Count];
            Vector3[] normalsArray   = new Vector3[normalsList.Count];
            Vector2[] uvArray        = new Vector2[uvList.Count];
            Color[]   colorsArray    = new Color[colorsList.Count];
            int[]     trianglesArray = new int[trianglesList.Count];

            positionsList.CopyTo(positionsArray);
            normalsList.CopyTo(normalsArray);
            uvList.CopyTo(uvArray);
            trianglesList.CopyTo(trianglesArray);
            colorsList.CopyTo(colorsArray);

            mesh.vertices = positionsArray;
            mesh.normals  = normalsArray;
            mesh.uv       = uvArray;
            mesh.colors   = colorsArray;

            if (meshGroupHolder.position != Vector3.zero ||
                meshGroupHolder.rotation != Quaternion.identity ||
                meshGroupHolder.lossyScale != Vector3.one)
            {
                mesh.LocalizeToTransform(meshGroupHolder);
            }

            mesh.triangles = trianglesArray;

            if (buildSettings.GenerateTangents)
            {
                // Generate tangents, necessary for some shaders
                mesh.GenerateTangents();
            }

            buildContext.buildMetrics.TotalMeshes++;
            buildContext.buildMetrics.TotalVertices  += positionsArray.Length;
            buildContext.buildMetrics.TotalTriangles += trianglesArray.Length / 3;

            GameObject newGameObject = new GameObject("MaterialMesh");

            // Allow any editor dependent code to fire, e.g. lightmap unwrapping, static flags
            if (OnFinalizeVisualMesh != null)
            {
                OnFinalizeVisualMesh(newGameObject, mesh);
            }

            newGameObject.AddComponent <MeshFilter>().sharedMesh = mesh;
            MeshRenderer meshRenderer = newGameObject.AddComponent <MeshRenderer>();

            meshRenderer.sharedMaterial       = material;
            meshRenderer.shadowCastingMode    = buildSettings.ShadowCastingMode;
            meshRenderer.reflectionProbeUsage = buildSettings.ReflectionProbeUsage;
            //newGameObject.transform.parent = meshGroupHolder;

            newGameObject.transform.SetParent(meshGroupHolder, false);

#if UNITY_EDITOR
            if (buildSettings.SaveMeshesAsAssets)
            {
                // Make sure the folder exists to save into
                string path = SceneManager.GetActiveScene().path;
                path = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
                if (!Directory.Exists(path))
                {
                    UnityEditor.AssetDatabase.CreateFolder(Path.GetDirectoryName(path), Path.GetFileName(path));
                }
                // Save to a file rather than leaving it as a scene asset
                UnityEditor.AssetDatabase.CreateAsset(mesh, Path.Combine(path, "VisualMesh" + materialMeshDictionary.MeshCount + ".asset"));
            }
#endif

            materialMeshDictionary.Add(material, mesh, newGameObject);
        }
        internal static void FinalizeVisualMesh(Transform meshGroupHolder,
                                                Mesh mesh,
                                                Material material,
                                                CSGBuildSettings buildSettings,
                                                CSGBuildContext.BuildContext buildContext,
                                                List <Vector3> positionsList,
                                                List <Vector3> normalsList,
                                                List <Vector2> uvList,
                                                List <Color> colorsList,
                                                List <int> trianglesList,
                                                MaterialMeshDictionary materialMeshDictionary)
        {
            Vector3[] positionsArray = new Vector3[positionsList.Count];
            Vector3[] normalsArray   = new Vector3[normalsList.Count];
            Vector2[] uvArray        = new Vector2[uvList.Count];
            Color[]   colorsArray    = new Color[colorsList.Count];
            int[]     trianglesArray = new int[trianglesList.Count];

            positionsList.CopyTo(positionsArray);
            normalsList.CopyTo(normalsArray);
            uvList.CopyTo(uvArray);
            trianglesList.CopyTo(trianglesArray);
            colorsList.CopyTo(colorsArray);

            mesh.vertices = positionsArray;
            mesh.normals  = normalsArray;
            mesh.uv       = uvArray;
            mesh.colors   = colorsArray;


            if (meshGroupHolder.position != Vector3.zero ||
                meshGroupHolder.rotation != Quaternion.identity ||
                meshGroupHolder.lossyScale != Vector3.one)
            {
                mesh.LocalizeToTransform(meshGroupHolder);
            }

            mesh.triangles = trianglesArray;

            if (buildSettings.GenerateTangents)
            {
                // Generate tangents, necessary for some shaders
                mesh.GenerateTangents();
            }

            buildContext.buildMetrics.TotalMeshes++;
            buildContext.buildMetrics.TotalVertices  += positionsArray.Length;
            buildContext.buildMetrics.TotalTriangles += trianglesArray.Length / 3;

            GameObject newGameObject = new GameObject("MaterialMesh");

            // Allow any editor dependent code to fire, e.g. lightmap unwrapping, static flags
            if (OnFinalizeVisualMesh != null)
            {
                OnFinalizeVisualMesh(newGameObject, mesh);
            }


            newGameObject.AddComponent <MeshFilter>().sharedMesh       = mesh;
            newGameObject.AddComponent <MeshRenderer>().sharedMaterial = material;
//			newGameObject.transform.parent = meshGroupHolder;
            newGameObject.transform.SetParent(meshGroupHolder, false);

            materialMeshDictionary.Add(material, mesh, newGameObject);
        }