Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ノーマルベクトルの正規化テスト。
Shader "Debug/SimpleToon"
{
Properties
{
_BaseColor("Base Color", Color) = (1,1,1,1)
_HighColor("High Color", Color) = (1,1,1,1)
_Step("Step", Range(0, 1.0)) = 0.5
[KeywordEnum(ON, OFF)] _Normalize("Normalize", Float) = 0
}
SubShader
{
// SubShader Tags define when and under which conditions a SubShader block or
// a pass is executed.
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
// The HLSL code block. Unity SRP uses the HLSL language.
HLSLPROGRAM
// This line defines the name of the vertex shader.
#pragma vertex vert
// This line defines the name of the fragment shader.
#pragma fragment frag
#pragma shader_feature _NORMALIZE_ON _NORMALIZE_OFF
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float3 normalWS : TEXCOORD0;
float3 positionWS : TEXCOORD1;
};
half4 _BaseColor;
half4 _HighColor;
half _Step;
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);
OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
#ifdef _NORMALIZE_ON
// バーテックスシェーダーから渡された法線情報を正規化する
IN.normalWS = normalize(IN.normalWS);
#endif
// カメラ方向取得
half3 viewDirWS = GetWorldSpaceNormalizeViewDir(IN.positionWS);
// ライト方向取得
half3 lightDirection = GetMainLight().direction;
// カメラ方向とライト方向の中間方向ベクトル取得
half3 halfDirection = normalize(viewDirWS + lightDirection);
// ハイライト強度の計算
half highIntensity = step(_Step, dot(halfDirection, IN.normalWS));
// カラーを算出
half3 customColorRGB = _BaseColor.rgb + (_HighColor.rgb * highIntensity);
half4 customColor = half4(customColorRGB, _BaseColor.a);
return customColor;
}
ENDHLSL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment