-
-
Save bingxueyouwu/6ee8f9364e2d21894852b5f960882f01 to your computer and use it in GitHub Desktop.
Unity Universal Render Pipeline MatCap shader that also looks nice on flat surfaces.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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