Advertisement
Guest User

Greyscale texture reading with Xenko

a guest
Jan 7th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. //context - GraphicsContext
  2. //texture - Height map texture with disabled mipmaps, streaming and imported as greyscale!!!
  3. var image = texture.GetDataAsImage(context.CommandList);
  4. var heightData = GetHeightDataFromTexture(image);
  5.  //Method makes greyscale reading from Xenko image
  6.  private byte[,] GetHeightDataFromTexture(Image image)
  7.  {
  8.             var box = image.ToDataBox().First();
  9.             var dataArray = new int[box.SlicePitch / 4];
  10.             Marshal.Copy(box.DataPointer, dataArray, 0, dataArray.Length);
  11.             var imageWidth = box.RowPitch / 4;
  12.             var imageHeight = box.SlicePitch / box.RowPitch;
  13.             var heightData = new byte[imageWidth, imageHeight];
  14.             for (int y = 0; y < imageHeight; y++)
  15.             {
  16.                 for (int x = 0; x < imageWidth; x++)
  17.                 {
  18.                     heightData[x, y] = (byte) (dataArray[y * imageWidth + x] & 0xFF);
  19.                 }
  20.             }
  21.             return heightData;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement