Skip to content

Instantly share code, notes, and snippets.

@Agoxandr
Last active July 18, 2019 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Agoxandr/ea50c4f442b17af4074d1e2492d53b4c to your computer and use it in GitHub Desktop.
Save Agoxandr/ea50c4f442b17af4074d1e2492d53b4c to your computer and use it in GitHub Desktop.
Surface shader with ripples and drips. https://youtu.be/igxwtovIlO0
#pragma once
half heightBlend(half h1, half h2, half slope, half contrast)
{
h2 = 1 - h2;
return saturate((saturate((slope - min(h1, h2)) / max(abs(h1 - h2), 0.1)) - contrast) / max(1.0 - contrast, 0.1));
//half tween = saturate((slope - min(h1, h2)) / max(abs(h1 - h2), 0.1));
//half threshold = contrast;
//half width = 1.0 - contrast;
//return saturate((tween - threshold) / max(width, 0.1));
}
Shader "Custom/Standard MOHS" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
[NoScaleOffset] _MetallicGlossMap ("Metallic (R), AO (G), Height (B), Smoothness (A)", 2D) = "white" {}
[NoScaleOffset] _BumpMap ("Normalmap", 2D) = "bump" {}
_Parallax ("Height", Range(0,1)) = 0.02
[NoScaleOffset] _RainMap ("Rain Map", 2D) = "white" {}
[NoScaleOffset] _RainBumpMap ("Rain Normalmap", 2D) = "bump" {}
[NoScaleOffset] _DripBumpMap ("Drip Normalmap", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
#include<Blending.cginc>
sampler2D _MainTex;
sampler2D _MetallicGlossMap;
sampler2D _BumpMap;
sampler2D _RainMap;
sampler2D _RainBumpMap;
sampler2D _DripBumpMap;
struct Input {
float2 uv_MainTex;
float2 uv_RainMap;
float3 viewDir;
float3 worldPos;
float3 worldNormal;
INTERNAL_DATA
};
fixed4 _Color;
half _Parallax;
float _Global_RainIntensity;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) {
float2 parallax = ParallaxOffset(tex2D (_MetallicGlossMap, IN.uv_MainTex).b, _Parallax, IN.viewDir);
float2 uv0 = (IN.worldPos.xz + parallax) * .1;
float rainMap = tex2D(_RainMap, uv0).r;
float x = frac(_Time.y);
float r = clamp(rainMap - (1.0 - x), 0, 1);
r = clamp(1 - (distance(r, .05) / .05), 0, 1);
r *= -4 * (x - .5) * (x - .5) + 1;
float2 uv1 = (IN.worldPos.xz + float2(.2, .2) + parallax) * .1;
rainMap = tex2D(_RainMap, uv1);
x = frac(_Time.y + .5);
float r1 = clamp(rainMap - (1.0 - x), 0, 1);
r1 = clamp(1 - (distance(r1, .05) / .05), 0, 1);
r1 *= -4 * (x - .5) * (x - .5) + 1;
float interpolation = clamp(abs(sin(_Time.y * .5)), 0, 1);
float col = lerp(r, r1, interpolation);
float2 uv = parallax + IN.uv_MainTex;
fixed4 mohs = tex2D (_MetallicGlossMap, uv);
float3 worldNormal = WorldNormalVector (IN, float3(0, 0, 1));
half drip = clamp(dot(worldNormal, float3(0, 1, 0)) * 4 - 3, 0, 1);
half height = heightBlend(mohs.b, _Global_RainIntensity * .2, _Global_RainIntensity * .6, .6) * drip;
o.Occlusion = mohs.g;
o.Smoothness = clamp(mohs.a + _Global_RainIntensity, 0, 1);
half dripHeight = clamp(abs(dot(worldNormal, float3(0, 0, 1))) * 3 - 1.5, 0, 1) * (tex2D(_RainMap, IN.worldPos.xy + parallax).g * tex2D(_RainMap, float2(IN.worldPos.x, IN.worldPos.y + _Time.x) + parallax).b);
half dripHeight1 = clamp(abs(dot(worldNormal, float3(1, 0, 0))) * 3 - 1.5, 0, 1) * (tex2D(_RainMap, IN.worldPos.zy + parallax).g * tex2D(_RainMap, float2(IN.worldPos.z, IN.worldPos.y + _Time.x + .2) + parallax).b);
half dripMask = dripHeight + dripHeight1;
half s = 10 * _Global_RainIntensity;
float3 n1 = UnpackScaleNormal(tex2D(_DripBumpMap, IN.worldPos.xy + parallax), dripHeight * s);
float3 n2 = UnpackScaleNormal(tex2D(_DripBumpMap, IN.worldPos.zy + parallax), dripHeight1 * s);
n1.xyz*float3(2, 2, -2) + float3(-1, -1, 1);
n2*2 - 1;
float3 n;
n.x = dot(n1.zxx, n2.xyz);
n.y = dot(n1.yzy, n2.xyz);
n.z = dot(n1.xyz, n2.xyz);
n = normalize(n);
half mask = clamp(height + dripMask, 0, 1) * _Global_RainIntensity;
o.Albedo = lerp(tex2D (_MainTex, uv).rgb, fixed3(.05, .05, .1), mask);
o.Metallic = clamp(mohs.r + mask, 0, 1);
o.Normal = normalize(lerp(n, (lerp(UnpackNormal(tex2D(_BumpMap, uv)), lerp(float3(0, 0, 1), lerp(UnpackScaleNormal(tex2D(_RainBumpMap, uv0), 10 * drip), UnpackScaleNormal(tex2D(_RainBumpMap, uv1), 10 * drip), interpolation), col * height), height)), 1 - dripMask));
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment