Exemple #1
0
        /// <summary>
        /// By default, KSP merges all scatters that are on one quad into one gigantic mesh that is then rendered.
        /// Because Kopernicus wants to know where each scatter actually is, we have to create our own scatter objects
        /// </summary>
        private void CreateScatterMeshes(PQSMod_LandClassScatterQuad quad)
        {
            Random.InitState(quad.seed);

            // Redo the density calculation
            if (useBetterDensity)
            {
                Dictionary <String, Double> densities = quad.quad.GetComponent <DensityContainer>().densities;
                if (densities.ContainsKey(quad.scatter.scatterName))
                {
                    Double density = densities[quad.scatter.scatterName];
                    if (ignoreDensityGameSetting && PQS.Global_ScatterFactor > 0)
                    {
                        density /= PQS.Global_ScatterFactor;
                    }
                    Double scatterN = density * quad.scatter.densityFactor *
                                      (quad.quad.quadArea / quad.quad.sphereRoot.radius / 1000.0) *
                                      quad.scatter.maxScatter;
                    scatterN  += Random.Range(-0.5f, 0.5f);
                    quad.count = Math.Min((Int32)Math.Round(scatterN), quad.scatter.maxScatter);
                }
            }

            for (Int32 i = 0; i < quad.count; i++)
            {
                if (useBetterDensity)
                {
                    // Generate a random number between 0 and 1. If it is above the spawn chance, abort
                    if (Random.value > spawnChance)
                    {
                        continue;
                    }
                }

                Int32 num2 = -1;
                Int32 num3 = -1;
                while (num3 == num2)
                {
                    Int32 num4 = Random.Range(1, PQS.cacheRes + 1);
                    Int32 num5 = Random.Range(1, PQS.cacheRes + 1);
                    Int32 x    = num4 + Random.Range(-1, 1);
                    Int32 z    = num5 + Random.Range(-1, 1);
                    num3 = PQS.vi(num4, num5);
                    num2 = PQS.vi(x, z);
                }

                Vector3 scatterPos = Vector3.Lerp(quad.quad.verts[num3], quad.quad.verts[num2], Random.value);
                Vector3 scatterUp  = quad.quad.sphereRoot.surfaceRelativeQuads
                    ? (Vector3)(scatterPos + quad.quad.positionPlanet).normalized
                    : scatterPos.normalized;

                scatterPos += scatterUp * quad.scatter.verticalOffset;
                Single     scatterAngle = Random.Range(rotation[0], rotation[1]);
                Quaternion scatterRot   = QuaternionD.AngleAxis(scatterAngle, scatterUp) * quad.quad.quadRotation;
                Single     scatterScale = Random.Range(quad.scatter.minScale, quad.scatter.maxScale);

                // Create a new object for the scatter
                GameObject scatterObject = new GameObject("Scatter");
                scatterObject.transform.parent        = quad.obj.transform;
                scatterObject.transform.localPosition = scatterPos;
                scatterObject.transform.localRotation = scatterRot;
                scatterObject.transform.localScale    = Vector3.one * scatterScale;
                scatterObject.AddComponent <KopernicusSurfaceObject>().objectName = quad.scatter.scatterName;
                MeshFilter filter = scatterObject.AddComponent <MeshFilter>();
                filter.sharedMesh = meshes.Count > 0 ? meshes[Random.Range(0, meshes.Count)] : baseMesh;
                MeshRenderer renderer = scatterObject.AddComponent <MeshRenderer>();
                renderer.sharedMaterial    = quad.scatter.material;
                renderer.shadowCastingMode = quad.scatter.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off;
                renderer.receiveShadows    = quad.scatter.recieveShadows;
                scatterObject.layer        = GameLayers.LOCAL_SPACE;
                scatterObjects.Add(scatterObject);
            }

            quad.obj.name = "Kopernicus-" + quad.scatter.scatterName;
        }