HeightMapInfoContent.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // HeightMapInfoContent.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework.Content.Pipeline;
  14. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  15. using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
  16. using Microsoft.Xna.Framework;
  17. #endregion
  18. namespace HeightmapCollisionPipeline
  19. {
  20. /// <summary>
  21. /// HeightMapInfoContent contains information about a size and heights of a
  22. /// heightmap. When the game is being built, it is constructed by the
  23. /// TerrainProcessor, and attached to the finished terrain's Tag. When the game is
  24. /// run, it will be read in as a HeightMapInfo.
  25. /// </summary>
  26. public class HeightMapInfoContent
  27. {
  28. /// <summary>
  29. /// This propery is a 2D array of floats, and tells us the height that each
  30. /// position in the heightmap is.
  31. /// </summary>
  32. public float[,] Height
  33. {
  34. get { return height; }
  35. }
  36. float[,] height;
  37. /// <summary>
  38. /// TerrainScale is the distance between each entry in the Height property.
  39. /// For example, if TerrainScale is 30, Height[0,0] and Height[1,0] are 30
  40. /// units apart.
  41. /// </summary>
  42. public float TerrainScale
  43. {
  44. get { return terrainScale; }
  45. }
  46. private float terrainScale;
  47. /// <summary>
  48. /// This constructor will initialize the height array from the values in the
  49. /// bitmap. Each pixel in the bitmap corresponds to one entry in the height
  50. /// array.
  51. /// </summary>
  52. public HeightMapInfoContent(PixelBitmapContent<float> bitmap,
  53. float terrainScale, float terrainBumpiness)
  54. {
  55. this.terrainScale = terrainScale;
  56. height = new float[bitmap.Width, bitmap.Height];
  57. for (int y = 0; y < bitmap.Height; y++)
  58. {
  59. for (int x = 0; x < bitmap.Width; x++)
  60. {
  61. // the pixels will vary from 0 (black) to 1 (white).
  62. // by subtracting 1, our heights vary from -1 to 0, which we then
  63. // multiply by the "bumpiness" to get our final height.
  64. height[x, y] = (bitmap.GetPixel(x, y) - 1) * terrainBumpiness;
  65. }
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// A TypeWriter for HeightMapInfo, which tells the content pipeline how to save the
  71. /// data in HeightMapInfo. This class should match HeightMapInfoReader: whatever the
  72. /// writer writes, the reader should read.
  73. /// </summary>
  74. [ContentTypeWriter]
  75. public class HeightMapInfoWriter : ContentTypeWriter<HeightMapInfoContent>
  76. {
  77. protected override void Write(ContentWriter output, HeightMapInfoContent value)
  78. {
  79. output.Write(value.TerrainScale);
  80. output.Write(value.Height.GetLength(0));
  81. output.Write(value.Height.GetLength(1));
  82. foreach (float height in value.Height)
  83. {
  84. output.Write(height);
  85. }
  86. }
  87. /// <summary>
  88. /// Tells the content pipeline what CLR type the
  89. /// data will be loaded into at runtime.
  90. /// </summary>
  91. public override string GetRuntimeType(TargetPlatform targetPlatform)
  92. {
  93. return "HeightmapCollision.HeightMapInfo, " +
  94. "HeightmapCollision, Version=1.0.0.0, Culture=neutral";
  95. }
  96. /// <summary>
  97. /// Tells the content pipeline what worker type
  98. /// will be used to load the data.
  99. /// </summary>
  100. public override string GetRuntimeReader(TargetPlatform targetPlatform)
  101. {
  102. return "HeightmapCollision.HeightMapInfoReader, " +
  103. "HeightmapCollision, Version=1.0.0.0, Culture=neutral";
  104. }
  105. }
  106. }