Skip to content

Instantly share code, notes, and snippets.

@andybak
Created March 4, 2021 10:03
Show Gist options
  • Save andybak/a3b086e4ec021188c08a4afa990e988e to your computer and use it in GitHub Desktop.
Save andybak/a3b086e4ec021188c08a4afa990e988e to your computer and use it in GitHub Desktop.
// Standard shader with triplanar mapping
// https://github.com/keijiro/StandardTriplanar
Shader "Standard Triplanar Transparent"
{
Properties
{
_Color("", Color) = (1, 1, 1, 1)
_MainTex("", 2D) = "white" {}
_Glossiness("", Range(0, 1)) = 0.5
[Gamma] _Metallic("", Range(0, 1)) = 0
_BumpScale("", Float) = 1
_BumpMap("", 2D) = "bump" {}
_OcclusionStrength("", Range(0, 1)) = 1
_OcclusionMap("", 2D) = "white" {}
_MapScale("", Float) = 1
}
SubShader
{
Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
CGPROGRAM
#pragma surface surf Standard vertex:vert fullforwardshadows addshadow alpha
#pragma shader_feature _NORMALMAP
#pragma shader_feature _OCCLUSIONMAP
#pragma target 3.0
half4 _Color;
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
half _BumpScale;
sampler2D _BumpMap;
half _OcclusionStrength;
sampler2D _OcclusionMap;
half _MapScale;
struct Input
{
float3 localCoord;
float3 localNormal;
};
void vert(inout appdata_full v, out Input data)
{
UNITY_INITIALIZE_OUTPUT(Input, data);
data.localCoord = v.vertex.xyz;
data.localNormal = v.normal.xyz;
}
void surf(Input IN, inout SurfaceOutputStandard o)
{
// Blending factor of triplanar mapping
float3 bf = normalize(abs(IN.localNormal));
bf /= dot(bf, (float3)1);
// Triplanar mapping
float2 tx = IN.localCoord.yz * _MapScale;
float2 ty = IN.localCoord.zx * _MapScale;
float2 tz = IN.localCoord.xy * _MapScale;
// Base color
half4 cx = tex2D(_MainTex, tx) * bf.x;
half4 cy = tex2D(_MainTex, ty) * bf.y;
half4 cz = tex2D(_MainTex, tz) * bf.z;
half4 color = (cx + cy + cz) * _Color;
o.Albedo = color.rgb;
o.Alpha = color.a;
#ifdef _NORMALMAP
// Normal map
half4 nx = tex2D(_BumpMap, tx) * bf.x;
half4 ny = tex2D(_BumpMap, ty) * bf.y;
half4 nz = tex2D(_BumpMap, tz) * bf.z;
o.Normal = UnpackScaleNormal(nx + ny + nz, _BumpScale);
#endif
#ifdef _OCCLUSIONMAP
// Occlusion map
half ox = tex2D(_OcclusionMap, tx).g * bf.x;
half oy = tex2D(_OcclusionMap, ty).g * bf.y;
half oz = tex2D(_OcclusionMap, tz).g * bf.z;
o.Occlusion = lerp((half4)1, ox + oy + oz, _OcclusionStrength);
#endif
// Misc parameters
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
}
ENDCG
}
FallBack "Diffuse"
CustomEditor "StandardTriplanarInspector"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment