Merge pull request #15 from mlavik1/tf-saveload

#8: Save/load transfer functions
This commit is contained in:
Matias Lavik 2020-02-26 08:47:31 +01:00 коммит произвёл GitHub
Родитель 00d1aa900e ecb7f6e4c9
Коммит ebda446929
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 160 добавлений и 23 удалений

Просмотреть файл

@ -107,7 +107,7 @@ namespace UnityVolumeRendering
} }
// Add new rectangle // Add new rectangle
if (GUI.Button(new Rect(startX, startY + 100, 150.0f, 40.0f), "Add rectangle")) if (GUI.Button(new Rect(startX, startY + 100, 110.0f, 30.0f), "Add rectangle"))
{ {
tf2d.AddBox(0.1f, 0.1f, 0.8f, 0.8f, Color.white, 0.5f); tf2d.AddBox(0.1f, 0.1f, 0.8f, 0.8f, Color.white, 0.5f);
needsRegenTexture = true; needsRegenTexture = true;
@ -115,19 +115,40 @@ namespace UnityVolumeRendering
// Remove selected shape // Remove selected shape
if (selectedBoxIndex != -1) if (selectedBoxIndex != -1)
{ {
if (GUI.Button(new Rect(startX, startY + 150, 150.0f, 40.0f), "Remove selected shape")) if (GUI.Button(new Rect(startX, startY + 140, 110.0f, 30.0f), "Remove selected shape"))
{ {
tf2d.boxes.RemoveAt(selectedBoxIndex); tf2d.boxes.RemoveAt(selectedBoxIndex);
needsRegenTexture = true; needsRegenTexture = true;
} }
} }
if(GUI.Button(new Rect(startX, startY + 180, 110.0f, 30.0f), "Save"))
{
string filepath = EditorUtility.SaveFilePanel("Save transfer function", "", "default.tf2d", "tf2d");
if(filepath != "")
TransferFunctionDatabase.SaveTransferFunction2D(tf2d, filepath);
}
if(GUI.Button(new Rect(startX, startY + 220, 110.0f, 30.0f), "Load"))
{
string filepath = EditorUtility.OpenFilePanel("Save transfer function", "", "tf2d");
if(filepath != "")
{
TransferFunction2D newTF = TransferFunctionDatabase.LoadTransferFunction2D(filepath);
if(newTF != null)
{
volRendObject.transferFunction2D = tf2d = newTF;
needsRegenTexture = true;
}
}
}
// TODO: regenerate on add/remove/modify (and do it async) // TODO: regenerate on add/remove/modify (and do it async)
if (needsRegenTexture) if (needsRegenTexture)
{ {
tf2d.GenerateTexture(); tf2d.GenerateTexture();
needsRegenTexture = false; needsRegenTexture = false;
} }
// TODO:
volRendObject.GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_TFTex", tf2d.GetTexture()); volRendObject.GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_TFTex", tf2d.GetTexture());
volRendObject.GetComponent<MeshRenderer>().sharedMaterial.EnableKeyword("TF2D_ON"); volRendObject.GetComponent<MeshRenderer>().sharedMaterial.EnableKeyword("TF2D_ON");

Просмотреть файл

@ -64,9 +64,13 @@ namespace UnityVolumeRendering
Event currentEvent = new Event(Event.current); Event currentEvent = new Event(Event.current);
Color oldColour = GUI.color; Color oldColour = GUI.color;
float bgWidth = Mathf.Min(this.position.width - 20.0f, (this.position.height - 50.0f) * 2.0f);
Rect bgRect = new Rect(0.0f, 0.0f, bgWidth, bgWidth * 0.5f);
float contentWidth = Mathf.Min(this.position.width - 20.0f, (this.position.height - 100.0f) * 2.0f);
float contentHeight = contentWidth * 0.5f;
Rect bgRect = new Rect(0.0f, 0.0f, contentWidth, contentHeight);
// TODO:
tf.GenerateTexture(); tf.GenerateTexture();
tfGUIMat.SetTexture("_TFTex", tf.GetTexture()); tfGUIMat.SetTexture("_TFTex", tf.GetTexture());
@ -161,11 +165,29 @@ namespace UnityVolumeRendering
if (selectedColPointIndex != -1) if (selectedColPointIndex != -1)
{ {
TFColourControlPoint colPoint = tf.colourControlPoints[selectedColPointIndex]; TFColourControlPoint colPoint = tf.colourControlPoints[selectedColPointIndex];
colPoint.colourValue = EditorGUI.ColorField(new Rect(bgRect.x, bgRect.y + bgRect.height + 50, 100.0f, 40.0f), colPoint.colourValue); colPoint.colourValue = EditorGUI.ColorField(new Rect(150, bgRect.y + bgRect.height + 50, 100.0f, 40.0f), colPoint.colourValue);
tf.colourControlPoints[selectedColPointIndex] = colPoint; tf.colourControlPoints[selectedColPointIndex] = colPoint;
} }
if(GUI.Button(new Rect(0.0f, bgRect.y + bgRect.height + 50.0f, 70.0f, 30.0f), "Save"))
{
string filepath = EditorUtility.SaveFilePanel("Save transfer function", "", "default.tf", "tf");
if(filepath != "")
TransferFunctionDatabase.SaveTransferFunction(tf, filepath);
}
if(GUI.Button(new Rect(75.0f, bgRect.y + bgRect.height + 50.0f, 70.0f, 30.0f), "Load"))
{
string filepath = EditorUtility.OpenFilePanel("Save transfer function", "", "tf");
if(filepath != "")
{
TransferFunction newTF = TransferFunctionDatabase.LoadTransferFunction(filepath);
if(newTF != null)
volRendObject.transferFunction = tf = newTF;
}
}
GUI.skin.label.wordWrap = false; GUI.skin.label.wordWrap = false;
GUI.Label(new Rect(0.0f, bgRect.y + bgRect.height + 60.0f, 700.0f, 30.0f), "Left click to select and move a control point. Right click to add a control point, and ctrl + right click to delete."); GUI.Label(new Rect(0.0f, bgRect.y + bgRect.height + 85.0f, 700.0f, 30.0f), "Left click to select and move a control point. Right click to add a control point, and ctrl + right click to delete.");
// TEST!!! TODO // TEST!!! TODO
volRendObject.GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_TFTex", tfTexture); volRendObject.GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_TFTex", tfTexture);

Просмотреть файл

@ -2,6 +2,7 @@
namespace UnityVolumeRendering namespace UnityVolumeRendering
{ {
[System.Serializable]
public struct TFColourControlPoint public struct TFColourControlPoint
{ {
public float dataValue; public float dataValue;
@ -14,6 +15,7 @@ namespace UnityVolumeRendering
} }
} }
[System.Serializable]
public struct TFAlphaControlPoint public struct TFAlphaControlPoint
{ {
public float dataValue; public float dataValue;

Просмотреть файл

@ -4,8 +4,9 @@ using UnityEngine;
namespace UnityVolumeRendering namespace UnityVolumeRendering
{ {
public class TransferFunction2D : MonoBehaviour public class TransferFunction2D
{ {
[System.Serializable]
public struct TF2DBox public struct TF2DBox
{ {
public Color colour; public Color colour;

Просмотреть файл

@ -0,0 +1,104 @@
using UnityEngine;
using System.Collections.Generic;
using System.IO;
namespace UnityVolumeRendering
{
public class TransferFunctionDatabase
{
[System.Serializable]
private struct TF1DSerialisationData
{
public int version;
public List<TFColourControlPoint> colourPoints;
public List<TFAlphaControlPoint> alphaPoints;
public const int VERSION_ID = 1;
}
[System.Serializable]
private struct TF2DSerialisationData
{
public int version;
public List<TransferFunction2D.TF2DBox> boxes;
public const int VERSION_ID = 1;
}
public static TransferFunction CreateTransferFunction()
{
TransferFunction tf = new TransferFunction();
tf.AddControlPoint(new TFColourControlPoint(0.0f, new Color(0.11f, 0.14f, 0.13f, 1.0f)));
tf.AddControlPoint(new TFColourControlPoint(0.2415f, new Color(0.469f, 0.354f, 0.223f, 1.0f)));
tf.AddControlPoint(new TFColourControlPoint(0.3253f, new Color(1.0f, 1.0f, 1.0f, 1.0f)));
tf.AddControlPoint(new TFAlphaControlPoint(0.0f, 0.0f));
tf.AddControlPoint(new TFAlphaControlPoint(0.1787f, 0.0f));
tf.AddControlPoint(new TFAlphaControlPoint(0.2f, 0.024f));
tf.AddControlPoint(new TFAlphaControlPoint(0.28f, 0.03f));
tf.AddControlPoint(new TFAlphaControlPoint(0.4f, 0.546f));
tf.AddControlPoint(new TFAlphaControlPoint(0.547f, 0.5266f));
tf.GenerateTexture();
return tf;
}
public static TransferFunction2D CreateTransferFunction2D()
{
TransferFunction2D tf2D = new TransferFunction2D();
tf2D.AddBox(0.05f, 0.1f, 0.8f, 0.7f, Color.white, 0.4f);
return tf2D;
}
public static TransferFunction LoadTransferFunction(string filepath)
{
if(!File.Exists(filepath))
{
Debug.LogError(string.Format("File does not exist: {0}", filepath));
return null;
}
string jsonstring = File.ReadAllText(filepath);
TF1DSerialisationData data = JsonUtility.FromJson<TF1DSerialisationData>(jsonstring);
Debug.Log(jsonstring);
Debug.Log(data.colourPoints.ToString());
Debug.Log(data.alphaPoints.ToString());
TransferFunction tf = new TransferFunction();
tf.colourControlPoints = data.colourPoints;
tf.alphaControlPoints = data.alphaPoints;
return tf;
}
public static TransferFunction2D LoadTransferFunction2D(string filepath)
{
if(!File.Exists(filepath))
{
Debug.LogError(string.Format("File does not exist: {0}", filepath));
return null;
}
string jsonstring = File.ReadAllText(filepath);
TF2DSerialisationData data = JsonUtility.FromJson<TF2DSerialisationData>(jsonstring);
TransferFunction2D tf = new TransferFunction2D();
tf.boxes = data.boxes;
return tf;
}
public static void SaveTransferFunction(TransferFunction tf, string filepath)
{
TF1DSerialisationData data = new TF1DSerialisationData();
data.version = TF1DSerialisationData.VERSION_ID;
data.colourPoints = new List<TFColourControlPoint>(tf.colourControlPoints);
data.alphaPoints = new List<TFAlphaControlPoint>(tf.alphaControlPoints);
string jsonstring = JsonUtility.ToJson(data);
File.WriteAllText(filepath, jsonstring);
}
public static void SaveTransferFunction2D(TransferFunction2D tf2d, string filepath)
{
TF2DSerialisationData data = new TF2DSerialisationData();
data.version = TF2DSerialisationData.VERSION_ID;
data.boxes = new List<TransferFunction2D.TF2DBox>(tf2d.boxes);
string jsonstring = JsonUtility.ToJson(data);
File.WriteAllText(filepath, jsonstring);
}
}
}

Просмотреть файл

@ -53,26 +53,13 @@ namespace UnityVolumeRendering
const int noiseDimY = 512; const int noiseDimY = 512;
Texture2D noiseTexture = NoiseTextureGenerator.GenerateNoiseTexture(noiseDimX, noiseDimY); Texture2D noiseTexture = NoiseTextureGenerator.GenerateNoiseTexture(noiseDimX, noiseDimY);
TransferFunction tf = new TransferFunction(); TransferFunction tf = TransferFunctionDatabase.CreateTransferFunction();
tf.AddControlPoint(new TFColourControlPoint(0.0f, new Color(0.11f, 0.14f, 0.13f, 1.0f)));
tf.AddControlPoint(new TFColourControlPoint(0.2415f, new Color(0.469f, 0.354f, 0.223f, 1.0f)));
tf.AddControlPoint(new TFColourControlPoint(0.3253f, new Color(1.0f, 1.0f, 1.0f, 1.0f)));
tf.AddControlPoint(new TFAlphaControlPoint(0.0f, 0.0f));
tf.AddControlPoint(new TFAlphaControlPoint(0.1787f, 0.0f));
tf.AddControlPoint(new TFAlphaControlPoint(0.2f, 0.024f));
tf.AddControlPoint(new TFAlphaControlPoint(0.28f, 0.03f));
tf.AddControlPoint(new TFAlphaControlPoint(0.4f, 0.546f));
tf.AddControlPoint(new TFAlphaControlPoint(0.547f, 0.5266f));
tf.GenerateTexture();
Texture2D tfTexture = tf.GetTexture(); Texture2D tfTexture = tf.GetTexture();
volObj.transferFunction = tf; volObj.transferFunction = tf;
tf.histogramTexture = HistogramTextureGenerator.GenerateHistogramTexture(dataset); tf.histogramTexture = HistogramTextureGenerator.GenerateHistogramTexture(dataset);
TransferFunction2D tf2D = new TransferFunction2D(); TransferFunction2D tf2D = TransferFunctionDatabase.CreateTransferFunction2D();
tf2D.AddBox(0.05f, 0.1f, 0.8f, 0.7f, Color.white, 0.4f);
volObj.transferFunction2D = tf2D; volObj.transferFunction2D = tf2D;
meshRenderer.sharedMaterial.SetTexture("_DataTex", tex); meshRenderer.sharedMaterial.SetTexture("_DataTex", tex);