Exemplo n.º 1
0
 internal static Texture2D GetIconTexture(SourceTexture Source)
 {
     if (Source == SourceTexture.SpringObjects)
     {
         return(Game1.objectSpriteSheet);
     }
     else if (Source == SourceTexture.Craftables)
     {
         return(Game1.bigCraftableSpriteSheet);
     }
     else if (Source == SourceTexture.Debris)
     {
         return(Game1.debrisSpriteSheet);
     }
     else if (Source == SourceTexture.Tools)
     {
         return(Game1.toolSpriteSheet);
     }
     else if (Source == SourceTexture.Cursors)
     {
         return(Game1.mouseCursors);
     }
     else
     {
         return(null);
     }
 }
        private Color ExtractColorFromImage(int x, int y)
        {
            var res   = (int)(_scale / TextureScale);
            var image = SourceTexture.GetData();

            image.Lock();
            var color = image.GetPixel((x * res) + (res / 2), (y * res) + (res / 2));

            image.Unlock();
            return(color);
        }
Exemplo n.º 3
0
    public static SourceTexture GrabTexture(string rawStringPath)
    {
        SourceTexture srcTexture = null;

        string actualLocation = Locate(rawStringPath);

        if (loadedTextures.ContainsKey(actualLocation))
        {
            srcTexture = loadedTextures[actualLocation];
        }
        else
        {
            srcTexture = new SourceTexture(actualLocation);

            if (File.Exists(ApplicationPreferences.texturesDir + srcTexture.location + ".png"))
            {
                byte[] bytes = null;
                try { bytes = File.ReadAllBytes(ApplicationPreferences.texturesDir + srcTexture.location + ".png"); }
                catch (System.Exception e) { Debug.Log(e.Message); }
                if (bytes != null)
                {
                    srcTexture.texture = new Texture2D(0, 0);
                    srcTexture.texture.LoadImage(bytes);
                    bytes = null;
                }

                //Debug.Log("New Texture Loaded: " + ApplicationPreferences.texturesDir + srcTexture.location + ".png");
            }
            else if (Resources.Load <Texture2D>("Textures/Plain/" + srcTexture.location) != null)
            {
                srcTexture.texture = Resources.Load <Texture2D>("Textures/Plain/" + srcTexture.location);
                //Debug.Log("New Texture Loaded: " + "Textures/Plain/" + srcTexture.location + ".png");
            }
            else
            {
                Debug.Log("Could not find Texture: " + srcTexture.location + ".png");
            }

            if (srcTexture.texture != null)
            {
                if (ApplicationPreferences.averageTextures)
                {
                    AverageTexture(srcTexture.texture);
                }
                else if (ApplicationPreferences.decreaseTextureSizes)
                {
                    DecreaseTextureSize(srcTexture.texture, ApplicationPreferences.maxSizeAllowed);
                }
                srcTexture.texture.wrapMode = TextureWrapMode.Repeat;
            }
        }

        return(srcTexture);
    }
Exemplo n.º 4
0
        public void OnRender()
        {
            if (!(Context.CurrentPipeline is ScreenPipeline))
            {
                return;
            }

            vao.Bind();

            _shader.Bind();
            _shader.SetMatrix4("Model", GetModelMatrix());
            SourceTexture.Bind(0);
            _shader.SetInt("screenTexture", 0);

            GraphicsDevice.Default.CullFace = false;
            vao.Draw();
            GraphicsDevice.Default.CullFace = true;
        }
Exemplo n.º 5
0
 public unsafe void Load(byte[] buffer)
 {
     if (buffer == null)
     {
         return;
     }
     string theString;
     fixed (byte* pBuffer = &buffer[0])
     {
         theString = BufferReader.ReadString(pBuffer, buffer.Length);
     }
     Tokenizer tokenizer = new Tokenizer(theString, _datExpressions, addNewLine: true);
     tokenizer.First();
     Tokenizer.Token token;
     Character currentCharacter;
     while ((token = tokenizer.Current()) != null)
     {
         if (token.Type == CommonParser.EXPRESSION_COMMENT || token.Type == Tokenizer.EXPRESSION_END_OF_LINE)
         {
             CommonParser.GoToValue(tokenizer);
             continue;
         }
         if (token.Type != Tokenizer.EXPRESSION_NUMBER)
         {
             throw new OpenSAGEException(ErrorCode.AptParser, "Token '$TOKEN$' was not recognized", "token", token);
         }
         currentCharacter = new Character(uint.Parse(token.Value));
         if ((token = tokenizer.Next()).Type != EXPRESSION_IMAGE && token.Type != EXPRESSION_TEXTURE)
         {
             throw new OpenSAGEException(ErrorCode.AptParser, "Token '$TOKEN$' was not recognized", "token", token);
         }
         ISource source = null;
         switch (token.Type)
         {
             case EXPRESSION_IMAGE:
                 SourceImage sourceImage = new SourceImage();
                 token = tokenizer.Next();
                 if (token.Type == EXPRESSION_SOURCE_STANDARD)
                 {
                     sourceImage.Type = SourceImageType.STANDARD;
                 }
                 else if (token.Type == EXPRESSION_SOURCE_CLASSIC)
                 {
                     sourceImage.Type = SourceImageType.CLASSIC;
                 }
                 else
                 {
                     throw new OpenSAGEException(ErrorCode.AptParser, "Token '$TOKEN$' was not recognized", "token", token);
                 }
                 sourceImage.X = int.Parse(token.Match.Groups["X"].Value);
                 sourceImage.Y = int.Parse(token.Match.Groups["Y"].Value);
                 sourceImage.Width = int.Parse(token.Match.Groups["Width"].Value);
                 sourceImage.Height = int.Parse(token.Match.Groups["Height"].Value);
                 source = sourceImage;
                 break;
             case EXPRESSION_TEXTURE:
                 SourceTexture sourceTexture = new SourceTexture();
                 token = tokenizer.Next();
                 if (token.Type != Tokenizer.EXPRESSION_NUMBER)
                 {
                     throw new OpenSAGEException(ErrorCode.AptParser, "Token '$TOKEN$' was not recognized", "token", tokenizer.Current());
                 }
                 sourceTexture.Id = int.Parse(token.Value);
                 source = sourceTexture;
                 break;
         }
         currentCharacter.Source = source;
         _characters.Add(currentCharacter);
         tokenizer.Next();
     }
 }
 public void UnloadDataset()
 {
     SourceTexture.Dispose();
     //mSourceTexture.Dispose();
 }
Exemplo n.º 7
0
#pragma warning disable RECS0018 // Comparison of floating point numbers with equality operator
        private void Read(Leaf leaf)
        {
            var bspVertices = map.Lumps.GetVertices();

            var leafFaces = map.Lumps.GetLeafFaces();
            var faces     = map.Lumps.GetFaces();
            var edges     = map.Lumps.GetEdges();
            var surfEdges = map.Lumps.GetSurfaceEdges();
            var dispInfos = map.Lumps.GetDisplacementInfos();

            for (var leafFaceIndex = leaf.FirstLeafFace; leafFaceIndex < leaf.FirstLeafFace + leaf.LeafFacesCount; leafFaceIndex++)
            {
                var  faceIndex     = leafFaces[leafFaceIndex];
                var  face          = faces[faceIndex];
                var  texInfo       = map.Lumps.GetTextureInfo()[face.TextureInfo];
                var  texData       = map.Lumps.GetTextureData()[texInfo.TextureData];
                var  textureOffset = map.Lumps.GetTextureDataStringTable()[texData.NameStringTableId];
                var  textureName   = map.Lumps.GetTextureDataString()[textureOffset];
                uint rootVertex    = 0;

                SourceMaterial material = resourceManager.Get <SourceMaterial>(textureName);
                SourceTexture  texture  = resourceManager.Get <SourceTexture>(material?.BaseTextureName);

                float texWidth  = texData.Width;
                float texHeight = texData.Height;
                if (texture != null)
                {
                    if (texture.Header.Width != texData.Width || texture.Header.Height != texData.Height)
                    {
                        texWidth  = texture.Header.Width;
                        texHeight = texture.Header.Height;
                    }
                }

                var faceVertices = new Dictionary <uint, uint>(); //Just to make sure that there's no duplicates

                for (var surfEdgeIndex = face.FirstEdge; surfEdgeIndex < face.FirstEdge + face.EdgesCount; surfEdgeIndex++)
                {
                    var edgeIndex = surfEdges[surfEdgeIndex];
                    var edge      = edges[Math.Abs(edgeIndex)];

                    //Push the two vertices of the first edge
                    if (surfEdgeIndex == face.FirstEdge)
                    {
                        if (!faceVertices.ContainsKey(edge.VertexIndex[edgeIndex > 0 ? 0 : 1]))
                        {
                            rootVertex = edge.VertexIndex[edgeIndex > 0 ? 0 : 1];

                            var position = bspVertices[rootVertex];
                            var vertex   = new Vertex
                            {
                                Position     = position,
                                TextureCoord = CalculateUV(position, texInfo.TextureVecsS, texInfo.TextureVecsT, texWidth, texHeight)
                            };
                            faceVertices.Add(rootVertex, (uint)vertices.Count);
                            vertices.Add(vertex);
                        }
                        continue;
                    }

                    //Edge must not be connected to the root vertex
                    if (edge.VertexIndex[0] == rootVertex || edge.VertexIndex[1] == rootVertex)
                    {
                        continue;
                    }

                    //Edge is on opposite side of the first edge => push the
                    for (var i = 0; i < 2; i++)
                    {
                        if (!faceVertices.ContainsKey(edge.VertexIndex[i]))
                        {
                            var position = bspVertices[edge.VertexIndex[i]];
                            var vertex   = new Vertex
                            {
                                Position     = position,
                                TextureCoord = CalculateUV(position, texInfo.TextureVecsS, texInfo.TextureVecsT, texWidth, texHeight)
                            };
                            faceVertices.Add(edge.VertexIndex[i], (uint)vertices.Count);
                            vertices.Add(vertex);
                        }
                    }

                    var indices = GetIndices(textureName);

                    indices.Add(faceVertices[rootVertex]);
                    if (edgeIndex < 0)
                    {
                        indices.Add(faceVertices[edge.VertexIndex[1]]);
                        indices.Add(faceVertices[edge.VertexIndex[0]]);
                    }
                    else
                    {
                        indices.Add(faceVertices[edge.VertexIndex[0]]);
                        indices.Add(faceVertices[edge.VertexIndex[1]]);
                    }
                }

                if (face.DisplacementInfo != -1)
                {
                    var dispInfo = dispInfos[face.DisplacementInfo];
                    Read(dispInfo, texInfo);
                }
            }
        }
Exemplo n.º 8
0
    //public void BuildMap()
    //{
    //    StartCoroutine("StartBuilding");
    //}

    public void BuildMap()
    {
        //mapTextures = new List<Texture2D>();
        //textureLocations = new List<string>();
        bool usingPlainTextures = false;

        try
        {
            Debug.Log(mapLocation + mapName + ".bsp");
            if (mapLocation.Length > 0 && File.Exists(mapLocation + mapName + ".bsp"))
            {
                mapFile = new FileStream(mapLocation + mapName + ".bsp", FileMode.Open);
            }
            else if (File.Exists("Assets\\Resources\\Maps\\" + mapName + ".bsp"))
            {
                mapFile = new FileStream("Assets\\Resources\\Maps\\" + mapName + ".bsp", FileMode.Open);
            }
        }
        catch (System.Exception e) { Debug.Log(e.Message); }

        if (mapFile != null)
        {
            ReadFile();

            #region Load Static Props
            for (int i = 0; i < staticProps.staticPropDict.names.Length; i++)
            {
                string modelName = staticProps.staticPropDict.names[i], modelLocation = staticProps.staticPropDict.names[i];
                modelName     = modelName.Substring(modelName.LastIndexOf("/") + 1);
                modelName     = modelName.Substring(0, modelName.LastIndexOf("."));
                modelLocation = modelLocation.Substring(0, modelLocation.LastIndexOf("/"));

                SourceModel.GrabModel(modelName, ApplicationPreferences.modelsDir + modelLocation);
            }
            #endregion

            mainSurfaceMaterial = Resources.Load <Material>("Materials/MapMaterial");
            mapGameObject       = new GameObject(mapName);

            List <FaceMesh> allFaces = new List <FaceMesh>();
            #region Parse Faces
            foreach (dface_t face in faces)
            {
                FaceMesh currentFace = new FaceMesh();
                currentFace.face = face;

                #region Get Texture Info
                //texflags textureFlag = texflags.SURF_NODRAW;
                try { currentFace.textureFlag = ((texflags)texInfo[face.texinfo].flags); }
                catch (System.Exception) { }

                currentFace.rawTexture = textureStringData.Substring(Mathf.Abs(texStringTable[Mathf.Abs(texData[Mathf.Abs(texInfo[Mathf.Abs(face.texinfo)].texdata)].nameStringTableID)]));
                currentFace.rawTexture = currentFace.rawTexture.Substring(0, currentFace.rawTexture.IndexOf(BSPParser.TEXTURE_STRING_DATA_SPLITTER));
                SourceTexture srcTexture = SourceTexture.GrabTexture(currentFace.rawTexture);
                currentFace.textureLocation = srcTexture.location;

                currentFace.s       = new Vector3(texInfo[face.texinfo].textureVecs[0][0], texInfo[face.texinfo].textureVecs[0][2], texInfo[face.texinfo].textureVecs[0][1]);
                currentFace.t       = new Vector3(texInfo[face.texinfo].textureVecs[1][0], texInfo[face.texinfo].textureVecs[1][2], texInfo[face.texinfo].textureVecs[1][1]);
                currentFace.xOffset = texInfo[face.texinfo].textureVecs[0][3];
                currentFace.yOffset = texInfo[face.texinfo].textureVecs[1][3];

                bool undesired = false;
                foreach (string undesiredTexture in undesiredTextures)
                {
                    if (currentFace.rawTexture.Equals(undesiredTexture))
                    {
                        undesired = true; break;
                    }
                }
                #endregion

                if (!undesired && (currentFace.textureFlag & texflags.SURF_SKY2D) != texflags.SURF_SKY2D && (currentFace.textureFlag & texflags.SURF_SKY) != texflags.SURF_SKY && (currentFace.textureFlag & texflags.SURF_NODRAW) != texflags.SURF_NODRAW && (currentFace.textureFlag & texflags.SURF_SKIP) != texflags.SURF_SKIP)
                {
                    //if(!mapTextures.ContainsKey(srcTexture.location)) mapTextures.Add(srcTexture.location, srcTexture);
                    if (textureLocations.IndexOf(srcTexture.location) < 0)
                    {
                        mapTextures.Add(srcTexture);
                        textureLocations.Add(srcTexture.location);
                    }

                    currentFace.mesh = MakeFace(face);
                    currentFace.localToWorldMatrix = mapGameObject.transform.localToWorldMatrix;
                    allFaces.Add(currentFace);
                }
            }
            #endregion

            //Debug.Log("Parsed " + allFaces.Count + " Faces");

            if (!ApplicationPreferences.combineMeshes)
            {
                foreach (FaceMesh faceMesh in allFaces)
                {
                    //GameObject faceGO = new GameObject(faceMesh.textureLocation);
                    GameObject faceGO = new GameObject(faceMesh.rawTexture);
                    faceGO.transform.parent = mapGameObject.transform;
                    MeshFilter theFilter = faceGO.AddComponent <MeshFilter>();
                    theFilter.mesh = faceMesh.mesh;

                    #region Add Vertices as Children

                    /*foreach (Vector3 vertex in theFilter.mesh.vertices)
                     * {
                     *  GameObject sphereVertex = new GameObject();
                     *  sphereVertex.name = vertex.ToString();
                     *  sphereVertex.transform.position = vertex;
                     *  sphereVertex.transform.localScale = new Vector3(10f, 10f, 10f);
                     *  sphereVertex.transform.parent = faceGO.transform;
                     * }*/
                    #endregion

                    #region Set Material of GameObject
                    Material faceMaterial = mainSurfaceMaterial;

                    //int textureIndex = textureLocations.IndexOf(faceMesh.textureLocation);
                    //int textureIndex = textureLocations.IndexOf(faceMesh.rawTexture);
                    Texture2D faceTexture = null;
                    if (textureLocations.IndexOf(faceMesh.textureLocation) > -1)
                    {
                        faceTexture = mapTextures[textureLocations.IndexOf(faceMesh.textureLocation)].texture;
                    }
                    if (faceTexture != null)
                    {
                        faceMaterial = new Material(ApplicationPreferences.mapMaterial);
                        faceMaterial.mainTextureScale  = new Vector2(1, 1);
                        faceMaterial.mainTextureOffset = new Vector2(0, 0);
                        faceMaterial.mainTexture       = faceTexture;
                        faceTexture = null;
                    }
                    faceGO.AddComponent <MeshRenderer>().material = faceMaterial;
                    #endregion
                }

                #region Static Props
                GameObject staticPropsObject = new GameObject("StaticProps");
                staticPropsObject.transform.parent = mapGameObject.transform;
                for (int i = 0; i < staticProps.staticPropInfo.Length; i++)
                {
                    string modelName = staticProps.staticPropDict.names[staticProps.staticPropInfo[i].PropType], modelLocation = staticProps.staticPropDict.names[staticProps.staticPropInfo[i].PropType];
                    modelName     = modelName.Substring(modelName.LastIndexOf("/") + 1);
                    modelName     = modelName.Substring(0, modelName.LastIndexOf("."));
                    modelLocation = modelLocation.Substring(0, modelLocation.LastIndexOf("/"));

                    SourceModel propModel   = SourceModel.GrabModel(modelName, ApplicationPreferences.modelsDir + modelLocation);
                    GameObject  propModelGO = propModel.InstantiateGameObject();
                    propModelGO.transform.position      = staticProps.staticPropInfo[i].Origin;
                    propModelGO.transform.localRotation = Quaternion.Euler(staticProps.staticPropInfo[i].Angles.x, staticProps.staticPropInfo[i].Angles.y + 0, staticProps.staticPropInfo[i].Angles.z);
                    propModelGO.transform.parent        = staticPropsObject.transform;
                }
                #endregion

                //Debug.Log("Made Seperate Meshes");
            }
            else
            {
                #region Add Static Prop Meshes to allFaces and mapTextures
                for (int i = 0; i < staticProps.staticPropInfo.Length; i++)
                {
                    string modelName = staticProps.staticPropDict.names[staticProps.staticPropInfo[i].PropType], modelLocation = staticProps.staticPropDict.names[staticProps.staticPropInfo[i].PropType];
                    modelName     = modelName.Substring(modelName.LastIndexOf("/") + 1);
                    modelName     = modelName.Substring(0, modelName.LastIndexOf("."));
                    modelLocation = modelLocation.Substring(0, modelLocation.LastIndexOf("/"));

                    SourceModel propModel = SourceModel.GrabModel(modelName, ApplicationPreferences.modelsDir + modelLocation);
                    for (int j = 0; j < propModel.modelMeshes.Length; j++)
                    {
                        FaceMesh propMesh = new FaceMesh();
                        propMesh.mesh = propModel.modelMeshes[j];
                        if (j < propModel.modelTextures.Length)
                        {
                            if (textureLocations.IndexOf(propModel.modelTextures[j].location) < 0)
                            {
                                mapTextures.Insert(0, propModel.modelTextures[j]);
                                textureLocations.Insert(0, propModel.modelTextures[j].location);
                            }
                            propMesh.textureLocation = propModel.modelTextures[j].location;

                            #region GameObject for Position and Rotation
                            GameObject propModelGO = new GameObject("Empty");
                            propModelGO.transform.position      = staticProps.staticPropInfo[i].Origin;
                            propModelGO.transform.localRotation = Quaternion.Euler(staticProps.staticPropInfo[i].Angles.x, staticProps.staticPropInfo[i].Angles.y + 0, staticProps.staticPropInfo[i].Angles.z);
                            propMesh.localToWorldMatrix         = propModelGO.transform.localToWorldMatrix;
                            Object.DestroyImmediate(propModelGO);
                            #endregion
                        }
                        allFaces.Add(propMesh);
                    }
                }
                #endregion
                #region Create Atlas & Remap UVs
                AtlasMapper customAtlas = new AtlasMapper();
                if (usingPlainTextures)
                {
                    customAtlas.cushion = 0;
                }
                customAtlas.AddTextures(GetTexturesAsArray());
                Texture2D packedMapTextures = customAtlas.atlas;
                Rect[]    uvReMappers       = customAtlas.mappedUVs;
                Material  mapAtlas          = new Material(ApplicationPreferences.mapAtlasMaterial);
                if (usingPlainTextures)
                {
                    mapAtlas.SetFloat("_uv1FracOffset", 0.07f);
                }
                mapAtlas.mainTextureScale = new Vector2(1f, 1f);
                mapAtlas.mainTexture      = packedMapTextures;
                //mapAtlas.mainTexture.wrapMode = TextureWrapMode.Clamp;
                //List<string> textureKeys = mapTextures.Keys.ToList();
                for (int i = 0; i < allFaces.Count; i++)
                {
                    //if (i < 10) { Debug.Log(i + " Triangles: " + allFaces[i].mesh.triangles.Length); }
                    int textureIndex = textureLocations.IndexOf(allFaces[i].textureLocation);
                    //int textureIndex = textureKeys.IndexOf(allFaces[i].textureLocation);
                    //Texture2D faceTexture = null;
                    if (textureIndex > -1 && textureIndex < uvReMappers.Length)
                    {
                        //faceTexture = mapTextures[textureIndex];
                        Rect      surfaceTextureRect   = uvReMappers[textureIndex];
                        Mesh      surfaceMesh          = allFaces[i].mesh;
                        Vector2[] atlasTexturePosition = new Vector2[surfaceMesh.uv.Length];
                        Vector2[] atlasTextureSize     = new Vector2[surfaceMesh.uv.Length];
                        for (int j = 0; j < atlasTexturePosition.Length; j++)
                        {
                            atlasTexturePosition[j] = new Vector2(surfaceTextureRect.x + 0.0f, surfaceTextureRect.y + 0.0f);
                            atlasTextureSize[j]     = new Vector2(surfaceTextureRect.width - 0.0f, surfaceTextureRect.height - 0.0f);
                        }
                        surfaceMesh.uv2 = atlasTexturePosition;
                        surfaceMesh.uv3 = atlasTextureSize;
                    }
                }
                #endregion
                Debug.Log("Created Atlas and Remapped UVs");
                #region Calculate Minimum Submeshes Needed
                List <List <int> > combinesIndices = new List <List <int> >();
                combinesIndices.Add(new List <int>());
                int vertexCount = 0;
                for (int i = 0; i < allFaces.Count; i++)
                {
                    if (vertexCount + allFaces[i].mesh.vertices.Length >= ushort.MaxValue)
                    {
                        combinesIndices.Add(new List <int>());
                        vertexCount = 0;
                    }

                    combinesIndices[combinesIndices.Count - 1].Add(i);
                    vertexCount += allFaces[i].mesh.vertices.Length;
                }
                #endregion
                Debug.Log("Calculated Submeshes needed");
                #region Combine Meshes to Submeshes
                if (combinesIndices.Count == 1)
                {
                    CombineInstance[] currentCombine = new CombineInstance[combinesIndices[0].Count];
                    for (int i = 0; i < currentCombine.Length; i++)
                    {
                        currentCombine[i].mesh      = allFaces[combinesIndices[0][i]].mesh;
                        currentCombine[i].transform = allFaces[combinesIndices[0][i]].localToWorldMatrix;
                    }

                    Mesh combinedMesh = new Mesh();
                    combinedMesh.name = "Custom Combined Mesh";
                    combinedMesh.CombineMeshes(currentCombine);
                    mapGameObject.AddComponent <MeshFilter>().mesh       = combinedMesh;
                    mapGameObject.AddComponent <MeshRenderer>().material = mapAtlas;
                }
                else
                {
                    GameObject[] partialMeshes = new GameObject[combinesIndices.Count];
                    for (int i = 0; i < combinesIndices.Count; i++)
                    {
                        CombineInstance[] currentCombine = new CombineInstance[combinesIndices[i].Count];
                        for (int j = 0; j < currentCombine.Length; j++)
                        {
                            currentCombine[j].mesh      = allFaces[combinesIndices[i][j]].mesh;
                            currentCombine[j].transform = allFaces[combinesIndices[i][j]].localToWorldMatrix;
                        }

                        partialMeshes[i] = new GameObject(mapName + " Part " + (i + 1));
                        Mesh combinedMesh = new Mesh();
                        combinedMesh.name = "Custom Combined Mesh " + (i + 1);
                        combinedMesh.CombineMeshes(currentCombine);
                        partialMeshes[i].AddComponent <MeshFilter>().mesh       = combinedMesh;
                        partialMeshes[i].AddComponent <MeshRenderer>().material = mapAtlas;
                        partialMeshes[i].AddComponent <MeshCollider>();
                        partialMeshes[i].transform.parent = mapGameObject.transform;
                    }
                }
                #endregion
                //Debug.Log("Combined Meshes into Submeshes");
            }
        }
        else
        {
            mapGameObject = Object.Instantiate(Resources.Load("Models/CSGOMaps/" + mapName)) as GameObject;
        }

        //SaveUVValues("C:\\Users\\oxter\\Documents\\" + mapName + "_UV.txt");
    }
 protected override Vector2 GetAutomataBounds()
 {
     return(SourceTexture.GetSize() * TextureScale);
 }
Exemplo n.º 10
0
    public static SourceModel GrabModel(string name, string location)
    {
        SourceModel model = null;

        if (loadedModels.ContainsKey(location + name))
        {
            model = loadedModels[location + name];
        }
        else
        {
            model             = new SourceModel(name, location);
            model.modelPrefab = new GameObject(model.modelName);
            model.modelPrefab.transform.parent = staticPropLibrary.transform;
            model.modelPrefab.SetActive(false);

            model.ReadFiles();

            if (model.mdl == null)
            {
                Debug.Log("MDL missing"); return(null);
            }
            if (model.mdl.bodyParts == null)
            {
                Debug.Log("Body Parts missing"); return(null);
            }

            #region Grabbing Textures
            //if(model.mdl.texturePaths.Length == model.mdl.textures.Length)
            //{
            model.modelTextures = new SourceTexture[model.mdl.textures.Length];
            for (int i = 0; i < model.modelTextures.Length; i++)
            {
                string texturePath = "", textureName = "";
                if (model.mdl.texturePaths != null && model.mdl.texturePaths.Length > 0 && model.mdl.texturePaths[0] != null)
                {
                    texturePath = model.mdl.texturePaths[0];
                }
                if (model.mdl.textures[i] != null)
                {
                    textureName = model.mdl.textures[i].name;
                }
                model.modelTextures[i] = SourceTexture.GrabTexture(texturePath + textureName);
                //Debug.Log("Attempted to grab texture: " + model.modelTextures[i].location);
            }
            //}

            /*for (int i = 0; i < model.mdl.texturePaths.Length; i++)
             * {
             *  Debug.Log(model.mdl.texturePaths[i]);
             * }
             * for (int i = 0; i < model.mdl.textures.Length; i++)
             * {
             *  Debug.Log(model.mdl.textures[i].name);
             * }*/
            #endregion

            #region Building
            int         textureIndex = 0;
            List <Mesh> meshes       = new List <Mesh>();
            for (int i = 0; i < model.mdl.bodyParts.Length; i++)
            {
                GameObject bodyPartRepresentation = new GameObject(model.mdl.bodyParts[i].name);
                bodyPartRepresentation.transform.parent = model.modelPrefab.transform;
                for (int j = 0; j < model.mdl.bodyParts[i].models.Length; j++)
                {
                    GameObject modelRepresentation = new GameObject(new string(model.mdl.bodyParts[i].models[j].name));
                    modelRepresentation.transform.parent = bodyPartRepresentation.transform;

                    int currentPosition = 0;
                    for (int k = 0; k < model.mdl.bodyParts[i].models[j].theMeshes.Length; k++)
                    {
                        GameObject meshRepresentation = new GameObject(model.mdl.bodyParts[i].models[j].theMeshes[k].id.ToString());
                        meshRepresentation.transform.parent = modelRepresentation.transform;

                        Vector3[] vertices = new Vector3[model.mdl.bodyParts[i].models[j].theMeshes[k].vertexData.lodVertexCount[0]];
                        Vector3[] normals  = new Vector3[model.mdl.bodyParts[i].models[j].theMeshes[k].vertexData.lodVertexCount[0]];
                        Vector2[] uv       = new Vector2[model.mdl.bodyParts[i].models[j].theMeshes[k].vertexData.lodVertexCount[0]];
                        for (int l = 0; l < vertices.Length; l++)
                        {
                            if (currentPosition < model.vvd.vertices[0].Length)
                            {
                                vertices[l] = model.vvd.vertices[0][currentPosition].m_vecPosition;
                            }
                            if (currentPosition < model.vvd.vertices[0].Length)
                            {
                                normals[l] = model.vvd.vertices[0][currentPosition].m_vecNormal;
                            }
                            if (currentPosition < model.vvd.vertices[0].Length)
                            {
                                uv[l] = model.vvd.vertices[0][currentPosition].m_vecTexCoord;
                            }
                            currentPosition++;
                        }

                        int[] triangles = new int[model.vtx.bodyParts[i].theVtxModels[j].theVtxModelLods[0].theVtxMeshes[k].theVtxStripGroups[0].theVtxIndices.Length];
                        for (int l = 0; l < model.vtx.bodyParts[i].theVtxModels[j].theVtxModelLods[0].theVtxMeshes[k].theVtxStripGroups[0].theVtxIndices.Length; l++)
                        {
                            triangles[l + 0] = model.vtx.bodyParts[i].theVtxModels[j].theVtxModelLods[0].theVtxMeshes[k].theVtxStripGroups[0].theVtxVertices[model.vtx.bodyParts[i].theVtxModels[j].theVtxModelLods[0].theVtxMeshes[k].theVtxStripGroups[0].theVtxIndices[l + 0]].originalMeshVertexIndex;
                        }

                        Mesh mesh = new Mesh();
                        mesh.name      = "Custom Mesh";
                        mesh.vertices  = vertices;
                        mesh.triangles = triangles;
                        mesh.normals   = normals;
                        mesh.uv        = uv;
                        meshes.Add(mesh);

                        MeshFilter mesher = meshRepresentation.AddComponent <MeshFilter>();
                        mesher.sharedMesh = mesh;

                        Material meshMaterial = new Material(ApplicationPreferences.playerMaterial);
                        if (model.modelTextures != null && textureIndex < model.modelTextures.Length)
                        {
                            meshMaterial.mainTexture = model.modelTextures[textureIndex].texture; textureIndex++;
                        }
                        meshRepresentation.AddComponent <MeshRenderer>().material = meshMaterial;
                        meshRepresentation.AddComponent <MeshCollider>();
                    }
                }
            }
            model.modelMeshes = meshes.ToArray();
            #endregion
        }

        return(model);
    }