forked from Maxon-Computer/Redshift-OSL-Shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubstanceFlow.osl
159 lines (140 loc) · 3.74 KB
/
SubstanceFlow.osl
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// OSL version Mads Drøschler
// WebGL version by Witek https://www.shadertoy.com/view/ltKyRR)
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// Modified 03.22.2021 by Saul Espinosa for Redshift 3d
#include <vector2.h>
#define vec2 vector2
#define vec3 vector
#include <vector4.h>
#define vec4 vector4
float fract ( float x)
{
return x-floor(x);
}
vec3 random3(vec3 c)
{
float j = 4096.0*sin(dot(c, vec3(17.0, 59.4, 15.0)));
vec3 r;
r[2] = fract(512.0 * j);
j *= 0.125;
r[0] = fract(512.0 * j);
j *= 0.125;
r[1] = fract(512.0 * j);
return r - 0.5;
}
float simplex3d(vec3 p)
{
float F3 = 0.3333333;
float G3 = 0.1666667;
vec3 s = floor(p + dot(p, vec3(F3)));
vec3 x = p - s + dot(s, vec3(G3));
vec3 e = step(vec3(0.0), x - vec3(x[1],x[2],x[0]));
vec3 i1 = e*(1.0 - vec3(e[2],e[0],e[1]));
vec3 i2 = 1.0 - vec3(e[2],e[0],e[1])*(1.0 - e);
vec3 x1 = x - i1 + G3;
vec3 x2 = x - i2 + 2.0*G3;
vec3 x3 = x - 1.0 + 3.0*G3;
vec4 w, d;
w.x = dot(x, x);
w.y = dot(x1, x1);
w.z = dot(x2, x2);
w.w = dot(x3, x3);
w = max(0.6 - w, 0.0);
d.x = dot(random3(s), x);
d.y = dot(random3(s + i1), x1);
d.z = dot(random3(s + i2), x2);
d.w = dot(random3(s + 1.0), x3);
w *= w;
w *= w;
d *= w;
return dot(d, vec4(52.0,52.0,52.0,52.0));
}
float simplex3d_fractal(vec3 m, int steps)
{
float sum = 0.0;
for (int i = 0; i < steps; ++i)
{
float scale = pow(2.0, float(i));
sum += simplex3d(scale * m) / scale;
}
return sum;
}
vec3 flow_texture( vec3 p, float Time, int steps)
{
// animate initial coordinates
vec3 p1 = 0.1 * p + vec3(1.0 + Time * 0.0023, 2.0 - Time * 0.0017, 4.0 + Time * 0.0005);
// distort noise sampling coordinates using the same noise function
vec3 p2 = p + 8.1 * simplex3d_fractal(p1,steps) + 0.5;
vec3 p3 = p2 + 4.13 * simplex3d_fractal(0.5 * p2 + vec3(5.0, 4.0, 8.0 + Time * 0.07),steps) + 0.5;
vec3 ret;
ret[0] = simplex3d_fractal(p3 + vec3(0.0, 0.0, 0.0 + Time * 0.3),steps);
ret[1] = simplex3d_fractal(p3 + vec3(0.0, 0.0, 0.2 + Time * 0.3),steps);
ret[2] = simplex3d_fractal(p3 + vec3(0.0, 0.0, 0.3 + Time * 0.3),steps);
// scale output & map
ret = 0.5 + 0.5 * ret;
ret = smoothstep(vec3(0.15), vec3(0.85), ret);
return ret;
}
shader SubstanceFlow
[[ string help = "Substance Flow Noise",
string label = "Substance Flow" ]]
(
point Po = P
[[
string label = "UVW"
]],
float Time = 0
[[
float slidermin = 0,
float slidermax = 100
]],
int steps = 12
[[
int slidermin = 1,
int slidermax = 25
]],
float scale = 1
[[
float slidermin = 0,
float slidermax = 100
]],
int aces = 0
[[ string widget = "checkBox",
string label = "ACES",
int connectable = 0 ]],
output color outColor = 0,
)
{
// ACES sRGB Transform
matrix aces_tm = matrix(
0.6131, 0.0701, 0.0206, 0,
0.3395, 0.9164, 0.1096, 0,
0.0474, 0.0135, 0.8698, 0,
0, 0, 0, 1);
float iResolution = scale;
int numSamples = 1;
vec3 result = vec3(0.0);
vec2 fragCoord = vec2(Po[0],Po[1]);
// cheap AA
for (int x = 0; x < numSamples; ++x)
{
for (int y = 0; y < numSamples; ++y)
{
vec2 offset = vec2(float(x), float(y)) / float(numSamples);
vec2 uv = (fragCoord + offset) / iResolution;
vec3 p = vec3(uv.x,uv.y, Time*0.001);
result += flow_texture(p * 6.0,Time,steps);
}
}
result /= float(numSamples * numSamples);
vector Out = vec3(sqrt(result));
Out = pow(Out,2.2);
float r = Out[0], g = Out[1], b = Out[2];
// ACES Output
if (aces == 0)
outColor = Out;
else
{
outColor = transform(aces_tm, vector(r,g,b));
}
}