public static bool SetPlanetData(ref PlanetSimulator __instance, ref Transform ___lookCamera,
                                         ref UniverseSimulator ___universe, ref StarSimulator ___star, PlanetData planet)
        {
            __instance.planetData = planet;
            if (__instance.planetData.atmosMaterial != null)
            {
                __instance.atmoTrans0 = new GameObject("Atmosphere")
                {
                    layer = 31
                }.transform;
                __instance.atmoTrans0.parent        = __instance.transform;
                __instance.atmoTrans0.localPosition = Vector3.zero;
                if (planet.GetScaleFactored() >= 1)
                {
                    __instance.atmoTrans0.localScale *= planet.GetScaleFactored();
                }
                else
                {
                    __instance.atmoTrans0.localScale /= planet.GetScaleFactored();
                }

                var primitive = GameObject.CreatePrimitive(PrimitiveType.Quad);
                primitive.layer                     = 31;
                __instance.atmoTrans1               = primitive.transform;
                __instance.atmoTrans1.parent        = __instance.atmoTrans0;
                __instance.atmoTrans1.localPosition = Vector3.zero;
                Object.Destroy(primitive.GetComponent <Collider>());
                var component     = primitive.GetComponent <Renderer>();
                var atmosMaterial = __instance.planetData.atmosMaterial;
                component.sharedMaterial             = atmosMaterial;
                __instance.atmoMat                   = atmosMaterial;
                component.shadowCastingMode          = ShadowCastingMode.Off;
                component.receiveShadows             = false;
                component.lightProbeUsage            = LightProbeUsage.Off;
                component.motionVectorGenerationMode = MotionVectorGenerationMode.ForceNoMotion;
                __instance.atmoTrans1.localScale     = Vector3.one * (planet.realRadius * 5f * planet.GetScaleFactored());
                __instance.atmoMatRadiusParam        = __instance.atmoMat.GetVector("_PlanetRadius");
            }

            ___lookCamera = Camera.main.transform;
            ___universe   = GameMain.universeSimulator;
            ___star       = ___universe.FindStarSimulator(planet.star);

            return(false);
        }
 public static void AddFactoredRadius(this PlanetRawData planetRawData, PlanetData planet)
 {
     FactoredRadius[planetRawData] = planet.GetScaleFactored();
 }
Beispiel #3
0
        public static PlanetData ReworkCreatePlanet(
            ref GalaxyData galaxy,
            ref StarData star,
            ref GameDesc gameDesc,
            int index,
            int orbitAround,
            int orbitIndex,
            int number,
            bool gasGiant,
            int info_seed,
            int gen_seed)
        {
            Patch.Debug("ReworkCreatePlanet", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);

            var planetData = new PlanetData();
            var mainSeed   = new Random(info_seed);

            //Base data of the planet :
            planetData.index  = index;
            planetData.galaxy = star.galaxy;
            planetData.star   = star;
            planetData.seed   = gen_seed;
            // Index of the thing it's orbiting around , 0 for star , anything else mean it's a moon
            planetData.orbitAround = orbitAround;
            // Current orbit Index around the planet
            planetData.orbitIndex = orbitIndex;
            // index of the planet in the system ?
            planetData.number = number;
            // Global Id of the entity
            planetData.id = star.id * 100 + planetData.index + 1;

            var stars = galaxy.stars;

            Patch.Debug("Basic Information Setup", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);

            var randomNumber1  = mainSeed.NextDouble();
            var randomNumber2  = mainSeed.NextDouble();
            var randomNumber3  = mainSeed.NextDouble();
            var randomNumber4  = mainSeed.NextDouble();
            var randomNumber5  = mainSeed.NextDouble();
            var randomNumber6  = mainSeed.NextDouble();
            var randomNumber7  = mainSeed.NextDouble();
            var randomNumber8  = mainSeed.NextDouble();
            var randomNumber9  = mainSeed.NextDouble();
            var randomNumber10 = mainSeed.NextDouble();
            var randomNumber11 = mainSeed.NextDouble();
            var randomNumber12 = mainSeed.NextDouble();
            var randomNumber13 = mainSeed.NextDouble();
            var randomNumber14 = mainSeed.NextDouble();


            // Orbit definition
            Patch.Debug("Orbit definition", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            var baselineOrbitVariation = Mathf.Pow(1.2f, (float)(randomNumber1 * (randomNumber2 - 0.5) * 0.5));
            var orbitInclination       =
                UnityRandom.Range(0, Patch.MaxOrbitInclination.Value) * MathUtils.RangePlusMinusOne(mainSeed);

            Patch.Debug("Rotation definition", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            // rotation period
            planetData.rotationPeriod = randomNumber8 * randomNumber9 * Patch.RotationPeriodVariabilityFactor.Value +
                                        Patch.RotationPeriodBaseTime.Value;

            // Planet
            Patch.Debug("Body Stuff", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);

            //orbit longitude
            planetData.orbitLongitude = (float)(randomNumber4 * 360.0);
            //runtimeOrbitRotation
            planetData.runtimeOrbitRotation = Quaternion.AngleAxis(planetData.orbitLongitude, Vector3.up) *
                                              Quaternion.AngleAxis(orbitInclination, Vector3.forward);

            planetData.runtimeSystemRotation = planetData.runtimeOrbitRotation * Quaternion.AngleAxis(planetData.obliquity, Vector3.forward);

            if (planetData.IsNotAMoon())
            {
                Patch.Debug("Planets Stuff", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                //orbit
                var baselineOrbitSize = Patch.OrbitRadiusPlanetArray[orbitIndex] * star.orbitScaler;
                var orbitSize         = (float)((baselineOrbitVariation - 1.0) / Mathf.Max(1f, baselineOrbitSize) + 1.0);
                planetData.orbitRadius = baselineOrbitSize * orbitSize;

                // orbit Inclination + periods
                planetData.orbitInclination = orbitInclination;
                planetData.orbitalPeriod    = Math.Sqrt(pi2Rad * planetData.orbitRadius *
                                                        planetData.orbitRadius * planetData.orbitRadius /
                                                        (1.35385519905204E-06 * star.mass));

                //rotation period
                if (planetData.IsGasGiant() || planetData.star.type == EStarType.NeutronStar)
                {
                    planetData.rotationPeriod *= 0.200000002980232;
                }
                else if (planetData.star.type == EStarType.BlackHole)
                {
                    planetData.rotationPeriod *= 0.150000005960464;
                }

                planetData.sunDistance = planetData.orbitRadius;

                // rotation period
                planetData.rotationPeriod = 1.0 / (1.0 / planetData.orbitalPeriod * 2);

                //Tidal Lock Management
                if (randomNumber13 < Patch.ChanceTidalLock.Value)
                {
                    if (randomNumber13 < Patch.ChanceTidalLock1.Value)
                    {
                        planetData.obliquity     *= 0.01f;
                        planetData.rotationPeriod = planetData.orbitalPeriod;
                        planetData.IsTidallyLocked(TidalLevel.TidalLocked);
                    }
                    else if (randomNumber13 < Patch.ChanceTidalLock2.Value)
                    {
                        planetData.obliquity     *= 0.1f;
                        planetData.rotationPeriod = planetData.orbitalPeriod * 0.5;
                        planetData.IsTidallyLocked(TidalLevel.TidalLocked2);
                    }
                    else
                    {
                        planetData.obliquity     *= 0.2f;
                        planetData.rotationPeriod = planetData.orbitalPeriod * 0.25;
                        planetData.IsTidallyLocked(TidalLevel.TidalLocked4);
                    }
                }

                Patch.Debug("Planets Stuff Done", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
            }

            // Moon
            else
            {
                // the previous algo is using the number of the planet it's orbiting around, not the actual index --> so minus 1
                orbitAround -= 1;

                Patch.Debug("Moon Stuff", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                //affect the data of the planet of the moon
                Patch.Debug("orbitAround " + orbitAround, LogLevel.Debug, Patch.DebugReworkPlanetGen);

                planetData.orbitAroundPlanet = star.planets[orbitAround];
                Patch.Debug("orbitAround id : " + star.planets[orbitAround].index, LogLevel.Debug, Patch.DebugReworkPlanetGen);
                var orbitRadiusScaled = Patch.OrbitRadiusArrayMoons[orbitIndex] * star.orbitScaler *
                                        Mathf.Lerp(baselineOrbitVariation, 1f, 0.5f) *
                                        planetData.orbitAroundPlanet.GetGasGiantOrbitScaler();
                //orbit
                planetData.orbitRadius = orbitRadiusScaled;
                Patch.Debug("orbitRadius " + planetData.orbitRadius, LogLevel.Debug, Patch.DebugReworkPlanetGen);
                // orbit Inclination + periods
                planetData.orbitInclination = orbitInclination * Patch.MoonOrbitInclinationFactor.Value;
                Patch.Debug("orbitInclination " + planetData.orbitInclination, LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);

                planetData.orbitalPeriod = Math.Sqrt(pi2Rad * planetData.orbitRadius *
                                                     planetData.orbitRadius * planetData.orbitRadius / 1.08308421068537E-08);
                Patch.Debug("orbitalPeriod " + planetData.orbitalPeriod, LogLevel.Debug, Patch.DebugReworkPlanetGen);

                planetData.rotationPeriod *= Mathf.Pow(planetData.orbitRadius, 0.25f);
                Patch.Debug("rotationPeriod " + planetData.rotationPeriod, LogLevel.Debug, Patch.DebugReworkPlanetGen);


                // distance = planet of the moon
                planetData.sunDistance = planetData.orbitAroundPlanet.orbitRadius;

                Patch.Debug("sunDistance " + planetData.sunDistance, LogLevel.Debug, Patch.DebugReworkPlanetGen);

                // rotation period
                planetData.rotationPeriod =
                    1.0 / (1.0 / planetData.orbitAroundPlanet.orbitalPeriod + 1.0 / planetData.rotationPeriod);
                Patch.Debug("rotationPeriod - 2 " + planetData.rotationPeriod, LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);


                Patch.Debug("Tidal Lock " + planetData.rotationPeriod, LogLevel.Debug, Patch.DebugReworkPlanetGen);
                //Tidal Lock Management
                if (randomNumber13 < Patch.ChanceTidalLock.Value)
                {
                    if (randomNumber13 < Patch.ChanceTidalLock1.Value)
                    {
                        planetData.obliquity     *= 0.01f;
                        planetData.rotationPeriod = planetData.orbitAroundPlanet.orbitalPeriod;
                        planetData.IsTidallyLocked(TidalLevel.TidalLocked);
                    }
                    else if (randomNumber13 < Patch.ChanceTidalLock2.Value)
                    {
                        planetData.obliquity     *= 0.1f;
                        planetData.rotationPeriod = planetData.orbitAroundPlanet.orbitalPeriod * 0.5;
                        planetData.IsTidallyLocked(TidalLevel.TidalLocked2);
                    }
                    else
                    {
                        planetData.obliquity     *= 0.2f;
                        planetData.rotationPeriod = planetData.orbitAroundPlanet.orbitalPeriod * 0.25;
                        planetData.IsTidallyLocked(TidalLevel.TidalLocked4);
                    }
                }

                Patch.Debug("End Tidal Lock " + planetData.rotationPeriod, LogLevel.Debug, Patch.DebugReworkPlanetGen);

                //runtimeOrbitRotation

                planetData.runtimeOrbitRotation = planetData.orbitAroundPlanet.runtimeOrbitRotation *
                                                  planetData.runtimeOrbitRotation;
                Patch.Debug("runtimeOrbitRotation " + planetData.runtimeOrbitRotation, LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                Patch.Debug("Moon Stuff Done", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
            }

            Patch.Debug("Body orbit Phase", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            // orbit phase
            planetData.orbitPhase = (float)(randomNumber5 * 360.0);

            Patch.Debug("Body Rotation Phase", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            //rotation phase
            planetData.rotationPhase = (float)(randomNumber10 * 360.0);

            Patch.Debug("Body Orbit Obliquity", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            // orbit obliquity
            Patch.Debug("Body Obliquity Modification", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            if (randomNumber13 < Patch.ChancePlanetLaySide.Value)
            {
                planetData.obliquity = (float)randomNumber6 * MathUtils.RangePlusMinusOne(mainSeed) *
                                       Patch.LaySideBaseAngle.Value;
                planetData.obliquity += Patch.LaySideAddingAngle.Value * MathUtils.RangePlusMinusOne(mainSeed);
                planetData.HasLayingObliquity();
            }
            else if (randomNumber13 < Patch.ChanceBigObliquity.Value)
            {
                planetData.obliquity = (float)randomNumber6 * MathUtils.RangePlusMinusOne(mainSeed) *
                                       Patch.BigObliquityBaseAngle.Value;
                planetData.obliquity += Patch.BigObliquityAddingAngle.Value * MathUtils.RangePlusMinusOne(mainSeed);
            }
            else
            {
                planetData.obliquity = (float)randomNumber6 * MathUtils.RangePlusMinusOne(mainSeed) *
                                       Patch.StandardObliquityAngle.Value;
            }

            Patch.Debug("Body runtimeSystemRotation", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            //runtimeOrbitRotation obliquity adjustment
            planetData.runtimeSystemRotation = planetData.runtimeOrbitRotation *
                                               Quaternion.AngleAxis(planetData.obliquity, Vector3.forward);


            Patch.Debug("Body Retrograde", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            //Define if the orbit is retrograde
            if (randomNumber14 < Patch.ChanceRetrogradeOrbit.Value)
            {
                planetData.HasRetrogradeOrbit();
            }

            Patch.Debug("Body Neutron Star", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            // Anomaly around neutron stars
            if (planetData.star.type == EStarType.NeutronStar)
            {
                planetData.OrbitAroundNeutronStar();
            }

            Patch.Debug("Body Type Definition", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            // type of the planet :
            if (gasGiant)
            {
                Patch.Debug("Body is Gas Giant", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                planetData.type          = EPlanetType.Gas;
                planetData.habitableBias = 100f;
            }
            else
            {
                Patch.Debug("Body TypeDefinition ( planet / Moon )", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                var sunDistance            = planetData.sunDistance;
                var ratioHabitableDistance = Patch.HabitabilityBaseConstant.Value;
                Patch.Debug("Body Habitability", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                if (star.habitableRadius > 0.0 && sunDistance > 0.0)
                {
                    ratioHabitableDistance = sunDistance / star.habitableRadius;
                }

                Patch.Debug("Star Habitability radius ", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                var minRadiusHabitable = star.habitableRadius - Patch.HabitableRadiusAreaBaseline.Value;
                var maxRadiusHabitable = star.habitableRadius + Patch.HabitableRadiusAreaBaseline.Value;

                if (planetData.sunDistance < maxRadiusHabitable && planetData.sunDistance > minRadiusHabitable)
                {
                    planetData.habitableBias = Patch.ChanceBeingHabitable.Value;
                }

                Patch.Debug("Body Temperature ( planet / Moon )", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
                planetData.temperatureBias =
                    (float)(1.20000004768372 / (ratioHabitableDistance + 0.200000002980232) - 1.0);


                if (randomNumber11 < planetData.habitableBias)
                {
                    Patch.Debug("Body Type Ocean ( planet / Moon )", LogLevel.Debug,
                                Patch.DebugReworkPlanetGen);
                    planetData.type = EPlanetType.Ocean;
                    ++star.galaxy.habitableCount;
                }
                else if (ratioHabitableDistance < Patch.VolcanoPlanetDistanceRatio.Value)
                {
                    Patch.Debug("Body Type Volcano ( planet / Moon )", LogLevel.Debug,
                                Patch.DebugReworkPlanetGen);
                    planetData.type = EPlanetType.Vocano;
                }
                else if (ratioHabitableDistance > Patch.IcePlanetDistanceRatio.Value)
                {
                    Patch.Debug("Body Type Ice ( planet / Moon )", LogLevel.Debug,
                                Patch.DebugReworkPlanetGen);
                    planetData.type = EPlanetType.Ice;
                }
                else
                {
                    Patch.Debug("Body Type Desert ( planet / Moon )", LogLevel.Debug,
                                Patch.DebugReworkPlanetGen);
                    planetData.type = EPlanetType.Desert;
                }

                Patch.Debug("Body Type Defined ( planet / Moon )", LogLevel.Debug,
                            Patch.DebugReworkPlanetGen);
            }

            Patch.Debug("Body Luminosity( planet / Moon )", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            //Luminosity
            planetData.luminosity = Mathf.Pow(planetData.star.lightBalanceRadius / (planetData.sunDistance + 0.01f), 0.6f);
            if (planetData.luminosity > 1.0)
            {
                planetData.luminosity = Mathf.Log(planetData.luminosity) + 1f;
                planetData.luminosity = Mathf.Log(planetData.luminosity) + 1f;
                planetData.luminosity = Mathf.Log(planetData.luminosity) + 1f;
            }

            planetData.luminosity = Mathf.Round(planetData.luminosity * 100f) / 100f;

            Patch.Debug("Body Size Def ( planet / Moon )", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);
            //Size related stuff :
            if (planetData.type == EPlanetType.Gas)
            {
                var radiusGasGiantWanted = PatchSize.VanillaGasGiantSize;
                if (PatchSize.EnableResizingFeature.Value)
                {
                    //Default : 0.25
                    var minScalingGasGiantRatio =
                        (PatchSize.BaseGasGiantSize.Value - PatchSize.BaseGasGiantSizeVariationFactor.Value) /
                        (PatchSize.BaseGasGiantSize.Value + PatchSize.BaseGasGiantSizeVariationFactor.Value);

                    radiusGasGiantWanted = PatchSize.BaseGasGiantSize.Value +
                                           MathUtils.RangePlusMinusOne(mainSeed) *
                                           PatchSize.BaseGasGiantSizeVariationFactor.Value;
                    radiusGasGiantWanted -= radiusGasGiantWanted % 10;
                }

                planetData.scale  = PatchSize.VanillaGasGiantScale;
                planetData.radius = radiusGasGiantWanted / planetData.scale;

                planetData.precision = 64;
                planetData.segment   = 2;
            }
            else if (planetData.type != EPlanetType.None)
            {
                if (PatchSize.EnableResizingFeature.Value)
                {
                    var radiusTelluricWanted = PatchSize.VanillaTelluricSize;
                    if (planetData.IsNotAMoon())
                    {
                        radiusTelluricWanted = PatchSize.BaseTelluricSize.Value +
                                               MathUtils.RangePlusMinusOne(mainSeed) *
                                               PatchSize.BaseTelluricSizeVariationFactor.Value;
                    }
                    else
                    {
                        //A moon can only be smaller than it's host
                        if (planetData.orbitAroundPlanet.type != EPlanetType.Gas)
                        {
                            radiusTelluricWanted = planetData.orbitAroundPlanet.radius -
                                                   (float)mainSeed.NextDouble() *
                                                   PatchSize.BaseTelluricSizeVariationFactor.Value;
                            // clamp to avoid weird sizes
                            radiusTelluricWanted = Mathf.Clamp(radiusTelluricWanted,
                                                               PatchSize.MinTelluricSize.Value,
                                                               planetData.orbitAroundPlanet.radius);
                        }
                        else
                        {
                            radiusTelluricWanted = PatchSize.BaseTelluricSize.Value +
                                                   MathUtils.RangePlusMinusOne(mainSeed) *
                                                   PatchSize.BaseTelluricSizeVariationFactor.Value;
                        }
                    }

                    radiusTelluricWanted -= radiusTelluricWanted % 10;
                    planetData.radius     = Mathf.RoundToInt(radiusTelluricWanted);
                    Patch.Debug(" planetData.radius" + planetData.radius, LogLevel.Debug,
                                Patch.DebugReworkPlanetGenDeep);

                    planetData.scale = planetData.GetScaleFactored();
                    Patch.Debug(" planetData.scale" + planetData.scale, LogLevel.Debug,
                                Patch.DebugReworkPlanetGenDeep);

                    planetData.precision = planetData.GetPrecisionFactored();
                    Patch.Debug(" planetData.precision" + planetData.precision, LogLevel.Debug,
                                Patch.DebugReworkPlanetGenDeep);
                }
                else
                {
                    planetData.radius    = PatchSize.VanillaTelluricSize;
                    planetData.scale     = PatchSize.VanillaTelluricScale;
                    planetData.precision = PatchSize.VanillaTelluricPrecision;
                }

                planetData.segment = 5;
            }
            else
            {
                planetData.radius    = PatchSize.VanillaTelluricSize;
                planetData.precision = 64;
                planetData.segment   = 2;
            }

            star.planets[planetData.index] = planetData;
            planetData.star = star;

            Patch.Debug("Body Theme Def ( planet / Moon )", LogLevel.Debug, Patch.DebugReworkPlanetGen);
            Patch.Debug("planetData \n" +
                        "planetData.star.index " + planetData.star.index + "\n" +
                        "planetData.index " + planetData.index + "\n" +
                        "planetData.temperatureBias " + planetData.temperatureBias + "\n" +
                        "planetData.planets " + planetData.star.planets + "\n" +
                        "planetData.planets index : " + planetData.star.planets[planetData.index].type + "\n" +
                        "planetData.planets Lenght " + planetData.star.planets.Length + "\n" +
                        "planetData.type " + planetData.type + "\n" +
                        "planetData.mod_x " + planetData.mod_x + "\n" +
                        "planetData.mod_y " + planetData.mod_y + "\n" +
                        "planetData.algoId " + planetData.algoId + "\n"
                        , LogLevel.Debug, Patch.DebugReworkPlanetGen);

            star.galaxy.astroPoses[planetData.id].uRadius = planetData.realRadius;


            Patch.Debug("ReworkCreatePlanet Done !", LogLevel.Debug,
                        Patch.DebugReworkPlanetGen);

            return(planetData);
        }
Beispiel #4
0
        public static bool UpdateDirtyMesh(ref PlanetData __instance, ref bool __result, int dirtyIdx)
        {
            Patch.Debug("UpdateDirtyMesh", LogLevel.Debug,
                        Patch.DebugGeneral);

            Patch.Debug("UpdateDirtyMesh Start :", LogLevel.Debug,
                        Patch.DebugPlanetData);
            Patch.Debug("scaleFactor updateDirtyMesh :" + __instance.GetScaleFactored(), LogLevel.Debug,
                        Patch.DebugPlanetData);

            if (__instance.dirtyFlags[dirtyIdx])
            {
                __instance.dirtyFlags[dirtyIdx] = false;
                var precisionOnSegment = __instance.precision / __instance.segment;

                Patch.Debug("precisionOnSegment :" + precisionOnSegment, LogLevel.Debug,
                            Patch.DebugPlanetData);
                var segmentSquared = __instance.segment * __instance.segment;

                Patch.Debug("segmentSquared :" + segmentSquared, LogLevel.Debug,
                            Patch.DebugPlanetData);
                var dirtyIOnSSqu               = dirtyIdx / segmentSquared;
                var dirtyIOnSSquModulo         = dirtyIOnSSqu % 2;
                var dirtyIOnSSquByTwo          = dirtyIOnSSqu / 2;
                var dirtyIModuloSegmentSquared = dirtyIdx % segmentSquared;
                var DIMSQMod = dirtyIModuloSegmentSquared % __instance.segment * precisionOnSegment +
                               dirtyIOnSSquModulo * __instance.data.substride;

                Patch.Debug("DIMSQMod :" + DIMSQMod, LogLevel.Debug,
                            Patch.DebugPlanetData);
                var DIMSQBy = dirtyIModuloSegmentSquared / __instance.segment * precisionOnSegment +
                              dirtyIOnSSquByTwo * __instance.data.substride;

                Patch.Debug("DIMSQBy :" + DIMSQBy, LogLevel.Debug,
                            Patch.DebugPlanetData);
                var stride = __instance.data.stride;

                var radiusOffset = __instance.radius;

                Patch.Debug("radiusOffset :" + radiusOffset, LogLevel.Debug,
                            Patch.DebugPlanetData);
                var mesh     = __instance.meshes[dirtyIdx];
                var vertices = mesh.vertices;
                var normals  = mesh.normals;
                var IndexGeo = 0;
                for (var i = DIMSQBy; i <= DIMSQBy + precisionOnSegment; i++)
                {
                    for (var j = DIMSQMod; j <= DIMSQMod + precisionOnSegment; j++)
                    {
                        var strideIndexLoop      = j + i * stride;
                        var heightDataNormalized =
                            __instance.data.heightData[strideIndexLoop] * 0.01f;

                        Patch.Debug("heightDataNormalized :" + heightDataNormalized, LogLevel.Debug,
                                    Patch.DebugPlanetDataDeep);
                        var thirdOdModLevel =
                            __instance.data.GetModLevel(strideIndexLoop) * 0.3333333f;

                        Patch.Debug("thirdOdModLevel :" + thirdOdModLevel, LogLevel.Debug,
                                    Patch.DebugPlanetDataDeep);

                        var copyOfRadiusOffset = radiusOffset;
                        if (thirdOdModLevel > 0f)
                        {
                            copyOfRadiusOffset = __instance.data.GetModPlane(strideIndexLoop) *
                                                 __instance.GetScaleFactored() * 0.01f;
                        }

                        //patched copyOfRadiusOffset
                        // copyOfRadiusOffset *= scaleFactor;

                        Patch.Debug("copyOfRadiusOffset :" + copyOfRadiusOffset, LogLevel.Debug,
                                    Patch.DebugPlanetDataDeep);
                        var ploup = heightDataNormalized * (1f - thirdOdModLevel) +
                                    copyOfRadiusOffset * thirdOdModLevel;


                        Patch.Debug("ploup :" + ploup, LogLevel.Debug,
                                    Patch.DebugPlanetDataDeep);
                        if (ploup > __instance.radius)
                        {
                            ploup -= (float)0.2 * __instance.GetScaleFactored();
                            if (ploup < __instance.radius)
                            {
                                ploup = __instance.radius + 0.2f;
                            }
                        }


                        vertices[IndexGeo].x = __instance.data.vertices[strideIndexLoop].x * ploup;
                        vertices[IndexGeo].y = __instance.data.vertices[strideIndexLoop].y * ploup;
                        vertices[IndexGeo].z = __instance.data.vertices[strideIndexLoop].z * ploup;

                        normals[IndexGeo].x =
                            __instance.data.normals[strideIndexLoop].x * (1f - thirdOdModLevel) +
                            __instance.data.vertices[strideIndexLoop].x * thirdOdModLevel;
                        normals[IndexGeo].y =
                            __instance.data.normals[strideIndexLoop].y * (1f - thirdOdModLevel) +
                            __instance.data.vertices[strideIndexLoop].y * thirdOdModLevel;
                        normals[IndexGeo].z =
                            __instance.data.normals[strideIndexLoop].z * (1f - thirdOdModLevel) +
                            __instance.data.vertices[strideIndexLoop].z * thirdOdModLevel;
                        normals[IndexGeo].Normalize();
                        IndexGeo++;
                    }
                }

                mesh.vertices = vertices;
                mesh.normals  = normals;
                __instance.meshColliders[dirtyIdx].sharedMesh = null;
                __instance.meshColliders[dirtyIdx].sharedMesh = mesh;

                __result = true;
                return(false);
            }

            __result = false;
            return(false);
        }
        public static bool ModelingPlanetMain(PlanetData planet,
                                              ref Camera ___heightmapCamera,
                                              ref List <Mesh> ___tmpMeshList,
                                              ref PlanetData ___currentModelingPlanet,
                                              ref int ___currentModelingStage,
                                              ref int ___currentModelingSeamNormal,
                                              ref PlanetData ___currentFactingPlanet,
                                              ref int ___currentFactingStage,
                                              ref List <MeshRenderer> ___tmpMeshRendererList,
                                              ref List <MeshCollider> ___tmpMeshColliderList,
                                              ref Collider ___tmpOceanCollider,
                                              ref List <Vector3> ___tmpVerts,
                                              ref List <Vector3> ___tmpNorms,
                                              ref List <Vector4> ___tmpTgnts,
                                              ref VegeProto[] ___vegeProtos,
                                              ref List <Vector2> ___tmpUvs,
                                              ref List <Vector4> ___tmpUv2s,
                                              ref List <int> ___tmpTris,
                                              ref GameObject ___tmpPlanetGameObject,
                                              ref GameObject ___tmpPlanetBodyGameObject,
                                              ref GameObject ___tmpPlanetReformGameObject,
                                              ref MeshRenderer ___tmpPlanetReformRenderer)
        {
            Patch.Debug("ModelingPlanetMain", LogLevel.Debug,
                        Patch.DebugGeneral);


            var themeProto = LDB.themes.Select(planet.theme);

            planet.data.AddFactoredRadius(planet);

            Patch.Debug("Planet " + planet.name, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Modeling Main Start :", LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet radius :" + planet.radius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet RealRadius :" + planet.realRadius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet precision :" + planet.precision, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Scale :" + planet.scale, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Algo :" + planet.algoId, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet RealRadius :" + planet.realRadius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Scale :" + planet.scale, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet scaleFactor :" + planet.GetScaleFactored(), LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Scale :" + planet.radius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);


            //data.heightData = scaledHeightData;

            if (___currentModelingStage == 2)
            {
                var planetPrecisionBySegment = planet.precision / planet.segment;
                var planetPrecision          = planet.precision;
                var data        = planet.data;
                var planetScale = planet.scale;
                // float planetRadiusScaled = (float) (planet.radius * (double) planetScale + 0.200000002980232);
                var planetRadiusScaled = (float)(planet.radius * (double)planetScale + 0.200000002980232);
                Patch.Debug("planetRadiusScaled " + planetRadiusScaled, LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);
                // planetRadiusScaled *= scaleFactor;
                Patch.Debug("planetRadiusScaled Patched " + planetRadiusScaled, LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);
                var stride         = data.stride;
                var num6           = 0;
                var stateOfTheGame = !GameMain.isLoading ? 2 : 3;
                var num8           = 0;
                for (var index1 = 0; index1 < 4; ++index1)
                {
                    var num9  = index1 % 2 * (planetPrecision + 1);
                    var num10 = index1 / 2 * (planetPrecision + 1);
                    for (var index2 = 0; index2 < planetPrecision; index2 += planetPrecisionBySegment)
                    {
                        for (var index3 = 0; index3 < planetPrecision; index3 += planetPrecisionBySegment)
                        {
                            if (num8 == 0 && num6 < ___tmpMeshList.Count)
                            {
                                ++num6;
                            }
                            else
                            {
                                var mesh = new Mesh();
                                ___tmpMeshList.Add(mesh);
                                ___tmpVerts.Clear();
                                ___tmpNorms.Clear();
                                ___tmpTgnts.Clear();
                                ___tmpUvs.Clear();
                                ___tmpUv2s.Clear();
                                var gameObject = new GameObject("Surface");
                                gameObject.layer = 30;
                                gameObject.transform.SetParent(___tmpPlanetBodyGameObject.transform, false);
                                for (var index4 = index2;
                                     index4 <= index2 + planetPrecisionBySegment &&
                                     index4 <= planetPrecision;
                                     ++index4)
                                {
                                    for (var index5 = index3;
                                         index5 <= index3 + planetPrecisionBySegment &&
                                         index5 <= planetPrecision;
                                         ++index5)
                                    {
                                        var num11  = num9 + index5;
                                        var num12  = num10 + index4;
                                        var index6 = num11 + num12 * stride;
                                        var num13  = index6;
                                        if (index4 == 0)
                                        {
                                            var num14 = (index1 + 3) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = planetPrecision;
                                            var num18 = planetPrecision - index5;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }
                                        else if (index5 == 0)
                                        {
                                            var num14 = (index1 + 3) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = planetPrecision - index4;
                                            var num18 = planetPrecision;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }

                                        if (index4 == planetPrecision)
                                        {
                                            var num14 = (index1 + 1) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = 0;
                                            var num18 = planetPrecision - index5;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }
                                        else if (index5 == planetPrecision)
                                        {
                                            var num14 = (index1 + 1) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = planetPrecision - index4;
                                            var num18 = 0;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }
                                        Patch.Debug("data.heightData[index6]  :  " + data.heightData[index6],
                                                    LogLevel.Debug,
                                                    Patch.DebugPlanetModelingManagerDeep);
                                        var heightDataScaled = data.heightData[index6] * 0.01f;
                                        //If GasGiant
                                        if (planet.type == EPlanetType.Gas)
                                        {
                                            heightDataScaled *= planetScale;
                                        }
                                        Patch.Debug("heightDataScaled  :  " + heightDataScaled,
                                                    LogLevel.Debug,
                                                    Patch.DebugPlanetModelingManagerDeep);
                                        var thirdOfModLevel = data.GetModLevel(index6) * 0.3333333f;

                                        Patch.Debug("thirdOfModLevel  :  " + thirdOfModLevel,
                                                    LogLevel.Debug,
                                                    Patch.DebugPlanetModelingManagerDeep);

                                        if (thirdOfModLevel > 0.0)
                                        {
                                            //data.GetModPlane(index6)) 20000 + * 0.01f --> 200 +
                                            Patch.Debug(
                                                "data.GetModPlane(index6) :  " + data.GetModPlaneInt(index6),
                                                LogLevel.Debug,
                                                Patch.DebugPlanetModelingManagerDeep);
                                            var modPlanePatched = data.GetModPlaneInt(index6);

                                            Patch.Debug("patch modPlane:  " + modPlanePatched,
                                                        LogLevel.Debug,
                                                        Patch.DebugPlanetModelingManagerDeep);
                                            planetRadiusScaled = modPlanePatched * 0.01f;

                                            Patch.Debug(
                                                "planetRadiusScaled is modified :  " + planetRadiusScaled,
                                                LogLevel.Debug,
                                                Patch.DebugPlanetModelingManagerDeep);
                                        }

                                        // final height modification ?
                                        var finalHeight = (float)(heightDataScaled * (1.0 - thirdOfModLevel) + planetRadiusScaled * (double)thirdOfModLevel);


                                        Patch.Debug("finalHeight :  " + finalHeight, LogLevel.Debug,
                                                    Patch.DebugPlanetModelingManagerDeep);

                                        var vector3_1 = data.vertices[index6] * finalHeight;
                                        ___tmpVerts.Add(vector3_1);
                                        ___tmpNorms.Add(data.vertices[index6]);
                                        var vector3_2 = Vector3.Cross(data.vertices[index6], Vector3.up).normalized;
                                        if (vector3_2.sqrMagnitude == 0.0)
                                        {
                                            vector3_2 = Vector3.right;
                                        }

                                        ___tmpTgnts.Add(new Vector4(vector3_2.x, vector3_2.y, vector3_2.z, 1f));
                                        ___tmpUvs.Add(new Vector2((num11 + 0.5f) / stride, (num12 + 0.5f) / stride));
                                        ___tmpUv2s.Add(new Vector4(data.biomoData[index6] * 0.01f, data.temprData[index6] * 0.01f, index6 + 0.3f, num13 + 0.3f));
                                    }
                                }

                                mesh.indexFormat = IndexFormat.UInt16;
                                mesh.SetVertices(___tmpVerts);
                                mesh.SetNormals(___tmpNorms);
                                mesh.SetTangents(___tmpTgnts);
                                mesh.SetUVs(0, ___tmpUvs);
                                mesh.SetUVs(1, ___tmpUv2s);
                                mesh.SetTriangles(___tmpTris, 0, true, 0);
                                mesh.RecalculateNormals();
                                mesh.GetNormals(___tmpNorms);
                                for (var index4 = 0; index4 < ___tmpNorms.Count; ++index4)
                                {
                                    var z = (int)___tmpUv2s[index4].z;
                                    var w = (int)___tmpUv2s[index4].w;
                                    data.normals[z] = data.normals[z] + ___tmpNorms[index4];
                                    data.normals[w] = data.normals[w] + ___tmpNorms[index4];
                                }

                                var meshFilter2  = gameObject.AddComponent <MeshFilter>();
                                var meshRenderer = gameObject.AddComponent <MeshRenderer>();
                                var meshCollider = gameObject.AddComponent <MeshCollider>();
                                meshFilter2.sharedMesh         = mesh;
                                meshRenderer.sharedMaterial    = planet.terrainMaterial;
                                meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
                                meshRenderer.receiveShadows    = false;
                                meshRenderer.lightProbeUsage   = LightProbeUsage.Off;
                                meshCollider.sharedMesh        = mesh;
                                ___tmpMeshRendererList.Add(meshRenderer);
                                ___tmpMeshColliderList.Add(meshCollider);
                                ++num8;
                                if (num8 == stateOfTheGame)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }

                var num23 = !GameMain.isLoading ? 5 : 15;
                for (var index1 = 0; index1 < ___tmpMeshList.Count; ++index1)
                {
                    var num9 = index1 / num23;
                    if (num9 >= ___currentModelingSeamNormal)
                    {
                        if (num9 > ___currentModelingSeamNormal)
                        {
                            ++___currentModelingSeamNormal;
                            return(false);
                        }

                        var tmpMesh = ___tmpMeshList[index1];
                        ___tmpNorms.Clear();
                        ___tmpUv2s.Clear();
                        var vertexCount = tmpMesh.vertexCount;
                        tmpMesh.GetUVs(1, ___tmpUv2s);
                        for (var index2 = 0; index2 < vertexCount; ++index2)
                        {
                            var z = (int)___tmpUv2s[index2].z;
                            ___tmpNorms.Add(data.normals[z].normalized);
                        }

                        tmpMesh.SetNormals(___tmpNorms);
                    }
                }
                ___currentModelingStage = 3;
            }



            return(true);
        }
        public static bool ModelingPlanetMain(PlanetData planet,
                                              ref Camera ___heightmapCamera,
                                              ref List <Mesh> ___tmpMeshList,
                                              ref PlanetData ___currentModelingPlanet,
                                              ref int ___currentModelingStage,
                                              ref int ___currentModelingSeamNormal,
                                              ref PlanetData ___currentFactingPlanet,
                                              ref int ___currentFactingStage,
                                              ref List <MeshRenderer> ___tmpMeshRendererList,
                                              ref List <MeshCollider> ___tmpMeshColliderList,
                                              ref Collider ___tmpOceanCollider,
                                              ref List <Vector3> ___tmpVerts,
                                              ref List <Vector3> ___tmpNorms,
                                              ref List <Vector4> ___tmpTgnts,
                                              ref VegeProto[] ___vegeProtos,
                                              ref List <Vector2> ___tmpUvs,
                                              ref List <Vector4> ___tmpUv2s,
                                              ref List <int> ___tmpTris,
                                              ref GameObject ___tmpPlanetGameObject,
                                              ref GameObject ___tmpPlanetBodyGameObject,
                                              ref GameObject ___tmpPlanetReformGameObject,
                                              ref MeshRenderer ___tmpPlanetReformRenderer)
        {
            Patch.Debug("ModelingPlanetMain", LogLevel.Debug,
                        Patch.DebugGeneral);


            var themeProto = LDB.themes.Select(planet.theme);

            planet.data.AddFactoredRadius(planet);

            Patch.Debug("Planet " + planet.name, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Modeling Main Start :", LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet radius :" + planet.radius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet RealRadius :" + planet.realRadius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet precision :" + planet.precision, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Scale :" + planet.scale, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Algo :" + planet.algoId, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet RealRadius :" + planet.realRadius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Scale :" + planet.scale, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet scaleFactor :" + planet.GetScaleFactored(), LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);
            Patch.Debug("Planet Scale :" + planet.radius, LogLevel.Debug,
                        Patch.DebugPlanetModelingManager);


            //data.heightData = scaledHeightData;

            var planetPrecisionBySegment = planet.precision / planet.segment;

            switch (___currentModelingStage)
            {
            case 0: {
                Patch.Debug("___currentModelingStage :" + 0, LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);

                if (___tmpMeshList == null)
                {
                    ___tmpMeshList         = new List <Mesh>(100);
                    ___tmpMeshRendererList = new List <MeshRenderer>(100);
                    ___tmpMeshColliderList = new List <MeshCollider>(100);
                    ___tmpOceanCollider    = null;
                    ___tmpVerts            = new List <Vector3>(1700);
                    ___tmpNorms            = new List <Vector3>(1700);
                    ___tmpTgnts            = new List <Vector4>(1700);
                    ___tmpUvs  = new List <Vector2>(1700);
                    ___tmpUv2s = new List <Vector4>(1700);
                    ___tmpTris = new List <int>(10000);
                }

                if (planet.heightmap == null)
                {
                    planet.heightmap = new RenderTexture(
                        new RenderTextureDescriptor(512, 512, RenderTextureFormat.RGHalf, 0)
                        {
                            dimension        = TextureDimension.Cube,
                            useMipMap        = false,
                            autoGenerateMips = false
                        });
                }
                if (___heightmapCamera == null)
                {
                    var gameObject = new GameObject("Heightmap Camera");
                    ___heightmapCamera                        = gameObject.AddComponent <Camera>();
                    ___heightmapCamera.cullingMask            = 1073741824;
                    ___heightmapCamera.enabled                = false;
                    ___heightmapCamera.farClipPlane           = 900f;
                    ___heightmapCamera.nearClipPlane          = 10f;
                    ___heightmapCamera.renderingPath          = RenderingPath.Forward;
                    ___heightmapCamera.allowDynamicResolution = false;
                    ___heightmapCamera.allowMSAA              = false;
                    ___heightmapCamera.allowHDR               = true;
                    ___heightmapCamera.depthTextureMode       = DepthTextureMode.None;
                    ___heightmapCamera.clearFlags             = CameraClearFlags.Color;
                    ___heightmapCamera.backgroundColor        = Color.black;
                    ___heightmapCamera.depth                  = 0.0f;
                    ___heightmapCamera.SetReplacementShader(Configs.builtin.heightmapShader, "ReplaceTag");
                    gameObject.SetActive(false);
                }

                if (planet.terrainMaterial == null)
                {
                    if (themeProto != null &&
                        themeProto.terrainMat != null)
                    {
                        planet.terrainMaterial =
                            Instantiate(themeProto.terrainMat);
                        planet.terrainMaterial.name = planet.displayName + " Terrain";
                        planet.terrainMaterial.SetFloat("_Radius", planet.realRadius);
                    }
                    else
                    {
                        planet.terrainMaterial =
                            Instantiate(Configs.builtin.planetSurfaceMatProto);
                    }
                }

                if (planet.oceanMaterial == null)
                {
                    if (themeProto != null &&
                        themeProto.oceanMat != null)
                    {
                        planet.oceanMaterial =
                            Instantiate(themeProto.oceanMat);
                        planet.oceanMaterial.name = planet.displayName + " Ocean";
                        planet.oceanMaterial.SetFloat("_Radius", planet.realRadius);
                    }
                    else
                    {
                        planet.oceanMaterial = null;
                    }
                }

                if (planet.atmosMaterial == null)
                {
                    if (themeProto != null &&
                        themeProto.atmosMat != null)
                    {
                        planet.atmosMaterial =
                            Instantiate(themeProto.atmosMat);
                        planet.atmosMaterial.name = planet.displayName + " Atmos";
                    }
                    else
                    {
                        planet.atmosMaterial = null;
                    }
                }

                if (planet.reformMaterial == null)
                {
                    planet.reformMaterial =
                        Instantiate(Configs.builtin.planetReformMatProto);
                }
                if (planet.ambientDesc == null)
                {
                    planet.ambientDesc =
                        themeProto == null || !(themeProto.ambientDesc !=
                                                null)
                                ? null
                                : themeProto.ambientDesc;
                }
                if (planet.ambientSfx == null &&
                    themeProto != null &&
                    themeProto.ambientSfx != null)
                {
                    planet.ambientSfx       = themeProto.ambientSfx;
                    planet.ambientSfxVolume = themeProto.SFXVolume;
                }

                if (planet.minimapMaterial == null)
                {
                    planet.minimapMaterial =
                        themeProto == null || !(themeProto.minimapMat !=
                                                null)
                                ? Instantiate(Configs.builtin.planetMinimapDefault)
                                : Instantiate(themeProto.minimapMat);
                    planet.minimapMaterial.name = planet.displayName + " Minimap";
                    planet.minimapMaterial.SetTexture("_HeightMap", planet.heightmap);
                }

                ___tmpMeshList.Clear();
                ___tmpMeshRendererList.Clear();
                ___tmpMeshColliderList.Clear();
                ___tmpOceanCollider = null;
                ___tmpTris.Clear();
                ___currentModelingStage = 1;
                break;
            }

            case 1: {
                Patch.Debug("___currentModelingStage :" + 1, LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);

                ___tmpPlanetGameObject       = new GameObject(planet.displayName);
                ___tmpPlanetGameObject.layer = 31;
                GameMain.universeSimulator.SetPlanetSimulator(
                    ___tmpPlanetGameObject.AddComponent <PlanetSimulator>(), planet);

                ___tmpPlanetGameObject.transform.localPosition = Vector3.zero;
                ___tmpPlanetBodyGameObject = new GameObject("Planet Body");
                ___tmpPlanetBodyGameObject.transform.SetParent(___tmpPlanetGameObject.transform, false);
                ___tmpPlanetBodyGameObject.layer = 31;
                ___tmpPlanetReformGameObject     = new GameObject("Terrain Reform");
                ___tmpPlanetReformGameObject.transform.SetParent(___tmpPlanetBodyGameObject.transform,
                                                                 false);
                ___tmpPlanetReformGameObject.layer = 14;
                var meshFilter1 = ___tmpPlanetReformGameObject.AddComponent <MeshFilter>();
                ___tmpPlanetReformRenderer = ___tmpPlanetReformGameObject.AddComponent <MeshRenderer>();
                meshFilter1.sharedMesh     = Configs.builtin.planetReformMesh;
                ___tmpPlanetReformRenderer.sharedMaterial    = planet.reformMaterial;
                ___tmpPlanetReformRenderer.receiveShadows    = false;
                ___tmpPlanetReformRenderer.lightProbeUsage   = LightProbeUsage.Off;
                ___tmpPlanetReformRenderer.shadowCastingMode = ShadowCastingMode.Off;
                // radius x2 = diameter
                var planetReformDiameter =
                    (float)((planet.realRadius + 0.200000002980232 + 0.025000000372529) * 2.0);


                Patch.Debug("planetReformDiameter :" + planetReformDiameter, LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);

                planetReformDiameter /= 2;

                Patch.Debug("Try patching reform Diameter / 2  :" + planetReformDiameter,
                            LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);

                ___tmpPlanetReformRenderer.transform.localScale = new Vector3(planetReformDiameter,
                                                                              planetReformDiameter, planetReformDiameter);
                ___tmpPlanetReformRenderer.transform.rotation = Quaternion.identity;
                if (planet.waterItemId != 0)
                {
                    var gameObject =
                        Instantiate(Configs.builtin.oceanSphere,
                                    ___tmpPlanetBodyGameObject.transform);
                    gameObject.name  = "Ocean Sphere";
                    gameObject.layer = 31;
                    gameObject.transform.localPosition = Vector3.zero;
                    gameObject.transform.localScale    = Vector3.one *
                                                         (float)((planet.realRadius +
                                                                  (double)planet.waterHeight) * 2.0);

                    Patch.Debug("Ocean Sphere Scale : realRadius : " + planet.realRadius +
                                " water height " + planet.waterHeight + " x 2 ", LogLevel.Debug,
                                Patch.DebugPlanetModelingManager);


                    Patch.Debug("Ocean Sphere Scale :" + gameObject.transform.localScale,
                                LogLevel.Debug,
                                Patch.DebugPlanetModelingManager);

                    var component = gameObject.GetComponent <Renderer>();
                    ___tmpOceanCollider = gameObject.GetComponent <Collider>();
                    if (component != null)
                    {
                        component.enabled = planet.oceanMaterial !=
                                            null;
                        component.shadowCastingMode = ShadowCastingMode.Off;
                        component.receiveShadows    = false;
                        component.lightProbeUsage   = LightProbeUsage.Off;
                        component.sharedMaterial    = planet.oceanMaterial;
                    }
                }

                Patch.Debug(
                    "Planet Precision  : " + planet.precision + " Planet Segments " + planet.segment,
                    LogLevel.Debug,
                    Patch.DebugPlanetModelingManager);

                var planetPrecisionBySegmentPlusOne = planetPrecisionBySegment + 1;


                Patch.Debug("Planet tris Generation", LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);
                for (var index1 = 0; index1 < planetPrecisionBySegment; ++index1)
                {
                    for (var index2 = 0; index2 < planetPrecisionBySegment; ++index2)
                    {
                        ___tmpTris.Add(index1 + 1 + (index2 + 1) * planetPrecisionBySegmentPlusOne);
                        ___tmpTris.Add(index1 + (index2 + 1) * planetPrecisionBySegmentPlusOne);
                        ___tmpTris.Add(index1 + index2 * planetPrecisionBySegmentPlusOne);
                        ___tmpTris.Add(index1 + index2 * planetPrecisionBySegmentPlusOne);
                        ___tmpTris.Add(index1 + 1 + index2 * planetPrecisionBySegmentPlusOne);
                        ___tmpTris.Add(index1 + 1 + (index2 + 1) * planetPrecisionBySegmentPlusOne);
                    }
                }

                Patch.Debug("___currentModelingStage end of 1", LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);
                ___currentModelingStage = 2;
                break;
            }

            case 2: {
                var planetPrecision = planet.precision;
                var data            = planet.data;
                var planetScale     = planet.scale;
                // float planetRadiusScaled = (float) (planet.radius * (double) planetScale + 0.200000002980232);
                var planetRadiusScaled =
                    (float)(planet.radius + 0.200000002980232);
                Patch.Debug("planetRadiusScaled " + planetRadiusScaled, LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);
                // planetRadiusScaled *= scaleFactor;
                Patch.Debug("planetRadiusScaled Patched " + planetRadiusScaled, LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);
                var stride         = data.stride;
                var num6           = 0;
                var stateOfTheGame = !GameMain.isLoading ? 2 : 3;
                var num8           = 0;
                for (var index1 = 0; index1 < 4; ++index1)
                {
                    var num9  = index1 % 2 * (planetPrecision + 1);
                    var num10 = index1 / 2 * (planetPrecision + 1);
                    for (var index2 = 0; index2 < planetPrecision; index2 += planetPrecisionBySegment)
                    {
                        for (var index3 = 0; index3 < planetPrecision; index3 += planetPrecisionBySegment)
                        {
                            if (num8 == 0 && num6 < ___tmpMeshList.Count)
                            {
                                ++num6;
                            }
                            else
                            {
                                var mesh = new Mesh();
                                ___tmpMeshList.Add(mesh);
                                ___tmpVerts.Clear();
                                ___tmpNorms.Clear();
                                ___tmpTgnts.Clear();
                                ___tmpUvs.Clear();
                                ___tmpUv2s.Clear();
                                var gameObject = new GameObject("Surface");
                                gameObject.layer = 30;
                                gameObject.transform.SetParent(___tmpPlanetBodyGameObject.transform, false);
                                for (var index4 = index2;
                                     index4 <= index2 + planetPrecisionBySegment &&
                                     index4 <= planetPrecision;
                                     ++index4)
                                {
                                    for (var index5 = index3;
                                         index5 <= index3 + planetPrecisionBySegment &&
                                         index5 <= planetPrecision;
                                         ++index5)
                                    {
                                        var num11  = num9 + index5;
                                        var num12  = num10 + index4;
                                        var index6 = num11 + num12 * stride;
                                        var num13  = index6;
                                        if (index4 == 0)
                                        {
                                            var num14 = (index1 + 3) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = planetPrecision;
                                            var num18 = planetPrecision - index5;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }
                                        else if (index5 == 0)
                                        {
                                            var num14 = (index1 + 3) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = planetPrecision - index4;
                                            var num18 = planetPrecision;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }

                                        if (index4 == planetPrecision)
                                        {
                                            var num14 = (index1 + 1) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = 0;
                                            var num18 = planetPrecision - index5;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }
                                        else if (index5 == planetPrecision)
                                        {
                                            var num14 = (index1 + 1) % 4;
                                            var num15 = num14 % 2 * (planetPrecision + 1);
                                            var num16 = num14 / 2 * (planetPrecision + 1);
                                            var num17 = planetPrecision - index4;
                                            var num18 = 0;
                                            num13 = num15 + num17 + (num16 + num18) * stride;
                                        }

                                        var heightDataScaled = data.heightData[index6] * 0.01f;
                                        if (planet.type == EPlanetType.Gas)
                                        {
                                            heightDataScaled *= planetScale;
                                        }
                                        Patch.Debug("heightDataScaled  :  " + heightDataScaled,
                                                    LogLevel.Debug,
                                                    Patch.DebugPlanetModelingManagerDeep);
                                        var thirdOfModLevel =
                                            data.GetModLevel(index6) * 0.3333333f;

                                        Patch.Debug("thirdOfModLevel  :  " + thirdOfModLevel,
                                                    LogLevel.Debug,
                                                    Patch.DebugPlanetModelingManagerDeep);

                                        if (thirdOfModLevel > 0.0)
                                        {
                                            //data.GetModPlane(index6)) 20000 + * 0.01f --> 200 +
                                            Patch.Debug(
                                                "data.GetModPlane(index6) :  " + data.GetModPlane(index6),
                                                LogLevel.Debug,
                                                Patch.DebugPlanetModelingManagerDeep);
                                            var modPlanePatched = data.GetModPlane(index6) *
                                                                  planet.GetScaleFactored();

                                            Patch.Debug("patch modPlane:  " + modPlanePatched,
                                                        LogLevel.Debug,
                                                        Patch.DebugPlanetModelingManagerDeep);
                                            planetRadiusScaled = modPlanePatched * 0.01f *
                                                                 planetScale;

                                            Patch.Debug(
                                                "planetRadiusScaled is modified :  " + planetRadiusScaled,
                                                LogLevel.Debug,
                                                Patch.DebugPlanetModelingManagerDeep);
                                        }

                                        // final height modification ?
                                        var finalHeight =
                                            (float)(heightDataScaled *
                                                    (1.0 - thirdOfModLevel) +
                                                    planetRadiusScaled *
                                                    (double)thirdOfModLevel);


                                        Patch.Debug("finalHeight :  " + finalHeight, LogLevel.Debug,
                                                    Patch.DebugPlanetModelingManagerDeep);

                                        var vector3_1 = data.vertices[index6] * finalHeight;
                                        ___tmpVerts.Add(vector3_1);
                                        ___tmpNorms.Add(data.vertices[index6]);
                                        var vector3_2 = Vector3.Cross(data.vertices[index6], Vector3.up)
                                                        .normalized;
                                        if (vector3_2.sqrMagnitude == 0.0)
                                        {
                                            vector3_2 = Vector3.right;
                                        }

                                        ___tmpTgnts.Add(new Vector4(vector3_2.x, vector3_2.y, vector3_2.z,
                                                                    1f));
                                        ___tmpUvs.Add(new Vector2((num11 + 0.5f) / stride,
                                                                  (num12 + 0.5f) / stride));
                                        ___tmpUv2s.Add(new Vector4(data.biomoData[index6] * 0.01f,
                                                                   data.temprData[index6] * 0.01f, index6 + 0.3f,
                                                                   num13 + 0.3f));
                                    }
                                }

                                mesh.indexFormat = IndexFormat.UInt16;
                                mesh.SetVertices(___tmpVerts);
                                mesh.SetNormals(___tmpNorms);
                                mesh.SetTangents(___tmpTgnts);
                                mesh.SetUVs(0, ___tmpUvs);
                                mesh.SetUVs(1, ___tmpUv2s);
                                mesh.SetTriangles(___tmpTris, 0, true, 0);
                                mesh.RecalculateNormals();
                                mesh.GetNormals(___tmpNorms);
                                for (var index4 = 0; index4 < ___tmpNorms.Count; ++index4)
                                {
                                    var z = (int)___tmpUv2s[index4].z;
                                    var w = (int)___tmpUv2s[index4].w;
                                    data.normals[z] = data.normals[z] + ___tmpNorms[index4];
                                    data.normals[w] = data.normals[w] + ___tmpNorms[index4];
                                }

                                var meshFilter2  = gameObject.AddComponent <MeshFilter>();
                                var meshRenderer = gameObject.AddComponent <MeshRenderer>();
                                var meshCollider = gameObject.AddComponent <MeshCollider>();
                                meshFilter2.sharedMesh         = mesh;
                                meshRenderer.sharedMaterial    = planet.terrainMaterial;
                                meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
                                meshRenderer.receiveShadows    = false;
                                meshRenderer.lightProbeUsage   = LightProbeUsage.Off;
                                meshCollider.sharedMesh        = mesh;
                                ___tmpMeshRendererList.Add(meshRenderer);
                                ___tmpMeshColliderList.Add(meshCollider);
                                ++num8;
                                if (num8 == stateOfTheGame)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }

                var num23 = !GameMain.isLoading ? 5 : 15;
                for (var index1 = 0; index1 < ___tmpMeshList.Count; ++index1)
                {
                    var num9 = index1 / num23;
                    if (num9 >= ___currentModelingSeamNormal)
                    {
                        if (num9 > ___currentModelingSeamNormal)
                        {
                            ++___currentModelingSeamNormal;
                            return(false);
                        }

                        var tmpMesh = ___tmpMeshList[index1];
                        ___tmpNorms.Clear();
                        ___tmpUv2s.Clear();
                        var vertexCount = tmpMesh.vertexCount;
                        tmpMesh.GetUVs(1, ___tmpUv2s);
                        for (var index2 = 0; index2 < vertexCount; ++index2)
                        {
                            var z = (int)___tmpUv2s[index2].z;
                            ___tmpNorms.Add(data.normals[z].normalized);
                        }

                        tmpMesh.SetNormals(___tmpNorms);
                    }
                }

                ___currentModelingStage = 3;
                break;
            }

            case 3: {
                ___tmpPlanetBodyGameObject.SetActive(true);
                ___tmpPlanetReformGameObject.SetActive(true);
                ___heightmapCamera.transform.localPosition = ___tmpPlanetGameObject.transform.localPosition;
                ___heightmapCamera.RenderToCubemap(planet.heightmap, 63);
                ___currentModelingStage = 4;
                break;
            }

            case 4: {
                Patch.Debug("___currentModelingStage start of 4", LogLevel.Debug,
                            Patch.DebugPlanetModelingManager);

                if (planet.wanted)
                {
                    Patch.Debug("planet.wanted is True", LogLevel.Debug,
                                Patch.DebugPlanetModelingManager);

                    planet.gameObject = ___tmpPlanetGameObject;
                    planet.bodyObject = ___tmpPlanetBodyGameObject;
                    var component = ___tmpPlanetGameObject.GetComponent <PlanetSimulator>();
                    component.surfaceRenderer = new Renderer[___tmpMeshRendererList.Count];
                    component.surfaceCollider = new Collider[___tmpMeshColliderList.Count];
                    for (var index = 0; index < ___tmpMeshList.Count; ++index)
                    {
                        planet.meshes[index]        = ___tmpMeshList[index];
                        planet.meshRenderers[index] = ___tmpMeshRendererList[index];
                        planet.meshColliders[index] = ___tmpMeshColliderList[index];
                    }

                    for (var index = 0; index < ___tmpMeshRendererList.Count; ++index)
                    {
                        ___tmpMeshRendererList[index].gameObject.layer  = 31;
                        ___tmpMeshRendererList[index].sharedMaterial    = planet.terrainMaterial;
                        ___tmpMeshRendererList[index].receiveShadows    = false;
                        ___tmpMeshRendererList[index].shadowCastingMode = ShadowCastingMode.Off;
                        component.surfaceRenderer[index] = ___tmpMeshRendererList[index];
                        component.surfaceCollider[index] = ___tmpMeshColliderList[index];
                    }

                    component.oceanCollider  = ___tmpOceanCollider;
                    component.sphereCollider = ___tmpPlanetBodyGameObject.AddComponent <SphereCollider>();
                    if (component.sphereCollider != null)
                    {
                        component.sphereCollider.enabled = false;
                    }
                    component.sphereCollider.radius = planet.realRadius;
                    component.reformRenderer        = ___tmpPlanetReformRenderer;
                    component.reformMat             = planet.reformMaterial;
                    var sharedMaterial = component.surfaceRenderer[0].sharedMaterial;
                    if (planet.type != EPlanetType.Gas)
                    {
                        component.reformMat.SetColor("_AmbientColor0",
                                                     sharedMaterial.GetColor("_AmbientColor0"));
                        component.reformMat.SetColor("_AmbientColor1",
                                                     sharedMaterial.GetColor("_AmbientColor1"));
                        component.reformMat.SetColor("_AmbientColor2",
                                                     sharedMaterial.GetColor("_AmbientColor2"));
                        component.reformMat.SetColor("_LightColorScreen",
                                                     sharedMaterial.GetColor("_LightColorScreen"));
                        component.reformMat.SetFloat("_Multiplier", sharedMaterial.GetFloat("_Multiplier"));
                        component.reformMat.SetFloat("_AmbientInc", sharedMaterial.GetFloat("_AmbientInc"));
                    }

                    ___tmpPlanetGameObject.transform.localPosition = Vector3.zero;
                    ___heightmapCamera.transform.localPosition     = Vector3.zero;
                    ___tmpPlanetBodyGameObject.SetActive(true);
                    ___tmpPlanetReformGameObject.SetActive(true);
                    ___tmpPlanetGameObject       = null;
                    ___tmpPlanetBodyGameObject   = null;
                    ___tmpPlanetReformGameObject = null;
                    ___tmpPlanetReformRenderer   = null;
                    ___tmpMeshList.Clear();
                    ___tmpMeshRendererList.Clear();
                    ___tmpMeshColliderList.Clear();
                    ___tmpOceanCollider = null;
                    ___tmpTris.Clear();
                    ___tmpVerts.Clear();
                    ___tmpNorms.Clear();
                    ___tmpTgnts.Clear();
                    ___tmpUvs.Clear();
                    ___tmpUv2s.Clear();
                    ___currentModelingPlanet     = null;
                    ___currentModelingStage      = 0;
                    ___currentModelingSeamNormal = 0;
                    planet.NotifyLoaded();
                    if (!planet.star.loaded)
                    {
                        break;
                    }

                    planet.star.NotifyLoaded();

                    Patch.Debug("___currentModelingStage end of 4", LogLevel.Debug,
                                Patch.DebugPlanetModelingManager);

                    break;
                }

                for (var index = 0; index < ___tmpMeshList.Count; ++index)
                {
                    Destroy(___tmpMeshList[index]);
                }
                Destroy(___tmpPlanetGameObject);
                ___tmpPlanetGameObject       = null;
                ___tmpPlanetBodyGameObject   = null;
                ___tmpPlanetReformGameObject = null;
                ___tmpPlanetReformRenderer   = null;
                ___tmpMeshList.Clear();
                ___tmpTris.Clear();
                ___tmpVerts.Clear();
                ___tmpNorms.Clear();
                ___tmpTgnts.Clear();
                ___tmpUvs.Clear();
                ___tmpUv2s.Clear();
                ___currentModelingPlanet     = null;
                ___currentModelingStage      = 0;
                ___currentModelingSeamNormal = 0;
                break;
            }
            }

            return(false);
        }