参考
理论参考
实践参考
学习
perlin噪声

Shader "Unlit/GenerateNoise"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float random (float2 st) {
return frac(sin(dot(st.xy,float2(12.9898,78.233)))*43758.5453123);
}
float hash(float n) {
return frac(sin(n) * 1e4);
}
fixed2 randVec(fixed2 value)
{
fixed2 vec = fixed2(dot(value, fixed2(127.1, 337.1)), dot(value, fixed2(269.5, 183.3)));
vec = -1 + 2 * frac(sin(vec) * 43758.5453123);
return vec;
}
float perlinNoise(float2 uv)
{
float a, b, c, d;
float x0 = floor(uv.x);
float x1 = ceil(uv.x);
float y0 = floor(uv.y);
float y1 = ceil(uv.y);
fixed2 pos = frac(uv);
a = dot(randVec(fixed2(x0, y0)), pos - fixed2(0, 0));
b = dot(randVec(fixed2(x0, y1)), pos - fixed2(0, 1));
c = dot(randVec(fixed2(x1, y1)), pos - fixed2(1, 1));
d = dot(randVec(fixed2(x1, y0)), pos - fixed2(1, 0));
float2 st = 6 * pow(pos, 5) - 15 * pow(pos, 4) + 10 * pow(pos, 3);
a = lerp(a, d, st.x);
b = lerp(b, c, st.x);
a = lerp(a, b, st.y);
return a;
}
float fbm(float2 uv)
{
float f = 0;
float a = 1;
for(int i = 0; i < 3; i++)
{
f += a * perlinNoise(uv);
uv *= 2;
a /= 2;
}
return f;
}
half remap(half x, half t1, half t2, half s1, half s2)
{
return (x - t1) / (t2 - t1) * (s2 - s1) + s1;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
int density=90;
float v=fbm(i.uv*density);
if(v>0.3)
{
v=remap(v,0,1,0.7,1);
}
return saturate(v);
}
ENDCG
}
}
}