Advertisement
Guest User

Gemstone.shader

a guest
Sep 8th, 2019
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "Toon/Gemstones"
  3. {
  4.     Properties
  5.     {
  6.         [Header(Main)]
  7.         _ToonRamp("Toonramp Color", Color) = (0,0,0,1)
  8.         _MainTex("Main Texture (RGB)", 2D) = "white" {}
  9.  
  10.         [Header(Parallax)]
  11.         _Parallax("Parallax", Range(0,0.5)) = 0.1
  12.         _HeightMap("Height Map", 2D) = "white" {}
  13.         _Normal("Normal Map", 2D) = "bump" {}
  14.  
  15.         [Header(Texture)]      
  16.         _Tint("Texture Tint", Color) = (1,1,0,1)
  17.         _ColorTop("Top Col", Color) = (0,1,1,1)
  18.         _BottomCol("Bottom Col", Color) = (0,1,0,1)
  19.         _Offset("Gradient Offset", Range(-1,1)) = 0.1
  20.         _Scale("Main Texture Scale", Range(0,10)) = 1  
  21.         _Brightness("Texture Brigthness", Range(0,5)) = 1
  22.        
  23.         [Header(Spec and Rim)]
  24.         [Toggle(COLORSPEC)] _COLORSPEC("Colored Specular?", Float) = 0
  25.         _SpecSize("Specular Size", Range(0.2,0.5)) = 0.47
  26.         _RimPower("Rimpower", Range(0,20)) = 0.47
  27.         _SoftRimColor("SoftRim Color", Color) = (0,0,0,1)
  28.         _HardRimColor("HardRim Color", Color) = (0,0,0,1)
  29.         _Brightness2("Colored Spec Brightness", Range(0,30)) = 1
  30.     }
  31.         SubShader
  32.         {
  33.             Tags { "RenderType" = "Opaque" }
  34.             LOD 200
  35.           CGPROGRAM
  36.             // Physically based Standard lighting model, and enable shadows on all light types
  37. #pragma shader_feature COLORSPEC
  38. #pragma surface surf ToonRamp fullforwardshadows vertex:vert
  39. #pragma target 3.5
  40.  
  41.         float4 _ToonRamp;
  42.  
  43.         // custom lighting function that uses a texture ramp based
  44.         // on angle between light direction and normal
  45.     #pragma lighting ToonRamp exclude_path:prepass
  46.         inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  47.         {
  48.     #ifndef USING_DIRECTIONAL_LIGHT
  49.             lightDir = normalize(lightDir);
  50.     #endif
  51.             float d = dot(s.Normal, lightDir);
  52.             float dChange = fwidth(d);
  53.             float3 lightIntensity = smoothstep(0 , dChange + 0.05, d) + (_ToonRamp);
  54.  
  55.             half4 c;
  56.             c.rgb = s.Albedo * _LightColor0.rgb * (lightIntensity) * (atten * 2);      
  57.             c.a = s.Alpha;
  58.             return c;
  59.         }
  60.  
  61.             sampler2D  _MainTex,_HeightMap, _Normal;
  62.             struct Input
  63.             {
  64.                 float2 uv_MainTex;
  65.                 float2 uv_Normal;
  66.                 float3 objPos;
  67.                 float3 lightDir;
  68.                 float3 viewDir2;// something weird happens to viewdir when using parallax, so it's gettin recalculated
  69.                 float3 vertexNormal;
  70.                 float3 viewDir;
  71.  
  72.             };
  73.             float _RimPower;
  74.             float4 _SoftRimColor, _HardRimColor;
  75.             float _Offset, _Brightness2;
  76.             fixed4  _ColorTop, _BottomCol, _Tint;
  77.             float _SpecSize,  _Scale, _Parallax, _Brightness;
  78.  
  79.             void vert(inout appdata_full v, out Input o) {
  80.                 UNITY_INITIALIZE_OUTPUT(Input, o);
  81.                 o.objPos = v.vertex;
  82.                 o.lightDir = WorldSpaceLightDir(v.vertex); // get the worldspace lighting direction
  83.                 o.vertexNormal = mul(unity_ObjectToWorld, v.normal);
  84.                 o.viewDir2 = WorldSpaceViewDir(v.vertex);
  85.             }
  86.  
  87.             void surf(Input IN, inout SurfaceOutput o)
  88.             {
  89.  
  90.                 //parallax
  91.                 float heightTex = tex2D(_HeightMap, IN.uv_MainTex).r;
  92.                 float2 parallaxOffset = ParallaxOffset(heightTex, _Parallax, IN.viewDir);
  93.  
  94.                 // specular
  95.                 half s = dot((IN.vertexNormal), normalize(IN.lightDir + IN.viewDir2))*0.5;
  96.                 s = step(_SpecSize, s);
  97.  
  98.                 // normals
  99.                 o.Normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal + parallaxOffset));
  100.  
  101.                 // tex
  102.                 float4 c = tex2D(_MainTex, (float2(IN.uv_MainTex.xy) - parallaxOffset * _Scale));
  103.  
  104.                 // add extra tint
  105.                 c *= _Tint;
  106.                 c *= _Brightness;
  107.            
  108.                 // rim lighting
  109.                 float Rim = 1.0 - saturate(dot(normalize(o.Normal), normalize(IN.viewDir)));// calculate a soft fresnel based on the view direction and the normals of the object
  110.                 float softRim = pow(Rim, _RimPower);
  111.                 float hardRim = round(softRim);
  112.            
  113.                 float4 softRimColored = softRim * _SoftRimColor;
  114.                 float4 hardRimColored = hardRim * _HardRimColor;
  115.  
  116.                 // lerp colors
  117.                 float4 colors = lerp(_BottomCol, _ColorTop, saturate(IN.objPos.y+ _Offset)) + c;
  118.                
  119.                 // specular and rim emmision
  120. #if COLORSPEC
  121.                 o.Emission = ((s * colors) * _Brightness2) + softRimColored + hardRimColored;
  122. #else
  123.                 o.Emission = s  + softRimColored + hardRimColored;
  124. #endif
  125.                 o.Albedo = colors;
  126.             }
  127.             ENDCG
  128.         }
  129.             FallBack "Diffuse"
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement