|
1 |
| -using UnityEngine; |
| 1 | +using System.Linq; |
| 2 | +using UnityEngine; |
2 | 3 |
|
3 | 4 | namespace UnityVolumeRendering
|
4 | 5 | {
|
@@ -41,6 +42,58 @@ public static Texture2D GenerateHistogramTexture(VolumeDataset dataset)
|
41 | 42 | cols[iSample] = new Color(Mathf.Log10((float)values[iSample]) / Mathf.Log10((float)maxFreq), 0.0f, 0.0f, 1.0f);
|
42 | 43 |
|
43 | 44 | texture.SetPixels(cols);
|
| 45 | + //texture.filterMode = FilterMode.Point; |
| 46 | + texture.Apply(); |
| 47 | + |
| 48 | + return texture; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Generates a histogram (but computaion is done on GPU) where: |
| 54 | + /// X-axis = the data sample (density) value |
| 55 | + /// Y-axis = the sample count (number of data samples with the specified density) |
| 56 | + /// </summary> |
| 57 | + /// <param name="dataset"></param> |
| 58 | + /// <returns></returns> |
| 59 | + public static Texture2D GenerateHistogramTextureOnGPU(VolumeDataset dataset) |
| 60 | + { |
| 61 | + ComputeShader computeHistogram = Resources.Load("ComputeHistogram") as ComputeShader; |
| 62 | + int handleInitialize = computeHistogram.FindKernel("HistogramInitialize"); |
| 63 | + int handleMain = computeHistogram.FindKernel("HistogramMain"); |
| 64 | + |
| 65 | + ComputeBuffer histogramBuffer = new ComputeBuffer(256, sizeof(uint) * 1); |
| 66 | + uint[] histogramData = new uint[256]; |
| 67 | + Color32 [] histogramCols = new Color32[256]; |
| 68 | + |
| 69 | + Texture3D dataTexture = dataset.GetDataTexture(); |
| 70 | + |
| 71 | + if (handleInitialize < 0 || handleMain < 0) |
| 72 | + { |
| 73 | + Debug.LogError("Histogram compute shader initialization failed."); |
| 74 | + } |
| 75 | + |
| 76 | + computeHistogram.SetTexture(handleMain, "VolumeTexture", dataTexture); |
| 77 | + computeHistogram.SetBuffer(handleMain, "HistogramBuffer", histogramBuffer); |
| 78 | + computeHistogram.SetBuffer(handleInitialize, "HistogramBuffer", histogramBuffer); |
| 79 | + |
| 80 | + computeHistogram.Dispatch(handleInitialize, 256 / 8, 1, 1); |
| 81 | + computeHistogram.Dispatch(handleMain, (dataTexture.width + 7) / 8, (dataTexture.height + 7) / 8, (dataTexture.depth + 7) / 8); |
| 82 | + |
| 83 | + histogramBuffer.GetData(histogramData); |
| 84 | + |
| 85 | + int maxValue = (int)histogramData.Max(); |
| 86 | + |
| 87 | + Texture2D texture = new Texture2D(256, 1, TextureFormat.RGBA32, false); |
| 88 | + for (int iSample = 0; iSample < 256; iSample++) |
| 89 | + { |
| 90 | + histogramCols[iSample] = new Color(Mathf.Log10((float)histogramData[iSample]) / Mathf.Log10((float)maxValue), 0.0f, 0.0f, 1.0f); |
| 91 | + //if (histogramData[iSample] == 0) |
| 92 | + // Debug.Log (iSample); |
| 93 | + } |
| 94 | + |
| 95 | + texture.SetPixels32(histogramCols); |
| 96 | + //texture.filterMode = FilterMode.Point; |
44 | 97 | texture.Apply();
|
45 | 98 |
|
46 | 99 | return texture;
|
|
0 commit comments