示例#1
0
 public DetailMesh(DetailMesh d)
 {
     mesh             = d.mesh;
     material         = d.material;
     meshFraction     = d.meshFraction;
     meshOffsetUp     = d.meshOffsetUp;
     meshScale        = d.meshScale;
     useGPUInstancing = d.useGPUInstancing;
     isGrass          = d.isGrass;
     posRots          = new PosRot[0];
 }
示例#2
0
        public IEnumerator GenerateGrass()
        {
            MeshRenderer meshRenderer = GetComponent <MeshRenderer>();

            MeshCollider collider = GetComponent <MeshCollider>();

            float size = meshRenderer.bounds.size.magnitude / Utils.sqrt2 * 1.2f;

            Vector3 down = planet.Vector3Down(meshRenderer.bounds.center);
            Vector3 x    = Vector3.Cross(down, Vector3.right).normalized;

            Vector3 y      = Vector3.Cross(x, down);
            Vector3 origin = meshRenderer.bounds.center + (down * -2500f);

            FoliageGenerator fm = new FoliageGenerator(origin, QuaternionD.Inverse(planet.rotation), transform.position, collider, down, x, y, int.Parse(quad.index.Substring(quad.index.Length - 7, 6)));

            int       numberOfPoints = planet.grassPerQuad;
            const int pointsPerFrame = 1000;

            int length = (numberOfPoints / pointsPerFrame);

            if (length <= 1)
            {
                fm.PointCloud(numberOfPoints, size); //generate if points per quad is less than points per frame
                length = 0;
            }

            for (int i = 0; i < length; i++)
            {
                if (planet.originMoved)
                {
                    down = planet.Vector3Down(meshRenderer.bounds.center);
                    x    = Vector3.Cross(down, Vector3.right).normalized;
                    y    = Vector3.Cross(x, down);

                    origin = meshRenderer.bounds.center + (down * -500f);
                    //int.Parse(quad.index.Substring(Mathf.Max(0, quad.index.Length - 9)))
                    fm             = new FoliageGenerator(origin, QuaternionD.Inverse(planet.rotation), transform.position, collider, down, x, y, System.DateTime.Now.Millisecond);
                    i              = 0;
                    numberOfPoints = planet.grassPerQuad;
                }

                if (numberOfPoints - pointsPerFrame > 0)
                {
                    numberOfPoints -= pointsPerFrame;
                    fm.PointCloud(pointsPerFrame, size);

                    if (!planet.planetIsRotating)
                    {
                        yield return(null);
                    }
                }
                else
                {
                    fm.PointCloud(numberOfPoints, size);
                }
            }

            List <DetailPrefab> detailPrefabs = new List <DetailPrefab>();

            for (int i = 0; i < planet.detailMeshes.Count; i++)
            {
                if (planet.detailMeshes[i].meshFraction != 0f)
                {
                    DetailMesh dO;
                    dO = new DetailMesh(planet.detailMeshes[i]);
                    detailObjects.Add(dO);
                    dO.posRots = fm.Positions(planet.detailMeshes[i].meshFraction, planet.detailMeshes[i]);
                }
            }

            for (int i = 0; i < planet.detailPrefabs.Count; i++)
            {
                if (planet.detailPrefabs[i].meshFraction != 0f)
                {
                    DetailPrefab dO;// = new DetailObject(planet.detailObjects[i]);
                    dO = new DetailPrefab(planet.detailPrefabs[i]);
                    detailPrefabs.Add(dO);
                    dO.posRots = fm.Positions(planet.detailPrefabs[i].meshFraction, planet.detailPrefabs[i]);
                }
            }

            //All points that remain after mesh and prefab spawning are used for grass.
            if (planet.generateGrass)
            {
                DetailMesh grass = new DetailMesh(fm.CreateMesh(), planet.grassMaterial);
                grass.posRots      = new PosRot[] { new PosRot(fm.position, Quaternion.Euler(0f, 0f, 0f)) };
                grass.meshFraction = 0f;
                grass.isGrass      = true;
                detailObjects.Add(grass);
            }

            for (int i = 0; i < detailPrefabs.Count; i++)
            {
                for (int j = 0; j < detailPrefabs[i].posRots.Length; j++)
                {
                    spawnedPrefabs.Add(Instantiate(detailPrefabs[i].prefab, detailPrefabs[i].posRots[j].position + transform.position, detailPrefabs[i].posRots[j].rotation * transform.rotation, transform));
                }
            }

            planet.detailObjectsGenerating--;
            generating = false;
            Initialize();
        }