Skip to content

Commit 5d80d8f

Browse files
committed
cleanup
1 parent 30dc9f7 commit 5d80d8f

File tree

2 files changed

+65
-58
lines changed

2 files changed

+65
-58
lines changed

Assets/Editor/VolumeRenderedObjectCustomInspector.cs

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
55
using System.IO;
6+
using UnityEngine.Events;
67

78
namespace UnityVolumeRendering
89
{
@@ -148,11 +149,25 @@ public override void OnInspectorGUI()
148149
{
149150
if (GUILayout.Button("Load PET (NRRD, NIFTI)"))
150151
{
151-
ImportPetScan(volrendObj);
152+
ImportImageFileDataset(volrendObj, (VolumeDataset dataset) =>
153+
{
154+
TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
155+
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
156+
secondaryTransferFunction.GenerateTexture();
157+
volrendObj.SetOverlayDataset(dataset);
158+
volrendObj.SetSecondaryTransferFunction(secondaryTransferFunction);
159+
});
152160
}
153161
if (GUILayout.Button("Load PET (DICOM)"))
154162
{
155-
ImportPetScanDicom(volrendObj);
163+
ImportDicomDataset(volrendObj, (VolumeDataset dataset) =>
164+
{
165+
TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
166+
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
167+
secondaryTransferFunction.GenerateTexture();
168+
volrendObj.SetOverlayDataset(dataset);
169+
volrendObj.SetSecondaryTransferFunction(secondaryTransferFunction);
170+
});
156171
}
157172
}
158173
else
@@ -198,12 +213,18 @@ public override void OnInspectorGUI()
198213
}
199214
if (GUILayout.Button("Add segmentation (NRRD, NIFTI)"))
200215
{
201-
ImportSegmentation(volrendObj);
216+
ImportImageFileDataset(volrendObj, (VolumeDataset dataset) =>
217+
{
218+
volrendObj.AddSegmentation(dataset);
219+
});
202220
}
203-
/*if (GUILayout.Button("Add segmentation (DICOM)"))
221+
if (GUILayout.Button("Add segmentation (DICOM)"))
204222
{
205-
ImportSegmentationDicom(volrendObj);
206-
}*/
223+
ImportDicomDataset(volrendObj, (VolumeDataset dataset) =>
224+
{
225+
volrendObj.AddSegmentation(dataset);
226+
});
227+
}
207228
if (GUILayout.Button("Clear segmentations"))
208229
{
209230
volrendObj.ClearSegmentations();
@@ -224,7 +245,7 @@ public override void OnInspectorGUI()
224245
volrendObj.SetSamplingRateMultiplier(EditorGUILayout.Slider("Sampling rate multiplier", volrendObj.GetSamplingRateMultiplier(), 0.2f, 2.0f));
225246
}
226247
}
227-
private static async void ImportPetScan(VolumeRenderedObject targetObject)
248+
private static async void ImportImageFileDataset(VolumeRenderedObject targetObject, UnityAction<VolumeDataset> onLoad)
228249
{
229250
string filePath = EditorUtility.OpenFilePanel("Select a folder to load", "", "");
230251
ImageFileFormat imageFileFormat = DatasetFormatUtilities.GetImageFileFormat(filePath);
@@ -241,67 +262,37 @@ private static async void ImportPetScan(VolumeRenderedObject targetObject)
241262

242263
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
243264
{
244-
progressHandler.StartStage(1.0f, "Importing PET dataset");
265+
progressHandler.StartStage(1.0f, "Importing dataset");
245266
IImageFileImporter importer = ImporterFactory.CreateImageFileImporter(imageFileFormat);
246267
Task<VolumeDataset> importTask = importer.ImportAsync(filePath);
247268
await importTask;
248269
progressHandler.EndStage();
249270

250-
TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
251-
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
252-
secondaryTransferFunction.GenerateTexture();
253-
targetObject.SetOverlayDataset(importTask.Result);
254-
targetObject.SetSecondaryTransferFunction(secondaryTransferFunction);
271+
if (importTask.Result != null)
272+
{
273+
onLoad.Invoke(importTask.Result);
274+
}
255275
}
256276
}
257277

258-
private static async void ImportPetScanDicom(VolumeRenderedObject targetObject)
278+
private static async void ImportDicomDataset(VolumeRenderedObject targetObject, UnityAction<VolumeDataset> onLoad)
259279
{
260280
string dir = EditorUtility.OpenFolderPanel("Select a folder to load", "", "");
261281
if (Directory.Exists(dir))
262282
{
263283
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
264284
{
265-
progressHandler.StartStage(1.0f, "Importing PET dataset");
285+
progressHandler.StartStage(1.0f, "Importing dataset");
266286
Task<VolumeDataset[]> importTask = EditorDatasetImportUtils.ImportDicomDirectoryAsync(dir, progressHandler);
267287
await importTask;
268288
progressHandler.EndStage();
269289

270-
Debug.Assert(importTask.Result.Length > 0);
271-
TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
272-
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
273-
secondaryTransferFunction.GenerateTexture();
274-
targetObject.SetOverlayDataset(importTask.Result[0]);
275-
targetObject.SetSecondaryTransferFunction(secondaryTransferFunction);
290+
if (importTask.Result.Length > 0)
291+
{
292+
onLoad.Invoke(importTask.Result[0]);
293+
}
276294
}
277295
}
278296
}
279-
280-
private static async void ImportSegmentation(VolumeRenderedObject targetObject)
281-
{
282-
string filePath = EditorUtility.OpenFilePanel("Select a folder to load", "", "");
283-
ImageFileFormat imageFileFormat = DatasetFormatUtilities.GetImageFileFormat(filePath);
284-
if (!File.Exists(filePath))
285-
{
286-
Debug.LogError($"File doesn't exist: {filePath}");
287-
return;
288-
}
289-
if (imageFileFormat == ImageFileFormat.Unknown)
290-
{
291-
Debug.LogError($"Invalid file format: {Path.GetExtension(filePath)}");
292-
return;
293-
}
294-
295-
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
296-
{
297-
progressHandler.StartStage(1.0f, "Importing segmentation dataset");
298-
IImageFileImporter importer = ImporterFactory.CreateImageFileImporter(imageFileFormat);
299-
Task<VolumeDataset> importTask = importer.ImportAsync(filePath);
300-
await importTask;
301-
progressHandler.EndStage();
302-
303-
targetObject.AddSegmentation(importTask.Result);
304-
}
305-
}
306297
}
307298
}

Assets/Shaders/DirectVolumeRenderingShader.shader

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#pragma multi_compile __ USE_MAIN_LIGHT
4141
#pragma multi_compile __ CUBIC_INTERPOLATION_ON
4242
#pragma multi_compile __ SECONDARY_VOLUME_ON
43+
#pragma multi_compile MULTIVOLUME_NONE MULTIVOLUME_OVERLAY MULTIVOLUME_ISOLATE
4344
#pragma vertex vert
4445
#pragma fragment frag
4546

@@ -215,11 +216,7 @@
215216
// Gets the density of the secondary volume at the specified position
216217
float getSecondaryDensity(float3 pos)
217218
{
218-
#if CUBIC_INTERPOLATION_ON
219-
return interpolateTricubicFast(_SecondaryDataTex, float3(pos.x, pos.y, pos.z), _TextureSize);
220-
#else
221219
return tex3Dlod(_SecondaryDataTex, float4(pos.x, pos.y, pos.z, 0.0f));
222-
#endif
223220
}
224221

225222
// Gets the density at the specified position, without tricubic interpolation
@@ -344,12 +341,14 @@
344341
continue;
345342
#endif
346343

347-
#if SECONDARY_VOLUME_ON
348-
const float density2 = getSecondaryDensity(currPos);
349-
float4 src2 = getSecondaryTF1DColour(density2);
350-
//src.rgb = src.rgb * (1.0 - src2.a) + src2.rgb * src2.a;
351-
src = src2.a > 0.0 ? src2 : src;
352-
//src.a = src2.a > 0.0 ? src.a : 0.0;
344+
#if defined(MULTIVOLUME_OVERLAY) || defined(MULTIVOLUME_ISOLATE)
345+
const float secondaryDensity = getSecondaryDensity(currPos);
346+
float4 secondaryColour = getSecondaryTF1DColour(secondaryDensity);
347+
#if MULTIVOLUME_OVERLAY
348+
src = secondaryColour.a > 0.0 ? secondaryColour : src;
349+
#elif MULTIVOLUME_ISOLATE
350+
src.a = secondaryColour.a > 0.0 ? src.a : 0.0;
351+
#endif
353352
#endif
354353

355354
// Calculate gradient (needed for lighting and 2D transfer functions)
@@ -469,6 +468,23 @@
469468
#endif
470469

471470
const float density = getDensity(currPos);
471+
#if MULTIVOLUME_ISOLATE
472+
const float secondaryDensity = getSecondaryDensity(currPos);
473+
if (secondaryDensity <= 0.0)
474+
continue;
475+
#elif MULTIVOLUME_OVERLAY
476+
const float secondaryDensity = getSecondaryDensity(currPos);
477+
if (secondaryDensity > 0.0)
478+
{
479+
col = getSecondaryTF1DColour(secondaryDensity);
480+
float3 gradient = getGradient(currPos);
481+
float gradMag = length(gradient);
482+
float3 normal = gradient / gradMag;
483+
col.rgb = calculateLighting(col.rgb, normal, getLightDirection(-ray.direction), -ray.direction, 0.15);
484+
col.a = 1.0;
485+
break;
486+
}
487+
#endif
472488
if (density > _MinVal && density < _MaxVal)
473489
{
474490
float3 gradient = getGradient(currPos);

0 commit comments

Comments
 (0)