Skip to content

Instantly share code, notes, and snippets.

@hammanandre
Last active May 21, 2018 23:53
Show Gist options
  • Save hammanandre/fa45dcca873f52b2c7f5d27f8868ab6f to your computer and use it in GitHub Desktop.
Save hammanandre/fa45dcca873f52b2c7f5d27f8868ab6f to your computer and use it in GitHub Desktop.
3D Gradient Noise Vertex Sampled
Shader "Noise/3D Gradient Noise Vertex Sampled"
{
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.localCoord = mul(unity_ObjectToWorld, v.vertex).xyz; //Uncomment for Global vert position
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)
{
float D3Sampled = noise(float3(IN.localCoord.x, IN.localCoord.y, IN.localCoord.z) * _Octaves);
//If Shader feature on remap noise to color ramp
#if defined(_RampTex_Map)
half4 color = tex2D(_RampTex, float2(saturate(D3Sampled), 0.5));
o.Albedo = color.rgb;
o.Alpha = color.a;
#else
o.Albedo = float3(D3Sampled, D3Sampled, D3Sampled);
o.Alpha = 1;
#endif
}
ENDCG
}
}
@hammanandre
Copy link
Author

3dgradientnoisevertexsampled

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