示例#1
0
        private List <Vector3> GenerateTreePositions(Texture2D treeMap, VertexMultiTextured[] terrainVertices)
        {
            Color[] treeMapColors = new Color[treeMap.Width * treeMap.Height];
            treeMap.GetData(treeMapColors);

            int[ , ] noiseData = new int[treeMap.Width, treeMap.Height];
            for (int x = 0; x < treeMap.Width; x++)
            {
                for (int y = 0; y < treeMap.Height; y++)
                {
                    noiseData[x, y] = treeMapColors[y + x * treeMap.Height].R;
                }
            }


            List <Vector3> treeList = new List <Vector3>();
            Random         random   = new Random();

            for (int x = 0; x < mTerrain.Width; x++)
            {
                for (int y = 0; y < mTerrain.Length; y++)
                {
                    float terrainHeight = mTerrain.HeightAt(x, y);
                    if ((terrainHeight > 8) && (terrainHeight < 14))
                    {
                        float flatness    = Vector3.Dot(terrainVertices[x + y * mTerrain.Width].Normal, new Vector3(0, 1, 0));
                        float minFlatness = (float)Math.Cos(MathHelper.ToRadians(15));
                        if (flatness > minFlatness)
                        {
                            float relx = (float)x / (float)mTerrain.Width;
                            float rely = (float)y / (float)mTerrain.Length;

                            float noiseValueAtCurrentPosition = noiseData[(int)(relx * treeMap.Width), (int)(rely * treeMap.Height)];
                            float treeDensity;
                            if (noiseValueAtCurrentPosition > 200)
                            {
                                treeDensity = 5;
                            }
                            else if (noiseValueAtCurrentPosition > 150)
                            {
                                treeDensity = 4;
                            }
                            else if (noiseValueAtCurrentPosition > 100)
                            {
                                treeDensity = 3;
                            }
                            else
                            {
                                treeDensity = 0;
                            }

                            for (int currDetail = 0; currDetail < treeDensity; currDetail++)
                            {
                                float   rand1   = (float)random.Next(1000) / 1000.0f;
                                float   rand2   = (float)random.Next(1000) / 1000.0f;
                                Vector3 treePos = new Vector3((float)x - rand1, 0, -(float)y - rand2);
                                treePos.Y = mTerrain.HeightAt(x, y);
                                treeList.Add(treePos);
                            }
                        }
                    }
                }
            }

            return(treeList);
        }