forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea.shader
94 lines (84 loc) · 1.98 KB
/
area.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Shader "Area"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
Subshader
{
Pass
{
CGPROGRAM
#pragma vertex vertex_shader
#pragma fragment pixel_shader
#pragma target 5.0
sampler2D _MainTex;
struct structure
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
float noise (float3 n)
{
return frac(sin(dot(n, float3(95.43583, 93.323197, 94.993431))) * 65536.32);
}
float perlin_a (float3 n)
{
float3 base = floor(n * 64.0) * 0.015625;
float3 dd = float3(0.015625, 0.0, 0.0);
float a = noise(base);
float b = noise(base + dd.xyy);
float c = noise(base + dd.yxy);
float d = noise(base + dd.xxy);
float3 p = (n - base) * 64.0;
float t = lerp(a, b, p.x);
float tt = lerp(c, d, p.x);
return lerp(t, tt, p.y);
}
float perlin_b (float3 n)
{
float3 base = float3(n.x, n.y, floor(n.z * 64.0) * 0.015625);
float3 dd = float3(0.015625, 0.0, 0.0);
float3 p = (n - base) * 64.0;
float front = perlin_a(base + dd.yyy);
float back = perlin_a(base + dd.yyx);
return lerp(front, back, p.z);
}
float fbm(float3 n)
{
float total = 0.0;
float m1 = 1.0;
float m2 = 0.1;
for (int i = 0; i < 5; i++)
{
total += perlin_b(n * m1) * m2;
m2 *= 2.0;
m1 *= 0.5;
}
return total;
}
float3 heightmap (float3 n)
{
return float3(fbm((5.0 * n) + fbm((5.0 * n) * 3.0 - 1000.0) * 0.05),0,0);
}
structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
{
structure vs;
vs.vertex = UnityObjectToClipPos (vertex);
vs.uv=uv;
return vs;
}
float4 pixel_shader (structure ps) : SV_TARGET
{
float4 fragColor = float4(0,0,0,1);
float color = saturate(float4(float3((heightmap(float3(ps.uv.xy*5.0,_Time.g)*0.02)-1.0)),1.0).r);
if (color<0.5)
fragColor=float4(0,0,0,1.0);
else
fragColor=float4(1,1,1,1.0);
return fragColor;
}
ENDCG
}
}
}