public static bool GetModPlane(int index, ref PlanetRawData __instance, ref short __result)
        {
            Patch.Debug("scaleFactor " + __instance.GetFactoredScale(), LogLevel.Debug,
                        Patch.DebugGetModPlane);

            Patch.Debug("index " + index, LogLevel.Debug,
                        Patch.DebugGetModPlane);

            Patch.Debug("__instance.modData.Length " + __instance.modData.Length, LogLevel.Debug,
                        Patch.DebugGetModPlane);

            Patch.Debug(
                "test " + ((__instance.modData[index >> 1] >> (((index & 1) << 2) + 2)) & 3) * 133, LogLevel.Debug,
                Patch.DebugGetModPlane);

            float baseHeight = 20;

            baseHeight += __instance.GetFactoredScale() * 200 * 100;

            Patch.Debug("baseHeight " + baseHeight, LogLevel.Debug,
                        Patch.DebugGetModPlane);
            __result = (short)(((__instance.modData[index >> 1] >> (((index & 1) << 2) + 2)) & 3) * 133 +
                               baseHeight);

            Patch.Debug("GetModPlane __result " + __result, LogLevel.Debug,
                        Patch.DebugGetModPlane);

            return(false);
        }
 public static bool ComputeFlattenTerrainReform(
     Vector3[] points,
     Vector3 center,
     float radius,
     int pointsCount,
     float fade0 = 3f,
     float fade1 = 1f)
 {
     Patch.Debug("ComputeFlattenTerrainReform", LogLevel.Debug,
                 Patch.DebugPlanetFactory);
     return(true);
 }
        public static bool GetReformIndexForPosition(ref PlatformSystem __instance, Vector3 pos, ref int __result)
        {
            pos.Normalize();
            var num1 = Mathf.Asin(pos.y);
            var num2 = Mathf.Atan2(pos.x, -pos.z);
            //float f1 = num1 / (2* (float)Math.PI) * (float) __instance.segment;
            var   f1 = num1 / 6.283185f * __instance.segment;
            float longitudeSegmentCount = PlatformSystem.DetermineLongitudeSegmentCount(Mathf.FloorToInt(Mathf.Abs(f1)), __instance.segment);
            // float f2 = num2 / (2* (float)Math.PI)  * longitudeSegmentCount;
            var f2   = num2 / 6.283185f * longitudeSegmentCount;
            var f3   = Mathf.Round(f1 * 10f);
            var f4   = Mathf.Round(f2 * 10f);
            var num3 = Mathf.Abs(f3);
            var num4 = Mathf.Abs(f4);

            if (num3 % 2.0 != 1.0)
            {
                num3 = Mathf.FloorToInt(Mathf.Abs(f1) * 10f);
                if (num3 % 2.0 != 1.0)
                {
                    ++num3;
                }
            }
            var num5 = (double)f3 < 0.0 ? -num3 : num3;

            if (num4 % 2.0 != 1.0)
            {
                num4 = Mathf.FloorToInt(Mathf.Abs(f2) * 10f);
                if (num4 % 2.0 != 1.0)
                {
                    ++num4;
                }
            }
            var num6          = (double)f4 < 0.0 ? -num4 : num4;
            var _latitudeSeg  = num5 / 10f;
            var _longitudeSeg = num6 / 10f;

            float num7 = __instance.latitudeCount / 10;

            __result = (double)_latitudeSeg >= (double)num7 || (double)_latitudeSeg <= -(double)num7 ? -1 : __instance.GetReformIndexForSegment(_latitudeSeg, _longitudeSeg);

            Patch.Debug("PlatformSystem - __result --> " + __result, LogLevel.Debug, true);

            return(false);
        }
        public static void ReworkUpdate(ref PlanetAtmoBlur instance)
        {
            Patch.Debug("ReworkPlanetAtmoBlur", LogLevel.Debug, Patch.DebugAtmoBlur);
            instance.cam.fieldOfView = Camera.main.fieldOfView;
            if (GameMain.localPlanet != null && GameMain.localPlanet.type != EPlanetType.Gas)
            {
                Patch.Debug("GameMain.localPlanet" + GameMain.localPlanet, LogLevel.Debug, Patch.DebugAtmoBlur);
                instance.blurMat.SetVector("_PlanetRadius",
                                           new Vector4(200f * GameMain.localPlanet.GetScaleFactored(), 200f * GameMain.localPlanet.GetScaleFactored(), 240f * GameMain.localPlanet.GetScaleFactored(),
                                                       Mathf.Clamp01((float)((1100.0 - GameMain.mainPlayer.position.magnitude) / 600.0))));

                Patch.Debug("instance.blurMat" + instance.blurMat.GetVector("_PlanetRadius"), LogLevel.Debug, Patch.DebugAtmoBlur);
            }
            else
            {
                instance.blurMat.SetVector("_PlanetRadius", new Vector4(200f, 200f, 200f, 0.0f));
                Patch.Debug("else" + instance.blurMat.GetVector("_PlanetRadius"), LogLevel.Debug, Patch.DebugAtmoBlur);
            }

            Shader.SetGlobalTexture("_Global_AtmoBlurTex", instance.planetAtmoBlurRTex);
        }
Beispiel #5
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 QueryModifiedHeight(ref PlanetRawData __instance,
                                               ref float __result, Vector3 vpos)
        {
            Patch.Debug("QueryModifiedHeight ", LogLevel.Debug,
                        Patch.DebugPlanetRawData);
            vpos.Normalize();
            var index1          = __instance.indexMap[__instance.PositionHash(vpos)];
            var radiusPrecision =
                (float)(3.14159274101257 / (__instance.precision * 2) * 1.20000004768372);

            Patch.Debug("radiusPrecision " + radiusPrecision, LogLevel.Debug,
                        Patch.DebugPlanetRawData);
            var radiusPrecisionSq = radiusPrecision * radiusPrecision;

            Patch.Debug("radiusPrecisionSq " + radiusPrecisionSq, LogLevel.Debug,
                        Patch.DebugPlanetRawData);
            var magnetudeOnPrecisionDummy = 0.0f;
            var HeightTimePrecision       = 0.0f;
            var stride = __instance.stride;

            for (var index2 = -1; index2 <= 3; ++index2)
            {
                for (var index3 = -1; index3 <= 3; ++index3)
                {
                    var index4 = index1 + index2 + index3 * stride;
                    if ((uint)index4 < __instance.dataLength)
                    {
                        var sqrMagnitude = (__instance.vertices[index4] - vpos).sqrMagnitude;

                        Patch.Debug("sqrMagnitude " + sqrMagnitude, LogLevel.Debug,
                                    Patch.DebugPlanetRawData);
                        if (sqrMagnitude <= (double)radiusPrecisionSq)
                        {
                            var magnetudeOnPrecision =
                                (float)(1.0 - Mathf.Sqrt(sqrMagnitude) / (double)radiusPrecision);

                            Patch.Debug("MagnetudeOnPrecision " + magnetudeOnPrecision, LogLevel.Debug,
                                        Patch.DebugPlanetRawData);
                            magnetudeOnPrecision *= __instance.GetFactoredScale();

                            Patch.Debug("MagnetudeOnPrecision Patched " + magnetudeOnPrecision,
                                        LogLevel.Debug,
                                        Patch.DebugPlanetRawData);
                            var   modLevel        = __instance.GetModLevel(index4);
                            float heightDataFinal = __instance.heightData[index4];

                            Patch.Debug("heightDataFinal First " + heightDataFinal, LogLevel.Debug,
                                        Patch.DebugPlanetRawData);
                            if (modLevel > 0)
                            {
                                // try patching here
                                var modPlane = __instance.GetModPlaneInt(index4);

                                Patch.Debug("modPlane " + modPlane, LogLevel.Debug,
                                            Patch.DebugPlanetRawData);
                                if (modLevel == 3)
                                {
                                    heightDataFinal = modPlane;

                                    Patch.Debug("heightDataFinal Second " + heightDataFinal, LogLevel.Debug,
                                                Patch.DebugPlanetRawData);
                                }
                                else
                                {
                                    var num7 = modLevel * 0.3333333f;
                                    heightDataFinal =
                                        (float)(__instance.heightData[index4] * (1.0 - num7) +
                                                modPlane * (double)num7);

                                    Patch.Debug("heightDataFinal Third " + heightDataFinal, LogLevel.Debug,
                                                Patch.DebugPlanetRawData);
                                }
                            }

                            Patch.Debug("__result num6 " + heightDataFinal, LogLevel.Debug,
                                        Patch.DebugPlanetRawData);

                            magnetudeOnPrecisionDummy += magnetudeOnPrecision;
                            HeightTimePrecision       += heightDataFinal * magnetudeOnPrecision;

                            Patch.Debug("heightDataFinal Third " + heightDataFinal, LogLevel.Debug,
                                        Patch.DebugPlanetRawData);
                        }
                    }
                }
            }

            if (magnetudeOnPrecisionDummy != 0.0)
            {
                __result = (float)(HeightTimePrecision / (double)magnetudeOnPrecisionDummy *
                                   0.00999999977648258);

                Patch.Debug("__result magnetudeOnPrecisionDummy" + __result, LogLevel.Debug,
                            Patch.DebugPlanetRawData);
                return(false);
            }

            Debug.LogWarning("bad query");
            __result = __instance.heightData[0] * 0.01f;

            Patch.Debug("__result bad query" + __result, LogLevel.Debug,
                        Patch.DebugPlanetRawData);

            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 UpdateUniversalPosition(ref PlanetSimulator __instance, ref StarSimulator ___star, ref bool ___isLocal, ref Transform ___lookCamera, Vector3 playerLPos, VectorLF3 playerUPos, Vector3 cameraPos)
        {
            if (__instance.planetData == null || __instance.planetData.loading || __instance.planetData.factoryLoading)
            {
                return(false);
            }
            __instance.SetLayers();
            var localPlanet = GameMain.localPlanet;
            var flag1       = ___isLocal != (localPlanet == __instance.planetData);

            ___isLocal = localPlanet == __instance.planetData;
            var flag2 = PlanetSimulator.sOptionCastShadow != __instance.optionCastShadow;

            if (flag1 || flag2)
            {
                foreach (var renderer in __instance.surfaceRenderer)
                {
                    renderer.receiveShadows    = ___isLocal;
                    renderer.shadowCastingMode = !___isLocal || !PlanetSimulator.sOptionCastShadow
                        ? ShadowCastingMode.Off
                        : ShadowCastingMode.On;
                }

                __instance.optionCastShadow = PlanetSimulator.sOptionCastShadow;
            }

            __instance.reformRenderer.receiveShadows = ___isLocal;
            var flag3 = ___isLocal && __instance.planetData.type != EPlanetType.Gas;

            if (__instance.sphereCollider.enabled == flag3)
            {
                foreach (var collider in __instance.surfaceCollider)
                {
                    collider.enabled = flag3;
                }
                if (__instance.oceanCollider != null)
                {
                    __instance.oceanCollider.enabled = flag3;
                }
                __instance.sphereCollider.enabled = !flag3;
            }

            var       uPosition  = __instance.planetData.uPosition;
            var       quaternion = Quaternion.identity;
            VectorLF3 vectorLf3;

            if (localPlanet != null)
            {
                quaternion = localPlanet.runtimeRotation;
                if (localPlanet == __instance.planetData)
                {
                    vectorLf3 = VectorLF3.zero;
                }
                else
                {
                    var v = uPosition - localPlanet.uPosition;
                    vectorLf3 = Maths.QInvRotateLF(quaternion, v);
                }
            }
            else
            {
                vectorLf3 = uPosition - playerUPos;
            }

            var     vscale = 1f;
            Vector3 vpos;

            UniverseSimulator.VirtualMapping(vectorLf3.x, vectorLf3.y, vectorLf3.z, cameraPos, out vpos, out vscale);
            var vector3_1 = Vector3.one * vscale;

            if (__instance.transform.localPosition != vpos)
            {
                __instance.transform.localPosition = vpos;
            }
            if (__instance.planetData == localPlanet)
            {
                if (__instance.transform.localPosition.sqrMagnitude > 0.0)
                {
                    __instance.transform.localPosition = Vector3.zero;
                }
                if (__instance.transform.localRotation != Quaternion.identity)
                {
                    __instance.transform.localRotation = Quaternion.identity;
                }
            }
            else
            {
                __instance.transform.localRotation =
                    Quaternion.Inverse(quaternion) * __instance.planetData.runtimeRotation;
            }

            if (__instance.transform.localScale != vector3_1)
            {
                __instance.transform.localScale = vector3_1;
            }
            var vector3_2 = Quaternion.Inverse(quaternion) *
                            (__instance.planetData.star.uPosition - __instance.planetData.uPosition).normalized;
            var lhs           = ___lookCamera.localPosition - __instance.transform.localPosition;
            var magnitude     = lhs.magnitude;
            var localRotation = __instance.transform.localRotation;
            var vector4_1     = new Vector4(localRotation.x, localRotation.y, localRotation.z, localRotation.w);

            if (__instance.surfaceRenderer != null && __instance.surfaceRenderer.Length > 0)
            {
                var sharedMaterial = __instance.surfaceRenderer[0].sharedMaterial;
                sharedMaterial.SetVector("_SunDir", vector3_2);
                sharedMaterial.SetVector("_Rotation",
                                         new Vector4(localRotation.x, localRotation.y, localRotation.z, localRotation.w));
                sharedMaterial.SetFloat("_Distance", magnitude);
            }

            if (__instance.reformRenderer != null &&
                __instance.reformMat != null && __instance.planetData.factory != null)
            {
                var platformSystem      = __instance.planetData.factory.platformSystem;
                var reformOffsetsBuffer = platformSystem.reformOffsetsBuffer;
                var reformDataBuffer    = platformSystem.reformDataBuffer;
                __instance.reformMat.SetFloat("_LatitudeCount", platformSystem.latitudeCount);
                __instance.reformMat.SetVector("_SunDir", vector3_2);
                __instance.reformMat.SetFloat("_Distance", magnitude);
                __instance.reformMat.SetVector("_Rotation", vector4_1);
                if (platformSystem.reformData != null && reformDataBuffer != null)
                {
                    reformOffsetsBuffer.SetData(platformSystem.reformOffsets);
                    reformDataBuffer.SetData(platformSystem.reformData);
                    __instance.reformMat.SetBuffer("_OffsetsBuffer", reformOffsetsBuffer);
                    __instance.reformMat.SetBuffer("_DataBuffer", reformDataBuffer);
                }
            }

            if (!(__instance.atmoTrans0 != null))
            {
                return(false);
            }
            __instance.atmoTrans0.rotation = ___lookCamera.localRotation;
            Vector4 vector4_2 = !(GameCamera.generalTarget == null)
                ? GameCamera.generalTarget.position
                : Vector3.zero;

            // ********************************* Fix Here for release :)
            var positionOffset = 0;

            if (localPlanet != null)
            {
                if (localPlanet.type != EPlanetType.Gas)
                {
                    positionOffset = Mathf.RoundToInt(Mathf.Abs(localPlanet.radius - 200) / 2);
                }
            }
            Patch.Debug("positionOffset " + positionOffset, LogLevel.Debug, Patch.DebugAtmoBlur);


            __instance.atmoTrans1.localPosition = new Vector3(0, 0,
                                                              Mathf.Clamp(Vector3.Dot(lhs, ___lookCamera.forward) + 10f / __instance.planetData.GetScaleFactored(), 0.0f, Math.Max(320f, 320f * __instance.planetData.GetScaleFactored())));
            var num1 = Mathf.Clamp01(8000f * __instance.planetData.GetScaleFactored() / magnitude);
            var num2 = Mathf.Clamp01(4000f * __instance.planetData.GetScaleFactored() / magnitude);
            var atmoMatRadiusParam = __instance.atmoMatRadiusParam;

            atmoMatRadiusParam.z = atmoMatRadiusParam.x +
                                   (float)((__instance.atmoMatRadiusParam.z - (double)__instance.atmoMatRadiusParam.x) *
                                           (2.70000004768372 - num2 * 1.70000004768372));
            Patch.Debug("__instance.atmoTrans1  " + __instance.atmoTrans1.localScale, LogLevel.Debug, Patch.DebugAtmoBlur);
            var vector4_3 = atmoMatRadiusParam * vscale * __instance.planetData.GetScaleFactored();

            Patch.Debug("vector4_3 " + __instance.planetData.name + vector4_3, LogLevel.Debug, Patch.DebugAtmoBlur);
            __instance.atmoMat.SetVector("_PlanetPos", __instance.transform.localPosition);
            __instance.atmoMat.SetVector("_SunDir", vector3_2);
            __instance.atmoMat.SetVector("_PlanetRadius", vector4_3);
            __instance.atmoMat.SetColor("_Color4", ___star.sunAtmosColor);
            __instance.atmoMat.SetColor("_Sky4", ___star.sunriseAtmosColor);
            __instance.atmoMat.SetVector("_LocalPos", vector4_2);
            __instance.atmoMat.SetFloat("_SunRiseScatterPower",
                                        Mathf.Max(60f * __instance.planetData.GetScaleFactored(),
                                                  (float)((magnitude - __instance.planetData.realRadius * 2.0) * 0.180000007152557)));
            __instance.atmoMat.SetFloat("_IntensityControl", num1);
            __instance.atmoMat.renderQueue = __instance.planetData != localPlanet ? 2989 : 2991;

            return(false);
        }
        public static bool FlattenTerrainReform(
            ref PlanetFactory __instance,
            ref Dictionary <int, int> ___tmp_levelChanges,
            ref int[] ___tmp_entity_ids,
            ref int[] ___tmp_ids,
            Vector3 center,
            float radius,
            int reformSize,
            bool veinBuried,
            float fade0 = 3f)
        {
            if (___tmp_ids == null)
            {
                ___tmp_ids = new int[1024];
            }
            if (___tmp_entity_ids == null)
            {
                ___tmp_entity_ids = new int[1024];
            }
            Array.Clear(___tmp_ids, 0, ___tmp_ids.Length);
            Array.Clear(___tmp_entity_ids, 0, ___tmp_entity_ids.Length);
            var zero       = Vector3.zero;
            var data       = __instance.planet.data;
            var heightData = data.heightData;
            var num1       = Mathf.Min(9f,
                                       Mathf.Abs((float)((heightData[data.QueryIndex(center)] -
                                                          __instance.planet.realRadius * 100.0) * 0.00999999977648258 * 2.0)));

            fade0 += num1;
            var areaRadius = radius + fade0;
            var num2       = (short)(__instance.planet.realRadius * 100.0);
            var levelized  = __instance.planet.levelized;
            var plane      = Mathf.RoundToInt(
                (float)((center.magnitude - (double)__instance.planet.realRadius) /
                        1.33333325386047));
            var num3 = plane * 133 + num2 - 60;
            var num4 = (float)(__instance.planet.radius * 100.0 - 20.0);

            foreach (var tmpLevelChange in ___tmp_levelChanges)
            {
                if (tmpLevelChange.Value > 0)
                {
                    var num5 = heightData[tmpLevelChange.Key];
                    if (levelized)
                    {
                        if (num5 >= num3)
                        {
                            if (data.GetModLevel(tmpLevelChange.Key) < 3)
                            {
                                data.SetModPlane(tmpLevelChange.Key, plane);
                            }
                            __instance.planet.AddHeightMapModLevel(tmpLevelChange.Key, tmpLevelChange.Value);
                        }
                    }
                    else
                    {
                        __instance.planet.AddHeightMapModLevel(tmpLevelChange.Key, tmpLevelChange.Value);
                    }

                    if (num5 < (double)num4)
                    {
                        __instance.planet.landPercentDirty = true;
                    }
                }
            }

            if (__instance.planet.UpdateDirtyMeshes())
            {
                __instance.RenderLocalPlanetHeightmap();
            }
            radius -= reformSize * 0.15f;
            var nearColliderLogic        = __instance.planet.physics.nearColliderLogic;
            var vegetablesInAreaNonAlloc =
                nearColliderLogic.GetVegetablesInAreaNonAlloc(center, areaRadius, ___tmp_ids);
            var num6 = radius * radius;

            for (var index = 0; index < vegetablesInAreaNonAlloc; ++index)
            {
                var tmpId   = ___tmp_ids[index];
                var vector3 = __instance.vegePool[tmpId].pos - center;
                if (vector3.x * (double)vector3.x + vector3.y * (double)vector3.y +
                    vector3.z * (double)vector3.z <= num6 + 2.20000004768372)
                {
                    __instance.RemoveVegeWithComponents(tmpId);
                }
                else
                {
                    var num5 = data.QueryModifiedHeight(__instance.vegePool[tmpId].pos) - 0.03f;
                    __instance.vegePool[tmpId].pos = __instance.vegePool[tmpId].pos.normalized * num5;
                    GameMain.gpuiManager.AlterModel(__instance.vegePool[tmpId].modelIndex,
                                                    __instance.vegePool[tmpId].modelId, tmpId, __instance.vegePool[tmpId].pos,
                                                    __instance.vegePool[tmpId].rot, false);
                }
            }

            var num7 = 50f;
            var num8 = !veinBuried
                ? nearColliderLogic.GetVeinsInOceanInAreaNonAlloc(center, areaRadius, ___tmp_ids)
                : nearColliderLogic.GetVeinsInAreaNonAlloc(center, areaRadius, ___tmp_ids);

            for (var index1 = 0; index1 < num8; ++index1)
            {
                var tmpId     = ___tmp_ids[index1];
                var pos       = __instance.veinPool[tmpId].pos;
                var num5      = __instance.planet.realRadius + 0.2f;
                var vector3_1 = pos.normalized * num5 - center;
                if (vector3_1.x * (double)vector3_1.x + vector3_1.y * (double)vector3_1.y +
                    vector3_1.z * (double)vector3_1.z <= num6 + 2.0)
                {
                    var physics      = __instance.planet.physics;
                    var colliderId   = __instance.veinPool[tmpId].colliderId;
                    var colliderData = physics.GetColliderData(colliderId);
                    if (veinBuried)
                    {
                        num5 -= num7;
                    }
                    else
                    {
                        var center1 = pos.normalized * num5;
                        if (nearColliderLogic.GetEntitiesInAreaWhenReformNonAlloc(center1, colliderData.radius,
                                                                                  ___tmp_entity_ids) > 0)
                        {
                            num5 = pos.magnitude;
                        }
                    }

                    var vector3_2 = colliderData.pos.normalized * num5;
                    var index2    = colliderId >> 20;
                    var index3    = colliderId & 1048575;
                    physics.colChunks[index2].colliderPool[index3].pos = vector3_2;
                    __instance.veinPool[tmpId].pos = pos.normalized * num5;
                    physics.RefreshColliders();
                    GameMain.gpuiManager.AlterModel(__instance.veinPool[tmpId].modelIndex,
                                                    __instance.veinPool[tmpId].modelId, tmpId, __instance.veinPool[tmpId].pos, false);
                }
            }

            ___tmp_levelChanges.Clear();
            Array.Clear(___tmp_ids, 0, ___tmp_ids.Length);
            Array.Clear(___tmp_ids, 0, ___tmp_entity_ids.Length);
            GameMain.gpuiManager.SyncAllGPUBuffer();
            Debug.Log("FlattenTerrainReform -- Begining ");
            Patch.Debug("FlattenTerrainReform ", LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            return(false);
        }
        public static bool FlattenTerrain(
            ref PlanetFactory __instance,
            ref int __result,
            ref Dictionary <int, int> ___tmp_levelChanges,
            ref int[] ___tmp_ids,
            Vector3 pos,
            Quaternion rot,
            Bounds bound,
            float fade0     = 6f,
            float fade1     = 1f,
            bool removeVein = false,
            bool lift       = false)
        {
            Debug.Log("FlattenTerrain -- Begining ");
            Patch.Debug("FlattenTerrain -- Begining ", LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            if (___tmp_levelChanges == null)
            {
                ___tmp_levelChanges = new Dictionary <int, int>();
            }

            if (___tmp_ids == null)
            {
                ___tmp_ids = new int[1024];
            }

            ___tmp_levelChanges.Clear();
            Array.Clear(___tmp_ids, 0, ___tmp_ids.Length);
            bound.extents = new Vector3(bound.extents.x, bound.extents.y + 0.5f, bound.extents.z);
            var quaternion1 = rot;

            quaternion1.w = -quaternion1.w;
            var quaternion2 = Maths.SphericalRotation(pos, 22.5f);
            var realRadius  = __instance.planet.realRadius;

            Patch.Debug("realRadius --  " + realRadius, LogLevel.Debug,
                        Patch.DebugPlanetFactory);

            var areaRadius = bound.extents.magnitude + fade0;

            Patch.Debug("areaRadius --  " + areaRadius, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var areaRadiusSq = areaRadius * areaRadius;

            Patch.Debug("areaRadiusSq --  " + areaRadiusSq, LogLevel.Debug,
                        Patch.DebugPlanetFactory);

            // Def something related to that that mess up everything
            //areaRadiusSq *= scaleFactor;

            Patch.Debug("Patching areaRadiusSq --  " + areaRadiusSq, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var circlePeriphery = (float)(realRadius * 3.14159274101257 /
                                          (__instance.planet.precision * 2.0));

            Patch.Debug("circlePeriphery --  " + circlePeriphery, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var levelAffected =
                Mathf.CeilToInt(
                    (float)(areaRadius * 1.41400003433228 / circlePeriphery + 0.5));

            Patch.Debug("levelAffected --  " + levelAffected, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var vector3Array = new Vector3[9] {
                pos,
                pos + quaternion2 * (new Vector3(0.0f, 0.0f, 1.414f) * areaRadius),
                pos + quaternion2 * (new Vector3(0.0f, 0.0f, -1.414f) * areaRadius),
                pos + quaternion2 * (new Vector3(1.414f, 0.0f, 0.0f) * areaRadius),
                pos + quaternion2 * (new Vector3(-1.414f, 0.0f, 0.0f) * areaRadius),
                pos + quaternion2 * (new Vector3(1f, 0.0f, 1f) * areaRadius),
                pos + quaternion2 * (new Vector3(-1f, 0.0f, -1f) * areaRadius),
                pos + quaternion2 * (new Vector3(1f, 0.0f, -1f) * areaRadius),
                pos + quaternion2 * (new Vector3(-1f, 0.0f, 1f) * areaRadius)
            };


            var stride = __instance.planet.data.stride;

            Patch.Debug("stride --  " + stride, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var dataLength = __instance.planet.data.dataLength;

            Patch.Debug("dataLength --  " + dataLength, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var vertices = __instance.planet.data.vertices;

            Patch.Debug("vertices --  " + vertices, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var heightData = __instance.planet.data.heightData;

            Patch.Debug("heightData --  " + heightData, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var radiusHeight100 = (short)(__instance.planet.realRadius * 100.0);

            Patch.Debug("radiusHeight100 --  " + radiusHeight100, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var fRoundToInt = 0;

            foreach (var vpos in vector3Array)
            {
                var posIndex = __instance.planet.data.QueryIndex(vpos);
                for (var index1 = -levelAffected; index1 <= levelAffected; ++index1)
                {
                    var strideIndex = posIndex + index1 * stride;
                    if (strideIndex >= 0 && strideIndex < dataLength)
                    {
                        for (var index2 = -levelAffected; index2 <= levelAffected; ++index2)
                        {
                            var index3 = strideIndex + index2;
                            if ((uint)index3 < dataLength &&
                                (lift || heightData[index3] > radiusHeight100))
                            {
                                Vector3 vector3_1;
                                vector3_1.x = vertices[index3].x * realRadius;
                                vector3_1.y = vertices[index3].y * realRadius;
                                vector3_1.z = vertices[index3].z * realRadius;
                                Vector3 vector3_2;
                                vector3_2.x = vector3_1.x - pos.x;
                                vector3_2.y = vector3_1.y - pos.y;
                                vector3_2.z = vector3_1.z - pos.z;
                                if (vector3_2.x * (double)vector3_2.x +
                                    vector3_2.y * (double)vector3_2.y +
                                    vector3_2.z * (double)vector3_2.z < areaRadiusSq &&
                                    !___tmp_levelChanges.ContainsKey(index3))
                                {
                                    Patch.Debug("vector3_2 less than areaRadiusSq --  " + vector3_2,
                                                LogLevel.Debug,
                                                Patch.DebugPlanetFactoryDeep);
                                    vector3_1 = quaternion1 * (vector3_1 - pos);

                                    Patch.Debug("vector3_1 less than areaRadiusSq --  " + vector3_1,
                                                LogLevel.Debug,
                                                Patch.DebugPlanetFactoryDeep);

                                    if (bound.Contains(vector3_1))
                                    {
                                        ___tmp_levelChanges[index3] = 3;
                                    }
                                    else
                                    {
                                        var boundDistance = Vector3.Distance(bound.ClosestPoint(vector3_1),
                                                                             vector3_1);
                                        var boundDistanceFade =
                                            (int)((fade0 - (double)boundDistance) /
                                                  (fade0 - (double)fade1) * 3.0 + 0.5);
                                        if (boundDistanceFade < 0)
                                        {
                                            boundDistanceFade = 0;
                                        }
                                        else if (boundDistanceFade > 3)
                                        {
                                            boundDistanceFade = 3;
                                        }

                                        var modLevel = __instance.planet.data.GetModLevel(index3);

                                        Patch.Debug("modLevel --  " + modLevel, LogLevel.Debug,
                                                    Patch.DebugPlanetFactoryDeep);
                                        var boundDiff = boundDistanceFade - modLevel;

                                        Patch.Debug("boundDiff --  " + boundDiff, LogLevel.Debug,
                                                    Patch.DebugPlanetFactoryDeep);
                                        if (boundDistanceFade >= modLevel && boundDiff != 0)
                                        {
                                            ___tmp_levelChanges[index3] = boundDistanceFade;
                                            var currentHeightData = heightData[index3] * 0.01f;

                                            Patch.Debug("currentHeightData --  " + currentHeightData,
                                                        LogLevel.Debug,
                                                        Patch.DebugPlanetFactoryDeep);

                                            var currentHeightDataOnRadius =
                                                realRadius - currentHeightData;

                                            Patch.Debug(
                                                "currentHeightDataOnRadius --  " + currentHeightDataOnRadius,
                                                LogLevel.Debug,
                                                Patch.DebugPlanetFactoryDeep);
                                            //anomaly here
                                            //currentHeightDataOnRadius *= scaleFactor;

                                            Patch.Debug(
                                                "Patching currentHeightDataOnRadius --  " +
                                                currentHeightDataOnRadius, LogLevel.Debug,
                                                Patch.DebugPlanetFactoryDeep);

                                            var f = (float)(100.0 * boundDiff *
                                                            currentHeightDataOnRadius *
                                                            0.333333313465118);

                                            Patch.Debug("f --  " + f, LogLevel.Debug,
                                                        Patch.DebugPlanetFactoryDeep);
                                            fRoundToInt += -Mathf.FloorToInt(f);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            var levelized = __instance.planet.levelized;
            var plane     = Mathf.RoundToInt(
                (float)((pos.magnitude - (double)__instance.planet.realRadius) /
                        1.33333325386047));


            Patch.Debug("plane --  " + plane, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            var num13 = plane * 133 + radiusHeight100 - 60;

            Patch.Debug("num13 --  " + num13, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
            foreach (var tmpLevelChange in ___tmp_levelChanges)
            {
                if (tmpLevelChange.Value > 0)
                {
                    if (levelized)
                    {
                        if (heightData[tmpLevelChange.Key] >= num13)
                        {
                            if (__instance.planet.data.GetModLevel(tmpLevelChange.Key) < 3)
                            {
                                Patch.Debug("SetModPlane --  plane" + plane, LogLevel.Debug,
                                            Patch.DebugPlanetFactoryDeep);

                                Patch.Debug(
                                    "SetModPlane --  heightData[tmpLevelChange.Key]" +
                                    heightData[tmpLevelChange.Key], LogLevel.Debug,
                                    Patch.DebugPlanetFactoryDeep);
                                __instance.planet.data.SetModPlane(tmpLevelChange.Key, plane);
                            }


                            Patch.Debug(
                                "If AddHeightMapModLevel --  tmpLevelChange.Value" + tmpLevelChange.Value,
                                LogLevel.Debug,
                                Patch.DebugPlanetFactoryDeep);

                            Patch.Debug(
                                "If AddHeightMapModLevel --  heightData[tmpLevelChange.Key]" +
                                heightData[tmpLevelChange.Key], LogLevel.Debug,
                                Patch.DebugPlanetFactoryDeep);

                            __instance.planet.AddHeightMapModLevel(tmpLevelChange.Key, tmpLevelChange.Value);
                        }
                    }
                    else
                    {
                        Patch.Debug(
                            "Else AddHeightMapModLevel --  tmpLevelChange.Value" + tmpLevelChange.Value,
                            LogLevel.Debug,
                            Patch.DebugPlanetFactoryDeep);

                        Patch.Debug(
                            "Else AddHeightMapModLevel --  heightData[tmpLevelChange.Key]" +
                            heightData[tmpLevelChange.Key], LogLevel.Debug,
                            Patch.DebugPlanetFactoryDeep);

                        __instance.planet.AddHeightMapModLevel(tmpLevelChange.Key, tmpLevelChange.Value);
                    }
                }
            }

            var flag = __instance.planet.UpdateDirtyMeshes();

            if (GameMain.isRunning && flag)
            {
                __instance.RenderLocalPlanetHeightmap();
            }

            bound.extents += new Vector3(1.5f, 1.5f, 1.5f);
            var nearColliderLogic        = __instance.planet.physics.nearColliderLogic;
            var vegetablesInAreaNonAlloc =
                nearColliderLogic.GetVegetablesInAreaNonAlloc(pos, areaRadius, ___tmp_ids);

            for (var index = 0; index < vegetablesInAreaNonAlloc; ++index)
            {
                var tmpId = ___tmp_ids[index];
                var pos1  = __instance.vegePool[tmpId].pos;
                var point = quaternion1 * (pos1 - pos);
                if (bound.Contains(point))
                {
                    __instance.RemoveVegeWithComponents(tmpId);
                }
                else
                {
                    var num6 = __instance.planet.data.QueryModifiedHeight(__instance.vegePool[tmpId].pos);

                    Patch.Debug(
                        "num6 --  __instance.planet.data.QueryModifiedHeight(__instance.vegePool[tmpId].pos) - 0.03f + heightData[tmpLevelChange.Key]" +
                        num6, LogLevel.Debug,
                        Patch.DebugPlanetFactory);
                    __instance.vegePool[tmpId].pos = __instance.vegePool[tmpId].pos.normalized * num6;

                    Patch.Debug("__instance.vegePool[tmpId].pos --  _" + __instance.vegePool[tmpId].pos,
                                LogLevel.Debug,
                                Patch.DebugPlanetFactory);
                    GameMain.gpuiManager.AlterModel(__instance.vegePool[tmpId].modelIndex,
                                                    __instance.vegePool[tmpId].modelId, tmpId, __instance.vegePool[tmpId].pos,
                                                    __instance.vegePool[tmpId].rot,
                                                    false);
                }
            }

            var veinsInAreaNonAlloc = nearColliderLogic.GetVeinsInAreaNonAlloc(pos, areaRadius, ___tmp_ids);

            for (var index = 0; index < veinsInAreaNonAlloc; ++index)
            {
                var tmpId = ___tmp_ids[index];
                var pos1  = __instance.veinPool[tmpId].pos;
                if (removeVein && bound.Contains(pos1))
                {
                    __instance.RemoveVeinWithComponents(tmpId);
                }
                else if (pos1.magnitude > (double)__instance.planet.realRadius)
                {
                    var num6 = __instance.planet.data.QueryModifiedHeight(pos1) - 0.13f;
                    __instance.veinPool[tmpId].pos = pos1.normalized * num6;
                    GameMain.gpuiManager.AlterModel(__instance.veinPool[tmpId].modelIndex,
                                                    __instance.veinPool[tmpId].modelId, tmpId, __instance.veinPool[tmpId].pos, false);
                }
            }

            ___tmp_levelChanges.Clear();
            Array.Clear(___tmp_ids, 0, ___tmp_ids.Length);
            GameMain.gpuiManager.SyncAllGPUBuffer();
            __result = fRoundToInt;


            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;

            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);
        }