AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
VolumeTextureUtils.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
12  public static class VolumeTextureUtils
13  {
14  public static byte[] Color32ArrayToByteArray(Color32[] vals)
15  {
16  var result = new byte[vals.Length * 4];
17  for (var i = 0; i < vals.Length; ++i)
18  {
19  var v = vals[i];
20  var ndx = (i * 4);
21  result[ndx + 0] = v.r;
22  result[ndx + 1] = v.g;
23  result[ndx + 2] = v.b;
24  result[ndx + 3] = v.a;
25  }
26  return result;
27  }
28 
29  public static Color32[] ByteArrayToColor32Array(byte[] data, Int3 volumeSize, Int3 volumeSizePow2)
30  {
31  if (data == null)
32  {
33  throw new NullReferenceException();
34  }
35 
36  var colors = new Color32[volumeSizePow2.sqrMagnitude];
37 
38  Int3 n;
39 
40  for (n.z = 0; n.z < volumeSize.z; ++n.z)
41  {
42  for (n.y = 0; n.y < volumeSize.y; ++n.y)
43  {
44  for (n.x = 0; n.x < volumeSize.x; ++n.x)
45  {
46  var colorIndex = MathExtensions.CubicToLinearIndex(n, volumeSizePow2);
47  var dataOffset = MathExtensions.CubicToLinearIndex(n, volumeSize) * 4;
48 
49  byte r = data[dataOffset];
50  byte g = data[dataOffset + 1];
51  byte b = data[dataOffset + 2];
52  byte a = data[dataOffset + 3];
53 
54  var col = new Color32(r, g, b, a);
55 
56  colors[colorIndex] = Color32Extensions.PremultiplyAlpha(col);
57  }
58  }
59  }
60 
61  return colors;
62  }
63 
64  public static Texture3D BuildTexture(byte[] data, Int3 volumeSize, Int3 volumeSizePow2)
65  {
66  var colorData = VolumeTextureUtils.ByteArrayToColor32Array(data, volumeSize, volumeSizePow2);
67 
68  var tex = new Texture3D(volumeSizePow2.x, volumeSizePow2.y, volumeSizePow2.z, TextureFormat.RGBA4444, false);
69  tex.filterMode = FilterMode.Bilinear;
70  tex.wrapMode = TextureWrapMode.Clamp;
71 
72  tex.SetPixels32(colorData);
73  tex.Apply();
74 
75  return tex;
76  }
77  }
78 }
Extension methods for Unity&#39;s Color32 class
static Color PremultiplyAlpha(Color col)
static byte [] Color32ArrayToByteArray(Color32[] vals)
static Texture3D BuildTexture(byte[] data, Int3 volumeSize, Int3 volumeSizePow2)
Extension methods and helper functions for various math data
static int CubicToLinearIndex(Int3 ndx, Int3 size)
3D integer class - operates similarly to Unity&#39;s Vector3D
Definition: Int3.cs:16
static Color32 [] ByteArrayToColor32Array(byte[] data, Int3 volumeSize, Int3 volumeSizePow2)
Helper functions for dealing with volume data