Skip to content

Commit 59fee4c

Browse files
authored
Merge pull request mlavik1#19 from mlavik1/volumeserialisation
mlavik1#18: Serialise volume data
2 parents acc72a4 + c5e6107 commit 59fee4c

File tree

7 files changed

+74
-23
lines changed

7 files changed

+74
-23
lines changed

Assets/Editor/TransferFunctionEditorWindow.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class TransferFunctionEditorWindow : EditorWindow
1313
private int selectedColPointIndex = -1;
1414

1515
private VolumeRenderedObject volRendObject = null;
16+
private Texture2D histTex = null;
1617

1718
[MenuItem("Volume Rendering/1D Transfer Function")]
1819
public static void ShowWindow()
@@ -76,8 +77,11 @@ private void OnGUI()
7677
// TODO:
7778
tf.GenerateTexture();
7879

80+
if(histTex == null)
81+
histTex = HistogramTextureGenerator.GenerateHistogramTexture(volRendObject.dataset);
82+
7983
tfGUIMat.SetTexture("_TFTex", tf.GetTexture());
80-
tfGUIMat.SetTexture("_HistTex", tf.histogramTexture);
84+
tfGUIMat.SetTexture("_HistTex", histTex);
8185
Graphics.DrawTexture(bgRect, tf.GetTexture(), tfGUIMat);
8286

8387
Texture2D tfTexture = tf.GetTexture();

Assets/Scripts/TransferFunction/TransferFunction.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
using UnityEngine;
22
using System.Collections.Generic;
3+
using System;
34

45
namespace UnityVolumeRendering
56
{
6-
public class TransferFunction
7+
[Serializable]
8+
public class TransferFunction : ScriptableObject
79
{
10+
[SerializeField]
811
public List<TFColourControlPoint> colourControlPoints = new List<TFColourControlPoint>();
12+
[SerializeField]
913
public List<TFAlphaControlPoint> alphaControlPoints = new List<TFAlphaControlPoint>();
1014

11-
public Texture2D histogramTexture = null;
12-
1315
private Texture2D texture = null;
14-
Color[] tfCols;
16+
private Color[] tfCols;
1517

1618
private const int TEXTURE_WIDTH = 512;
1719
private const int TEXTURE_HEIGHT = 2;
1820

19-
public TransferFunction()
20-
{
21-
TextureFormat texformat = SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf) ? TextureFormat.RGBAHalf : TextureFormat.RGBAFloat;
22-
texture = new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT, texformat, false);
23-
tfCols = new Color[TEXTURE_WIDTH * TEXTURE_HEIGHT];
24-
}
25-
2621
public void AddControlPoint(TFColourControlPoint ctrlPoint)
2722
{
2823
colourControlPoints.Add(ctrlPoint);
@@ -43,6 +38,9 @@ public Texture2D GetTexture()
4338

4439
public void GenerateTexture()
4540
{
41+
if (texture == null)
42+
CreateTexture();
43+
4644
List<TFColourControlPoint> cols = new List<TFColourControlPoint>(colourControlPoints);
4745
List<TFAlphaControlPoint> alphas = new List<TFAlphaControlPoint>(alphaControlPoints);
4846

@@ -96,5 +94,12 @@ public void GenerateTexture()
9694
texture.SetPixels(tfCols);
9795
texture.Apply();
9896
}
97+
98+
private void CreateTexture()
99+
{
100+
TextureFormat texformat = SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf) ? TextureFormat.RGBAHalf : TextureFormat.RGBAFloat;
101+
texture = new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT, texformat, false);
102+
tfCols = new Color[TEXTURE_WIDTH * TEXTURE_HEIGHT];
103+
}
99104
}
100105
}

Assets/Scripts/TransferFunction/TransferFunction2D.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
45

56
namespace UnityVolumeRendering
67
{
7-
public class TransferFunction2D
8+
[Serializable]
9+
public class TransferFunction2D : ScriptableObject
810
{
911
[System.Serializable]
1012
public struct TF2DBox
@@ -15,19 +17,14 @@ public struct TF2DBox
1517
public Rect rect;
1618
}
1719

20+
[SerializeField]
1821
public List<TF2DBox> boxes = new List<TF2DBox>();
1922

2023
private Texture2D texture = null;
2124

2225
private const int TEXTURE_WIDTH = 512;
2326
private const int TEXTURE_HEIGHT = 512;
2427

25-
public TransferFunction2D()
26-
{
27-
TextureFormat texformat = SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf) ? TextureFormat.RGBAHalf : TextureFormat.RGBAFloat;
28-
texture = new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT, texformat, false);
29-
}
30-
3128
public void AddBox(float x, float y, float width, float height, Color colour, float alpha)
3229
{
3330
TF2DBox box = new TF2DBox();
@@ -42,11 +39,23 @@ public void AddBox(float x, float y, float width, float height, Color colour, fl
4239

4340
public Texture2D GetTexture()
4441
{
42+
if(texture == null)
43+
GenerateTexture();
44+
4545
return texture;
4646
}
4747

48+
private void CreateTexture()
49+
{
50+
TextureFormat texformat = SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf) ? TextureFormat.RGBAHalf : TextureFormat.RGBAFloat;
51+
texture = new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT, texformat, false);
52+
}
53+
4854
public void GenerateTexture()
4955
{
56+
if (texture == null)
57+
CreateTexture();
58+
5059
Color[] cols = new Color[TEXTURE_WIDTH * TEXTURE_HEIGHT];
5160
for (int iX = 0; iX < TEXTURE_WIDTH; iX++)
5261
{

Assets/Scripts/VolumeData/VolumeDataset.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
namespace UnityVolumeRendering
55
{
6-
public class VolumeDataset
6+
[Serializable]
7+
public class VolumeDataset : ScriptableObject
78
{
9+
[SerializeField]
810
public int[] data = null;
11+
[SerializeField]
912
public int dimX, dimY, dimZ;
1013

1114
private int minDataValue = int.MaxValue;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UnityEngine;
2+
3+
namespace UnityVolumeRendering
4+
{
5+
public class MaterialFactory
6+
{
7+
public static Material CreateMaterialDVR(VolumeDataset dataset)
8+
{
9+
Shader shader = Shader.Find("VolumeRendering/DirectVolumeRenderingShader");
10+
Material material = new Material(shader);
11+
12+
const int noiseDimX = 512;
13+
const int noiseDimY = 512;
14+
Texture2D noiseTexture = NoiseTextureGenerator.GenerateNoiseTexture(noiseDimX, noiseDimY);
15+
material.SetTexture("_NoiseTex", noiseTexture);
16+
material.SetTexture("_DataTex", dataset.GetDataTexture());
17+
18+
return material;
19+
}
20+
}
21+
}

Assets/Scripts/VolumeObject/MaterialFactory.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/VolumeObject/VolumeObjectFactory.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public static VolumeRenderedObject CreateObject(VolumeDataset dataset)
3030
Texture2D tfTexture = tf.GetTexture();
3131
volObj.transferFunction = tf;
3232

33-
tf.histogramTexture = HistogramTextureGenerator.GenerateHistogramTexture(dataset);
34-
3533
TransferFunction2D tf2D = TransferFunctionDatabase.CreateTransferFunction2D();
3634
volObj.transferFunction2D = tf2D;
3735

0 commit comments

Comments
 (0)