Skip to content

Instantly share code, notes, and snippets.

@hammanandre
Created May 21, 2018 23:10
Show Gist options
  • Save hammanandre/f4a4b2c2c550e8769499a86d789beb92 to your computer and use it in GitHub Desktop.
Save hammanandre/f4a4b2c2c550e8769499a86d789beb92 to your computer and use it in GitHub Desktop.
Triplanar Mapped Gradient Noise
Shader "Noise/Triplanar RampMapped Noise" {
Properties
{
_Octaves("Octaves", Range(0,100)) = 0.5 // Octaves ish ?NoiseScale perhaps better term
[Toggle(_RampTex_Map)]_RampTexMap("Use Ramp Map", Float) = 0 //float just used for attribute to actually work. No other Purpose
_RampTex("Ramp Texture", 2D) = "white" {}
}
SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard vertex:vert fullforwardshadows
#pragma shader_feature _RampTex_Map
sampler2D _RampTex;
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;
}
half _Octaves;
// 3D Gradient Noise algo from https://www.shadertoy.com/view/Xsl3Dl by http://www.iquilezles.org/
float3 hash(float3 p)
{
p = float3( dot(p, float3(127.1, 311.7, 74.7)),
dot(p, float3(269.5, 183.3, 246.1)),
dot(p, float3(113.5, 271.9, 124.6)));
return -1.0 + 2.0*frac(sin(p)*43758.5453123);
}
float noise(in float3 p)
{
float3 i = floor(p);
float3 f = frac(p);
float3 u = f * f*(3.0 - 2.0*f);
return lerp(lerp(lerp(dot(hash(i + float3(0.0, 0.0, 0.0)), f - float3(0.0, 0.0, 0.0)),
dot(hash(i + float3(1.0, 0.0, 0.0)), f - float3(1.0, 0.0, 0.0)), u.x),
lerp(dot(hash(i + float3(0.0, 1.0, 0.0)), f - float3(0.0, 1.0, 0.0)),
dot(hash(i + float3(1.0, 1.0, 0.0)), f - float3(1.0, 1.0, 0.0)), u.x), u.y),
lerp(lerp(dot(hash(i + float3(0.0, 0.0, 1.0)), f - float3(0.0, 0.0, 1.0)),
dot(hash(i + float3(1.0, 0.0, 1.0)), f - float3(1.0, 0.0, 1.0)), u.x),
lerp(dot(hash(i + float3(0.0, 1.0, 1.0)), f - float3(0.0, 1.0, 1.0)),
dot(hash(i + float3(1.0, 1.0, 1.0)), f - float3(1.0, 1.0, 1.0)), u.x), u.y), u.z);
}
void surf(Input IN, inout SurfaceOutputStandard o) {
// Blending factor of triplanar mapping
float3 bf = normalize(abs(IN.localNormal));
bf /= dot(bf, (float3)1);
// Calculate Triplanar UV mapping
float2 tx = IN.localCoord.yz * _Octaves;
float2 ty = IN.localCoord.zx * _Octaves;
float2 tz = IN.localCoord.xy * _Octaves;
// Average out Base color
float cx = noise(float3(tx.x, tx.y, 0)) * bf.x;
float cy = noise(float3(ty.x, ty.y, 0)) * bf.y;
float cz = noise(float3(tz.x, tz.y, 0)) * bf.z;
float TriPlanarColor = (cx + cy + cz);
//If Shader feature on remap noise to color ramp
#if defined(_RampTex_Map)
half4 color = tex2D(_RampTex, float2(saturate(TriPlanarColor), 0.5));
o.Albedo = color.rgb;
o.Alpha = color.a;
#else
o.Albedo = float3(TriPlanarColor, TriPlanarColor, TriPlanarColor);
o.Alpha = 1;
#endif
}
ENDCG
}
}
@hammanandre
Copy link
Author

triplanarrampmappednoise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment