Skip to content

Instantly share code, notes, and snippets.

@Anthelmed
Created September 6, 2017 13:20
Show Gist options
  • Save Anthelmed/9e333bc0a2b444a1e4cb0ecf1d561ba1 to your computer and use it in GitHub Desktop.
Save Anthelmed/9e333bc0a2b444a1e4cb0ecf1d561ba1 to your computer and use it in GitHub Desktop.
Unity blending shader prototype
Shader "Custom/BlendingSurfaceShader" {
Properties {
[NoScaleOffset] _NoiseTex ("Noise", 2D) = "white" {}
[NoScaleOffset] _BottomDiffuseTex ("Bottom diffuse", 2D) = "white" {}
[NoScaleOffset] [Normal] _BottomNormalTex ("Bottom normal", 2D) = "bump" {}
[NoScaleOffset] _MiddleDiffuseTex ("Middle diffuse", 2D) = "white" {}
[NoScaleOffset] [Normal] _MiddleNormalTex ("Middle normal", 2D) = "bump" {}
[NoScaleOffset] _TopDiffuseTex ("Top diffuse", 2D) = "white" {}
[NoScaleOffset] [Normal] _TopNormalTex ("Top normal", 2D) = "bump" {}
_MiddleThreshold ("Middle threshold", Range (0, 1)) = 0.4
_BlendThreshold ("Blend threshold", Range (0, 1)) = 0.1
_Intensity ("Intensity", Range(0, 5)) = 3.0
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard vertex:vert addshadow
#pragma target 3.0
sampler2D _NoiseTex;
sampler2D _BottomDiffuseTex;
sampler2D _BottomNormalTex;
sampler2D _MiddleDiffuseTex;
sampler2D _MiddleNormalTex;
sampler2D _TopDiffuseTex;
sampler2D _TopNormalTex;
half _MiddleThreshold;
half _BlendThreshold;
half _Intensity;
half _Glossiness;
half _Metallic;
struct Input {
float2 uv_NoiseTex;
float2 uv_BottomDiffuseTex;
float2 uv_MiddleDiffuseTex;
float2 uv_TopDiffuseTex;
};
float map(float value, float low1, float high1, float low2, float high2){
return(low2 + (high2 - low2) * (value - low1) / (high1 - low1));
}
void vert (inout appdata_full v) {
v.vertex.y = tex2Dlod (_NoiseTex, float4(v.texcoord.xy,0,0)) * _Intensity;
v.normal.y = v.vertex.y = tex2Dlod (_NoiseTex, float4(v.texcoord.xy,0,0)) * _Intensity;
}
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 noiseTexColor = tex2D (_NoiseTex, IN.uv_NoiseTex);
fixed4 bottomTexColor = tex2D (_BottomDiffuseTex, IN.uv_BottomDiffuseTex);
fixed4 middleTexColor = tex2D (_MiddleDiffuseTex, IN.uv_MiddleDiffuseTex);
fixed4 topTexColor = tex2D (_TopDiffuseTex, IN.uv_TopDiffuseTex);
float middleBlendedThreshold1 = _MiddleThreshold + _BlendThreshold;
float middleBlendedThreshold2 = _MiddleThreshold - _BlendThreshold;
float blend1 = 0;
float blend2 = 0;
if (noiseTexColor.r < middleBlendedThreshold2) {
blend1 = 0;
} else if (noiseTexColor.r >= middleBlendedThreshold2 && noiseTexColor.r <= middleBlendedThreshold1) {
blend1 = map(noiseTexColor.r, middleBlendedThreshold2, _MiddleThreshold, 0, 1);
} else {
blend1 = 1;
}
if (noiseTexColor.r < middleBlendedThreshold2) {
blend2 = 0;
} else if (noiseTexColor.r >= middleBlendedThreshold2 && noiseTexColor.r <= middleBlendedThreshold1) {
blend2 = map(noiseTexColor.r, middleBlendedThreshold1, _MiddleThreshold, 1, 0);
} else {
blend2 = 1;
}
fixed4 finalColor = (0, 0, 0, 0);
if (noiseTexColor.r < _MiddleThreshold) {
finalColor = lerp(bottomTexColor, middleTexColor, blend1);
} else {
finalColor = lerp(middleTexColor, topTexColor, blend2);
}
o.Albedo = finalColor.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = finalColor.a;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment