-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathlayered_perlin_noise_1d.shader
More file actions
82 lines (64 loc) · 2.04 KB
/
Copy pathlayered_perlin_noise_1d.shader
File metadata and controls
82 lines (64 loc) · 2.04 KB
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
Shader "Tutorial/027_layered_noise/1d" {
Properties {
_CellSize ("Cell Size", Range(0, 2)) = 2
_Roughness ("Roughness", Range(1, 8)) = 3
_Persistance ("Persistance", Range(0, 1)) = 0.4
}
SubShader {
Tags{ "RenderType"="Opaque" "Queue"="Geometry"}
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
#include "Random.cginc"
//global shader variables
#define OCTAVES 4
float _CellSize;
float _Roughness;
float _Persistance;
struct Input {
float3 worldPos;
};
float easeIn(float interpolator){
return interpolator * interpolator * interpolator * interpolator * interpolator;
}
float easeOut(float interpolator){
return 1 - easeIn(1 - interpolator);
}
float easeInOut(float interpolator){
float easeInValue = easeIn(interpolator);
float easeOutValue = easeOut(interpolator);
return lerp(easeInValue, easeOutValue, interpolator);
}
float gradientNoise(float value){
float fraction = frac(value);
float interpolator = easeInOut(fraction);
float previousCellInclination = rand1dTo1d(floor(value)) * 2 - 1;
float previousCellLinePoint = previousCellInclination * fraction;
float nextCellInclination = rand1dTo1d(ceil(value)) * 2 - 1;
float nextCellLinePoint = nextCellInclination * (fraction - 1);
return lerp(previousCellLinePoint, nextCellLinePoint, interpolator);
}
float sampleLayeredNoise(float value){
float noise = 0;
float frequency = 1;
float factor = 1;
[unroll]
for(int i=0; i<OCTAVES; i++){
noise = noise + gradientNoise(value * frequency + i * 0.72354) * factor;
factor *= _Persistance;
frequency *= _Roughness;
}
return noise;
}
void surf (Input i, inout SurfaceOutputStandard o) {
float value = i.worldPos.x / _CellSize;
float noise = sampleLayeredNoise(value);
float dist = abs(noise - i.worldPos.y);
float pixelHeight = fwidth(i.worldPos.y);
float lineIntensity = smoothstep(2*pixelHeight, pixelHeight, dist);
o.Albedo = lerp(1, 0, lineIntensity);
}
ENDCG
}
FallBack "Standard"
}