StretchyTanks.StretchyTanks.changeTextures C# (CSharp) Method

changeTextures() public method

public changeTextures ( ) : void
return void
        public void changeTextures()
        {
            // upgrade to new system
            if (textureType >= 0)
            {
                switch (textureType)
                {
                    case 1:
                        textureSet = "Stockalike";
                        break;
                    case 2:
                        textureSet = "German";
                        break;
                    case 3:
                        textureSet = "Saturn";
                        break;
                    case 4:
                        textureSet = "SegmentedSRB";
                        break;
                    default:
                        textureSet = "Original";
                        break;
                }
                textureType = -1;
            }

            // get texture
            List<ConfigNode> textureSets = GetTextures();
            ConfigNode texInfo = null;
            foreach (ConfigNode t in textureSets)
                if (t.name.Equals(textureSet))
                    texInfo = t;
            if (texInfo == null)
            {
                print("*ST* Missing texture " + textureSet);
                if (textureSets.Count > 0)
                    texInfo = textureSets[0];
                else
                {
                    print("*ST* No Texturesets found!");
                    return;
                }
            }

            // Sanity check
            if (texInfo.GetNode("sides") == null || texInfo.GetNode("ends") == null)
            {
                print("*ST* Invalid Textureset " + textureSet);
                return;
            }
            if (!texInfo.GetNode("sides").HasValue("texture") || !texInfo.GetNode("ends").HasValue("texture"))
            {
                print("*ST* Invalid Textureset " + textureSet);
                return;
            }

            // Apply texture

            // get settings
            Vector2 scaleUV = new Vector2(2f, 1f);
            string sides, ends, sidesBump = "";
            bool autoScale = false;
            bool autoWidthDivide = false;
            float autoHeightSteps = 0f;
            sides = texInfo.GetNode("sides").GetValue("texture");
            ends = texInfo.GetNode("ends").GetValue("texture");
            if (texInfo.GetNode("sides").HasValue("bump"))
                sidesBump = texInfo.GetNode("sides").GetValue("bump");

            float ftmp;
            bool btmp;
            if (texInfo.GetNode("sides").HasValue("uScale"))
                if (float.TryParse(texInfo.GetNode("sides").GetValue("uScale"), out ftmp))
                    scaleUV.x = ftmp;
            if (texInfo.GetNode("sides").HasValue("vScale"))
                if (float.TryParse(texInfo.GetNode("sides").GetValue("vScale"), out ftmp))
                    scaleUV.y = ftmp;

            if (texInfo.GetNode("sides").HasValue("autoScale"))
                if (bool.TryParse(texInfo.GetNode("sides").GetValue("autoScale"), out btmp))
                    autoScale = btmp;
            if (texInfo.GetNode("sides").HasValue("autoWidthDivide"))
                if (bool.TryParse(texInfo.GetNode("sides").GetValue("autoWidthDivide"), out btmp))
                    autoWidthDivide = btmp;
            if (texInfo.GetNode("sides").HasValue("autoHeightSteps"))
                if (float.TryParse(texInfo.GetNode("sides").GetValue("autoHeightSteps"), out ftmp))
                    autoHeightSteps = ftmp;

            Texture main = null;
            Texture bump = null;
            Texture secondary = null;

            bool bumpEnabled = !sidesBump.Equals("");

            Texture[] textures = Resources.FindObjectsOfTypeAll(typeof(Texture)) as Texture[];
            foreach (Texture t in textures)
            {
                if (t.name.Equals(sides))
                {
                    main = t;
                }
                if (bumpEnabled && t.name.Equals(sidesBump))
                {
                    bump = t;
                }
                if (t.name.Equals(ends))
                {
                    secondary = t;
                }
            }
            if (main == null || secondary == null)
            {
                print("*ST* Textures not found for " + textureSet);
                return;
            }

            Transform tankModel = transform.GetChild(0).GetChild(0).GetChild(0);

            // Set shaders
            if (!part.Modules.Contains("ModulePaintable"))
            {
                if (bump != null)
                    tankModel.renderer.material.shader = Shader.Find("KSP/Bumped");
                else
                    tankModel.renderer.material.shader = Shader.Find("KSP/Diffuse");

                // top is no longer specular ever, just diffuse.
                tankModel.GetChild(0).renderer.material.shader = Shader.Find("KSP/Diffuse");
            }

            // set up UVs
            if (autoScale)
            {
                scaleUV.x = (float)Math.Round(scaleUV.x * getMappingRadialFactor());
                if (scaleUV.x < 1)
                    scaleUV.x = 1;
                if (autoWidthDivide)
                {
                    if (autoHeightSteps > 0)
                        scaleUV.y = (float)Math.Ceiling(scaleUV.y * stretchFactor / scaleUV.x * (1f / autoHeightSteps)) * autoHeightSteps;
                    else
                        scaleUV.y = scaleUV.y * stretchFactor / scaleUV.x;
                }
                else
                {
                    scaleUV.y *= stretchFactor;
                }
            }

            // apply
            tankModel.renderer.material.mainTextureScale = scaleUV;
            tankModel.renderer.material.SetTexture("_MainTex", main);
            if (bump != null)
            {
                tankModel.renderer.material.SetTextureScale("_BumpMap", scaleUV);
                tankModel.renderer.material.SetTexture("_BumpMap", bump);
            }
            tankModel.GetChild(0).renderer.material.SetTexture("_MainTex", secondary);
        }