|
1 | | -using UnityEngine; |
2 | | -using System.Collections.Generic; |
3 | | -using System; |
4 | | - |
5 | | -namespace UnityVolumeRendering |
6 | | -{ |
7 | | - [Serializable] |
8 | | - public class TransferFunction : ScriptableObject |
9 | | - { |
10 | | - [SerializeField] |
11 | | - public List<TFColourControlPoint> colourControlPoints = new List<TFColourControlPoint>(); |
12 | | - [SerializeField] |
13 | | - public List<TFAlphaControlPoint> alphaControlPoints = new List<TFAlphaControlPoint>(); |
14 | | - |
15 | | - protected Texture2D texture = null; |
16 | | - private Color[] tfCols; |
17 | | - |
18 | | - private const int TEXTURE_WIDTH = 1024; |
19 | | - private const int TEXTURE_HEIGHT = 2; |
20 | | - |
21 | | - public void AddControlPoint(TFColourControlPoint ctrlPoint) |
22 | | - { |
23 | | - colourControlPoints.Add(ctrlPoint); |
24 | | - } |
25 | | - |
26 | | - public void AddControlPoint(TFAlphaControlPoint ctrlPoint) |
27 | | - { |
28 | | - alphaControlPoints.Add(ctrlPoint); |
29 | | - } |
30 | | - |
31 | | - public Texture2D GetTexture() |
32 | | - { |
33 | | - if (texture == null) |
34 | | - GenerateTexture(); |
35 | | - |
36 | | - return texture; |
37 | | - } |
38 | | - |
39 | | - public void GenerateTexture() |
40 | | - { |
41 | | - if (texture == null) |
42 | | - CreateTexture(); |
43 | | - |
44 | | - List<TFColourControlPoint> cols = new List<TFColourControlPoint>(colourControlPoints); |
45 | | - List<TFAlphaControlPoint> alphas = new List<TFAlphaControlPoint>(alphaControlPoints); |
46 | | - |
47 | | - // Sort lists of control points |
48 | | - cols.Sort((a, b) => (a.dataValue.CompareTo(b.dataValue))); |
49 | | - alphas.Sort((a, b) => (a.dataValue.CompareTo(b.dataValue))); |
50 | | - |
51 | | - // Add colour points at beginning and end |
52 | | - if (cols.Count == 0 || cols[cols.Count - 1].dataValue < 1.0f) |
53 | | - cols.Add(new TFColourControlPoint(1.0f, Color.white)); |
54 | | - if (cols[0].dataValue > 0.0f) |
55 | | - cols.Insert(0, new TFColourControlPoint(0.0f, Color.white)); |
56 | | - |
57 | | - // Add alpha points at beginning and end |
58 | | - if (alphas.Count == 0 || alphas[alphas.Count - 1].dataValue < 1.0f) |
59 | | - alphas.Add(new TFAlphaControlPoint(1.0f, 1.0f)); |
60 | | - if (alphas[0].dataValue > 0.0f) |
61 | | - alphas.Insert(0, new TFAlphaControlPoint(0.0f, 0.0f)); |
62 | | - |
63 | | - int numColours = cols.Count; |
64 | | - int numAlphas = alphas.Count; |
65 | | - int iCurrColour = 0; |
66 | | - int iCurrAlpha = 0; |
67 | | - |
68 | | - for (int iX = 0; iX < TEXTURE_WIDTH; iX++) |
69 | | - { |
70 | | - float t = iX / (float)(TEXTURE_WIDTH - 1); |
71 | | - while (iCurrColour < numColours - 2 && cols[iCurrColour + 1].dataValue < t) |
72 | | - iCurrColour++; |
73 | | - while (iCurrAlpha < numAlphas - 2 && alphas[iCurrAlpha + 1].dataValue < t) |
74 | | - iCurrAlpha++; |
75 | | - |
76 | | - TFColourControlPoint leftCol = cols[iCurrColour]; |
77 | | - TFColourControlPoint rightCol = cols[iCurrColour + 1]; |
78 | | - TFAlphaControlPoint leftAlpha = alphas[iCurrAlpha]; |
79 | | - TFAlphaControlPoint rightAlpha = alphas[iCurrAlpha + 1]; |
80 | | - |
81 | | - float tCol = (Mathf.Clamp(t, leftCol.dataValue, rightCol.dataValue) - leftCol.dataValue) / (rightCol.dataValue - leftCol.dataValue); |
82 | | - float tAlpha = (Mathf.Clamp(t, leftAlpha.dataValue, rightAlpha.dataValue) - leftAlpha.dataValue) / (rightAlpha.dataValue - leftAlpha.dataValue); |
83 | | - |
84 | | - tCol = Mathf.SmoothStep(0.0f, 1.0f, tCol); |
85 | | - tAlpha = Mathf.SmoothStep(0.0f, 1.0f, tAlpha); |
86 | | - |
87 | | - Color pixCol = rightCol.colourValue * tCol + leftCol.colourValue * (1.0f - tCol); |
88 | | - pixCol.a = rightAlpha.alphaValue * tAlpha + leftAlpha.alphaValue * (1.0f - tAlpha); |
89 | | - |
90 | | - for (int iY = 0; iY < TEXTURE_HEIGHT; iY++) |
91 | | - { |
92 | | - tfCols[iX + iY * TEXTURE_WIDTH] = QualitySettings.activeColorSpace == ColorSpace.Linear ? pixCol.linear : pixCol; |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - texture.wrapMode = TextureWrapMode.Clamp; |
97 | | - texture.SetPixels(tfCols); |
98 | | - texture.Apply(); |
99 | | - } |
100 | | - |
101 | | - public Color GetColour(float x) |
102 | | - { |
103 | | - int index = Mathf.RoundToInt(x * TEXTURE_WIDTH); |
104 | | - return tfCols[index]; |
105 | | - } |
106 | | - |
107 | | - private void CreateTexture() |
108 | | - { |
109 | | - TextureFormat texformat = SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf) ? TextureFormat.RGBAHalf : TextureFormat.RGBAFloat; |
110 | | - texture = new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT, texformat, false); |
111 | | - tfCols = new Color[TEXTURE_WIDTH * TEXTURE_HEIGHT]; |
112 | | - } |
113 | | - } |
114 | | -} |
| 1 | +using UnityEngine; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace UnityVolumeRendering |
| 6 | +{ |
| 7 | + [Serializable] |
| 8 | + public class TransferFunction : ScriptableObject |
| 9 | + { |
| 10 | + [SerializeField] |
| 11 | + public List<TFColourControlPoint> colourControlPoints = new List<TFColourControlPoint>(); |
| 12 | + [SerializeField] |
| 13 | + public List<TFAlphaControlPoint> alphaControlPoints = new List<TFAlphaControlPoint>(); |
| 14 | + |
| 15 | + protected Texture2D texture = null; |
| 16 | + private Color[] tfCols; |
| 17 | + |
| 18 | + private const int TEXTURE_WIDTH = 1024; |
| 19 | + private const int TEXTURE_HEIGHT = 2; |
| 20 | + |
| 21 | + public void AddControlPoint(TFColourControlPoint ctrlPoint) |
| 22 | + { |
| 23 | + colourControlPoints.Add(ctrlPoint); |
| 24 | + } |
| 25 | + |
| 26 | + public void AddControlPoint(TFAlphaControlPoint ctrlPoint) |
| 27 | + { |
| 28 | + alphaControlPoints.Add(ctrlPoint); |
| 29 | + } |
| 30 | + |
| 31 | + public Texture2D GetTexture() |
| 32 | + { |
| 33 | + if (texture == null) |
| 34 | + GenerateTexture(); |
| 35 | + |
| 36 | + return texture; |
| 37 | + } |
| 38 | + |
| 39 | + public void GenerateTexture() |
| 40 | + { |
| 41 | + if (texture == null) |
| 42 | + CreateTexture(); |
| 43 | + |
| 44 | + List<TFColourControlPoint> cols = new List<TFColourControlPoint>(colourControlPoints); |
| 45 | + List<TFAlphaControlPoint> alphas = new List<TFAlphaControlPoint>(alphaControlPoints); |
| 46 | + |
| 47 | + // Sort lists of control points |
| 48 | + cols.Sort((a, b) => (a.dataValue.CompareTo(b.dataValue))); |
| 49 | + alphas.Sort((a, b) => (a.dataValue.CompareTo(b.dataValue))); |
| 50 | + |
| 51 | + // Add colour points at beginning and end |
| 52 | + if (cols.Count == 0 || cols[cols.Count - 1].dataValue < 1.0f) |
| 53 | + cols.Add(new TFColourControlPoint(1.0f, Color.white)); |
| 54 | + if (cols[0].dataValue > 0.0f) |
| 55 | + cols.Insert(0, new TFColourControlPoint(0.0f, Color.white)); |
| 56 | + |
| 57 | + // Add alpha points at beginning and end |
| 58 | + if (alphas.Count == 0 || alphas[alphas.Count - 1].dataValue < 1.0f) |
| 59 | + alphas.Add(new TFAlphaControlPoint(1.0f, 1.0f)); |
| 60 | + if (alphas[0].dataValue > 0.0f) |
| 61 | + alphas.Insert(0, new TFAlphaControlPoint(0.0f, 0.0f)); |
| 62 | + |
| 63 | + int numColours = cols.Count; |
| 64 | + int numAlphas = alphas.Count; |
| 65 | + int iCurrColour = 0; |
| 66 | + int iCurrAlpha = 0; |
| 67 | + |
| 68 | + for (int iX = 0; iX < TEXTURE_WIDTH; iX++) |
| 69 | + { |
| 70 | + float t = iX / (float)(TEXTURE_WIDTH - 1); |
| 71 | + while (iCurrColour < numColours - 2 && cols[iCurrColour + 1].dataValue < t) |
| 72 | + iCurrColour++; |
| 73 | + while (iCurrAlpha < numAlphas - 2 && alphas[iCurrAlpha + 1].dataValue < t) |
| 74 | + iCurrAlpha++; |
| 75 | + |
| 76 | + TFColourControlPoint leftCol = cols[iCurrColour]; |
| 77 | + TFColourControlPoint rightCol = cols[iCurrColour + 1]; |
| 78 | + TFAlphaControlPoint leftAlpha = alphas[iCurrAlpha]; |
| 79 | + TFAlphaControlPoint rightAlpha = alphas[iCurrAlpha + 1]; |
| 80 | + |
| 81 | + float tCol = (Mathf.Clamp(t, leftCol.dataValue, rightCol.dataValue) - leftCol.dataValue) / (rightCol.dataValue - leftCol.dataValue); |
| 82 | + float tAlpha = (Mathf.Clamp(t, leftAlpha.dataValue, rightAlpha.dataValue) - leftAlpha.dataValue) / (rightAlpha.dataValue - leftAlpha.dataValue); |
| 83 | + |
| 84 | + tCol = Mathf.SmoothStep(0.0f, 1.0f, tCol); |
| 85 | + tAlpha = Mathf.SmoothStep(0.0f, 1.0f, tAlpha); |
| 86 | + |
| 87 | + Color pixCol = rightCol.colourValue * tCol + leftCol.colourValue * (1.0f - tCol); |
| 88 | + pixCol.a = rightAlpha.alphaValue * tAlpha + leftAlpha.alphaValue * (1.0f - tAlpha); |
| 89 | + |
| 90 | + for (int iY = 0; iY < TEXTURE_HEIGHT; iY++) |
| 91 | + { |
| 92 | + tfCols[iX + iY * TEXTURE_WIDTH] = QualitySettings.activeColorSpace == ColorSpace.Linear ? pixCol.linear : pixCol; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + texture.wrapMode = TextureWrapMode.Clamp; |
| 97 | + texture.SetPixels(tfCols); |
| 98 | + texture.Apply(); |
| 99 | + } |
| 100 | + |
| 101 | + public Color GetColour(float x) |
| 102 | + { |
| 103 | + int index = Mathf.RoundToInt(x * TEXTURE_WIDTH); |
| 104 | + return tfCols[index]; |
| 105 | + } |
| 106 | + |
| 107 | + private void CreateTexture() |
| 108 | + { |
| 109 | + TextureFormat texformat = SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf) ? TextureFormat.RGBAHalf : TextureFormat.RGBAFloat; |
| 110 | + texture = new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT, texformat, false); |
| 111 | + tfCols = new Color[TEXTURE_WIDTH * TEXTURE_HEIGHT]; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments