Esempio n. 1
0
    static void GenerateCollision(BSPFile bsp, GameObject parent, BSPFace face)
    {
        GameObject levelCollision = PrefabCache.InstantiatePrefab("LevelCollision", "Assets/Prefabs");

        levelCollision.transform.parent = parent.transform;
        levelCollision.isStatic         = parent.isStatic;

        var verts = face.vertices;

        var collider = levelCollision.AddComponent <MeshCollider>();

        List <Vector3> vertices  = new List <Vector3>();
        List <int>     triangles = new List <int>();

        for (int vi = 0, ti = 0; vi < verts.Length; vi += 3, ti += 3)
        {
            vertices.Add(BSPFile.TransformVector(verts[vi + 0]));
            vertices.Add(BSPFile.TransformVector(verts[vi + 1]));
            vertices.Add(BSPFile.TransformVector(verts[vi + 2]));

            triangles.Add(ti + 2);
            triangles.Add(ti + 1);
            triangles.Add(ti + 0);
        }

        Mesh mesh = new Mesh();

        mesh.name      = "Collision Mesh";
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.RecalculateNormals();

        collider.sharedMesh = mesh;
    }
Esempio n. 2
0
        public void ClassifyFaceSpanTest()
        {
            // Arrange
            BSPFace face = new BSPFace();

            face.Points.Add(new BSPVertex {
                Point = new Vector3(-1.0f, -2.0f, 0.0f)
            });
            face.Points.Add(new BSPVertex {
                Point = new Vector3(0.0f, 2.0f, 0.0f)
            });
            face.Points.Add(new BSPVertex {
                Point = new Vector3(1.0f, -2.0f, 0.0f)
            });
            face.Point  = face.Points[0].Point;
            face.Normal = new Vector3(0.0f, 0.0f, 1.0f);

            Vector3 planePoint  = new Vector3(0.0f, 1.0f, 0.0f); // Just a basic plane
            Vector3 planeNormal = new Vector3(0.0f, 1.0f, 0.0f);

            BSPTree tree = new BSPTree();

            // Act
            tree.ClassifyFace(face, planePoint, planeNormal);

            // Assert
            Assert.AreEqual(BSPClassification.Spanning, face.Classification);
        }
Esempio n. 3
0
    BSPFace[] CreateFaces(GEOMETRY_T geometry)
    {
        var faces = geometry.faces;

        var collisions = new BSPFace[faces.Length];

        for (int i = 0; i < faces.Length; ++i)
        {
            var vertices = getVertices(geometry, faces[i]);
            collisions[i] = new BSPFace(i, vertices);
        }
        return(collisions);
    }
Esempio n. 4
0
    /// <summary>
    /// Reads face data and generates UV coordinates.
    /// </summary>
    private void AddFaceToMeshData(BSPFace f, bool hasTexture, bool hasLightmap)
    {
        int firstindex = f.edges[0].i1;

        for (int i = 1; i < f.edges.Count; i++)
        {
            //vertices and triangles
            verts.Add(BSPFile.verts[firstindex]);
            tris.Add(verts.Count - 1);
            verts.Add(BSPFile.verts[f.edges[i].i1]);
            tris.Add(verts.Count - 1);
            verts.Add(BSPFile.verts[f.edges[i].i2]);
            tris.Add(verts.Count - 1);

            //texture uvs
            if (hasTexture)
            {
                uvs.Add(Utils.GetUV(BSPFile.verts[firstindex], f));
                uvs.Add(Utils.GetUV(BSPFile.verts[f.edges[i].i1], f));
                uvs.Add(Utils.GetUV(BSPFile.verts[f.edges[i].i2], f));
            }

            //lightmap uvs
            if (hasLightmap)
            {
                //fetch unscaled lightmap uvs
                Rect    r   = f.atlas.rect[f.atlas_index];
                Vector2 uv1 = Utils.GetLightmapUV(BSPFile.verts[firstindex], f);
                Vector2 uv2 = Utils.GetLightmapUV(BSPFile.verts[f.edges[i].i1], f);
                Vector2 uv3 = Utils.GetLightmapUV(BSPFile.verts[f.edges[i].i2], f);

                //normalize the uvs
                uv1.x = uv1.x - Mathf.Floor(uv1.x);
                uv2.x = uv2.x - Mathf.Floor(uv2.x);
                uv3.x = uv3.x - Mathf.Floor(uv3.x);

                uv1.y = uv1.y - Mathf.Floor(uv1.y);
                uv2.y = uv2.y - Mathf.Floor(uv2.y);
                uv3.y = uv3.y - Mathf.Floor(uv3.y);

                //project uvs on the texture atlas
                uvs2.Add(uv1 * r.size + r.min);
                uvs2.Add(uv2 * r.size + r.min);
                uvs2.Add(uv3 * r.size + r.min);
            }
        }
    }
Esempio n. 5
0
    public static Vector2 GetLightmapUV(Vector3 p, BSPFace f)
    {
        float scale = Globals.scale.Value;

        Vector2 uv = new Vector2();

        uv.x = Vector3.Dot(p / scale, f.BSPTexInfo.u_axis) + f.BSPTexInfo.u_offset / scale;
        uv.y = Vector3.Dot(p / scale, f.BSPTexInfo.v_axis) + f.BSPTexInfo.v_offset / scale;

        float s = ((f.lightmap.width << 4) + 8) - f.lightmap.u_min;
        float t = ((f.lightmap.height << 4) + 8) - f.lightmap.v_min;

        uv.x += s;
        uv.y += t;

        uv.x *= 1f / (f.lightmap.width * 16f);
        uv.y *= 1f / (f.lightmap.height * 16f);
        return(uv);
    }
Esempio n. 6
0
    public static Vector2 GetUV(Vector3 p, BSPFace f)
    {
        float scale = Globals.scale.Value;

        if (f.BSPTexInfo.texfile_guid == Guid.Empty)
        {
            //no texture
            return(new Vector2());
        }
        Vector2 dims = TextureLoader.GetTextureDimensions(f.BSPTexInfo.texfile_guid);

        float u = Vector3.Dot(p, f.BSPTexInfo.u_axis) + f.BSPTexInfo.u_offset;
        float v = Vector3.Dot(p, f.BSPTexInfo.v_axis) + f.BSPTexInfo.v_offset;

        //scale uvs by texture dimensions
        u /= dims.x * scale;
        v /= dims.y * scale;

        return(new Vector2(u, v));
    }
Esempio n. 7
0
    private void LoadMap()
    {
        map = new BSPMap(MapFileName);

        int curModelCount = 0;

        foreach (BSPModel model in map.models)
        {
            GameObject modelObj = new GameObject("model_" + curModelCount);
            modelObj.transform.parent = transform;

            int findex = 0;

            for (int f = 0; f < model.faceCount; f++)
            {
                findex = (int)model.faceIndex + f;

                BSPFace face = map.faces[findex];

                if (IgnoreTriggers && map.textures[(int)map.textureSurfaces[face.textureInfoIndex].textureIndex].name.Contains("trigger"))
                {
                    continue;
                }

                // Vertices
                Vector3[] vertices = new Vector3[face.edgeCount];

                int index = 0;

                for (int i = (int)face.edgeListIndex; i < (int)face.edgeListIndex + face.edgeCount; i++)
                {
                    if (map.edgeList[(int)face.edgeListIndex + index] < 0)
                    {
                        vertices[index] = map.vertices[map.edges[Mathf.Abs(map.edgeList[i])].startIndex];
                    }
                    else
                    {
                        vertices[index] = map.vertices[map.edges[map.edgeList[i]].endIndex];
                    }

                    index++;
                }

                // Triangles
                int[] triangles = new int[(face.edgeCount - 2) * 3];

                int step = 1;

                for (int i = 1; i < vertices.Length - 1; i++)
                {
                    triangles[step - 1] = 0;
                    triangles[step]     = i;
                    triangles[step + 1] = i + 1;
                    step += 3;
                }

                // UVs
                BSPTextureSurface textureSurface = map.textureSurfaces[face.textureInfoIndex];
                Vector2[]         uvs            = new Vector2[face.edgeCount];

                for (int i = 0; i < face.edgeCount; i++)
                {
                    uvs[i].x = ((Vector3.Dot(vertices[i], textureSurface.u) + textureSurface.uOffset) / (float)map.textures[(int)map.textureSurfaces[face.textureInfoIndex].textureIndex].width);
                    uvs[i].y = ((Vector3.Dot(vertices[i], textureSurface.v) + textureSurface.vOffset) / (float)map.textures[(int)map.textureSurfaces[face.textureInfoIndex].textureIndex].height);
                }

                // Create the mesh for the face
                GameObject faceObj = new GameObject();
                Mesh       mesh    = new Mesh();
                mesh.vertices  = vertices;
                mesh.uv        = uvs;
                mesh.triangles = triangles;
                mesh.RecalculateNormals();
                faceObj.AddComponent <MeshFilter>().mesh = mesh;
                MeshRenderer rend = faceObj.AddComponent <MeshRenderer>();

                rend.material.shader                 = Shader.Find("Unlit/Texture");
                rend.material.mainTexture            = map.textures[(int)map.textureSurfaces[face.textureInfoIndex].textureIndex].texture;
                rend.material.mainTexture.filterMode = FilterMode.Point;
                faceObj.transform.parent             = modelObj.transform;
            }

            curModelCount++;
        }
    }
Esempio n. 8
0
    GameObject GenerateFaceObject(BSPFace face)
    {
        GameObject faceObject = new GameObject("BSPface " + faceCount.ToString());

        faceObject.transform.parent = gameObject.transform;
        Mesh faceMesh = new Mesh();

        faceMesh.name = "BSPmesh";



        // grab our verts
        Vector3[] verts    = new Vector3[face.numberEdges];
        int       edgestep = (int)face.firstEdgeIndex;

        for (int i = 0; i < face.numberEdges; i++)
        {
            if (map.edgeLump.SURFEDGES[face.firstEdgeIndex + i] < 0)
            {
                verts[i] = map.vertLump.ConvertScaleVertex(map.vertLump.verts[map.edgeLump.edges[Mathf.Abs(map.edgeLump.SURFEDGES[edgestep])].vert1]);
            }
            else
            {
                verts[i] = map.vertLump.ConvertScaleVertex(map.vertLump.verts[map.edgeLump.edges[map.edgeLump.SURFEDGES[edgestep]].vert2]);
            }

            edgestep++;
        }

        // whip up tris
        int[] tris    = new int[(face.numberEdges - 2) * 3];
        int   tristep = 1;

        for (int i = 1; i < verts.Length - 1; i++)
        {
            tris[tristep - 1] = 0;
            tris[tristep]     = i;
            tris[tristep + 1] = i + 1;
            tristep          += 3;
        }

        // whip up uvs
        float scales = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].width;
        float scalet = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].height;

        Vector2[] uvs = new Vector2[face.numberEdges];
        for (int i = 0; i < face.numberEdges; i++)
        {
            uvs[i] = new Vector2((Vector3.Dot(verts[i], map.texinfoLump.texinfo[face.texinfo_id].vec3s) + map.texinfoLump.texinfo[face.texinfo_id].offs) / scales, (Vector3.Dot(verts[i], map.texinfoLump.texinfo[face.texinfo_id].vec3t) + map.texinfoLump.texinfo[face.texinfo_id].offt) / scalet);
        }


        faceMesh.vertices  = verts;
        faceMesh.triangles = tris;
        faceMesh.uv        = uvs;
        faceMesh.RecalculateNormals();
        faceObject.AddComponent <MeshFilter> ();
        faceObject.GetComponent <MeshFilter> ().mesh = faceMesh;
        faceObject.AddComponent <MeshRenderer> ();



        if (face.texinfo_id >= 0 && renderlights && face.lightmapOffset < map.lightlump.Length)
        {
            Material bspMaterial = new Material(Shader.Find("Legacy Shaders/Lightmapped/Diffuse"));
            //bspMaterial.color = new Color(1,1,1,1);



            Vector3 v0, v1;

            Vertex[] pVertexList = new Vertex[verts.Length];

            float fUMin = 100000.0f;
            float fUMax = -10000.0f;
            float fVMin = 100000.0f;
            float fVMax = -10000.0f;

            float pMipTexheight = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].height;
            float pMipTexwidth  = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].width;
            for (int nEdge = 0; nEdge < verts.Length; nEdge++)
            {
                // Add vertex information
                Vertex vertex = new Vertex(verts[nEdge]);

                // Generate texture coordinates for face
                vertex.u  = verts[nEdge].x * map.texinfoLump.texinfo[face.texinfo_id].vec3s.x + verts[nEdge].y * map.texinfoLump.texinfo[face.texinfo_id].vec3s.y + verts[nEdge].z * map.texinfoLump.texinfo[face.texinfo_id].vec3s.z + map.texinfoLump.texinfo[face.texinfo_id].offs;
                vertex.v  = verts[nEdge].x * map.texinfoLump.texinfo[face.texinfo_id].vec3t.x + verts[nEdge].y * map.texinfoLump.texinfo[face.texinfo_id].vec3t.y + verts[nEdge].z * map.texinfoLump.texinfo[face.texinfo_id].vec3t.z + map.texinfoLump.texinfo[face.texinfo_id].offt;
                vertex.u /= pMipTexwidth;
                vertex.v /= pMipTexheight;
                vertex.lu = vertex.u;
                vertex.lv = vertex.v;

                fUMin = (vertex.u < fUMin) ? vertex.u : fUMin;
                fUMax = (vertex.u > fUMax) ? vertex.u : fUMax;
                fVMin = (vertex.v < fVMin) ? vertex.v : fVMin;
                fVMax = (vertex.v > fVMax) ? vertex.v : fVMax;

                pVertexList[nEdge] = vertex;
            }


            int lightMapWidth  = (int)(Mathf.Ceil((fUMax * pMipTexwidth) / 16.0f) - Mathf.Floor((fUMin * pMipTexwidth) / 16.0f) + 1.0f);
            int lightMapHeight = (int)(Mathf.Ceil((fVMax * pMipTexheight) / 16.0f) - Mathf.Floor((fVMin * pMipTexheight) / 16.0f) + 1.0f);

            float cZeroTolerance = 1e-06f;
            // Update light-map vertex u, v coordinates.  These should range from [0.0 -> 1.0] over face.
            float fUDel = (fUMax - fUMin);
            if (fUDel > cZeroTolerance)
            {
                fUDel = 1.0f / fUDel;
            }
            else
            {
                fUDel = 1.0f;
            }
            float fVDel = (fVMax - fVMin);
            if (fVDel > cZeroTolerance)
            {
                fVDel = 1.0f / fVDel;
            }
            else
            {
                fVDel = 1.0f;
            }
            for (int n = 0; n < pVertexList.Length; n++)
            {
                (pVertexList)[n].lu = ((pVertexList)[n].lu - fUMin) * fUDel;
                (pVertexList)[n].lv = ((pVertexList)[n].lv - fVMin) * fVDel;
            }



            //    Debug.Log(lightMapWidth+" "+ lightMapHeight);
            Texture2D lightTex = new Texture2D(lightMapWidth, lightMapHeight);



            Color[] colourarray = new Color[lightMapWidth * lightMapHeight];

            int tempCount = (int)face.lightmapOffset;
            for (int k = 0; k < lightMapWidth * lightMapHeight; k++)
            {
                if (tempCount + 3 > map.lightlump.Length)
                {
                    break;
                }
                colourarray[k] = new Color32(map.lightlump[tempCount], map.lightlump[tempCount + 1], map.lightlump[tempCount + 2], 100);
                tempCount     += 3;
            }

            lightTex.SetPixels(colourarray);
            // lightTex.filterMode = FilterMode.Bilinear;
            lightTex.wrapMode = TextureWrapMode.Clamp;

            lightTex.Apply();

            Texture2D lmap = lightTex;



            List <Vector2> lvs = new List <Vector2>();
            for (int a = 0; a < pVertexList.Length; a++)

            {
                lvs.Add(new Vector2(pVertexList[a].lu, pVertexList[a].lv));
            }
            faceMesh.SetUVs(1, lvs);
            // faceMesh.SetUVs(2, vlist);

            if (map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture == null)
            {
                bspMaterial.mainTexture = missingtexture;
            }
            else
            {
                bspMaterial.mainTexture      = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture;
                bspMaterial.mainTexture.name = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].name;
            }



            bspMaterial.SetTexture("_LightMap", lmap);
            bspMaterial.mainTexture.filterMode = FilterMode.Bilinear;

            faceObject.GetComponent <Renderer>().material = bspMaterial;
        }
        else
        {
            if (map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture == null)
            {
                //faceObject.renderer.material.mainTexture = missingtexture;
                faceObject.GetComponent <Renderer>().material.mainTexture = missingtexture;
                Debug.LogError(map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].name.ToString() + "not loaded");
            }
            else
            {
                faceObject.GetComponent <Renderer>().material.mainTexture = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture;


                faceObject.GetComponent <Renderer>().material.mainTexture.name = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].name;
            }
        }
        faceObject.AddComponent <MeshCollider> ();
        faceObject.isStatic = true;
        //faceObject.renderer.enabled = false;

        return(faceObject);
    }
Esempio n. 9
0
    GameObject GenerateFaceObject(BSPFace face)
    {
        GameObject faceObject = new GameObject("BSPface");

        faceObject.transform.parent = gameObject.transform;
        Mesh faceMesh = new Mesh();

        faceMesh.name = "BSPmesh";

        // grab our verts
        Vector3[] verts    = new Vector3[face.num_ledges];
        int       edgestep = face.ledge_index;

        for (int i = 0; i < face.num_ledges; i++)
        {
            if (map.edgeLump.ledges[face.ledge_index + i] < 0)
            {
                verts[i] = map.vertLump.verts[map.edgeLump.edges[Mathf.Abs(map.edgeLump.ledges[edgestep])].vert1];
            }
            else
            {
                verts[i] = map.vertLump.verts[map.edgeLump.edges[map.edgeLump.ledges[edgestep]].vert2];
            }
            edgestep++;
        }

        // whip up tris
        int[] tris    = new int[(face.num_ledges - 2) * 3];
        int   tristep = 1;

        for (int i = 1; i < verts.Length - 1; i++)
        {
            tris[tristep - 1] = 0;
            tris[tristep]     = i;
            tris[tristep + 1] = i + 1;
            tristep          += 3;
        }

        // whip up uvs
        float scales = map.miptexLump.textures[map.texinfoLump.texinfo[face.texinfo_id].miptex].width * BSP29map.QUAKE_TO_UNITY_CONVERSION_SCALE;
        float scalet = map.miptexLump.textures[map.texinfoLump.texinfo[face.texinfo_id].miptex].height * BSP29map.QUAKE_TO_UNITY_CONVERSION_SCALE;

        Vector2[] uvs = new Vector2[face.num_ledges];
        for (int i = 0; i < face.num_ledges; i++)
        {
            uvs[i]   = new Vector2((Vector3.Dot(verts[i], map.texinfoLump.texinfo[face.texinfo_id].vec3s) + map.texinfoLump.texinfo[face.texinfo_id].offs) / scales, (Vector3.Dot(verts[i], map.texinfoLump.texinfo[face.texinfo_id].vec3t) + map.texinfoLump.texinfo[face.texinfo_id].offt) / scalet);
            uvs[i].y = (1.0f - uvs[i].y);
        }

        faceMesh.vertices  = verts;
        faceMesh.triangles = tris;
        faceMesh.uv        = uvs;
        faceMesh.RecalculateNormals();
        faceObject.AddComponent <MeshFilter>();
        faceObject.GetComponent <MeshFilter>().mesh = faceMesh;
        faceObject.AddComponent <MeshRenderer>();

        // We make a material and then use shared material to work around a leak in the editor
        Material mat         = null;
        string   textureName = map.miptexLump.textures[map.texinfoLump.texinfo[face.texinfo_id].miptex].name;

        if (lightMapsEnabled)
        {
            // Lightmap wankery
            mat = new Material(Shader.Find("Legacy Shaders/Lightmapped/Diffuse"));
            int pointer = face.lightmap;
            if (pointer != -1)
            {
                int       size     = 10;
                Texture2D lightmap = new Texture2D(size, size);

                Color[] colors = new Color[size * size];

                for (int i = 0; i < size * size; i++)
                {
                    if (i >= map.lightLump.RawMaps.Length)
                    {
                        break;
                    }
                    var temp = map.lightLump.RawMaps[pointer + i];
                    colors[i] = new Color32(temp, temp, temp, 255);
                }
                lightmap.SetPixels(colors);

                mat.SetTexture("_LightMap", lightmap);
            }
        }
        else
        {
            if (materialDictionary.ContainsKey(textureName))
            {
                mat = materialDictionary[textureName];
            }
            else
            {
                if (!overrideMaterials)
                {
                    string matReadPath = "Assets/Resources/Materials/" + textureName + ".mat";
                    mat = AssetDatabase.LoadAssetAtPath <Material>(matReadPath);
                    if (mat)
                    {
                        materialDictionary[textureName] = mat;
                    }
                }

                if (mat == null)
                {
                    mat = new Material(Shader.Find("Diffuse"));
                    materialDictionary[textureName] = mat;
                }
            }
        }

        // Set the texture we made above, after possible lightmapping circlejerk
        mat.mainTexture = map.miptexLump.textures[map.texinfoLump.texinfo[face.texinfo_id].miptex];

        // Set the material
        faceObject.GetComponent <Renderer>().sharedMaterial = mat;

        // Turn off the renderer if the face is part of a trigger brush
        string texName = map.miptexLump.textures[map.texinfoLump.texinfo[face.texinfo_id].miptex].name;

        if (texName == "trigger")
        {
            faceObject.GetComponent <Renderer>().enabled = false;
        }

        // Skip any textures that show a sky, if we want
        if (skipSky && texName.StartsWith("sky"))
        {
            faceObject.GetComponent <Renderer>().enabled = false;
        }

        faceObject.AddComponent <MeshCollider>();
        faceObject.isStatic = true;

        return(faceObject);
    }
Esempio n. 10
0
		GameObject GenerateFaceObject (BSPFace face)
		{

        


				GameObject faceObject = new GameObject ("BSPface " + faceCount.ToString ());
				faceObject.transform.parent = gameObject.transform;
				Mesh faceMesh = new Mesh ();
				faceMesh.name = "BSPmesh";




        // grab our verts
        Vector3[] verts = new Vector3[face.numberEdges];
        int edgestep = (int)face.firstEdgeIndex;
        for (int i = 0; i < face.numberEdges; i++)
        {

            if (map.edgeLump.SURFEDGES[face.firstEdgeIndex + i] < 0)
            {
                verts[i] = map.vertLump.ConvertScaleVertex(map.vertLump.verts[map.edgeLump.edges[Mathf.Abs(map.edgeLump.SURFEDGES[edgestep])].vert1]);
              
            }
            else
            {
                verts[i] = map.vertLump.ConvertScaleVertex(map.vertLump.verts[map.edgeLump.edges[map.edgeLump.SURFEDGES[edgestep]].vert2]);
              
            }

            edgestep++;



            }

        // whip up tris
        int[] tris = new int[(face.numberEdges - 2) * 3];
        int tristep = 1;
        for (int i = 1; i < verts.Length - 1; i++)
        {
            tris[tristep - 1] = 0;
            tris[tristep] = i;
            tris[tristep + 1] = i + 1;
            tristep += 3;
        }

        // whip up uvs
        float scales = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].width ;
        float scalet = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].height ;
        Vector2[] uvs = new Vector2[face.numberEdges];
        for (int i = 0; i < face.numberEdges; i++)
        {
         
        uvs[i] = new Vector2((Vector3.Dot(verts[i], map.texinfoLump.texinfo[face.texinfo_id].vec3s) + map.texinfoLump.texinfo[face.texinfo_id].offs) / scales, (Vector3.Dot(verts[i], map.texinfoLump.texinfo[face.texinfo_id].vec3t) + map.texinfoLump.texinfo[face.texinfo_id].offt) / scalet);
        }


        faceMesh.vertices = verts;
				faceMesh.triangles = tris;
				faceMesh.uv = uvs;
				faceMesh.RecalculateNormals ();
				faceObject.AddComponent<MeshFilter> ();
				faceObject.GetComponent<MeshFilter> ().mesh = faceMesh;
				faceObject.AddComponent<MeshRenderer> ();









		if (face.texinfo_id >= 0 && renderlights && face.lightmapOffset < map.lightlump.Length)
				{

						Material bspMaterial = new Material (Shader.Find ("Legacy Shaders/Lightmapped/Diffuse"));
            //bspMaterial.color = new Color(1,1,1,1);
        
						

            Vector3 v0, v1;

            Vertex[] pVertexList = new Vertex[verts.Length];

            float fUMin = 100000.0f;
            float fUMax = -10000.0f;
            float fVMin = 100000.0f;
            float fVMax = -10000.0f;

            float pMipTexheight = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].height;
            float pMipTexwidth = map.miptexLump[map.texinfoLump.texinfo[face.texinfo_id].miptex].width;
            for (int nEdge = 0; nEdge < verts.Length; nEdge++)
            {


                // Add vertex information
                Vertex vertex = new Vertex(verts[nEdge]);

                // Generate texture coordinates for face
                vertex.u = verts[nEdge].x * map.texinfoLump.texinfo[face.texinfo_id].vec3s.x + verts[nEdge].y * map.texinfoLump.texinfo[face.texinfo_id].vec3s.y + verts[nEdge].z * map.texinfoLump.texinfo[face.texinfo_id].vec3s.z + map.texinfoLump.texinfo[face.texinfo_id].offs;
                vertex.v = verts[nEdge].x * map.texinfoLump.texinfo[face.texinfo_id].vec3t.x + verts[nEdge].y * map.texinfoLump.texinfo[face.texinfo_id].vec3t.y + verts[nEdge].z * map.texinfoLump.texinfo[face.texinfo_id].vec3t.z + map.texinfoLump.texinfo[face.texinfo_id].offt;
                vertex.u /= pMipTexwidth;
                vertex.v /= pMipTexheight;
                vertex.lu = vertex.u;
                vertex.lv = vertex.v;

                fUMin = (vertex.u < fUMin) ? vertex.u : fUMin;
                fUMax = (vertex.u > fUMax) ? vertex.u : fUMax;
                fVMin = (vertex.v < fVMin) ? vertex.v : fVMin;
                fVMax = (vertex.v > fVMax) ? vertex.v : fVMax;

                pVertexList[nEdge] = vertex;
            }


            int lightMapWidth = (int)(Mathf.Ceil((fUMax * pMipTexwidth) / 16.0f) - Mathf.Floor((fUMin * pMipTexwidth) / 16.0f) + 1.0f);
            int lightMapHeight = (int)(Mathf.Ceil((fVMax * pMipTexheight) / 16.0f) - Mathf.Floor((fVMin * pMipTexheight) / 16.0f) + 1.0f);

            float cZeroTolerance = 1e-06f;
            // Update light-map vertex u, v coordinates.  These should range from [0.0 -> 1.0] over face.
            float fUDel = (fUMax - fUMin);
            if (fUDel > cZeroTolerance)
                fUDel = 1.0f / fUDel;
            else
                fUDel = 1.0f;
            float fVDel = (fVMax - fVMin);
            if (fVDel > cZeroTolerance)
                fVDel = 1.0f / fVDel;
            else
                fVDel = 1.0f;
            for (int n = 0; n < pVertexList.Length; n++)
            {
                (pVertexList)[n].lu = ((pVertexList)[n].lu - fUMin) * fUDel;
                (pVertexList)[n].lv = ((pVertexList)[n].lv - fVMin) * fVDel;
            }




            //    Debug.Log(lightMapWidth+" "+ lightMapHeight);
            Texture2D lightTex = new Texture2D(lightMapWidth, lightMapHeight);



            Color[] colourarray = new Color[lightMapWidth * lightMapHeight];

            int tempCount = (int)face.lightmapOffset;
            for (int k = 0; k < lightMapWidth * lightMapHeight; k++)
            {
                if (tempCount + 3 > map.lightlump.Length) break;
                colourarray[k] = new Color32(map.lightlump[tempCount], map.lightlump[tempCount + 1], map.lightlump[tempCount + 2], 100);
                tempCount += 3;
            }

            lightTex.SetPixels(colourarray);
           // lightTex.filterMode = FilterMode.Bilinear;
            lightTex.wrapMode = TextureWrapMode.Clamp;
            
            lightTex.Apply();

            Texture2D lmap = lightTex;




            List<Vector2> lvs = new List<Vector2>();
            for (int a=0; a< pVertexList.Length;a++)

            {

                lvs.Add(new Vector2(pVertexList[a].lu, pVertexList[a].lv));


            }
            faceMesh.SetUVs(1, lvs);
            // faceMesh.SetUVs(2, vlist);

            if (map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture == null)
						{
			
								bspMaterial.mainTexture = missingtexture;

						}
						else
						{
								bspMaterial.mainTexture = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture;
								bspMaterial.mainTexture.name = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].name;
						}
			
		


			
						bspMaterial.SetTexture ("_LightMap", lmap);
            bspMaterial.mainTexture.filterMode = FilterMode.Bilinear;
          
                        faceObject.GetComponent<Renderer>().material = bspMaterial;


				}
				else
				{
			
						if (map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture == null)
						{
								//faceObject.renderer.material.mainTexture = missingtexture;
								faceObject.GetComponent<Renderer>().material.mainTexture = missingtexture;
								Debug.LogError (map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].name.ToString () + "not loaded");
						}
						else
						{
								faceObject.GetComponent<Renderer>().material.mainTexture = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].texture;
           

                faceObject.GetComponent<Renderer>().material.mainTexture.name = map.miptexLump [map.texinfoLump.texinfo [face.texinfo_id].miptex].name;
						}

				}
				faceObject.AddComponent<MeshCollider> ();
				faceObject.isStatic = true;
				//faceObject.renderer.enabled = false;

				return faceObject;
		}