Skip to content

Instantly share code, notes, and snippets.

@D4KU
Last active December 6, 2020 19:25
Show Gist options
  • Save D4KU/50c202b7a20c655d699a59b250103fe9 to your computer and use it in GitHub Desktop.
Save D4KU/50c202b7a20c655d699a59b250103fe9 to your computer and use it in GitHub Desktop.
Unity Universal Render Pipeline MatCap shader that also looks nice on flat surfaces.
Shader "Custom/MatCapImproved"
{
Properties
{
[NoScaleOffset] _MainTex("MatCap", 2D) = "black" {}
_ViewDirBlend("View direction blend", Range(0,1)) = 0
}
SubShader
{
Tags
{
"RenderType" = "Opaque"
"RenderPipeline" = "UniversalRenderPipeline"
}
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
real _ViewDirBlend;
struct Attributes
{
real3 positionOS : POSITION;
real3 normalOS : NORMAL;
};
struct Varyings
{
real4 positionHCS : SV_POSITION;
real2 uv : TEXCOORD0;
};
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS);
real3x3 objectToViewMatrix = (real3x3)
mul(GetObjectToWorldMatrix(), GetWorldToViewMatrix());
real3 uvw = IN.normalOS;
//This one line adds the view direction dependency
uvw += SafeNormalize(IN.positionOS) * _ViewDirBlend;
uvw = normalize(mul(objectToViewMatrix, uvw));
//Remap from [-1, 1] to [0, 1]
OUT.uv = uvw.xy * .5 + .5;
return OUT;
}
real4 frag(Varyings IN) : SV_Target
{
return SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
}
ENDHLSL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment