7
7
8
8
namespace UnityVolumeRendering
9
9
{
10
- [ System . Serializable ]
11
- public struct SegmentationLabel
12
- {
13
- public int id ;
14
- public string name ;
15
- public Color colour ;
16
- }
17
-
18
10
[ ExecuteInEditMode ]
19
11
public class VolumeRenderedObject : MonoBehaviour
20
12
{
@@ -54,6 +46,12 @@ public class VolumeRenderedObject : MonoBehaviour
54
46
[ SerializeField , HideInInspector ]
55
47
private List < SegmentationLabel > segmentationLabels = new List < SegmentationLabel > ( ) ;
56
48
49
+ [ SerializeField , HideInInspector ]
50
+ private OverlayType overlayType = OverlayType . None ;
51
+
52
+ [ SerializeField , HideInInspector ]
53
+ private SegmentationRenderMode segmentationRenderMode = SegmentationRenderMode . OverlayColour ;
54
+
57
55
// Minimum and maximum gradient threshold for lighting contribution. Values below min will be unlit, and between min and max will be partly shaded.
58
56
[ SerializeField , HideInInspector ]
59
57
private Vector2 gradientLightingThreshold = new Vector2 ( 0.02f , 0.15f ) ;
@@ -102,15 +100,9 @@ public SlicingPlane CreateSlicingPlane()
102
100
return slicingPlaneComp ;
103
101
}
104
102
105
- public VolumeDataset GetSecondaryDataset ( )
103
+ public OverlayType GetOverlayType ( )
106
104
{
107
- return this . secondaryDataset ;
108
- }
109
-
110
- public void SetSecondaryDataset ( VolumeDataset dataset )
111
- {
112
- this . secondaryDataset = dataset ;
113
- UpdateMaterialProperties ( ) ;
105
+ return this . overlayType ;
114
106
}
115
107
116
108
public TransferFunction GetSecondaryTransferFunction ( )
@@ -124,6 +116,34 @@ public void SetSecondaryTransferFunction(TransferFunction tf)
124
116
UpdateMaterialProperties ( ) ;
125
117
}
126
118
119
+ public void SetOverlayDataset ( VolumeDataset dataset )
120
+ {
121
+ if ( dataset != null )
122
+ {
123
+ this . overlayType = OverlayType . Overlay ;
124
+ }
125
+ else if ( this . overlayType == OverlayType . Overlay )
126
+ {
127
+ this . overlayType = OverlayType . None ;
128
+ }
129
+ this . secondaryDataset = dataset ;
130
+ UpdateMaterialProperties ( ) ;
131
+ }
132
+
133
+ public SegmentationRenderMode GetSegmentationRenderMode ( )
134
+ {
135
+ return segmentationRenderMode ;
136
+ }
137
+
138
+ public void SetSegmentationRenderMode ( SegmentationRenderMode mode )
139
+ {
140
+ if ( mode != segmentationRenderMode )
141
+ {
142
+ segmentationRenderMode = mode ;
143
+ UpdateMaterialProperties ( ) ;
144
+ }
145
+ }
146
+
127
147
public List < SegmentationLabel > GetSegmentationLabels ( )
128
148
{
129
149
return segmentationLabels ;
@@ -137,6 +157,8 @@ public void AddSegmentation(VolumeDataset dataset)
137
157
return ;
138
158
}
139
159
160
+ overlayType = OverlayType . Segmentation ;
161
+
140
162
int segmentationId = segmentationLabels . Count > 0 ? segmentationLabels . Max ( l => l . id ) + 1 : 1 ;
141
163
142
164
if ( segmentationLabels . Count == 0 )
@@ -161,10 +183,44 @@ public void AddSegmentation(VolumeDataset dataset)
161
183
UpdateSegmentationLabels ( ) ;
162
184
}
163
185
186
+ public void RemoveSegmentation ( int id )
187
+ {
188
+ int segmentationIndex = segmentationLabels . FindIndex ( s => s . id == id ) ;
189
+ if ( segmentationIndex != - 1 )
190
+ {
191
+ segmentationLabels . RemoveAt ( segmentationIndex ) ;
192
+ }
193
+ else
194
+ {
195
+ Debug . LogError ( $ "Segmentation not found: { id } ") ;
196
+ }
197
+ for ( int i = 0 ; i < secondaryDataset . data . Length ; i ++ )
198
+ {
199
+ secondaryDataset . data [ i ] = secondaryDataset . data [ i ] == id ? 0 : secondaryDataset . data [ i ] ;
200
+ }
201
+ secondaryDataset . RecalculateBounds ( ) ;
202
+ secondaryDataset . RecreateDataTexture ( ) ;
203
+ secondaryDataset . GetDataTexture ( ) . filterMode = FilterMode . Point ;
204
+ UpdateSegmentationLabels ( ) ;
205
+ }
206
+
207
+ public void ClearSegmentations ( )
208
+ {
209
+ if ( overlayType == OverlayType . Segmentation )
210
+ {
211
+ secondaryDataset = null ;
212
+ secondaryTransferFunction = null ;
213
+ overlayType = OverlayType . None ;
214
+ }
215
+ segmentationLabels . Clear ( ) ;
216
+ UpdateMaterialProperties ( ) ;
217
+ }
218
+
164
219
public void UpdateSegmentationLabels ( )
165
220
{
166
221
if ( segmentationLabels . Count == 0 )
167
222
{
223
+ UpdateMaterialProperties ( ) ;
168
224
return ;
169
225
}
170
226
@@ -454,16 +510,26 @@ private void UpdateMatInternal(Texture3D dataTexture, Texture3D gradientTexture,
454
510
meshRenderer . sharedMaterial . SetTexture ( "_GradientTex" , gradientTexture ) ;
455
511
}
456
512
457
- if ( secondaryDataTexture != null )
513
+ if ( overlayType != OverlayType . None && secondaryDataTexture != null )
458
514
{
459
515
Texture2D secondaryTF = secondaryTransferFunction . GetTexture ( ) ;
460
516
meshRenderer . sharedMaterial . SetTexture ( "_SecondaryDataTex" , secondaryDataTexture ) ;
461
517
meshRenderer . sharedMaterial . SetTexture ( "_SecondaryTFTex" , secondaryTF ) ;
462
- meshRenderer . sharedMaterial . EnableKeyword ( "SECONDARY_VOLUME_ON" ) ;
518
+ if ( overlayType == OverlayType . Segmentation && segmentationRenderMode == SegmentationRenderMode . Isolate )
519
+ {
520
+ meshRenderer . sharedMaterial . EnableKeyword ( "MULTIVOLUME_ISOLATE" ) ;
521
+ meshRenderer . sharedMaterial . DisableKeyword ( "MULTIVOLUME_OVERLAY" ) ;
522
+ }
523
+ else if ( overlayType == OverlayType . Overlay )
524
+ {
525
+ meshRenderer . sharedMaterial . EnableKeyword ( "MULTIVOLUME_OVERLAY" ) ;
526
+ meshRenderer . sharedMaterial . DisableKeyword ( "MULTIVOLUME_ISOLATE" ) ;
527
+ }
463
528
}
464
529
else
465
530
{
466
- meshRenderer . sharedMaterial . DisableKeyword ( "SECONDARY_VOLUME_ON" ) ;
531
+ meshRenderer . sharedMaterial . DisableKeyword ( "MULTIVOLUME_OVERLAY" ) ;
532
+ meshRenderer . sharedMaterial . DisableKeyword ( "MULTIVOLUME_ISOLATE" ) ;
467
533
}
468
534
469
535
if ( meshRenderer . sharedMaterial . GetTexture ( "_NoiseTex" ) == null )
0 commit comments