PatchQuest

Paper Shader

Dec 15th, 2017
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Paper" {
  2.  
  3.     Properties {
  4.         [HideInInspector]_MainTex ("Base (RGB)", 2D) = "white" {}
  5.         _DetailTex ("Detail", 2D) = "white" {}
  6.  
  7.         _Tint("Colorise Tint", Color) = (1, 1, 1, 0)
  8.         _Alpha("Alpha", Float) = 1
  9.         _Silhouette("Silhouette", Range(0, 1)) = 0
  10.  
  11.         _Hue("Hue Shift", Float) = 0
  12.         _Sat("Saturation", Range(0, 1)) = 1
  13.         _Val("Value", Range(0, 1)) = 1
  14.  
  15.         _WindTime("Wind Time", Float) = 0
  16.         _WindScale("Wind Scale", Float) = 0
  17.  
  18.         _DistortScale("Distort Scale", Float) = 1
  19.     }
  20.  
  21.     SubShader {
  22.         // Configure as a Unity UI sprite
  23.         Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
  24.         ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off
  25.  
  26.         Pass {
  27.             // Basic shader setup
  28.             CGPROGRAM
  29.             #include "UnityCG.cginc"
  30.             #pragma vertex vert
  31.             #pragma fragment frag
  32.             #pragma fragmentoption ARB_precision_hint_fastest
  33.  
  34.             // Distortion multi-comple feature
  35.             #pragma multi_compile DISTORT_OFF DISTORT_ON
  36.            
  37.             // Define vertex shader input struct
  38.             struct vertexInput {
  39.                 float4 vertex   : POSITION;
  40.                 float4 color    : COLOR;
  41.                 float2 texcoord : TEXCOORD0;
  42.             };
  43.  
  44.             // Define fragment shader input struct
  45.             struct fragmentInput {
  46.                 half2 texcoord  : TEXCOORD0;
  47.                 float4 vertex   : SV_POSITION;
  48.                 fixed4 color    : COLOR;
  49.                 float3 worldPos : TEXCOORD1;
  50.                 float3 worldOrigin : TEXCOORD3;
  51.             };
  52.  
  53.             // Declare variables
  54.             sampler2D _MainTex;
  55.             sampler2D _DetailTex;
  56.             float4 _MainTex_TexelSize;
  57.             float _Hue;
  58.             float _Sat;
  59.             float _Val;
  60.             float4 _Tint;
  61.             fixed _Alpha;
  62.             float _Silhouette;
  63.  
  64.             float _WindTime;
  65.             float _WindScale;
  66.  
  67.             float _DistortScale;
  68.  
  69.             // Colorisation helper method
  70.             float3 colorise(float3 RGB, float4 target) {
  71.                 float grayscale = (RGB.r + RGB.g + RGB.b) / 3;
  72.                 float3 colorised = float3(target.r * grayscale, target.g * grayscale, target.b * grayscale);
  73.                 return lerp(RGB, colorised, target.a);
  74.             }
  75.  
  76.             // Linear interpolation helper method
  77.             float3 lerp(float3 input, float3 target, float amount) {
  78.                 float3 lerped;
  79.                 lerped.r = (input.r + (target.r - input.r) * amount);
  80.                 lerped.g = (input.g + (target.g - input.g) * amount);
  81.                 lerped.b = (input.b + (target.b - input.b) * amount);
  82.                 return lerped;
  83.             }
  84.  
  85.             // HSV shift helper method
  86.             float3 shift_col(float3 RGB, float3 shift) {
  87.                 float3 RESULT = float3(RGB);
  88.                 float a1= shift.z*shift.y;
  89.                 float a2= shift.x*3.14159265/180;
  90.                 float VSU = a1*cos(a2);
  91.                 float VSW = a1*sin(a2);
  92.  
  93.                 RESULT.x = (.299*shift.z+.701*VSU+.168*VSW)*RGB.x
  94.                 + (.587*shift.z-.587*VSU+.330*VSW)*RGB.y
  95.                 + (.114*shift.z-.114*VSU-.497*VSW)*RGB.z;
  96.  
  97.                 RESULT.y = (.299*shift.z-.299*VSU-.328*VSW)*RGB.x
  98.                 + (.587*shift.z+.413*VSU+.035*VSW)*RGB.y
  99.                 + (.114*shift.z-.114*VSU+.292*VSW)*RGB.z;
  100.  
  101.                 RESULT.z = (.299*shift.z-.3*VSU+1.25*VSW)*RGB.x
  102.                 + (.587*shift.z-.588*VSU-1.05*VSW)*RGB.y
  103.                 + (.114*shift.z+.886*VSU-.203*VSW)*RGB.z;
  104.  
  105.                 return (RESULT);
  106.             }
  107.  
  108.             // Silhouette helper method
  109.             float3 silhouette(float3 RGB, float3 amount) {
  110.                 float3 RESULT = float3(RGB);
  111.                 RESULT.x = RESULT.x + amount;
  112.                 RESULT.y = RESULT.y + amount;
  113.                 RESULT.z = RESULT.z + amount;
  114.                 return RESULT;
  115.             }
  116.  
  117.             // Vertex shader
  118.             fragmentInput vert(vertexInput IN) {
  119.  
  120.                 // Copy basic data over
  121.                 fragmentInput OUT;
  122.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
  123.                 OUT.texcoord = IN.texcoord;
  124.                 OUT.color = IN.color;
  125.  
  126.                 // Calculate extra location data
  127.                 OUT.worldPos = mul(unity_ObjectToWorld, IN.vertex).xyz;
  128.                 OUT.worldOrigin = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
  129.  
  130.                 // Apply wind
  131.                 fixed yVal = (OUT.worldPos.y - 0.2) * .005;
  132.                 fixed sinOffset = OUT.worldPos.x * 0.5 + OUT.worldPos.z * 0.5;
  133.                 OUT.vertex.x += _WindScale * (sin (_WindTime + sinOffset) * yVal);
  134.  
  135.                 return OUT;
  136.             }
  137.  
  138.             // Fragment shader
  139.             float4 frag (fragmentInput i) : COLOR {
  140.  
  141.                 // Find the source color
  142.                 fixed2 coord = i.texcoord;
  143.  
  144.                 #if DISTORT_ON
  145.                     // Apply distortion
  146.                     float groundSmoothing = min(i.worldPos.y, 1);
  147.                     float distortOffset = i.worldPos.y * 3 + _Time * 20 * _DistortScale;
  148.                     coord.x += groundSmoothing * sin (distortOffset) * 0.03;
  149.                 #endif
  150.  
  151.                 fixed4 c = tex2D(_MainTex, coord) * i.color;
  152.  
  153.                 // Apply paper detail texture
  154.                 fixed2 detailPos = (i.worldPos.xy - i.worldOrigin.xy) * 0.3;
  155.                 detailPos.y += i.worldPos.z * 0.3; // Fixes issue with horizontal sprites
  156.  
  157.                 fixed detailShift = tex2D(_DetailTex, detailPos).r - 0.5;
  158.                 detailShift *= 1.5; // Scale up paper effect
  159.                 c += fixed4(detailShift, detailShift, detailShift, 0);
  160.  
  161.                 // Apply colorisation
  162.                 c.rgb = colorise(c, _Tint);
  163.  
  164.                 // Apply HSV shift
  165.                 float3 shift = float3(_Hue, _Sat, _Val);
  166.                 c.rgb = shift_col(c, shift);
  167.                 c.rgb = silhouette(c, _Silhouette);
  168.  
  169.                 // Don't render any pixels below the floor plane
  170.                 fixed above0 = i.worldPos.y > 0;
  171.                 c.a = c.a*_Alpha*above0;
  172.  
  173.                 return c;
  174.             }
  175.             ENDCG
  176.         }
  177.  
  178.     }
  179.     Fallback "Sprites/Default"
  180.     CustomEditor "ContentShaderInspector"
  181. }
Advertisement
Add Comment
Please, Sign In to add comment