From c89a894536e2e11f2f14e7daab5f3011dd379af1 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 20 Feb 2019 20:53:55 +0100 Subject: [PATCH] - Added an importer to the editor. - Cleaned up the project structure and object hierarchy. --- Assets/Editor/DatasetImporterEditorWIndow.cs | 96 ++++++++++++ .../DatasetImporterEditorWIndow.cs.meta} | 2 +- Assets/Editor/SliceRenderingEditorWindow.cs | 5 +- .../Editor/TransferFunction2DEditorWindow.cs | 9 +- Assets/Editor/TransferFunctionEditorWindow.cs | 7 +- .../VolumeRenderedObjectCustomInspector.cs | 17 +++ ...olumeRenderedObjectCustomInspector.cs.meta | 11 ++ .../Editor/VolumeRendererEditorFunctions.cs | 25 ++++ .../VolumeRendererEditorFunctions.cs.meta | 11 ++ .../DirectVolumeRenderingMaterial.mat | 4 +- Assets/Resources/TransferFunction2DGUIMat.mat | 2 +- Assets/Resources/VolumeRenderedObject.prefab | 8 +- Assets/Scenes/TestScene.unity | 42 ------ Assets/{ => Scripts}/Importing.meta | 0 .../Scripts/Importing/DatasetImporterBase.cs | 4 + .../Importing/DatasetImporterBase.cs.meta | 11 ++ .../Importing/RawDatasetImporter.cs | 4 +- .../Importing/RawDatasetImporter.cs.meta | 0 Assets/{ => Scripts}/TransferFunction.meta | 0 .../TransferFunction/TFControlPoints.cs | 0 .../TransferFunction/TFControlPoints.cs.meta | 0 .../TransferFunction/TransferFunction.cs | 0 .../TransferFunction/TransferFunction.cs.meta | 0 .../TransferFunction/TransferFunction2D.cs | 0 .../TransferFunction2D.cs.meta | 0 Assets/{ => Scripts}/Utils.meta | 0 .../Utils/HistogramTextureGenerator.cs | 0 .../Utils/HistogramTextureGenerator.cs.meta | 0 .../Utils/NoiseTextureGenerator.cs | 0 .../Utils/NoiseTextureGenerator.cs.meta | 0 Assets/{ => Scripts}/VolumeData.meta | 0 .../{ => Scripts}/VolumeData/VolumeDataset.cs | 0 .../VolumeData/VolumeDataset.cs.meta | 0 Assets/Scripts/VolumeObject.meta | 8 + Assets/Scripts/VolumeObject/RenderMode.cs | 6 + .../Scripts/VolumeObject/RenderMode.cs.meta | 11 ++ .../{ => VolumeObject}/SlicingPlane.cs | 1 + .../{ => VolumeObject}/SlicingPlane.cs.meta | 0 .../VolumeObject/VolumeObjectFactory.cs | 84 +++++++++++ .../VolumeObject/VolumeObjectFactory.cs.meta | 11 ++ .../VolumeObject/VolumeRenderedObject.cs | 67 +++++++++ .../VolumeObject/VolumeRenderedObject.cs.meta | 11 ++ Assets/Scripts/VolumeRenderer.cs | 138 ------------------ 43 files changed, 390 insertions(+), 205 deletions(-) create mode 100644 Assets/Editor/DatasetImporterEditorWIndow.cs rename Assets/{Scripts/VolumeRenderer.cs.meta => Editor/DatasetImporterEditorWIndow.cs.meta} (83%) create mode 100644 Assets/Editor/VolumeRenderedObjectCustomInspector.cs create mode 100644 Assets/Editor/VolumeRenderedObjectCustomInspector.cs.meta create mode 100644 Assets/Editor/VolumeRendererEditorFunctions.cs create mode 100644 Assets/Editor/VolumeRendererEditorFunctions.cs.meta rename Assets/{ => Scripts}/Importing.meta (100%) create mode 100644 Assets/Scripts/Importing/DatasetImporterBase.cs create mode 100644 Assets/Scripts/Importing/DatasetImporterBase.cs.meta rename Assets/{ => Scripts}/Importing/RawDatasetImporter.cs (93%) rename Assets/{ => Scripts}/Importing/RawDatasetImporter.cs.meta (100%) rename Assets/{ => Scripts}/TransferFunction.meta (100%) rename Assets/{ => Scripts}/TransferFunction/TFControlPoints.cs (100%) rename Assets/{ => Scripts}/TransferFunction/TFControlPoints.cs.meta (100%) rename Assets/{ => Scripts}/TransferFunction/TransferFunction.cs (100%) rename Assets/{ => Scripts}/TransferFunction/TransferFunction.cs.meta (100%) rename Assets/{ => Scripts}/TransferFunction/TransferFunction2D.cs (100%) rename Assets/{ => Scripts}/TransferFunction/TransferFunction2D.cs.meta (100%) rename Assets/{ => Scripts}/Utils.meta (100%) rename Assets/{ => Scripts}/Utils/HistogramTextureGenerator.cs (100%) rename Assets/{ => Scripts}/Utils/HistogramTextureGenerator.cs.meta (100%) rename Assets/{ => Scripts}/Utils/NoiseTextureGenerator.cs (100%) rename Assets/{ => Scripts}/Utils/NoiseTextureGenerator.cs.meta (100%) rename Assets/{ => Scripts}/VolumeData.meta (100%) rename Assets/{ => Scripts}/VolumeData/VolumeDataset.cs (100%) rename Assets/{ => Scripts}/VolumeData/VolumeDataset.cs.meta (100%) create mode 100644 Assets/Scripts/VolumeObject.meta create mode 100644 Assets/Scripts/VolumeObject/RenderMode.cs create mode 100644 Assets/Scripts/VolumeObject/RenderMode.cs.meta rename Assets/Scripts/{ => VolumeObject}/SlicingPlane.cs (92%) rename Assets/Scripts/{ => VolumeObject}/SlicingPlane.cs.meta (100%) create mode 100644 Assets/Scripts/VolumeObject/VolumeObjectFactory.cs create mode 100644 Assets/Scripts/VolumeObject/VolumeObjectFactory.cs.meta create mode 100644 Assets/Scripts/VolumeObject/VolumeRenderedObject.cs create mode 100644 Assets/Scripts/VolumeObject/VolumeRenderedObject.cs.meta delete mode 100644 Assets/Scripts/VolumeRenderer.cs diff --git a/Assets/Editor/DatasetImporterEditorWIndow.cs b/Assets/Editor/DatasetImporterEditorWIndow.cs new file mode 100644 index 0000000..ef09f28 --- /dev/null +++ b/Assets/Editor/DatasetImporterEditorWIndow.cs @@ -0,0 +1,96 @@ +using UnityEngine; +using UnityEditor; +using System.IO; + +public class DatasetImporterEditorWindow : EditorWindow +{ + private enum DatasetType + { + Unknown, + Raw, + DICOM + } + + private string fileToImport; + private DatasetType datasetType; + + private int dimX; // TODO: set good default value + private int dimY; // TODO: set good default value + private int dimZ; // TODO: set good default value + private int bytesToSkip = 0; + private DataContentFormat dataFormat = DataContentFormat.Int16; + + public DatasetImporterEditorWindow(string fileToImport) + { + this.fileToImport = fileToImport; + string extension = Path.GetExtension(fileToImport); + if (extension == ".dat" || extension == ".raw") + datasetType = DatasetType.Raw; + else + datasetType = DatasetType.Unknown; + + if(extension == ".dat") + { + FileStream fs = new FileStream(fileToImport, FileMode.Open); + BinaryReader reader = new BinaryReader(fs); + + dimX = reader.ReadUInt16(); + dimY = reader.ReadUInt16(); + dimZ = reader.ReadUInt16(); + bytesToSkip = 6; + + reader.Close(); + fs.Close(); + } + } + + private void ImportDataset() + { + DatasetImporterBase importer = null; + switch(datasetType) + { + case DatasetType.Raw: + { + importer = new RawDatasetImporter(fileToImport, dimX, dimY, dimZ, DataContentFormat.Int16, 6); + break; + } + } + + VolumeDataset dataset = null; + if(importer != null) + dataset = importer.Import(); + + if (dataset != null) + { + VolumeRenderedObject obj = VolumeObjectFactory.CreateObject(dataset); + } + else + { + Debug.LogError("Failed to import datset"); + } + + this.Close(); + } + + private void OnGUI() + { + switch(datasetType) + { + case DatasetType.Raw: + { + dimX = EditorGUILayout.IntField("X dimension", dimX); + dimY = EditorGUILayout.IntField("X dimension", dimY); + dimZ = EditorGUILayout.IntField("X dimension", dimZ); + bytesToSkip = EditorGUILayout.IntField("Bytes to skip", bytesToSkip); + dataFormat = (DataContentFormat)EditorGUILayout.EnumPopup("Data format", dataFormat); + break; + } + } + + if(GUILayout.Button("Import")) + ImportDataset(); + + if (GUILayout.Button("Cancel")) + this.Close(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/VolumeRenderer.cs.meta b/Assets/Editor/DatasetImporterEditorWIndow.cs.meta similarity index 83% rename from Assets/Scripts/VolumeRenderer.cs.meta rename to Assets/Editor/DatasetImporterEditorWIndow.cs.meta index d5b0c9b..b563b25 100644 --- a/Assets/Scripts/VolumeRenderer.cs.meta +++ b/Assets/Editor/DatasetImporterEditorWIndow.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a00a6dc9906286a46948a51d4f7c56ab +guid: a6938a09f67b77c4b9e832cf63203399 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Editor/SliceRenderingEditorWindow.cs b/Assets/Editor/SliceRenderingEditorWindow.cs index f614f87..d3e1b78 100644 --- a/Assets/Editor/SliceRenderingEditorWindow.cs +++ b/Assets/Editor/SliceRenderingEditorWindow.cs @@ -14,9 +14,6 @@ public class SliceRenderingEditorWindow : EditorWindow private void OnGUI() { - if (!Application.isPlaying) - return; - SlicingPlane[] spawnedPlanes = FindObjectsOfType(); if(spawnedPlanes.Length > 0) @@ -41,7 +38,7 @@ public class SliceRenderingEditorWindow : EditorWindow } if (GUI.Button(new Rect(240.0f, bgRect.y + bgRect.height + 20.0f, 100.0f, 100.0f), "+")) { - VolumeRenderer volRend = FindObjectOfType(); + VolumeRenderedObject volRend = FindObjectOfType(); if(volRend != null) { selectedPlaneIndex = spawnedPlanes.Length; diff --git a/Assets/Editor/TransferFunction2DEditorWindow.cs b/Assets/Editor/TransferFunction2DEditorWindow.cs index 60c5d9b..46af890 100644 --- a/Assets/Editor/TransferFunction2DEditorWindow.cs +++ b/Assets/Editor/TransferFunction2DEditorWindow.cs @@ -28,17 +28,14 @@ public class TransferFunction2DEditorWindow : EditorWindow private void OnGUI() { - if (!Application.isPlaying) - return; - - VolumeRenderer volRend = FindObjectOfType(); + VolumeRenderedObject volRend = FindObjectOfType(); if (volRend == null) return; if (hist2DTex == null) - hist2DTex = HistogramTextureGenerator.Generate2DHistogramTexture(volRend.volumeDataset); + hist2DTex = HistogramTextureGenerator.Generate2DHistogramTexture(volRend.dataset); - TransferFunction2D tf2d = volRend.tf2D; + TransferFunction2D tf2d = volRend.transferFunction2D; //tf.GenerateTexture(); diff --git a/Assets/Editor/TransferFunctionEditorWindow.cs b/Assets/Editor/TransferFunctionEditorWindow.cs index e7831c3..5ed786b 100644 --- a/Assets/Editor/TransferFunctionEditorWindow.cs +++ b/Assets/Editor/TransferFunctionEditorWindow.cs @@ -32,13 +32,10 @@ public class TransferFunctionEditorWindow : EditorWindow private void OnGUI() { - if (!Application.isPlaying) - return; - - VolumeRenderer volRend = FindObjectOfType(); + VolumeRenderedObject volRend = FindObjectOfType(); if (volRend == null) return; - tf = volRend.tf; + tf = volRend.transferFunction; Color oldColour = GUI.color; float bgWidth = Mathf.Min(this.position.width - 20.0f, (this.position.height - 50.0f) * 2.0f); diff --git a/Assets/Editor/VolumeRenderedObjectCustomInspector.cs b/Assets/Editor/VolumeRenderedObjectCustomInspector.cs new file mode 100644 index 0000000..d781a42 --- /dev/null +++ b/Assets/Editor/VolumeRenderedObjectCustomInspector.cs @@ -0,0 +1,17 @@ +using UnityEngine; +using UnityEditor; + +[CustomEditor(typeof(VolumeRenderedObject))] +public class VolumeRenderedObjectCustomInspector : Editor +{ + public override void OnInspectorGUI() + { + VolumeRenderedObject myTarget = (VolumeRenderedObject)target; + + RenderMode oldRenderMode = myTarget.GetRemderMode(); + RenderMode newRenderMode = (RenderMode)EditorGUILayout.EnumPopup("Render mode", oldRenderMode); + + if (newRenderMode != oldRenderMode) + myTarget.SetRenderMode(newRenderMode); + } +} diff --git a/Assets/Editor/VolumeRenderedObjectCustomInspector.cs.meta b/Assets/Editor/VolumeRenderedObjectCustomInspector.cs.meta new file mode 100644 index 0000000..68993a8 --- /dev/null +++ b/Assets/Editor/VolumeRenderedObjectCustomInspector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: af5b051c5cf4f824c999ee444c9acff6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/VolumeRendererEditorFunctions.cs b/Assets/Editor/VolumeRendererEditorFunctions.cs new file mode 100644 index 0000000..7e4fbff --- /dev/null +++ b/Assets/Editor/VolumeRendererEditorFunctions.cs @@ -0,0 +1,25 @@ +using UnityEditor; +using UnityEngine; +using System.IO; + +public class VolumeRendererEditorFunctions +{ + [MenuItem("Volume Rendering/Load dataset")] + static void ShowWindow() + { + string file = EditorUtility.OpenFilePanel("Select a dataset to load", "DataFiles", "*.*"); + if(File.Exists(file)) + { + DatasetImporterEditorWindow wnd = (DatasetImporterEditorWindow)EditorWindow.GetWindow(typeof(DatasetImporterEditorWindow)); + if (wnd != null) + wnd.Close(); + + wnd = new DatasetImporterEditorWindow(file); + wnd.Show(); + } + else + { + Debug.LogError("File doesn't exist: " + file); + } + } +} diff --git a/Assets/Editor/VolumeRendererEditorFunctions.cs.meta b/Assets/Editor/VolumeRendererEditorFunctions.cs.meta new file mode 100644 index 0000000..8672605 --- /dev/null +++ b/Assets/Editor/VolumeRendererEditorFunctions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3b47c51242b1c10429411c0ce01c5760 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Materials/DirectVolumeRenderingMaterial.mat b/Assets/Materials/DirectVolumeRenderingMaterial.mat index 9d905c3..f70f52a 100644 --- a/Assets/Materials/DirectVolumeRenderingMaterial.mat +++ b/Assets/Materials/DirectVolumeRenderingMaterial.mat @@ -8,7 +8,7 @@ Material: m_PrefabInternal: {fileID: 0} m_Name: DirectVolumeRenderingMaterial m_Shader: {fileID: 4800000, guid: f9df7f8cb5dda3a48a945ab37d6b0d83, type: 3} - m_ShaderKeywords: MODE_DVR TF2D_ON _MODE_DVR _MODE_MIP + m_ShaderKeywords: MODE_SURF _MODE_DVR _MODE_MIP m_LightmapFlags: 4 m_EnableInstancingVariants: 0 m_DoubleSidedGI: 0 @@ -77,7 +77,7 @@ Material: - _GlossyReflections: 1 - _MaxVal: 1 - _Metallic: 0 - - _MinVal: 0 + - _MinVal: 0.1 - _Mode: 0 - _OcclusionStrength: 1 - _Parallax: 0.02 diff --git a/Assets/Resources/TransferFunction2DGUIMat.mat b/Assets/Resources/TransferFunction2DGUIMat.mat index b4fc53e..3ee0d43 100644 --- a/Assets/Resources/TransferFunction2DGUIMat.mat +++ b/Assets/Resources/TransferFunction2DGUIMat.mat @@ -6,7 +6,7 @@ Material: m_ObjectHideFlags: 0 m_PrefabParentObject: {fileID: 0} m_PrefabInternal: {fileID: 0} - m_Name: TransferFunctionPaletteGUIMat + m_Name: TransferFunction2DGUIMat m_Shader: {fileID: 4800000, guid: b0b396ea7426c8b4385f14d5e5a951ed, type: 3} m_ShaderKeywords: m_LightmapFlags: 4 diff --git a/Assets/Resources/VolumeRenderedObject.prefab b/Assets/Resources/VolumeRenderedObject.prefab index 1607e0d..18e98a3 100644 --- a/Assets/Resources/VolumeRenderedObject.prefab +++ b/Assets/Resources/VolumeRenderedObject.prefab @@ -21,7 +21,7 @@ GameObject: - component: {fileID: 4022122026797268} - component: {fileID: 33312357750855204} - component: {fileID: 23086139309309076} - - component: {fileID: 114671375263017396} + - component: {fileID: 114985090201080716} m_Layer: 0 m_Name: VolumeRenderedObject m_TagString: Untagged @@ -84,7 +84,7 @@ MeshFilter: m_PrefabInternal: {fileID: 100100000} m_GameObject: {fileID: 1758459268553486} m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} ---- !u!114 &114671375263017396 +--- !u!114 &114985090201080716 MonoBehaviour: m_ObjectHideFlags: 1 m_PrefabParentObject: {fileID: 0} @@ -92,7 +92,7 @@ MonoBehaviour: m_GameObject: {fileID: 1758459268553486} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: a00a6dc9906286a46948a51d4f7c56ab, type: 3} + m_Script: {fileID: 11500000, guid: acf4a5cf6519d714895a068f9a4e8456, type: 3} m_Name: m_EditorClassIdentifier: - tf2D: {fileID: 0} + transferFunction2D: {fileID: 0} diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index fd26825..3e5b226 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -251,45 +251,3 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 9.994, y: -21.144001, z: 0.49300003} ---- !u!1001 &1540645132 -Prefab: - m_ObjectHideFlags: 0 - serializedVersion: 2 - m_Modification: - m_TransformParent: {fileID: 0} - m_Modifications: - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_LocalPosition.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_LocalPosition.y - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_LocalPosition.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_LocalRotation.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_LocalRotation.y - value: -0.7071068 - objectReference: {fileID: 0} - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_LocalRotation.z - value: -0.7071068 - objectReference: {fileID: 0} - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_LocalRotation.w - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4022122026797268, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - propertyPath: m_RootOrder - value: 2 - objectReference: {fileID: 0} - m_RemovedComponents: [] - m_ParentPrefab: {fileID: 100100000, guid: 06d1ae9362ecddb44844bf342a6fc7f3, type: 2} - m_IsPrefabParent: 0 diff --git a/Assets/Importing.meta b/Assets/Scripts/Importing.meta similarity index 100% rename from Assets/Importing.meta rename to Assets/Scripts/Importing.meta diff --git a/Assets/Scripts/Importing/DatasetImporterBase.cs b/Assets/Scripts/Importing/DatasetImporterBase.cs new file mode 100644 index 0000000..94e0e5f --- /dev/null +++ b/Assets/Scripts/Importing/DatasetImporterBase.cs @@ -0,0 +1,4 @@ +public abstract class DatasetImporterBase +{ + public abstract VolumeDataset Import(); +} diff --git a/Assets/Scripts/Importing/DatasetImporterBase.cs.meta b/Assets/Scripts/Importing/DatasetImporterBase.cs.meta new file mode 100644 index 0000000..11af9cb --- /dev/null +++ b/Assets/Scripts/Importing/DatasetImporterBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 693790aa0dad1104b8eca0e740092fd9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Importing/RawDatasetImporter.cs b/Assets/Scripts/Importing/RawDatasetImporter.cs similarity index 93% rename from Assets/Importing/RawDatasetImporter.cs rename to Assets/Scripts/Importing/RawDatasetImporter.cs index 4057eb5..97c1635 100644 --- a/Assets/Importing/RawDatasetImporter.cs +++ b/Assets/Scripts/Importing/RawDatasetImporter.cs @@ -12,7 +12,7 @@ public enum DataContentFormat Uint32 } -public class RawDatasetImporter +public class RawDatasetImporter : DatasetImporterBase { string filePath; private int dimX; @@ -31,7 +31,7 @@ public class RawDatasetImporter this.skipBytes = skipBytes; } - public VolumeDataset Import() + public override VolumeDataset Import() { VolumeDataset dataset = new VolumeDataset(); diff --git a/Assets/Importing/RawDatasetImporter.cs.meta b/Assets/Scripts/Importing/RawDatasetImporter.cs.meta similarity index 100% rename from Assets/Importing/RawDatasetImporter.cs.meta rename to Assets/Scripts/Importing/RawDatasetImporter.cs.meta diff --git a/Assets/TransferFunction.meta b/Assets/Scripts/TransferFunction.meta similarity index 100% rename from Assets/TransferFunction.meta rename to Assets/Scripts/TransferFunction.meta diff --git a/Assets/TransferFunction/TFControlPoints.cs b/Assets/Scripts/TransferFunction/TFControlPoints.cs similarity index 100% rename from Assets/TransferFunction/TFControlPoints.cs rename to Assets/Scripts/TransferFunction/TFControlPoints.cs diff --git a/Assets/TransferFunction/TFControlPoints.cs.meta b/Assets/Scripts/TransferFunction/TFControlPoints.cs.meta similarity index 100% rename from Assets/TransferFunction/TFControlPoints.cs.meta rename to Assets/Scripts/TransferFunction/TFControlPoints.cs.meta diff --git a/Assets/TransferFunction/TransferFunction.cs b/Assets/Scripts/TransferFunction/TransferFunction.cs similarity index 100% rename from Assets/TransferFunction/TransferFunction.cs rename to Assets/Scripts/TransferFunction/TransferFunction.cs diff --git a/Assets/TransferFunction/TransferFunction.cs.meta b/Assets/Scripts/TransferFunction/TransferFunction.cs.meta similarity index 100% rename from Assets/TransferFunction/TransferFunction.cs.meta rename to Assets/Scripts/TransferFunction/TransferFunction.cs.meta diff --git a/Assets/TransferFunction/TransferFunction2D.cs b/Assets/Scripts/TransferFunction/TransferFunction2D.cs similarity index 100% rename from Assets/TransferFunction/TransferFunction2D.cs rename to Assets/Scripts/TransferFunction/TransferFunction2D.cs diff --git a/Assets/TransferFunction/TransferFunction2D.cs.meta b/Assets/Scripts/TransferFunction/TransferFunction2D.cs.meta similarity index 100% rename from Assets/TransferFunction/TransferFunction2D.cs.meta rename to Assets/Scripts/TransferFunction/TransferFunction2D.cs.meta diff --git a/Assets/Utils.meta b/Assets/Scripts/Utils.meta similarity index 100% rename from Assets/Utils.meta rename to Assets/Scripts/Utils.meta diff --git a/Assets/Utils/HistogramTextureGenerator.cs b/Assets/Scripts/Utils/HistogramTextureGenerator.cs similarity index 100% rename from Assets/Utils/HistogramTextureGenerator.cs rename to Assets/Scripts/Utils/HistogramTextureGenerator.cs diff --git a/Assets/Utils/HistogramTextureGenerator.cs.meta b/Assets/Scripts/Utils/HistogramTextureGenerator.cs.meta similarity index 100% rename from Assets/Utils/HistogramTextureGenerator.cs.meta rename to Assets/Scripts/Utils/HistogramTextureGenerator.cs.meta diff --git a/Assets/Utils/NoiseTextureGenerator.cs b/Assets/Scripts/Utils/NoiseTextureGenerator.cs similarity index 100% rename from Assets/Utils/NoiseTextureGenerator.cs rename to Assets/Scripts/Utils/NoiseTextureGenerator.cs diff --git a/Assets/Utils/NoiseTextureGenerator.cs.meta b/Assets/Scripts/Utils/NoiseTextureGenerator.cs.meta similarity index 100% rename from Assets/Utils/NoiseTextureGenerator.cs.meta rename to Assets/Scripts/Utils/NoiseTextureGenerator.cs.meta diff --git a/Assets/VolumeData.meta b/Assets/Scripts/VolumeData.meta similarity index 100% rename from Assets/VolumeData.meta rename to Assets/Scripts/VolumeData.meta diff --git a/Assets/VolumeData/VolumeDataset.cs b/Assets/Scripts/VolumeData/VolumeDataset.cs similarity index 100% rename from Assets/VolumeData/VolumeDataset.cs rename to Assets/Scripts/VolumeData/VolumeDataset.cs diff --git a/Assets/VolumeData/VolumeDataset.cs.meta b/Assets/Scripts/VolumeData/VolumeDataset.cs.meta similarity index 100% rename from Assets/VolumeData/VolumeDataset.cs.meta rename to Assets/Scripts/VolumeData/VolumeDataset.cs.meta diff --git a/Assets/Scripts/VolumeObject.meta b/Assets/Scripts/VolumeObject.meta new file mode 100644 index 0000000..1038891 --- /dev/null +++ b/Assets/Scripts/VolumeObject.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 03893c9833273f240992663ef124c6a1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/VolumeObject/RenderMode.cs b/Assets/Scripts/VolumeObject/RenderMode.cs new file mode 100644 index 0000000..238b357 --- /dev/null +++ b/Assets/Scripts/VolumeObject/RenderMode.cs @@ -0,0 +1,6 @@ +public enum RenderMode +{ + DirectVolumeRendering, + MaximumIntensityProjectipon, + IsosurfaceRendering +} diff --git a/Assets/Scripts/VolumeObject/RenderMode.cs.meta b/Assets/Scripts/VolumeObject/RenderMode.cs.meta new file mode 100644 index 0000000..0c622a4 --- /dev/null +++ b/Assets/Scripts/VolumeObject/RenderMode.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bd1c234ee0e105046973293124d8566d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SlicingPlane.cs b/Assets/Scripts/VolumeObject/SlicingPlane.cs similarity index 92% rename from Assets/Scripts/SlicingPlane.cs rename to Assets/Scripts/VolumeObject/SlicingPlane.cs index af87cf9..023da56 100644 --- a/Assets/Scripts/SlicingPlane.cs +++ b/Assets/Scripts/VolumeObject/SlicingPlane.cs @@ -1,5 +1,6 @@ using UnityEngine; +[ExecuteInEditMode] public class SlicingPlane : MonoBehaviour { private MeshRenderer meshRenderer; diff --git a/Assets/Scripts/SlicingPlane.cs.meta b/Assets/Scripts/VolumeObject/SlicingPlane.cs.meta similarity index 100% rename from Assets/Scripts/SlicingPlane.cs.meta rename to Assets/Scripts/VolumeObject/SlicingPlane.cs.meta diff --git a/Assets/Scripts/VolumeObject/VolumeObjectFactory.cs b/Assets/Scripts/VolumeObject/VolumeObjectFactory.cs new file mode 100644 index 0000000..ccfb7c0 --- /dev/null +++ b/Assets/Scripts/VolumeObject/VolumeObjectFactory.cs @@ -0,0 +1,84 @@ +using System; +using UnityEngine; + +public class VolumeObjectFactory +{ + public static VolumeRenderedObject CreateObject(VolumeDataset dataset) + { + GameObject obj = GameObject.Instantiate((GameObject)Resources.Load("VolumeRenderedObject")); + VolumeRenderedObject volObj = obj.GetComponent(); + MeshRenderer meshRenderer = obj.GetComponent(); + + volObj.dataset = dataset; + + int dimX = dataset.dimX; + int dimY = dataset.dimY; + int dimZ = dataset.dimZ; + + int maxRange = dataset.maxDataValue - dataset.minDataValue; + + Color[] cols = new Color[dataset.data.Length]; + for (int x = 0; x < dataset.dimX; x++) + { + for (int y = 0; y < dataset.dimY; y++) + { + for (int z = 0; z < dataset.dimZ; z++) + { + int iData = x + y * dimX + z * (dimX * dimY); + + int x1 = dataset.data[Math.Min(x + 1, dimX - 1) + y * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; + int x2 = dataset.data[Math.Max(x - 1, 0) + y * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; + int y1 = dataset.data[x + Math.Min(y + 1, dimY - 1) * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; + int y2 = dataset.data[x + Math.Max(y - 1, 0) * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; + int z1 = dataset.data[x + y * dataset.dimX + Math.Min(z + 1, dimZ - 1) * (dataset.dimX * dataset.dimY)]; + int z2 = dataset.data[x + y * dataset.dimX + Math.Max(z - 1, 0) * (dataset.dimX * dataset.dimY)]; + + Vector3 grad = new Vector3((x2 - x1) / (float)maxRange, (y2 - y1) / (float)maxRange, (z2 - z1) / (float)maxRange); + + cols[iData] = new Color(grad.x, grad.y, grad.z, (float)dataset.data[iData] / (float)dataset.maxDataValue); + } + } + } + + dataset.texture.SetPixels(cols); + dataset.texture.Apply(); + + Texture3D tex = dataset.texture; + + const int noiseDimX = 512; + const int noiseDimY = 512; + Texture2D noiseTexture = NoiseTextureGenerator.GenerateNoiseTexture(noiseDimX, noiseDimY); + + 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(); + Texture2D tfTexture = tf.GetTexture(); + volObj.transferFunction = tf; + + tf.histogramTexture = HistogramTextureGenerator.GenerateHistogramTexture(dataset); + + TransferFunction2D tf2D = new TransferFunction2D(); + tf2D.AddBox(0.05f, 0.1f, 0.8f, 0.7f, Color.white, 0.4f); + volObj.transferFunction2D = tf2D; + + meshRenderer.sharedMaterial.SetTexture("_DataTex", tex); + meshRenderer.sharedMaterial.SetTexture("_NoiseTex", noiseTexture); + meshRenderer.sharedMaterial.SetTexture("_TFTex", tfTexture); + + meshRenderer.sharedMaterial.EnableKeyword("MODE_DVR"); + meshRenderer.sharedMaterial.DisableKeyword("MODE_MIP"); + meshRenderer.sharedMaterial.DisableKeyword("MODE_SURF"); + + return volObj; + } +} diff --git a/Assets/Scripts/VolumeObject/VolumeObjectFactory.cs.meta b/Assets/Scripts/VolumeObject/VolumeObjectFactory.cs.meta new file mode 100644 index 0000000..bf7943d --- /dev/null +++ b/Assets/Scripts/VolumeObject/VolumeObjectFactory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6b116dd3827f30444b1fee36feceb17d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/VolumeObject/VolumeRenderedObject.cs b/Assets/Scripts/VolumeObject/VolumeRenderedObject.cs new file mode 100644 index 0000000..cc6e4b1 --- /dev/null +++ b/Assets/Scripts/VolumeObject/VolumeRenderedObject.cs @@ -0,0 +1,67 @@ +using UnityEngine; + +public class VolumeRenderedObject : MonoBehaviour +{ + [HideInInspector] + public TransferFunction transferFunction; + + [HideInInspector] + public TransferFunction2D transferFunction2D; + + [HideInInspector] + public VolumeDataset dataset; + + private RenderMode remderMode; + + public SlicingPlane CreateSlicingPlane() + { + GameObject sliceRenderingPlane = GameObject.Instantiate(Resources.Load("SlicingPlane")); + sliceRenderingPlane.transform.parent = transform; + sliceRenderingPlane.transform.localPosition = Vector3.zero; + sliceRenderingPlane.transform.localRotation = Quaternion.identity; + MeshRenderer sliceMeshRend = sliceRenderingPlane.GetComponent(); + sliceMeshRend.material = new Material(sliceMeshRend.sharedMaterial); + Material sliceMat = sliceRenderingPlane.GetComponent().sharedMaterial; + sliceMat.SetTexture("_DataTex", dataset.texture); + sliceMat.SetTexture("_TFTex", transferFunction.GetTexture()); + sliceMat.SetMatrix("_parentInverseMat", transform.worldToLocalMatrix); + sliceMat.SetMatrix("_planeMat", Matrix4x4.TRS(sliceRenderingPlane.transform.position, sliceRenderingPlane.transform.rotation, Vector3.one)); // TODO: allow changing scale + + return sliceRenderingPlane.GetComponent(); + } + + public void SetRenderMode(RenderMode mode) + { + remderMode = mode; + + switch (mode) + { + case RenderMode.DirectVolumeRendering: + { + GetComponent().sharedMaterial.EnableKeyword("MODE_DVR"); + GetComponent().sharedMaterial.DisableKeyword("MODE_MIP"); + GetComponent().sharedMaterial.DisableKeyword("MODE_SURF"); + break; + } + case RenderMode.MaximumIntensityProjectipon: + { + GetComponent().sharedMaterial.DisableKeyword("MODE_DVR"); + GetComponent().sharedMaterial.EnableKeyword("MODE_MIP"); + GetComponent().sharedMaterial.DisableKeyword("MODE_SURF"); + break; + } + case RenderMode.IsosurfaceRendering: + { + GetComponent().sharedMaterial.DisableKeyword("MODE_DVR"); + GetComponent().sharedMaterial.DisableKeyword("MODE_MIP"); + GetComponent().sharedMaterial.EnableKeyword("MODE_SURF"); + break; + } + } + } + + public RenderMode GetRemderMode() + { + return remderMode; + } +} diff --git a/Assets/Scripts/VolumeObject/VolumeRenderedObject.cs.meta b/Assets/Scripts/VolumeObject/VolumeRenderedObject.cs.meta new file mode 100644 index 0000000..0bbae6c --- /dev/null +++ b/Assets/Scripts/VolumeObject/VolumeRenderedObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: acf4a5cf6519d714895a068f9a4e8456 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/VolumeRenderer.cs b/Assets/Scripts/VolumeRenderer.cs deleted file mode 100644 index eb19c6d..0000000 --- a/Assets/Scripts/VolumeRenderer.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using System; -using System.IO; -using UnityEngine; - -public class VolumeRenderer : MonoBehaviour -{ - public TransferFunction tf = null; - public TransferFunction2D tf2D = null; - public VolumeDataset volumeDataset = null; - - private GameObject sliceRenderingPlane; - - private void Start() - { - string fileToLoad = "DataFiles//manix.dat"; - FileStream fs = new FileStream(fileToLoad, FileMode.Open); - BinaryReader reader = new BinaryReader(fs); - - ushort dimX = reader.ReadUInt16(); - ushort dimY = reader.ReadUInt16(); - ushort dimZ = reader.ReadUInt16(); - - reader.Close(); - fs.Close(); - - Debug.Log(dimX + ", " + dimY + ", " + dimZ); - - int uDimension = dimX * dimY * dimZ; - - RawDatasetImporter importer = new RawDatasetImporter(fileToLoad, dimX, dimY, dimZ, DataContentFormat.Int16, 6); - VolumeDataset dataset = importer.Import(); - volumeDataset = dataset; - - int maxRange = dataset.maxDataValue - dataset.minDataValue; - //const float maxNormalisedMagnitude = 1.75f; // sqrt(1^2 + 1^2 + 1^2) = swrt(3) = a bit less than 1.75 - - Color[] cols = new Color[dataset.data.Length]; - for(int x = 0; x < dataset.dimX; x++) - { - for (int y = 0; y < dataset.dimY; y++) - { - for (int z = 0; z < dataset.dimZ; z++) - { - int iData = x + y * dimX + z * (dimX * dimY); - - int x1 = dataset.data[Math.Min(x + 1, dimX - 1) + y * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; - int x2 = dataset.data[Math.Max(x - 1, 0) + y * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; - int y1 = dataset.data[x + Math.Min(y + 1, dimY - 1) * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; - int y2 = dataset.data[x + Math.Max(y - 1, 0) * dataset.dimX + z * (dataset.dimX * dataset.dimY)]; - int z1 = dataset.data[x + y * dataset.dimX + Math.Min(z + 1, dimZ - 1) * (dataset.dimX * dataset.dimY)]; - int z2 = dataset.data[x + y * dataset.dimX + Math.Max(z - 1, 0) * (dataset.dimX * dataset.dimY)]; - - Vector3 grad = new Vector3((x2 - x1) / (float)maxRange, (y2 - y1) / (float)maxRange, (z2 - z1) / (float)maxRange); - - cols[iData] = new Color(grad.x, grad.y, grad.z, (float)dataset.data[iData] / (float)dataset.maxDataValue); - } - } - } - - dataset.texture.SetPixels(cols); - dataset.texture.Apply(); - - Texture3D tex = dataset.texture; - - const int noiseDimX = 512; - const int noiseDimY = 512; - Texture2D noiseTexture = NoiseTextureGenerator.GenerateNoiseTexture(noiseDimX, noiseDimY); - - 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(); - Texture2D tfTexture = tf.GetTexture(); - - tf.histogramTexture = HistogramTextureGenerator.GenerateHistogramTexture(dataset); - - tf2D = new TransferFunction2D(); - tf2D.AddBox(0.05f, 0.1f, 0.8f, 0.7f, Color.white, 0.4f); - - GetComponent().sharedMaterial.SetTexture("_DataTex", tex); - GetComponent().sharedMaterial.SetTexture("_NoiseTex", noiseTexture); - GetComponent().sharedMaterial.SetTexture("_TFTex", tfTexture); - - GetComponent().sharedMaterial.EnableKeyword("MODE_DVR"); - GetComponent().sharedMaterial.DisableKeyword("MODE_MIP"); - GetComponent().sharedMaterial.DisableKeyword("MODE_SURF"); - } - - public SlicingPlane CreateSlicingPlane() - { - sliceRenderingPlane = GameObject.Instantiate(Resources.Load("SlicingPlane")); - sliceRenderingPlane.transform.parent = transform; - sliceRenderingPlane.transform.localPosition = Vector3.zero; - sliceRenderingPlane.transform.localRotation = Quaternion.identity; - MeshRenderer sliceMeshRend = sliceRenderingPlane.GetComponent(); - sliceMeshRend.material = new Material(sliceMeshRend.sharedMaterial); - Material sliceMat = sliceRenderingPlane.GetComponent().sharedMaterial; - sliceMat.SetTexture("_DataTex", volumeDataset.texture); - sliceMat.SetTexture("_TFTex", tf.GetTexture()); - sliceMat.SetMatrix("_parentInverseMat", transform.worldToLocalMatrix); - sliceMat.SetMatrix("_planeMat", Matrix4x4.TRS(sliceRenderingPlane.transform.position, sliceRenderingPlane.transform.rotation, Vector3.one)); // TODO: allow changing scale - - return sliceRenderingPlane.GetComponent(); - } - - private void Update() - { - if (Input.GetKeyDown(KeyCode.A)) - { - GetComponent().sharedMaterial.EnableKeyword("MODE_DVR"); - GetComponent().sharedMaterial.DisableKeyword("MODE_MIP"); - GetComponent().sharedMaterial.DisableKeyword("MODE_SURF"); - } - else if (Input.GetKeyDown(KeyCode.S)) - { - GetComponent().sharedMaterial.DisableKeyword("MODE_DVR"); - GetComponent().sharedMaterial.EnableKeyword("MODE_MIP"); - GetComponent().sharedMaterial.DisableKeyword("MODE_SURF"); - } - else if (Input.GetKeyDown(KeyCode.D)) - { - GetComponent().sharedMaterial.DisableKeyword("MODE_DVR"); - GetComponent().sharedMaterial.DisableKeyword("MODE_MIP"); - GetComponent().sharedMaterial.EnableKeyword("MODE_SURF"); - } - } -}