-
Notifications
You must be signed in to change notification settings - Fork 16
/
demo-DeepOpacity.glsl
291 lines (231 loc) · 6.55 KB
/
demo-DeepOpacity.glsl
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
-- VS
in vec4 Position;
out vec4 vPosition;
uniform mat4 ModelviewProjection;
void main()
{
gl_Position = ModelviewProjection * Position;
vPosition = Position;
}
-- GS
layout(points) in;
layout(triangle_strip, max_vertices = 24) out;
in vec4 vPosition[1];
uniform mat4 ModelviewProjection;
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 Modelview;
vec4 objCube[8]; // Object space coordinate of cube corner
vec4 ndcCube[8]; // Normalized device coordinate of cube corner
ivec4 faces[6]; // Vertex indices of the cube faces
void emit_vert(int vert)
{
gl_Position = ndcCube[vert];
EmitVertex();
}
void emit_face(int face)
{
emit_vert(faces[face][1]); emit_vert(faces[face][0]);
emit_vert(faces[face][3]); emit_vert(faces[face][2]);
EndPrimitive();
}
void main()
{
faces[0] = ivec4(0,1,3,2); faces[1] = ivec4(5,4,6,7);
faces[2] = ivec4(4,5,0,1); faces[3] = ivec4(3,2,7,6);
faces[4] = ivec4(0,3,4,7); faces[5] = ivec4(2,1,6,5);
vec4 P = vPosition[0];
vec4 I = vec4(1,0,0,0);
vec4 J = vec4(0,1,0,0);
vec4 K = vec4(0,0,1,0);
objCube[0] = P+K+I+J; objCube[1] = P+K+I-J;
objCube[2] = P+K-I-J; objCube[3] = P+K-I+J;
objCube[4] = P-K+I+J; objCube[5] = P-K+I-J;
objCube[6] = P-K-I-J; objCube[7] = P-K-I+J;
// Transform the corners of the box:
for (int vert = 0; vert < 8; vert++)
ndcCube[vert] = ModelviewProjection * objCube[vert];
// Emit the six faces:
for (int face = 0; face < 6; face++)
emit_face(face);
}
-- FS
out vec4 FragColor;
uniform sampler3D Density;
uniform sampler3D LightCache;
uniform float Absorption = 10.0;
uniform mat4 Modelview;
uniform float FocalLength;
uniform vec2 WindowSize;
uniform vec3 RayOrigin;
uniform vec3 Ambient = vec3(0.15, 0.15, 0.20);
uniform float StepSize;
uniform int ViewSamples;
const bool Jitter = false;
float GetDensity(vec3 pos)
{
return 2.0 * texture(Density, pos).x;
}
struct Ray {
vec3 Origin;
vec3 Dir;
};
struct AABB {
vec3 Min;
vec3 Max;
};
bool IntersectBox(Ray r, AABB aabb, out float t0, out float t1)
{
vec3 invR = 1.0 / r.Dir;
vec3 tbot = invR * (aabb.Min-r.Origin);
vec3 ttop = invR * (aabb.Max-r.Origin);
vec3 tmin = min(ttop, tbot);
vec3 tmax = max(ttop, tbot);
vec2 t = max(tmin.xx, tmin.yz);
t0 = max(t.x, t.y);
t = min(tmax.xx, tmax.yz);
t1 = min(t.x, t.y);
return t0 <= t1;
}
float randhash(uint seed, float b)
{
const float InverseMaxInt = 1.0 / 4294967295.0;
uint i=(seed^12345391u)*2654435769u;
i^=(i<<6u)^(i>>26u);
i*=2654435769u;
i+=(i<<5u)^(i>>12u);
return float(b * i) * InverseMaxInt;
}
void main()
{
vec3 rayDirection;
rayDirection.xy = 2.0 * gl_FragCoord.xy / WindowSize - 1.0;
rayDirection.x /= WindowSize.y / WindowSize.x;
rayDirection.z = -FocalLength;
rayDirection = (vec4(rayDirection, 0) * Modelview).xyz;
Ray eye = Ray( RayOrigin, normalize(rayDirection) );
AABB aabb = AABB(vec3(-1), vec3(1));
float tnear, tfar;
IntersectBox(eye, aabb, tnear, tfar);
if (tnear < 0.0) tnear = 0.0;
vec3 rayStart = eye.Origin + eye.Dir * tnear;
vec3 rayStop = eye.Origin + eye.Dir * tfar;
rayStart = 0.5 * (rayStart + 1.0);
rayStop = 0.5 * (rayStop + 1.0);
vec3 pos = rayStart;
vec3 viewDir = normalize(rayStop-rayStart) * StepSize;
float T = 1.0;
vec3 Lo = Ambient;
if (Jitter) {
uint seed = uint(gl_FragCoord.x) * uint(gl_FragCoord.y);
pos += viewDir * (-0.5 + randhash(seed, 1.0));
}
float remainingLength = distance(rayStop, rayStart);
for (int i=0; i < ViewSamples && remainingLength > 0.0;
++i, pos += viewDir, remainingLength -= StepSize) {
float density = GetDensity(pos);
vec3 lightColor = vec3(1);
if (pos.z < 0.1) {
density = 10;
lightColor = 3*Ambient;
} else if (density <= 0.01) {
continue;
}
T *= 1.0 - density * StepSize * Absorption;
if (T <= 0.01)
break;
vec3 Li = lightColor * texture(LightCache, pos).xxx;
Lo += Li * T * density * StepSize;
}
//Lo = 1-Lo;
FragColor.rgb = Lo;
FragColor.a = 1-T;
}
-- Light.Cache
in float gLayer;
out float FragColor;
uniform sampler3D Density;
uniform vec3 LightPosition = vec3(1.0, 1.0, 2.0);
uniform float LightIntensity = 10.0;
uniform float Absorption = 10.0;
uniform float LightStep;
uniform int LightSamples;
uniform float InverseSize;
float GetDensity(vec3 pos)
{
return texture(Density, pos).x;
}
void main()
{
vec3 pos = vec3(InverseSize) * vec3(gl_FragCoord.xy, gLayer);
vec3 lightDir = normalize(LightPosition-pos) * LightStep;
float Tl = 1.0;
vec3 lpos = pos + lightDir;
for (int s = 0; s < LightSamples; ++s) {
float ld = GetDensity(lpos);
Tl *= 1.0 - Absorption * LightStep * ld;
if (Tl <= 0.01)
break;
// Would be faster if this coniditional is replaced with a tighter loop
if (lpos.x < 0 || lpos.y < 0 || lpos.z < 0 ||
lpos.x > 1 || lpos.y > 1 || lpos.z > 1)
break;
lpos += lightDir;
}
float Li = LightIntensity*Tl;
FragColor = Li;
}
-- Light.Blur
in float gLayer;
out float FragColor;
uniform sampler3D Density;
uniform float InverseSize;
uniform float StepSize;
uniform float DensityScale;
float GetDensity(vec3 pos)
{
return texture(Density, pos).x * DensityScale;
}
// This implements a super stupid filter in 3-Space that takes 7 samples.
// A three-pass seperable Gaussian would be way better.
void main()
{
vec3 pos = InverseSize * vec3(gl_FragCoord.xy, gLayer);
float e = StepSize;
float z = e;
float density = GetDensity(pos);
density += GetDensity(pos + vec3(e,e,0));
density += GetDensity(pos + vec3(-e,e,0));
density += GetDensity(pos + vec3(e,-e,0));
density += GetDensity(pos + vec3(-e,-e,0));
density += GetDensity(pos + vec3(0,0,-z));
density += GetDensity(pos + vec3(0,0,+z));
density /= 7;
FragColor = density;
}
-- Fluid.Vertex
in vec4 Position;
out int vInstance;
void main()
{
gl_Position = Position;
vInstance = gl_InstanceID;
}
-- Fluid.PickLayer
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in int vInstance[3];
out float gLayer;
uniform float InverseSize;
void main()
{
gl_Layer = vInstance[0];
gLayer = float(gl_Layer) + 0.5;
gl_Position = gl_in[0].gl_Position;
EmitVertex();
gl_Position = gl_in[1].gl_Position;
EmitVertex();
gl_Position = gl_in[2].gl_Position;
EmitVertex();
EndPrimitive();
}