Skip to content

Instantly share code, notes, and snippets.

@runewake2
Created April 2, 2017 19:33
Show Gist options
  • Save runewake2/b849c851a22690255a9a4f1f284f2469 to your computer and use it in GitHub Desktop.
Save runewake2/b849c851a22690255a9a4f1f284f2469 to your computer and use it in GitHub Desktop.
Initial pass at a triplanar tesselated shader as created in this video: https://youtu.be/KCKN44-dMOY
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
Shader "Custom/TriPlanarTesselatedShader" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_Displacement("Displacement", Range(0, 1.0)) = 0.3
_EdgeLength("Edge length", Range(2,50)) = 5
_Phong("Phong Strengh", Range(0,1)) = 0.5
_Size("Size", Float) = 4
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard addshadow fullforwardshadows vertex:vert tessellate:tessEdge tessphong:_Phong nolightmap
#include "Tessellation.cginc"
#pragma target 4.6
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct Input {
float2 uv_MainTex;
float3 worldPos;
float3 worldNormal;
};
float _Phong;
float _EdgeLength;
float4 tessEdge(appdata v0, appdata v1, appdata v2)
{
return UnityEdgeLengthBasedTess(v0.vertex, v1.vertex, v2.vertex, _EdgeLength);
}
sampler2D _DispTex;
float _Displacement;
void vert(inout appdata v) {
float d = tex2Dlod(_DispTex, float4(v.texcoord.xy,0,0)).r * _Displacement;
v.vertex.xyz += v.normal * d;
}
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
fixed4 _Color;
float _Size;
void surf (Input IN, inout SurfaceOutputStandard o) {
float3 vNormalOS = normalize(mul(unity_WorldToObject, float4(IN.worldNormal, 0.0)));
vNormalOS.x = abs(vNormalOS.x);
vNormalOS.y = abs(vNormalOS.y);
vNormalOS.z = abs(vNormalOS.z);
float3 vPositionOS = mul(unity_WorldToObject, float4(IN.worldPos, 0.0));
fixed4 topColor = tex2D(_MainTex, float2(vPositionOS.x, vPositionOS.z) * _Size);
fixed4 forwardColor = tex2D(_MainTex, float2(vPositionOS.y, vPositionOS.x) * _Size);
fixed4 rightColor = tex2D(_MainTex, float2(vPositionOS.z, vPositionOS.y) * _Size);
// Albedo comes from a texture tinted by color
fixed4 c = topColor * vNormalOS.y + forwardColor * vNormalOS.z + rightColor * vNormalOS.x;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment