Texture2D.GetData

Strange Texture2D.GetData works.
Wrong size: 21600 vs 43200


In XNA 4 everything worked.

Very little info code shown in a picture this is at best hard to see no mention of what platform you are on.
In the picture were half most of your code is blocked from vision.

With no actual question in words.

Ill give you the benifit of the doubt and believe you are actually asking for help instead of just implying that get data is broken on a random ? platform.

You have heightmapcolors = new Color[width * height

Just above that you are loading a heightMap texture in the parameter.
It seems to me the first red flag here is that you are not calling.

heightmapcolors = new Color[heightMap.Width * heightMap.Height]

My bet is on the guess that you have messed up those values for width and height that you are using elsewere and they don’t match what get data see’s.

Your texture is in Dxt1 format, which is a compressed format. You’re expecting it to be 4 bytes per pixel which it is not.

Here all code of the construktor. Everything is checked. Monogame 3.7, Win Decktop.

`public Terrain(Material material, Texture2D _heightMap, float _cellSizeX, float _cellSizeZ, float _maxHeight, Vector3 _offsetPosition)
{
this.device = Commons.device;
this.material = material;
this.heightMap = _heightMap;
this.width = heightMap.Width;
this.height = heightMap.Height;
this.cellSizeX = _cellSizeX;
this.cellSizeZ = _cellSizeZ;
this.cellSizeRatio = (cellSizeX * width) / (cellSizeZ * height);
this.offsetPosition = _offsetPosition;
this.maxHeight = _maxHeight;

        nVertices = width * height;
        nIndices = width * 2 * (height - 1);
        LoadHeightData(heightMap);
        VertexPositionNormalTexture[] vertices = CreateTerrainVertices();
        int[] indices = CreateTerrainIndices();
        vertices = GenerateNormals(vertices, indices);
        CreateBuffers(vertices, indices);
    }`

About Dxt1 it is already more interesting.
How do I need to change the code?

private void LoadHeightData2(Texture2D heightMap) { Color[] heightMapColors = new Color[width * height]; heightMap.GetData<Color>(heightMapColors, 0, heightMapColors.Length); heights = new float[width, height]; for (int y = 0; y < height; y++) {..............

I recommend turning compression off. I also recommend using a custom processor/writer so you can store the color data in RAM when loading the texture and you don’t have to call GetData. If you’re worried about file size you can reduce it by setting the compression flag on your project root in the Pipeline Tool.

1 Like

Thanks. Replaced the .dds file with .png.
After reassembly works without problems.

1 Like