static void CreateDisplacements()
        {
            Dictionary <Int32, List <Int32> > MeshInfo = new Dictionary <Int32, List <Int32> >();

            for (Int32 i = 0; i < BSP_DispInfo.Length; i++)
            {
                if (!MeshInfo.ContainsKey(BSP_TexData[BSP_TexInfo[BSP_Faces[BSP_DispInfo[i].MapFace].TexInfo].TexData].NameStringTableID))
                {
                    MeshInfo.Add(BSP_TexData[BSP_TexInfo[BSP_Faces[BSP_DispInfo[i].MapFace].TexInfo].TexData].NameStringTableID, new List <Int32>());
                }

                MeshInfo[BSP_TexData[BSP_TexInfo[BSP_Faces[BSP_DispInfo[i].MapFace].TexInfo].TexData].NameStringTableID].Add(BSP_DispInfo[i].MapFace);
            }

            for (Int32 i = 0; i < BSP_TextureStringData.Length; i++)
            {
                if (!MeshInfo.ContainsKey(i))
                {
                    continue;
                }

                List <Face> Faces = new List <Face>();

                List <Vector3> Vertices  = new List <Vector3>();
                List <Color32> Colors    = new List <Color32>();
                List <Int32>   Triangles = new List <Int32>();
                List <Vector2> UV        = new List <Vector2>();

                for (Int32 j = 0; j < MeshInfo[i].Count; j++)
                {
                    if (BSP_Faces[MeshInfo[i][j]].DispInfo != -1)
                    {
                        Faces.Add(BSP_CDisp[BSP_Faces[MeshInfo[i][j]].DispInfo]);

                        Int32 PointOffset = Vertices.Count;
                        for (Int32 n = 0; n < Faces[j].Triangles.Length; n++)
                        {
                            Triangles.Add(Faces[j].Triangles[n] + PointOffset);
                        }

                        Vertices.AddRange(Faces[j].Vertices);
                        Colors.AddRange(Faces[j].Colors);
                        UV.AddRange(Faces[j].UV);
                    }
                }

                GameObject MeshObject = new GameObject(BSP_TextureStringData[i]);
                MeshObject.transform.localScale = new Vector3(1, 1, -1);
                MeshObject.transform.parent     = BSP_WorldSpawn.transform;
                MeshObject.isStatic             = true;

                MeshRenderer MeshRenderer = MeshObject.AddComponent <MeshRenderer>();
                MeshRenderer.sharedMaterial = MaterialLoader.Load(BSP_TextureStringData[i]);
                MeshRenderer.lightmapIndex  = ConfigLoader.CurrentLightmap;

                if (MaterialLoader.HasAnimation)
                {
                    AnimatedTexture AnimationControlScript = MeshObject.AddComponent <AnimatedTexture>();
                    MaterialLoader.SetupAnimations(ref AnimationControlScript);
                }

                Mesh Mesh = MeshObject.AddComponent <MeshFilter>().sharedMesh = new Mesh();
                MeshObject.AddComponent <MeshCollider>();

                Mesh.SetVertices(Vertices);
                Mesh.SetTriangles(Triangles, 0);
                Mesh.SetColors(Colors);
                Mesh.SetUVs(0, UV);

                {
                    List <Vector2> UV2          = new List <Vector2>();
                    Texture2D      Lightmap_tex = new Texture2D(1, 1);

                    CreateLightMap(Faces, ref Lightmap_tex, ref UV2);

                    if (ConfigLoader.LoadLightmapsAsTextureShader)
                    {
                        if (MeshRenderer.sharedMaterial != null)
                        {
                            MeshRenderer.sharedMaterial.SetTexture("_LightMap", Lightmap_tex);
                        }
                    }

                    Mesh.SetUVs(1, UV2);
                }

                Mesh.RecalculateNormals();
            }
        }
 public static void SetupAnimations(ref AnimatedTexture ControlScript)
 {
     ControlScript.AnimatedTextureFramerate = float.Parse(Items["animatedtextureframerate"]);
     ControlScript.Frames = TextureLoader.Frames;
 }
        static void CreateModels()
        {
            BSP_Brushes = new List <GameObject>();

            for (Int32 Index = 0; Index < BSP_Models.Length; Index++)
            {
                GameObject Model = new GameObject("*" + Index);
                Model.transform.parent = BSP_WorldSpawn.transform;

                Dictionary <Int32, List <Int32> > MeshInfo = new Dictionary <Int32, List <Int32> >();

                for (Int32 i = BSP_Models[Index].FirstFace; i < BSP_Models[Index].FirstFace + BSP_Models[Index].NumFaces; i++)
                {
                    if (!MeshInfo.ContainsKey(BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID))
                    {
                        MeshInfo.Add(BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID, new List <Int32>());
                    }

                    MeshInfo[BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID].Add(i);
                    MeshInfo[BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID].Add(i);
                }

                for (Int32 i = 0; i < BSP_TextureStringData.Length; i++)
                {
                    if (!MeshInfo.ContainsKey(i))
                    {
                        continue;
                    }

                    List <Face> Faces = new List <Face>();

                    List <Vector3> Vertices  = new List <Vector3>();
                    List <Color32> Colors    = new List <Color32>();
                    List <Int32>   Triangles = new List <Int32>();
                    List <Vector2> UV        = new List <Vector2>();

                    for (Int32 j = 0; j < MeshInfo[i].Count; j++)
                    {
                        dface_t CFace = BSP_Faces[MeshInfo[i][j]];

                        if (CFace.DispInfo == -1)
                        {
                            Faces.Add(BSP_CFaces[MeshInfo[i][j]]);

                            Int32 PointOffset = Vertices.Count;
                            for (Int32 n = 0; n < Faces[j].Triangles.Length; n++)
                            {
                                Triangles.Add(Faces[j].Triangles[n] + PointOffset);
                            }

                            Vertices.AddRange(Faces[j].Vertices);
                            Colors.AddRange(Faces[j].Colors);
                            UV.AddRange(Faces[j].UV);
                        }
                    }

                    GameObject MeshObject = new GameObject(BSP_TextureStringData[i]);
                    MeshObject.transform.parent = Model.transform;
                    MeshObject.isStatic         = true;

                    MeshRenderer MeshRenderer = MeshObject.AddComponent <MeshRenderer>();
                    MeshRenderer.sharedMaterial    = MaterialLoader.Load(BSP_TextureStringData[i]);
                    MeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
                    MeshRenderer.lightmapIndex     = ConfigLoader.CurrentLightmap;

                    if (MaterialLoader.HasAnimation)
                    {
                        AnimatedTexture AnimationControlScript = MeshObject.AddComponent <AnimatedTexture>();
                        MaterialLoader.SetupAnimations(ref AnimationControlScript);
                    }

                    Mesh Mesh = MeshObject.AddComponent <MeshFilter>().sharedMesh = new Mesh();
                    Mesh.SetVertices(Vertices);
                    Mesh.SetTriangles(Triangles, 0);
                    Mesh.SetColors(Colors);
                    Mesh.SetUVs(0, UV);

                    if (BSP_TextureStringData[i].Contains("TOOLS/"))
                    {
                        MeshRenderer.enabled = false;
                        BSP_Brushes.Add(MeshObject);
                    }
                    else
                    {
                        MeshObject.AddComponent <MeshCollider>();

                        List <Vector2> UV2          = new List <Vector2>();
                        Texture2D      Lightmap_tex = new Texture2D(1, 1);

                        CreateLightMap(Faces, ref Lightmap_tex, ref UV2);

                        if (ConfigLoader.LoadLightmapsAsTextureShader)
                        {
                            if (MeshRenderer.sharedMaterial != null)
                            {
                                MeshRenderer.sharedMaterial.SetTexture("_LightMap", Lightmap_tex);
                            }
                        }

                        Mesh.SetUVs(1, UV2);
                    }

                    Mesh.RecalculateNormals();
                }
            }
        }