diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2018-10-08 12:25:24 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2018-10-08 18:20:09 +0300 |
commit | eea22dd5ef0aaf58066bbff460315c8867741920 (patch) | |
tree | 9b835f732c4395b81e570ddf103d39770da20e92 /source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl | |
parent | ba3ef44a6b57970b278c3a99a7ee00906efa3eb7 (diff) |
Workbench: Smoke: Fix display
Includes the following fixes
- Fix smoke texture creation: data was interpreted as Byte instead of Floats.
- Fix Velocity texture not being free after draw: also was causing crashes.
- Fix display_thickness not being copied during COW.
- Fix Blending and general volume rendering algorithm.
- Add Volume Shadowing support.
Diffstat (limited to 'source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl')
-rw-r--r-- | source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl index 1f14e506dcf..fb3fcd2a4b6 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl @@ -3,9 +3,11 @@ uniform mat4 ProjectionMatrix; uniform mat4 ModelMatrixInverse; uniform mat4 ModelViewMatrixInverse; uniform mat4 ModelMatrix; +uniform vec3 OrcoTexCoFactors[2]; uniform sampler2D depthBuffer; uniform sampler3D densityTexture; +uniform sampler3D shadowTexture; uniform int samplesLen = 256; uniform float stepLength; /* Step length in local space. */ @@ -62,15 +64,23 @@ float line_unit_box_intersect_dist(vec3 lineorigin, vec3 linedirection) void volume_properties(vec3 ls_pos, out vec3 scattering, out float extinction) { - scattering = vec3(0.0); - extinction = 1e-8; - - vec4 density = texture(densityTexture, ls_pos * 0.5 + 0.5); - density.rgb /= density.a; - density *= densityScale; + vec3 co = ls_pos * 0.5 + 0.5; + float shadows = texture(shadowTexture, co).r; + vec4 density = texture(densityTexture, co); /* rgb: color, a: density */ + density.a *= densityScale; + + scattering = density.rgb * density.a; + extinction = max(1e-4, dot(scattering, vec3(0.33333))); + scattering *= shadows * M_PI; +} - scattering = density.rgb; - extinction = max(1e-8, density.a); +void eval_volume_step(inout vec3 Lscat, float extinction, float step_len, out float Tr) +{ + Lscat *= phase_function_isotropic(); + /* Evaluate Scattering */ + Tr = exp(-extinction * step_len); + /* integrate along the current step segment */ + Lscat = (Lscat - Lscat * Tr) / extinction; } #define P(x) ((x + 0.5) * (1.0 / 16.0)) @@ -96,18 +106,15 @@ vec4 volume_integration( vec3 ls_pos = ray_ori + ray_dir * ray_len; vec3 Lscat; - float s_extinction; + float s_extinction, Tr; volume_properties(ls_pos, Lscat, s_extinction); - /* Evaluate Scattering */ - float Tr = exp(-s_extinction * step_len); - /* integrate along the current step segment */ - Lscat = (Lscat - Lscat * Tr) / s_extinction; + eval_volume_step(Lscat, s_extinction, step_len, Tr); /* accumulate and also take into account the transmittance from previous steps */ final_scattering += final_transmittance * Lscat; final_transmittance *= Tr; } - return vec4(final_scattering, 1.0 - final_transmittance); + return vec4(final_scattering, final_transmittance); } void main() @@ -134,15 +141,11 @@ void main() step_len = 1.0 / step_len; vec3 Lscat; - float s_extinction; + float s_extinction, Tr; volume_properties(localPos, Lscat, s_extinction); - /* Evaluate Scattering */ - float Tr = exp(-s_extinction * step_len); - /* integrate along the current step segment */ - Lscat = (Lscat - Lscat * Tr) / s_extinction; - - fragColor = vec4(Lscat, 1.0 - Tr); + eval_volume_step(Lscat, s_extinction, step_len, Tr); + fragColor = vec4(Lscat, Tr); #else vec2 screen_uv = gl_FragCoord.xy / vec2(textureSize(depthBuffer, 0).xy); bool is_persp = ProjectionMatrix[3][3] == 0.0; @@ -156,10 +159,13 @@ void main() vec3 vs_ray_dir = (is_persp) ? (vs_ray_end - vs_ray_ori) : vec3(0.0, 0.0, -1.0); vs_ray_dir /= abs(vs_ray_dir.z); - vec3 ls_ray_dir = mat3(ModelViewMatrixInverse) * vs_ray_dir; + vec3 ls_ray_dir = mat3(ModelViewMatrixInverse) * vs_ray_dir * OrcoTexCoFactors[1] * 2.0; vec3 ls_ray_ori = (ModelViewMatrixInverse * vec4(vs_ray_ori, 1.0)).xyz; vec3 ls_ray_end = (ModelViewMatrixInverse * vec4(vs_ray_end, 1.0)).xyz; + ls_ray_ori = (OrcoTexCoFactors[0] + ls_ray_ori * OrcoTexCoFactors[1]) * 2.0 - 1.0; + ls_ray_end = (OrcoTexCoFactors[0] + ls_ray_end * OrcoTexCoFactors[1]) * 2.0 - 1.0; + /* TODO: Align rays to volume center so that it mimics old behaviour of slicing the volume. */ float dist = line_unit_box_intersect_dist(ls_ray_ori, ls_ray_dir); |