Esempio n. 1
0
        /// <summary>
        /// Apply all the mest details to the object
        /// </summary>
        /// <param name="baseobject">The object to add the mesh too</param>
        public void ApplyMeshDetails(GameObject baseobject)
        {
#if DEBUG
            CenterMesh(baseobject);

            Mesh mesh = new Mesh();
            mesh.name = baseobject.name + "Mesh";
            baseobject.GetComponent <MeshFilter>().mesh = mesh;
            mesh.vertices = MeshVertices.ToArray();
            mesh.uv       = MeshUVs.ToArray();

            // Create the material and assign triangles
            Renderer        r         = baseobject.GetComponent <Renderer>();
            List <Material> materials = new List <Material>();
            int             count     = 0;
            mesh.subMeshCount = _meshMaterialsTriangles.Count;

            foreach (MaterialFrequency mf in RoadConstructorHelper.MaterialFrequencySet.GetDetails)
            {
                int[] tris = GetTriangles(mf.Material.name).ToArray();
                if (tris.Length == 0)
                {
                    continue;
                }

                materials.Add(mf.Material);
                mesh.SetTriangles(tris, count++);
            }

            mesh.subMeshCount = count; // just in case we didn't add all of them

            r.materials = materials.ToArray();

            mesh.RecalculateNormals();
            mesh.RecalculateTangents();
            mesh.RecalculateBounds();

            if (RoadConstructorHelper.Lighting.BakedLighting)
            {
                if (count != 0)
                {
                    UnwrapParam up = new UnwrapParam();
                    up.hardAngle  = RoadConstructorHelper.Lighting.HardAngle;
                    up.packMargin = RoadConstructorHelper.Lighting.PackMargin;
                    up.angleError = RoadConstructorHelper.Lighting.AngleError;
                    up.areaError  = RoadConstructorHelper.Lighting.AngleError;

                    Unwrapping.GenerateSecondaryUVSet(mesh, up);
                }
            }

#if UNITY_5_5_OR_NEWER
#if UNITY_EDITOR
            UnityEditor.MeshUtility.Optimize(mesh);
#endif
#else
            mesh.Optimize();
#endif
#endif
        }
Esempio n. 2
0
        /// <summary>
        /// Apply the mesh details to the object
        /// </summary>
        /// <param name="baseobject">The object to add the mesh too</param>
        public void ApplyMeshDetails(GameObject baseobject)
        {
#if DEBUG
            Mesh mesh = new Mesh();
            mesh.name = "RoadMesh";
            baseobject.GetComponent <MeshFilter>().mesh = mesh;

            ApplyObjectOffSet(baseobject.transform.position);

            mesh.vertices = MeshVertices.ToArray();
            mesh.uv       = MeshUVs.ToArray();

            // Create the material and assign triangles
            Renderer        r         = baseobject.GetComponent <Renderer>();
            List <Material> materials = new List <Material>();
            int             count     = 0;
            mesh.subMeshCount = _meshMaterialsTriangles.Count;

            List <Material> AllMaterials = FindAllMaterials(baseobject);

            foreach (KeyValuePair <string, List <int> > meshTris in _meshMaterialsTriangles)
            {
                if (meshTris.Value.Count == 0)
                {
                    continue;
                }

                Material mat = AllMaterials.Find((m) => m.name == meshTris.Key);
                materials.Add(mat);
                mesh.SetTriangles(meshTris.Value.ToArray(), count++);
            }

            mesh.subMeshCount = count; // just in case we didn't add all of them

            r.materials = materials.ToArray();
            mesh.RecalculateNormals();
            mesh.RecalculateTangents();
            mesh.RecalculateBounds();

            if (RoadConstructorHelper.Lighting.BakedLighting)
            {
                Debug.Log("Appling unwrapping for Baked Lighting");

                UnwrapParam up = new UnwrapParam();
                up.hardAngle  = RoadConstructorHelper.Lighting.HardAngle;
                up.packMargin = RoadConstructorHelper.Lighting.PackMargin;
                up.angleError = RoadConstructorHelper.Lighting.AngleError;
                up.areaError  = RoadConstructorHelper.Lighting.AngleError;

                Unwrapping.GenerateSecondaryUVSet(mesh, up);
            }

#if UNITY_5_5_OR_NEWER
#if UNITY_EDITOR
            // TODO: Added option UnityEditor.MeshUtility.SetMeshCompression(mesh, UnityEditor.ModelImporterMeshCompression.);
            UnityEditor.MeshUtility.Optimize(mesh);
#endif
#else
            mesh.Optimize();
#endif
#endif
        }