本記事は、Unityで自分が使うときの最低構成のシェーダーをまとめたものです。
Unlit.shader
Shader "Custom/Unlit"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {
"RenderType"="Opaque"
"RenderPipeline"="UniversalPipeline"
}
LOD 100
Pass
{
Name "ForwardLit"
Tags { "LightMode"="UniversalForward" }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct in_vert
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct in_frag
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
CBUFFER_END
in_frag vert (in_vert v)
{
in_frag o;
o.vertex = TransformObjectToHClip(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 frag (in_frag i) : SV_Target
{
float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
return color;
}
ENDHLSL
}
}
}
lit.shader
Shader "Custom/lit"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {
"RenderType"="Opaque"
"RenderPipeline"="UniversalPipeline"
}
LOD 100
Pass
{
Name "ForwardLit"
Tags { "LightMode"="UniversalForward" }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
struct in_vert
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal_o : NORMAL;
};
struct in_frag
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float3 pos_w : TEXCOORD1;
float3 normal_w : TEXCOORD2;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
CBUFFER_END
float3 MainLightDir(float3 w_pos)
{
float4 shadowCoord = TransformWorldToShadowCoord(w_pos);
Light mainLight = GetMainLight(shadowCoord);
return mainLight.direction;
}
Light MainLight(float3 w_pos)
{
float4 shadowCoord = TransformWorldToShadowCoord(w_pos);
Light mainLight = GetMainLight(shadowCoord);
return mainLight;
}
in_frag vert (in_vert v)
{
in_frag o;
o.vertex = TransformObjectToHClip(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.pos_w = TransformObjectToWorld(v.vertex.xyz);
o.normal_w = TransformObjectToWorldNormal(v.normal_o);
return o;
}
float4 frag (in_frag i) : SV_Target
{
Light mainLight = MainLight(i.pos_w);
float L = mainLight.direction;
float N = i.normal_w;
float NdotL = dot(N,L);
float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
color.rgb = NdotL;
return color;
}
ENDHLSL
}
}
}