Example #1
0
        public static void DumpCarPrefab(CommandArg[] args)
        {
            if (Terminal.IssuedError)
            {
                return;
            }

            string name = args[0].String;

            if (Enum.TryParse(name, out TrainCarType carType))
            {
                GameObject prefab = CarTypes.GetCarPrefab(carType);
                if (!prefab)
                {
                    Debug.LogError($"CarType {name} has missing prefab");
                    return;
                }

                JToken contents = GameObjectDumper.DumpObject(prefab);
                GameObjectDumper.SendJsonToFile(name, "prefab", contents);
            }
            else
            {
                Debug.LogWarning("Invalid car type " + name);
            }
        }
Example #2
0
        public static void ExportInteriorColliders(CommandArg[] args)
        {
            if (Terminal.IssuedError)
            {
                return;
            }

            string name = args[0].String;

            if (Enum.TryParse(name, out TrainCarType carType))
            {
                GameObject prefab = CarTypes.GetCarPrefab(carType);
                if (!prefab)
                {
                    Debug.LogError($"CarType {name} has missing prefab");
                    return;
                }

                TrainCar car = prefab.GetComponent <TrainCar>();
                if (!car)
                {
                    Debug.LogError($"Couldn't find TrainCar on carType {name}");
                    return;
                }

                if (!car.interiorPrefab)
                {
                    Debug.LogWarning($"TrainCar on carType {name} doesn't have an interiorPrefab assigned");
                    return;
                }

                var colliderJson = GetCollidersRecursive(car.interiorPrefab.transform);
                GameObjectDumper.SendJsonToFile(name, "interior_colliders", colliderJson);
            }
        }
Example #3
0
        public SGLib()
        {
            if (Instance != null)
            {
                Instance = null;
            }
            Instance = this;

            ExportVanillaTextures();
            StoreVanillaTextures();

            var sourceMat = new Material(
                CarTypes.GetCarPrefab(TrainCarType.LocoDiesel).GetComponent <TrainCar>().interiorPrefab.transform
                .Find("offset/I Indicator lamps/I gauges backlights/lamp emission indicator/gauge_labels").GetComponent <MeshRenderer>().sharedMaterial
                );

            // SHUNTER
            tex_loco_621 = new TextureSet();
            TryLoadTextures(Tex_LocoShunter, "shunt_gauges_01", TexPath_LocoShunter + Main.settings.currentLocoShunterDirName, 1024, 1024);
            mat_loco_621_gauge = new Material(sourceMat);

            // STEAMER
            tex_loco_steam_H = new TextureSet();
            TryLoadTextures(Tex_LocoSteam_Gauge, "SH_gauges_01", TexPath_LocoSteamHeavy + Main.settings.currentLocoSteamerDirName, 1024, 1024);
            mat_loco_steam_H_gauge = new Material(sourceMat);

            tex_loco_steam_H_WaterLevel = new TextureSet();
            TryLoadTextures(Tex_LocoSteam_WaterLevel, "waterlevel_01", TexPath_LocoSteamHeavy + Main.settings.currentLocoSteamerDirName, 8, 128);
            mat_loco_steam_H_WaterLevel = new Material(sourceMat);

            // DIESEL
            tex_LocoDiesel = new TextureSet();
            TryLoadTextures(Tex_LocoDiesel, "LocoDiesel_gauges_01", TexPath_LocoDiesel + Main.settings.currentLocoDieselDirName, 2048, 1024);
            mat_LocoDiesel_gauge = new Material(sourceMat);
        }
        public bool Receive(TrainCarInformationPacket packet, ClientId client)
        {
            var player = MultiPlayerManager.Instance.RemotePlayers[client];

            Logger.LogInfo($"Spawning remote car {packet.CarType} at {packet.Position} moved by {WorldMover.currentMove} for {player}");

            var prefab = CarTypes.GetCarPrefab(packet.CarType);

            var(rail, _) = RailTrack.GetClosest(packet.Position + WorldMover.currentMove);
            TrainCar train;

            using (SpawningCar)
                train = CarSpawner.SpawnCar(prefab, rail, packet.Position + WorldMover.currentMove, packet.Forward);

            if (train.GetComponent <LocoControllerShunter>())
            {
                train.gameObject.AddComponent <LocoStateShunterSync>().Init(packet.Id);
            }
            //else if (train.GetComponent<LocoControllerDiesel>()) train.gameObject.AddComponent<LocoStateDieselSync>().Init(packet.Id);
            //else if (train.GetComponent<LocoControllerSteam>()) train.gameObject.AddComponent<LocoStateHandcarSync>().Init(packet.Id);
            //else if (train.GetComponent<LocoControllerHandcar>()) train.gameObject.AddComponent<LocoStateHandcarSync>().Init(packet.Id);
            //else if (train.GetComponent<LocoControllerBase>()) train.gameObject.AddComponent<LocoStateSync>().Init(packet.Id);
            else
            {
                train.gameObject.AddComponent <TrainCarSync>().Init(packet.Id);
            }

            train.logicCar.ID = packet.Name;
            train.trainPlatesCtrl.trainCarPlates.ForEach(plate => plate.id.text = packet.Name);

            return(true);
        }
Example #5
0
        public static void ExportLocoControllerCurves(CommandArg[] args)
        {
            if (Terminal.IssuedError)
            {
                return;
            }

            string name = args[0].String;

            if (Enum.TryParse(name, out TrainCarType carType))
            {
                GameObject prefab = CarTypes.GetCarPrefab(carType);
                if (!prefab)
                {
                    Debug.LogError($"CarType {name} has missing prefab");
                    return;
                }

                LocoControllerBase locoController = prefab.GetComponent <LocoControllerBase>();
                if (!locoController)
                {
                    Debug.LogWarning($"CarType {name} prefab does not have a loco controller");
                    return;
                }

                var props = new JObject();

                // brake & traction
                JObject brakeCurve = ComponentsToJson.AnimationCurve(locoController.brakePowerCurve);
                props.Add("brakePowerCurve", brakeCurve);

                if (locoController is LocoControllerDiesel lcd)
                {
                    var tractionCurve = ComponentsToJson.AnimationCurve(lcd.tractionTorqueCurve);
                    props.Add("tractionTorqueCurve", tractionCurve);
                }
                else if (locoController is LocoControllerSteam lcs)
                {
                    var tractionCurve = ComponentsToJson.AnimationCurve(lcs.tractionTorqueCurve);
                    props.Add("tractionTorqueCurve", tractionCurve);
                }
                else if (locoController is LocoControllerShunter lcShunt)
                {
                    var tractionCurve = ComponentsToJson.AnimationCurve(lcShunt.tractionTorqueCurve);
                    props.Add("tractionTorqueCurve", tractionCurve);
                }

                // driving force
                props.Add("drivingForce", ComponentsToJson.DrivingForce(locoController.drivingForce));

                GameObjectDumper.SendJsonToFile(name, "loco_curves", props);
            }
        }
Example #6
0
        private static void RestoreShunterDefaults(TrainCar trainCar)
        {
            var prefab   = CarTypes.GetCarPrefab(TrainCarType.LocoShunter).GetComponent <TrainCar>().interiorPrefab.transform;
            var gaugeMat = prefab.Find("C dashboard buttons controller/I gauges backlights/lamp emmision indicator/gauge_stickers model").GetComponent <MeshRenderer>().material;

            gaugeMat.SetColor("_EmissionColor", trainCar.interior.GetComponentInChildren <ShunterDashboardControls>().cabLightRotary.GetComponent <ControlImplBase>().Value > 0 ? Color.white : Color.black);

            // set gauges material
            trainCar.interior.Find($"loco_621_interior(Clone)/C dashboard buttons controller/I gauges backlights/lamp emmision indicator/gauge_stickers model")
            .GetComponent <MeshRenderer>().material = gaugeMat;
            // set needles material
            SHUNTER_NEEDLE_TRANSFORMS.ToList().ForEach(t => trainCar.interior.Find($"loco_621_interior(Clone)/{t}").GetComponent <MeshRenderer>().material = gaugeMat);
        }
Example #7
0
 private void SetCarToSpawn(CustomCar car)
 {
     carPrefabToSpawn = CarTypes.GetCarPrefab(car.TrainCarType);
     if (carPrefabToSpawn == null)
     {
         Debug.LogError(
             $"Couldn't load car prefab: {car.TrainCarType}! Won't be able to spawn this car.", this);
     }
     else
     {
         //var component = carPrefabToSpawn.GetComponent<TrainCar>();
         carBounds = car.Bounds;
         display.SetContent(car.identifier + "\n" + car.InterCouplerDistance.ToString("F") + "m");
     }
 }
Example #8
0
        public static void ExportDamageProperties(CommandArg[] args)
        {
            if (Terminal.IssuedError)
            {
                return;
            }

            string name = args[0].String;

            if (Enum.TryParse(name, out TrainCarType carType))
            {
                GameObject prefab = CarTypes.GetCarPrefab(carType);
                if (!prefab)
                {
                    Debug.LogError($"CarType {name} has missing prefab");
                    return;
                }

                var damage = prefab.GetComponent <DamageController>();
                if (!damage)
                {
                    Debug.LogWarning($"CarType {name} prefab does not have a damage controller");
                    return;
                }

                var ctrlProps = new JObject
                {
                    { "wheelsHP", damage.wheels.fullHitPoints },
                    { "speedToBrakeDamageCurve", ComponentsToJson.AnimationCurve(damage.speedToBrakeDamageCurve) },
                };

                if (damage is DamageControllerDiesel dcd)
                {
                    ctrlProps.Add("engineHP", dcd.engine.fullHitPoints);
                }
                else if (damage is DamageControllerShunter dcs)
                {
                    ctrlProps.Add("engineHP", dcs.engine.fullHitPoints);
                }

                if (TrainCarAndCargoDamageProperties.carDamageProperties.TryGetValue(carType, out CarDamageProperties dmgProps))
                {
                    ctrlProps.Add("bodyDamage", ComponentsToJson.CarDamageProperties(dmgProps));
                }

                GameObjectDumper.SendJsonToFile(name, "damage", ctrlProps);
            }
        }
Example #9
0
        public static void ExportCabControls(CommandArg[] args)
        {
            if (Terminal.IssuedError)
            {
                return;
            }

            string name = args[0].String;

            if (Enum.TryParse(name, out TrainCarType carType))
            {
                GameObject prefab = CarTypes.GetCarPrefab(carType);
                if (!prefab)
                {
                    Debug.LogError($"CarType {name} has missing prefab");
                    return;
                }

                TrainCar car = prefab.GetComponent <TrainCar>();
                if (!car)
                {
                    Debug.LogError($"Couldn't find TrainCar on carType {name}");
                    return;
                }

                if (!car.interiorPrefab)
                {
                    Debug.LogWarning($"TrainCar on carType {name} doesn't have an interiorPrefab assigned");
                    return;
                }

                var specList     = new JArray();
                var controlSpecs = car.interiorPrefab.GetComponentsInChildren <ControlSpec>();
                foreach (ControlSpec spec in controlSpecs)
                {
                    specList.Add(ComponentsToJson.GenericObject(spec));
                }

                var indicators = car.interiorPrefab.GetComponentsInChildren <Indicator>();
                foreach (Indicator ind in indicators)
                {
                    specList.Add(ComponentsToJson.GenericObject(ind));
                }

                GameObjectDumper.SendJsonToFile(name, "control_spec", specList);
            }
        }
Example #10
0
        public static void ExportCarColliders(CommandArg[] args)
        {
            if (Terminal.IssuedError)
            {
                return;
            }

            string name = args[0].String;

            if (Enum.TryParse(name, out TrainCarType carType))
            {
                GameObject prefab = CarTypes.GetCarPrefab(carType);
                if (!prefab)
                {
                    Debug.LogError($"CarType {name} has missing prefab");
                    return;
                }

                Transform colliderRoot = prefab.transform.Find("[colliders]");
                if (!colliderRoot)
                {
                    Debug.LogWarning($"CarType {name} does not have a colliders root transform");
                    return;
                }

                var colliderDict = new JObject();

                foreach (string categoryName in colliderCategories)
                {
                    Transform subTransform = colliderRoot.Find(categoryName);
                    if (subTransform)
                    {
                        Collider[] colliders = subTransform.gameObject.GetComponentsInChildren <Collider>();

                        var colliderCategory = ComponentsToJson.Colliders(colliders);
                        colliderDict.Add(new JProperty(categoryName, colliderCategory));
                    }
                }

                GameObjectDumper.SendJsonToFile(name, "colliders", colliderDict);
            }
            else
            {
                Debug.LogWarning("Invalid car type " + name);
            }
        }
Example #11
0
        private static void RestoreDieselDefaults(TrainCar trainCar)
        {
            if (!trainCar.IsInteriorLoaded)
            {
                return;
            }

            var prefab   = CarTypes.GetCarPrefab(TrainCarType.LocoDiesel).GetComponent <TrainCar>().interiorPrefab.transform;
            var gaugeMat = prefab.Find("offset/I Indicator lamps/I gauges backlights/lamp emission indicator/gauge_labels").GetComponent <MeshRenderer>().material;

            gaugeMat.SetColor("_EmissionColor", trainCar.interior.GetComponentInChildren <DieselDashboardControls>().cabLightRotary.GetComponent <ControlImplBase>().Value > 0 ? Color.white : Color.black);

            // set gauges material
            trainCar.interior.Find($"LocoDiesel_interior(Clone)/offset/I Indicator lamps/I gauges backlights/lamp emission indicator/gauge_labels").GetComponent <MeshRenderer>().material = gaugeMat;
            // set needles material
            DIESEL_NEEDLE_TRANSFORMS.ToList().ForEach(t => trainCar.interior.Find($"LocoDiesel_interior(Clone)/{t}").GetComponent <MeshRenderer>().material = gaugeMat);
        }
Example #12
0
        private void ExportVanillaTextures(bool forceExport = false)
        {
            if (!forceExport && Directory.Exists(TexPath + "[Vanilla]"))
            {
                return;
            }
            var vanDir = Directory.CreateDirectory(TexPath + "[Vanilla]");

            string   path;
            Material mat;

            // SHUNTER
            path = TexPath + "[Vanilla]/loco_621/";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            mat = CarTypes.GetCarPrefab(TrainCarType.LocoShunter).GetComponent <TrainCar>().interiorPrefab.transform
                  .Find("C dashboard buttons controller/I gauges backlights/lamp emmision indicator/gauge_stickers model").GetComponent <MeshRenderer>().sharedMaterial;
            ExportMaterialTextures(path, mat);
            // STEAMER
            path = TexPath + "[Vanilla]/loco_steam_H/";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            mat = CarTypes.GetCarPrefab(TrainCarType.LocoSteamHeavy).GetComponent <TrainCar>().interiorPrefab.transform
                  .Find("Gauges").GetComponent <MeshRenderer>().sharedMaterial;
            ExportMaterialTextures(path, mat);
            mat = CarTypes.GetCarPrefab(TrainCarType.LocoSteamHeavy).GetComponent <TrainCar>().interiorPrefab.transform
                  .Find("I boiler water/water level").GetComponent <MeshRenderer>().sharedMaterial;
            ExportMaterialTextures(path, mat);
            // DIESEL
            path = TexPath + "[Vanilla]/LocoDiesel/";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            mat = CarTypes.GetCarPrefab(TrainCarType.LocoDiesel).GetComponent <TrainCar>().interiorPrefab.transform
                  .Find("offset/I Indicator lamps/I gauges backlights/lamp emission indicator/gauge_labels").GetComponent <MeshRenderer>().sharedMaterial;
            ExportMaterialTextures(path, mat);

            vanDir.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.ReadOnly;
        }
Example #13
0
        private static void RestoreSteamerDefaults(TrainCar trainCar)
        {
            DestroySteamerWaterLevelBacklight();

            if (!trainCar.IsInteriorLoaded)
            {
                return;
            }

            var prefab   = CarTypes.GetCarPrefab(TrainCarType.LocoSteamHeavy).GetComponent <TrainCar>().interiorPrefab.transform;
            var gaugeMat = prefab.Find("Gauges").GetComponent <MeshRenderer>().material;

            // set gauges material
            trainCar.interior.Find($"loco_steam_H_interior(Clone)/Gauges").GetComponent <MeshRenderer>().material = gaugeMat;
            // set needles material
            STEAMER_NEEDLE_TRANSFORMS.ToList().ForEach(t => trainCar.interior.Find($"loco_steam_H_interior(Clone)/{t}").GetComponent <MeshRenderer>().material = gaugeMat);
            // set waterlevels material
            trainCar.interior.Find("loco_steam_H_interior(Clone)/I boiler water/water level").GetComponent <MeshRenderer>().material =
                prefab.Find("I boiler water/water level").GetComponent <MeshRenderer>().material;
        }
Example #14
0
        public static void DumpCarInterior(CommandArg[] args)
        {
            if (Terminal.IssuedError)
            {
                return;
            }

            string name = args[0].String;

            if (Enum.TryParse(name, out TrainCarType carType))
            {
                GameObject prefab = CarTypes.GetCarPrefab(carType);
                if (!prefab)
                {
                    Debug.LogError($"CarType {name} has missing prefab");
                    return;
                }

                TrainCar car = prefab.GetComponent <TrainCar>();
                if (!car)
                {
                    Debug.LogError($"Couldn't find TrainCar on carType {name}");
                    return;
                }

                if (!car.interiorPrefab)
                {
                    Debug.LogWarning($"TrainCar on carType {name} doesn't have an interiorPrefab assigned");
                    return;
                }

                JToken contents = GameObjectDumper.DumpObject(car.interiorPrefab);
                GameObjectDumper.SendJsonToFile(name, "interior", contents);
            }
            else
            {
                Debug.LogWarning("Invalid car type " + name);
            }
        }
Example #15
0
        public static void SetSuperGauges(bool restoreDefaults = false, params TrainCarType[] types)
        {
            if (types == null || types.Length == 0)
            {
                types = new TrainCarType[] { TrainCarType.LocoShunter, TrainCarType.LocoSteamHeavy, TrainCarType.LocoDiesel }
            }
            ;

            if (restoreDefaults)
            {
                if (types.Contains(TrainCarType.LocoShunter))
                {
                    SetSuperGauge(CarTypes.GetCarPrefab(TrainCarType.LocoShunter), true);
                }
                if (types.Contains(TrainCarType.LocoSteamHeavy))
                {
                    SetSuperGauge(CarTypes.GetCarPrefab(TrainCarType.LocoSteamHeavy), true);
                }
                if (types.Contains(TrainCarType.LocoDiesel))
                {
                    SetSuperGauge(CarTypes.GetCarPrefab(TrainCarType.LocoDiesel), true);
                }
                return;
            }

            if (types.Contains(TrainCarType.LocoShunter))
            {
                SetSuperGauge(CarTypes.GetCarPrefab(TrainCarType.LocoShunter), !Main.settings.LocoShunterTRUE);
            }
            if (types.Contains(TrainCarType.LocoSteamHeavy))
            {
                SetSuperGauge(CarTypes.GetCarPrefab(TrainCarType.LocoSteamHeavy), !Main.settings.LocoSteamerHeavyTRUE);
            }
            if (types.Contains(TrainCarType.LocoDiesel))
            {
                SetSuperGauge(CarTypes.GetCarPrefab(TrainCarType.LocoDiesel), !Main.settings.LocoDieselTRUE);
            }
        }
Example #16
0
        static void LoadSkins()
        {
            foreach (var prefab in prefabMap)
            {
                Skin defSkin = CreateDefaultSkin(prefab.Key, prefab.Value);
                defaultSkins.Add(prefab.Key, defSkin);

                var dir = modPath + "Skins\\" + prefab.Value;

                if (Directory.Exists(dir))
                {
                    var            subDirectories = Directory.GetDirectories(dir);
                    var            skinGroup      = new SkinGroup(prefab.Key);
                    var            carPrefab      = CarTypes.GetCarPrefab(prefab.Key);
                    var            cmps           = carPrefab.gameObject.GetComponentsInChildren <MeshRenderer>();
                    MeshRenderer[] interiorCmps   = null;

                    var trainCar = carPrefab.GetComponent <TrainCar>();

                    if (trainCar.interiorPrefab != null)
                    {
                        interiorCmps = trainCar.interiorPrefab.GetComponentsInChildren <MeshRenderer>();
                    }

                    foreach (var subDir in subDirectories)
                    {
                        var dirInfo = new DirectoryInfo(subDir);
                        var files   = Directory.GetFiles(subDir);
                        var skin    = new Skin(dirInfo.Name);

                        foreach (var file in files)
                        {
                            FileInfo fileInfo = new FileInfo(file);
                            string   fileName = Path.GetFileNameWithoutExtension(fileInfo.Name);
                            byte[]   fileData = File.ReadAllBytes(file);

                            foreach (var cmp in cmps)
                            {
                                if (!cmp.material)
                                {
                                    continue;
                                }

                                var diffuse  = GetMaterialTexture(cmp, "_MainTex");
                                var normal   = GetMaterialTexture(cmp, "_BumpMap");
                                var specular = GetMaterialTexture(cmp, "_MetallicGlossMap");
                                var emission = GetMaterialTexture(cmp, "_EmissionMap");

                                if (diffuse != null)
                                {
                                    if ((diffuse.name == fileName || aliasNames.ContainsKey(diffuse.name) && aliasNames[diffuse.name] == fileName) && !skin.ContainsTexture(diffuse.name))
                                    {
                                        var texture = new Texture2D(diffuse.width, diffuse.height);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(diffuse.name, texture));
                                    }
                                }

                                if (normal != null)
                                {
                                    if ((normal.name == fileName || aliasNames.ContainsKey(normal.name) && aliasNames[normal.name] == fileName) && !skin.ContainsTexture(normal.name))
                                    {
                                        var texture = new Texture2D(normal.width, normal.height, TextureFormat.ARGB32, true, true);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(normal.name, texture));
                                    }
                                }


                                if (specular != null)
                                {
                                    if ((specular.name == fileName || aliasNames.ContainsKey(specular.name) && aliasNames[specular.name] == fileName) && !skin.ContainsTexture(specular.name))
                                    {
                                        var texture = new Texture2D(specular.width, specular.height);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(specular.name, texture));
                                    }
                                }

                                if (emission != null)
                                {
                                    if ((emission.name == fileName || aliasNames.ContainsKey(emission.name) && aliasNames[emission.name] == fileName) && !skin.ContainsTexture(emission.name))
                                    {
                                        var texture = new Texture2D(emission.width, emission.height);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(emission.name, texture));
                                    }
                                }
                            }

                            if (interiorCmps != null)
                            {
                                foreach (var cmp in interiorCmps)
                                {
                                    if (!cmp.material)
                                    {
                                        continue;
                                    }

                                    var diffuse  = GetMaterialTexture(cmp, "_MainTex");
                                    var normal   = GetMaterialTexture(cmp, "_BumpMap");
                                    var specular = GetMaterialTexture(cmp, "_MetallicGlossMap");
                                    var emission = GetMaterialTexture(cmp, "_EmissionMap");

                                    if (diffuse != null)
                                    {
                                        if ((diffuse.name == fileName || aliasNames.ContainsKey(diffuse.name) && aliasNames[diffuse.name] == fileName) && !skin.ContainsTexture(diffuse.name))
                                        {
                                            var texture = new Texture2D(diffuse.width, diffuse.height);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(diffuse.name, texture));
                                        }
                                    }

                                    if (normal != null)
                                    {
                                        if ((normal.name == fileName || aliasNames.ContainsKey(normal.name) && aliasNames[normal.name] == fileName) && !skin.ContainsTexture(normal.name))
                                        {
                                            var texture = new Texture2D(normal.width, normal.height, TextureFormat.ARGB32, true, true);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(normal.name, texture));
                                        }
                                    }


                                    if (specular != null)
                                    {
                                        if ((specular.name == fileName || aliasNames.ContainsKey(specular.name) && aliasNames[specular.name] == fileName) && !skin.ContainsTexture(specular.name))
                                        {
                                            var texture = new Texture2D(specular.width, specular.height);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(specular.name, texture));
                                        }
                                    }

                                    if (emission != null)
                                    {
                                        if ((emission.name == fileName || aliasNames.ContainsKey(emission.name) && aliasNames[emission.name] == fileName) && !skin.ContainsTexture(emission.name))
                                        {
                                            var texture = new Texture2D(emission.width, emission.height);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(emission.name, texture));
                                        }
                                    }
                                }
                            }
                        }

                        skinGroup.skins.Add(skin);
                    }

                    skinGroups.Add(prefab.Key, skinGroup);
                }
            }
        }
Example #17
0
        private static Skin CreateDefaultSkin(TrainCarType carType, string typeName)
        {
            GameObject carPrefab = CarTypes.GetCarPrefab(carType);

            if (carPrefab == null)
            {
                return(null);
            }

            Skin defSkin = new Skin($"Default_{typeName}");

            var cmps = carPrefab.gameObject.GetComponentsInChildren <MeshRenderer>();

            foreach (var cmp in cmps)
            {
                if (!cmp.material)
                {
                    continue;
                }

                if (GetMaterialTexture(cmp, "_MainTex") is Texture2D diffuse)
                {
                    defSkin.skinTextures.Add(new SkinTexture(diffuse.name, diffuse));
                }

                if (GetMaterialTexture(cmp, "_BumpMap") is Texture2D normal)
                {
                    defSkin.skinTextures.Add(new SkinTexture(normal.name, normal));
                }

                if (GetMaterialTexture(cmp, "_MetallicGlossMap") is Texture2D specular)
                {
                    defSkin.skinTextures.Add(new SkinTexture(specular.name, specular));
                }

                if (GetMaterialTexture(cmp, "_EmissionMap") is Texture2D emission)
                {
                    defSkin.skinTextures.Add(new SkinTexture(emission.name, emission));
                }
            }

            var trainCar = carPrefab.GetComponent <TrainCar>();

            if (trainCar?.interiorPrefab != null)
            {
                foreach (var cmp in trainCar.interiorPrefab.GetComponentsInChildren <MeshRenderer>())
                {
                    if (!cmp.material)
                    {
                        continue;
                    }

                    if (GetMaterialTexture(cmp, "_MainTex") is Texture2D diffuse)
                    {
                        defSkin.skinTextures.Add(new SkinTexture(diffuse.name, diffuse));
                    }

                    if (GetMaterialTexture(cmp, "_BumpMap") is Texture2D normal)
                    {
                        defSkin.skinTextures.Add(new SkinTexture(normal.name, normal));
                    }

                    if (GetMaterialTexture(cmp, "_MetallicGlossMap") is Texture2D specular)
                    {
                        defSkin.skinTextures.Add(new SkinTexture(specular.name, specular));
                    }

                    if (GetMaterialTexture(cmp, "_EmissionMap") is Texture2D emission)
                    {
                        defSkin.skinTextures.Add(new SkinTexture(emission.name, emission));
                    }
                }
            }

            return(defSkin);
        }
Example #18
0
        public static void DumpTextures(TrainCarType trainCarType)
        {
            MeshRenderer[] cmps;
            Dictionary <string, Texture> textureList;

            var obj = CarTypes.GetCarPrefab(trainCarType);

            var path = modPath + "Exported\\" + obj.name;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            cmps        = obj.GetComponentsInChildren <MeshRenderer>();
            textureList = new Dictionary <string, Texture>();

            foreach (var cmp in cmps)
            {
                if (!cmp.material)
                {
                    continue;
                }

                var diffuse  = GetMaterialTexture(cmp, "_MainTex");
                var normal   = GetMaterialTexture(cmp, "_BumpMap");
                var specular = GetMaterialTexture(cmp, "_MetallicGlossMap");
                var emission = GetMaterialTexture(cmp, "_EmissionMap");

                ExportTexture(path, diffuse, textureList);
                ExportTexture(path, normal, textureList, true);
                ExportTexture(path, specular, textureList);
                ExportTexture(path, emission, textureList);
            }

            var trainCar = obj.GetComponent <TrainCar>();

            if (trainCar.interiorPrefab != null)
            {
                cmps        = trainCar.interiorPrefab.GetComponentsInChildren <MeshRenderer>();
                textureList = new Dictionary <string, Texture>();

                foreach (var cmp in cmps)
                {
                    if (!cmp.material)
                    {
                        continue;
                    }

                    var diffuse  = GetMaterialTexture(cmp, "_MainTex");
                    var normal   = GetMaterialTexture(cmp, "_BumpMap");
                    var specular = GetMaterialTexture(cmp, "_MetallicGlossMap");
                    var emission = GetMaterialTexture(cmp, "_EmissionMap");

                    ExportTexture(path, diffuse, textureList);
                    ExportTexture(path, normal, textureList, true);
                    ExportTexture(path, specular, textureList);
                    ExportTexture(path, emission, textureList);
                }
            }
        }