Пример #1
0
 public Terrain(SerializationInfo info, StreamingContext context)
 {
     Actor            = (Actor)info.GetValue("Actor", typeof(Actor));
     Size             = (int)info.GetValue("Size", typeof(int));
     mGrass           = (Grass)info.GetValue("mGrass", typeof(Grass));
     TerrainMaxHeight = (float)info.GetValue("TerrainMaxHeight", typeof(float));
     mHeight          = (int)info.GetValue("mHeight", typeof(int));
     mWidth           = (int)info.GetValue("mWidth", typeof(int));
     mHeightMapPath   = (string)info.GetValue("mHeightMapPath", typeof(string));
     mTexturePath     = (string)info.GetValue("mTexturePath", typeof(string));
     mContent         = (ContentManager)info.GetValue("mContent", typeof(ContentManager));
     mGraphics        = (GraphicsDevice)info.GetValue("mGraphics", typeof(GraphicsDevice));
 }
Пример #2
0
        public Terrain(ContentManager content, GraphicsDevice device, string heightMapPath, string texturePath)
        {
            mContent  = content;
            mGraphics = device;

            mHeightMapPath = heightMapPath;
            mTexturePath   = texturePath;

            var meshData = new MeshData();

            var heightMapStream = new FileStream(heightMapPath, FileMode.Open, FileAccess.Read);
            var heightmap       = Texture2D.FromStream(device, heightMapStream);

            heightMapStream.Close();

            mWidth  = heightmap.Width;
            mHeight = heightmap.Height;

            meshData.mVertices = new VertexPositionTexture[mWidth * mHeight];
            meshData.mIndices  = new int[(mWidth * 2 + 2) * (mHeight - 1)];

            var subData  = new MeshData.SubData();
            var material = new Material();

            var textureStream = new FileStream(texturePath, FileMode.Open, FileAccess.Read);

            material.mDiffuseMap      = Texture2D.FromStream(device, textureStream);
            material.DiffuseMapStream = textureStream;
            material.mGraphicsDevice  = device;
            textureStream.Close();

            material.mDiffuseColor = new Vector3(1.0f);

            subData.mIndicesOffset = 0;
            subData.mMaterialIndex = 0;
            subData.mNumPrimitives = (heightmap.Width * 2 + 2) * (heightmap.Height - 1);

            meshData.mMaterials.Add(material);
            meshData.mSubDatas.Add(subData);

            meshData.mPrimitiveType      = PrimitiveType.TriangleStrip;
            meshData.mTotalNumPrimitives = (heightmap.Width * 2 + 2) * (heightmap.Height - 1);

            meshData.mBoundingSphere.Radius = (float)Math.Sqrt(Math.Pow(heightmap.Width / 2.0f, 2) + Math.Pow(heightmap.Height / 2.0f, 2));
            meshData.mIsTerrain             = true;

            Actor = new Actor(new Mesh.Mesh(mGraphics, meshData), null)
            {
                mCastShadow = false
            };
            Size = mWidth;

            LoadVertices(heightmap);

            LoadIndices();

            Actor.mMesh.UpdateData();

            mGrass = new Grass(this);
            mGrass.Generate();
        }
Пример #3
0
 public Terrain()
 {
     mGrass = new Grass(this);
 }