ボリューム レンダリング

初めてボリューム レンダリングを使用する場合は、概要を読むことをお勧めします。

3D テクスチャの表現

CPU で:

public struct Int3 { public int X, Y, Z; /* ... */ }
 public class VolumeHeader  {
   public readonly Int3 Size;
   public VolumeHeader(Int3 size) { this.Size = size;  }
   public int CubicToLinearIndex(Int3 index) {
     return index.X + (index.Y * (Size.X)) + (index.Z * (Size.X * Size.Y));
   }
   public Int3 LinearToCubicIndex(int linearIndex)
   {
     return new Int3((linearIndex / 1) % Size.X,
       (linearIndex / Size.X) % Size.Y,
       (linearIndex / (Size.X * Size.Y)) % Size.Z);
   }
   /* ... */
 }
 public class VolumeBuffer<T> {
   public readonly VolumeHeader Header;
   public readonly T[] DataArray;
   public T GetVoxel(Int3 pos)        {
     return this.DataArray[this.Header.CubicToLinearIndex(pos)];
   }
   public void SetVoxel(Int3 pos, T val)        {
     this.DataArray[this.Header.CubicToLinearIndex(pos)] = val;
   }
   public T this[Int3 pos] {
     get { return this.GetVoxel(pos); }
     set { this.SetVoxel(pos, value); }
   }
   /* ... */
 }

GPU で:

float3 _VolBufferSize;
 int3 UnitVolumeToIntVolume(float3 coord) {
   return (int3)( coord * _VolBufferSize.xyz );
 }
 int IntVolumeToLinearIndex(int3 coord, int3 size) {
   return coord.x + ( coord.y * size.x ) + ( coord.z * ( size.x * size.y ) );
 }
 uniform StructuredBuffer<float> _VolBuffer;
 float SampleVol(float3 coord3 ) {
   int3 intIndex3 = UnitVolumeToIntVolume( coord3 );
   int index1D = IntVolumeToLinearIndex( intIndex3, _VolBufferSize.xyz);
   return __VolBuffer[index1D];
 }

網掛けとグラデーション

便利な視覚化のために、MRI などのボリュームを網掛けする方法です。 主な方法は、その中の濃淡を表示する "濃淡ウィンドウ" (最小値と最大値) を用意し、そのスペースにスケーリングして白黒の濃淡を表示することです。 次に、"色傾斜" をその範囲内の値に適用し、テクスチャとして保存することで、濃淡スペクトルのさまざまな部分を異なる色で網掛けすることができます。

float4 ShadeVol( float intensity ) {
   float unitIntensity = saturate( intensity - IntensityMin / ( IntensityMax - IntensityMin ) );
   // Simple two point black and white intensity:
   color.rgba = unitIntensity;
   // Color ramp method:
   color.rgba = tex2d( ColorRampTexture, float2( unitIntensity, 0 ) );

Microsoft の多くのアプリケーションでは、未加工の濃淡値と "セグメンテーション インデックス" の両方をボリュームに格納しています (スキンやボーンなどのさまざまな部分をセグメント化するためです。これらのセグメントは、専門家によって専用ツールで作成されます)。 これを上記の方法と組み合わせて、セグメント インデックスごとに異なる色または異なる色傾斜を付けることができます。

// Change color to match segment index (fade each segment towards black):
 color.rgb = SegmentColors[ segment_index ] * color.a; // brighter alpha gives brighter color

シェーダーでのボリューム スライス

重要な最初の手順は、ボリューム内を移動できる "スライス平面" を作成し、"それをスライス" し、各ポイントでのスキャン値がどのようになっているかを確認することです。 これは、ワールド空間におけるボリュームの場所を表す 'VolumeSpace' キューブがあることを前提としています。これは、ポイントを配置するための参照として使用できます。

// In the vertex shader:
 float4 worldPos = mul(_Object2World, float4(input.vertex.xyz, 1));
 float4 volSpace = mul(_WorldToVolume, float4(worldPos, 1));
// In the pixel shader:
 float4 color = ShadeVol( SampleVol( volSpace ) );

シェーダーでのボリューム トレース

GPU を使用して、サブボリュームのトレースを実行する方法 (数ボクセルの深さを歩き、データを後ろから前にレイヤー化します)。

float4 AlphaBlend(float4 dst, float4 src) {
   float4 res = (src * src.a) + (dst - dst * src.a);
   res.a = src.a + (dst.a - dst.a*src.a);
   return res;
 }
 float4 volTraceSubVolume(float3 objPosStart, float3 cameraPosVolSpace) {
   float maxDepth = 0.15; // depth in volume space, customize!!!
   float numLoops = 10; // can be 400 on nice PC
   float4 curColor = float4(0, 0, 0, 0);
   // Figure out front and back volume coords to walk through:
   float3 frontCoord = objPosStart;
   float3 backCoord = frontPos + (normalize(cameraPosVolSpace - objPosStart) * maxDepth);
   float3 stepCoord = (frontCoord - backCoord) / numLoops;
   float3 curCoord = backCoord;
   // Add per-pixel random offset, avoids layer aliasing:
   curCoord += stepCoord * RandomFromPositionFast(objPosStart);
   // Walk from back to front (to make front appear in-front of back):
   for (float i = 0; i < numLoops; i++) {
     float intensity = SampleVol(curCoord);
     float4 shaded = ShadeVol(intensity);
     curColor = AlphaBlend(curColor, shaded);
     curCoord += stepCoord;
   }
   return curColor;
 }
// In the vertex shader:
 float4 worldPos = mul(_Object2World, float4(input.vertex.xyz, 1));
 float4 volSpace = mul(_WorldToVolume, float4(worldPos.xyz, 1));
 float4 cameraInVolSpace = mul(_WorldToVolume, float4(_WorldSpaceCameraPos.xyz, 1));
// In the pixel shader:
 float4 color = volTraceSubVolume( volSpace, cameraInVolSpace );

ボリューム全体のレンダリング

上記のサブボリューム コードを変更すると、次が得られます。

float4 volTraceSubVolume(float3 objPosStart, float3 cameraPosVolSpace) {
   float maxDepth = 1.73; // sqrt(3), max distance from point on cube to any other point on cube
   int maxSamples = 400; // just in case, keep this value within bounds
   // not shown: trim front and back positions to both be within the cube
   int distanceInVoxels = length(UnitVolumeToIntVolume(frontPos - backPos)); // measure distance in voxels
   int numLoops = min( distanceInVoxels, maxSamples ); // put a min on the voxels to sample

混合解像度シーンのレンダリング

シーンの一部を低解像度でレンダリングして元の場所に戻す方法:

  1. 2 台のオフスクリーン カメラをセットアップします。1 台はフレームごとに更新するそれぞれの目を追跡するように設定します
  2. カメラがレンダリングする 2 つの低解像度のレンダー ターゲット (それぞれ 200 x 200) を設定します
  3. ユーザーの前に移動するクワッドを設定します

各フレーム:

  1. それぞれの目のレンダー ターゲットを低解像度で描画します (ボリューム データ、負荷の高いシェーダーなど)
  2. シーンをフル解像度として通常どおり描画します (メッシュ、UI など)
  3. ユーザーの前にクワッドを描画し、そのシーンに対して低解像度のレンダリングを投影します
  4. 結果: フル解像度の要素と低解像度だが高密度のボリューム データの視覚的な組み合わせ