Ejemplo n.º 1
0
        /// <summary>
        /// Generates the zone renderable.
        /// </summary>
        /// <param name="faces">The grid faces of the renderable.</param>
        public void GenerateRenderable(HashSet <GridFace> faces)
        {
            Game game = Unit.Engine3D.Source as Game;

            Renderable.ListBuilder builder = new Renderable.ListBuilder();
            int n = faces.Count * 3;

            builder.Prepare(n);
            foreach (GridFace face in faces)
            {
                List <Vector3> vertices = new List <Vector3>();
                foreach (GridVertex vert in face.Corners())
                {
                    vertices.Add(vert.ToCartesianCoords3D(game.Terrain.HeightMap) + new Vector3(0, 0, 0.1f));
                    builder.AddEmptyBoneInfo();
                }
                builder.Vertices.AddRange(vertices);
                Vector3[] vArray = vertices.ToArray();
                Vector3   normal = Vector3.Cross(vArray[2] - vArray[0], vArray[1] - vArray[0]);
                normal.Normalize();
                builder.Normals.Add(normal);
                builder.Normals.Add(normal);
                builder.Normals.Add(normal);
                builder.Colors.Add(new Vector4(1, 0, 0, 0.4f));
                builder.Colors.Add(new Vector4(1, 0, 0, 0.4f));
                builder.Colors.Add(new Vector4(1, 0, 0, 0.4f));
                if (face.PointsUp())
                {
                    builder.TexCoords.Add(new Vector3(0, 0, 0));
                    builder.TexCoords.Add(new Vector3(0.5f, 1, 0));
                    builder.TexCoords.Add(new Vector3(1, 0, 0));
                }
                else
                {
                    builder.TexCoords.Add(new Vector3(1, 1, 0));
                    builder.TexCoords.Add(new Vector3(0.5f, 0, 0));
                    builder.TexCoords.Add(new Vector3(0, 1, 0));
                }
                uint count = (uint)builder.Normals.Count;
                builder.Indices.Add(count - 3);
                builder.Indices.Add(count - 2);
                builder.Indices.Add(count - 1);
            }
            Renderable rend = builder.Generate();

            Prop = new BasicMeshRenderableProperty()
            {
                DiffuseTexture = game.Client.Textures.White,
                Rend           = rend
            };
            game.UnitController.Entity.AddProperty(Prop);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Fired when entity is spawned.
        /// </summary>
        public override void OnSpawn()
        {
            Game game = Engine3D.Source as Game;
            // Generate the layer seeds, height map and materials
            int s2 = Size * 2;

            Seeds = new Vector2[Layers];
            for (int i = 0; i < Layers; i++)
            {
                Seeds[i] = new Vector2((float)game.Random.NextDouble(), (float)game.Random.NextDouble());
            }
            GenerateHeightMap(2.0f);
            ApplyClampedHeightMap(8.0f, 0.025f, 0.0f, 0.75f, Seeds[0]);
            ApplyHeightMap(0.5f, 0.25f, 0.5f, Seeds[1]);
            ApplyHighHeightMap(2.0f, 0.15f, 0.5f, 7.0f, Seeds[2]);
            GenerateMaterialLayer(GridMaterial.Grass);
            ApplyHighMaterialLayer(GridMaterial.Dirt, 0.05f, Seeds[3], 7.0f, 3.0f);
            ApplyHighMaterialLayer(GridMaterial.Rock, 0.025f, Seeds[4], 9.0f, 1.0f);
            ApplyLowMaterialLayer(GridMaterial.Water, 0.1f, Seeds[5], 3.0f, 0.5f);
            // Generate the terrain grid mesh: Vertices, Normals and Indices
            Builder = new Renderable.ListBuilder();
            int n = s2 * Size * 3;

            Builder.Prepare(n);
            for (int i = 1; i < s2; i++)
            {
                for (int j = 0; j < Size; j++)
                {
                    GridFace       face     = new GridFace(i, j);
                    List <Vector3> vertices = new List <Vector3>();
                    foreach (GridVertex vert in face.Corners())
                    {
                        vertices.Add(vert.ToCartesianCoords3D(HeightMap));
                        Builder.AddEmptyBoneInfo();
                    }
                    Builder.Vertices.AddRange(vertices);
                    Vector3[] vArray = vertices.ToArray();
                    Vector3   normal = Vector3.Cross(vArray[2] - vArray[0], vArray[1] - vArray[0]);
                    normal.Normalize();
                    Builder.Normals.Add(normal);
                    Builder.Normals.Add(normal);
                    Builder.Normals.Add(normal);
                    Vector4 c = Materials[i, j].Color.ToOpenTK();
                    Builder.Colors.Add(c);
                    Builder.Colors.Add(c);
                    Builder.Colors.Add(c);
                    if (face.PointsUp())
                    {
                        Builder.TexCoords.Add(new Vector3(0, 0, 0));
                        Builder.TexCoords.Add(new Vector3(0.5f, 1, 0));
                        Builder.TexCoords.Add(new Vector3(1, 0, 0));
                    }
                    else
                    {
                        Builder.TexCoords.Add(new Vector3(1, 1, 0));
                        Builder.TexCoords.Add(new Vector3(0.5f, 0, 0));
                        Builder.TexCoords.Add(new Vector3(0, 1, 0));
                    }
                }
            }
            int count = Builder.Vertices.Count;

            for (uint k = 0; k < count; k++)
            {
                Builder.Indices.Add(k);
            }
            Rend = Builder.Generate();
            BEPUutilities.Vector3    scaling     = new BEPUutilities.Vector3(0.5, 1, -0.866);
            BEPUutilities.Quaternion rotation    = BEPUutilities.Quaternion.CreateFromAxisAngle(BEPUutilities.Vector3.UnitX, MathHelper.PiOver2);
            BEPUutilities.Vector3    translation = BEPUutilities.Vector3.Zero;
            Body = new Terrain(new TerrainShape(HeightMap), new BEPUutilities.AffineTransform(ref scaling, ref rotation, ref translation))
            {
                ImproveBoundaryBehavior = true
            };
        }