夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

MRTKワイヤフレームShaderを調査する。 その⑤ ジオメトリシェーダーの処理

本日はMRTK調査枠です。

現在ジオメトリシェーダーの勉強を兼ねMRTKで提供されているワイヤフレームシェーダーの中身を追っています。

〇ジオメトリシェーダー

ジオメトリシェーダーはフラグメントシェーダーの前に実行される処理で、頂点シェーダーから受け取った頂点データをもとに面としての処理が可能なシェーダーです。

MRTKの場合ほかにも[HandTrianglesShader]など多くのシェーダーで使用されている処理です。

ジオメトリシェーダーを用いることでより複雑な描画の処理を行うことができますが、ワイヤフレームシェーダーの場合もジオメトリシェーダーを用いてラインを検出しているようです。

 ワイヤフレームシェーダーのジオメトリシェーダー部は次のようになります。

   [maxvertexcount(3)]
            void geom(triangle v2g i[3], inout TriangleStream<g2f> triStream)
            {
                // Calculate the vectors that define the triangle from the input points.
                float2 point0 = i[0].viewPos.xy / i[0].viewPos.w;
                float2 point1 = i[1].viewPos.xy / i[1].viewPos.w;
                float2 point2 = i[2].viewPos.xy / i[2].viewPos.w;

                // Calculate the area of the triangle.
                float2 vector0 = point2 - point1;
                float2 vector1 = point2 - point0;
                float2 vector2 = point1 - point0;
                float area = abs(vector1.x * vector2.y - vector1.y * vector2.x);

                float3 distScale[3];
                distScale[0] = float3(area / length(vector0), 0, 0);
                distScale[1] = float3(0, area / length(vector1), 0);
                distScale[2] = float3(0, 0, area / length(vector2));

                float wireScale = 800 - _WireThickness;

                // Output each original vertex with its distance to the opposing line defined
                // by the other two vertices.
                g2f o;
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

                [unroll]
                for (uint idx = 0; idx < 3; ++idx)
                {
                   o.viewPos = i[idx].viewPos;
                   o.inverseW = 1.0 / o.viewPos.w;
                   o.dist = distScale[idx] * o.viewPos.w * wireScale;
                   UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[idx], o);
                   triStream.Append(o);
                }
            }
   [maxvertexcount(3)]
            void geom(triangle v2g i[3], inout TriangleStream<g2f> triStream)
            {
               ...
            }

ジオメトリシェーダーでは[maxvertexcount(3)]のようにシェーダー内の処理で使用する頂点数を定義します。

今回は3でこれは面を構成する最小単位の頂点数です。

まず頂点の座標をもとに頂点のベクトルを計算します。

                float2 point0 = i[0].viewPos.xy / i[0].viewPos.w;
                float2 point1 = i[1].viewPos.xy / i[1].viewPos.w;
                float2 point2 = i[2].viewPos.xy / i[2].viewPos.w;

次に面を作成します。ここでは各頂点のベクトルをもとにポリゴンを作成します。

                float2 vector0 = point2 - point1;
                float2 vector1 = point2 - point0;
                float2 vector2 = point1 - point0;
                float area = abs(vector1.x * vector2.y - vector1.y * vector2.x);

[distScale]にはx,y,z各成分ごとのベクトルが代入され、それぞれdistScale[0],distScale[1],distScale[2]として格納されています。

                float3 distScale[3];
                distScale[0] = float3(area / length(vector0), 0, 0);
                distScale[1] = float3(0, area / length(vector1), 0);
                distScale[2] = float3(0, 0, area / length(vector2));

                float wireScale = 800 - _WireThickness;

最後にg2f構造体に頂点を出力します。

  [unroll]
                for (uint idx = 0; idx < 3; ++idx)
                {
                   o.viewPos = i[idx].viewPos;
                   o.inverseW = 1.0 / o.viewPos.w;
                   o.dist = distScale[idx] * o.viewPos.w * wireScale;
                   UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[idx], o);
                   triStream.Append(o);
                }
            }

出力される値を見てみるとそれぞれ以下のようになります

viewPos …各頂点の元の座標
inverseW...viewPos.w(各頂点のw成分)の逆数
dist...頂点の座標×元の座標×_WireScale(プロパティ変数 _WireThickness)

つまりここでは元のポリゴンからもともとのメッシュと、 _WireThicknessによって差分を得たメッシュ情報を出力しています。