Exemplo n.º 1
0
        //Heightmaps are RGB packed, generated by Generator app
        float HeightColorToHeight(int x, int y)
        {
            int pos = (y * x_res + x) * 4;

            //byte r = Heightmap.rgbValues[pos + 2];
            //byte g = Heightmap.rgbValues[pos + 1];
            //byte b = Heightmap.rgbValues[pos];
            if (Heightmap == null)
            {
                return(0);
            }
            float[] col    = Heightmap.GetPixel(x, y);
            byte    r      = (byte)(col[0] * 255);
            byte    g      = (byte)(col[1] * 255);
            byte    b      = (byte)(col[2] * 255);
            float   height = -10000 + ((r * 256 * 256 + g * 256 + b) * 0.1f);

            return(height);
        }
Exemplo n.º 2
0
        public void PlantTrees(MAstroBody planet, MTerrainTile tile)
        {
            //DistanceThreshold = tile.DistanceThreshold;
            Tile = tile;
            if (tile.material == null)
            {
                return;
            }
            MTexture tex = Tile.Biome;

            if (tex == null)
            {
                return;
            }

            this.transform.Position = tile.transform.Position;

            Random ran = new Random(1234);

            Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation());

            int i = 0;

            for (int z = 0; z < tile.z_res - 1; z++)
            {
                for (int x = 0; x < tile.x_res - 1; x++)
                {
                    float[] c = tex.GetPixel(x, z);
                    float   r = c[0];
                    float   g = c[1];
                    float   b = c[2];
                    float   a = c[3];

                    float t = 0;
                    //if ((b > r) && (b > g)) t = g;
                    if ((r < 0.05) && (g > 0.1) && (b < 0.05))
                    {
                        t = g;
                    }
                    else
                    {
                        continue;
                    }

                    if (ran.NextDouble() > Settings.TreeDensity)
                    {
                        continue;
                    }

                    //if (g < 0.7) continue;
                    //Console.WriteLine(c[0] + " " + c[1] + " " + c[2] + " " + c[3]);
                    if (i >= Settings.MaxTreesPerTerrain)
                    {
                        break;
                    }
                    Vector3d Treepos = new Vector3d(x,
                                                    0,
                                                    z);
                    //Vector3d PlantingPos = planet.GetNearestPointOnSphere(Treepos, 0);
                    Matrix4d TreeScale    = Matrix4d.Scale(1 + ran.NextDouble(), 1 + ran.NextDouble() * 2, 1 + ran.NextDouble());
                    Vector3d PlantingPos  = Tile.GetPointOnSurfaceFromGrid(Treepos); //; + new Vector3d(r.NextDouble()*5, r.NextDouble() * 5, r.NextDouble()*5);
                    Matrix4d TreePosition = Matrix4d.CreateTranslation(PlantingPos);
                    //find point at y with raycast
                    Matrix4 final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition);
                    mats[i] = final;
                    i++;
                }
            }

            TotalInstances = i;

            for (int j = TotalInstances; j < Settings.MaxTreesPerTerrain; j++)
            {
                Matrix4 final = Matrix4.CreateTranslation(j, 0, 0);
                mats[j] = final;
            }

            //Setup();
            //UploadBuffer();
            Planted = false;
        }