Skip to content
第14回 HLSL入門

4 / 4 HLSLを書く ​

HLSLを用いてシェーダーを記述する際の基本的な流れやポイントについて解説します。

座標変換 ​

座標変換は、頂点の位置をワールド座標やビュー座標、クリップ座標などに変換する処理です。
HLSLでは、行列を用いて座標変換を行います。例えば、モデル行列、ビュー行列、射影行列を用いて頂点の位置を変換することが一般的です。

例えば、頂点シェーダー内でモデル行列、ビュー行列、射影行列を用いて頂点の位置を変換する場合、以下のように記述します。

hlsl
float4x4 modelMatrix;
float4x4 viewMatrix;
float4x4 projectionMatrix;

struct VertexInput
{
    float3 position : POSITION;
};

struct VertexOutput
{
    float4 position : SV_POSITION;
};

VertexOutput vert(VertexInput input)
{
    VertexOutput output;
    float4 worldPosition = mul(modelMatrix, float4(input.position, 1.0));
    float4 viewPosition = mul(viewMatrix, worldPosition);
    output.position = mul(projectionMatrix, viewPosition);
    return output;
}

テクスチャーマップ ​

HLSLでは、テクスチャーマップを用いて表面の色や凹凸などの情報を表現することができます。
例えば、2Dテクスチャをサンプリングしてフラグメントシェーダーで使用する場合、以下のように記述します。

hlsl
Texture2D myTexture;
SamplerState mySampler;

struct VertexOutput
{
    float4 position : SV_POSITION;
    float2 uv : TEXCOORD0;
};

float4 frag(VertexOutput input) : SV_TARGET
{
    float4 color = myTexture.Sample(mySampler, input.uv);
    return color;
}

基本的なライティング計算 ​

例えば、法線ベクトルと光の方向ベクトルを用いて拡散反射を計算する場合、以下のように記述します。

hlsl
float3 lightDirection = normalize(float3(0.0, 1.0, 1.0));

struct VertexOutput
{
    float4 position : SV_POSITION;
    float3 normal : NORMAL;
};

float4 frag(VertexOutput input) : SV_TARGET
{
    float3 normal = normalize(input.normal);
    float diffuse = max(dot(normal, lightDirection), 0.0);
    float3 color = diffuse * float3(1.0, 1.0, 1.0); // 白色の光
    return float4(color, 1.0);
}

Unity での HLSL の使用 ​

Unity の機能を利用することで、シーン内に設定されたライトやカメラの情報、マテリアル上のテクスチャー設定を取得してシェーダー内で使用することができます。
また、Unityが提供するシェーダーライブラリを利用することで、ライティングやマテリアルの計算を簡単に行うことができます。

実際に記述する際は、Unityのシェーダーの仕組みやレンダーパイプラインに関する知識が必要です。
詳細はマニュアルを参照してください。

以下はUnity の機能を用いて、簡素な3Dシェーダーを実装する例です。

hlsl
Shader "Course/Lesson14/AppleLambert"
{
    Properties
    {
        _BaseMap ("Texture", 2D) = "white" {}
        _Tint ("Tint", Color) = (1, 1, 1, 1)
        _Ambient ("Constant Fill", Range(0, 1)) = 0.2
    }
    SubShader
    {
        Tags { "RenderPipeline" = "UniversalPipeline" "RenderType" = "Opaque" }
        Pass
        {
            Tags { "LightMode" = "SRPDefaultUnlit" }
            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            struct Attributes
            {
                float3 positionOS : POSITION;
                float2 uv : TEXCOORD0;
                float3 normalOS : NORMAL;
            };
            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 normalWS : TEXCOORD1;
            };
            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BaseMap);
            CBUFFER_START(UnityPerMaterial)
                float4 _BaseMap_ST;
                float4 _Tint;
                float _Ambient;
            CBUFFER_END

            Varyings Vert(Attributes input)
            {
                Varyings output;
                output.positionCS = TransformObjectToHClip(input.positionOS);
                output.uv = input.uv * _BaseMap_ST.xy + _BaseMap_ST.zw;
                output.normalWS = TransformObjectToWorldNormal(input.normalOS);
                return output;
            }
            float4 Frag(Varyings input) : SV_Target
            {
                float3 normalWS = normalize(input.normalWS);
                Light mainLight = GetMainLight();
                float3 lightDirWS = mainLight.direction;
                float diffuse = saturate(dot(normalWS, lightDirWS));
                float4 albedo = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _Tint;
                float lighting = diffuse + _Ambient;
                return float4(albedo.rgb * lighting, 1.0);
            }
            ENDHLSL
        }
    }
}

:::

例えば、単色を表示する最小限の Unity シェーダーは以下のように記述できます。

hlsl
Shader "Custom/SimpleColor"
{
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct VertexInput
            {
                float3 position : POSITION;
            };

            struct VertexOutput
            {
                float4 position : SV_POSITION;
            };

            VertexOutput vert(VertexInput input)
            {
                VertexOutput output;
                output.position = float4(input.position, 1.0);
                return output;
            }

            float4 frag(VertexOutput input) : SV_TARGET
            {
                return float4(1.0, 0.0, 0.0, 1.0); // 赤色
            }
            ENDCG
        }
    }
}