Workbench: Smoke: Fix display
authorClément Foucault <foucault.clem@gmail.com>
Mon, 8 Oct 2018 09:25:24 +0000 (11:25 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Mon, 8 Oct 2018 15:20:09 +0000 (17:20 +0200)
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.

source/blender/blenkernel/intern/smoke.c
source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl
source/blender/draw/engines/workbench/shaders/workbench_volume_vert.glsl
source/blender/draw/engines/workbench/workbench_volume.c
source/blender/draw/modes/object_mode.c
source/blender/gpu/intern/gpu_draw.c

index b3f1cbf6a1f92c2f406d44f8477ebeff4c3889ed..4f41a5514544a3a9c895c2bb524f0fcbabf545d7 100644 (file)
@@ -664,6 +664,7 @@ void smokeModifier_copy(const struct SmokeModifierData *smd, struct SmokeModifie
                tsds->data_depth = sds->data_depth;
                tsds->cache_file_format = sds->cache_file_format;
 
+               tsds->display_thickness = sds->display_thickness;
                tsds->slice_method = sds->slice_method;
                tsds->axis_slice_method = sds->axis_slice_method;
                tsds->slice_per_voxel = sds->slice_per_voxel;
index 1f14e506dcf49b84874592f7396c25c14bc86d72..fb3fcd2a4b6da1a3721576c4037f4a7670ed8192 100644 (file)
@@ -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);
index 90a22d9d02fe224c0751113ab609c69393090531..7ce21c3d5ca7af71068afee2984ff6b106f7b9ba 100644 (file)
@@ -1,5 +1,6 @@
 
 uniform mat4 ModelViewProjectionMatrix;
+uniform vec3 OrcoTexCoFactors[2];
 uniform float slicePosition;
 uniform int sliceAxis; /* -1 is no slice, 0 is X, 1 is Y, 2 is Z. */
 
@@ -23,9 +24,10 @@ void main()
        else {
                localPos = vec3(pos.xy, slicePosition * 2.0 - 1.0);
        }
-
-       gl_Position = ModelViewProjectionMatrix * vec4(localPos, 1.0);
+       vec3 final_pos = localPos;
 #else
-       gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
+       vec3 final_pos = pos;
 #endif
+       final_pos = ((final_pos * 0.5 + 0.5) - OrcoTexCoFactors[0]) / OrcoTexCoFactors[1];
+       gl_Position = ModelViewProjectionMatrix * vec4(final_pos, 1.0);
 }
index ea4152486af16b235c78195e1e08e24b1b86b411..e6563b3113b5f1389375783ba12c15eca55a972c 100644 (file)
@@ -61,7 +61,7 @@ void workbench_volume_engine_free(void)
 
 void workbench_volume_cache_init(WORKBENCH_Data *vedata)
 {
-       vedata->psl->volume_pass = DRW_pass_create("Volumes", DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_PREMUL | DRW_STATE_CULL_FRONT);
+       vedata->psl->volume_pass = DRW_pass_create("Volumes", DRW_STATE_WRITE_COLOR | DRW_STATE_TRANSMISSION | DRW_STATE_CULL_FRONT);
 }
 
 void workbench_volume_cache_populate(WORKBENCH_Data *vedata, Scene *scene, Object *ob, ModifierData *md)
@@ -101,6 +101,7 @@ void workbench_volume_cache_populate(WORKBENCH_Data *vedata, Scene *scene, Objec
 
                DRWShadingGroup *grp = DRW_shgroup_create(e_data.volume_slice_sh, vedata->psl->volume_pass);
                DRW_shgroup_uniform_texture(grp, "densityTexture", sds->tex);
+               DRW_shgroup_uniform_texture(grp, "shadowTexture", sds->tex_shadow);
                DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
                DRW_shgroup_uniform_float_copy(grp, "densityScale", 10.0f * sds->display_thickness);
                DRW_shgroup_uniform_float_copy(grp, "slicePosition", sds->slice_depth);
@@ -120,6 +121,7 @@ void workbench_volume_cache_populate(WORKBENCH_Data *vedata, Scene *scene, Objec
                DRW_shgroup_uniform_vec4(grp, "viewvecs[0]", (float *)wpd->viewvecs, 3);
                DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
                DRW_shgroup_uniform_texture(grp, "densityTexture", sds->tex);
+               DRW_shgroup_uniform_texture(grp, "shadowTexture", sds->tex_shadow);
                DRW_shgroup_uniform_float_copy(grp, "densityScale", 10.0f * sds->display_thickness);
                DRW_shgroup_uniform_int_copy(grp, "samplesLen", max_slices);
                /* TODO FIXME : This step size is in object space but the ray itself
index 9bf5263bacfe8c4a3a6499bb695e03f55abb3012..2901a718d597209aa35884a0c5f2ebc0e175ea19 100644 (file)
@@ -2040,7 +2040,7 @@ static void volumes_free_smoke_textures(void)
         * all viewport in a redraw at least. */
        for (LinkData *link = e_data.smoke_domains.first; link; link = link->next) {
                SmokeModifierData *smd = (SmokeModifierData *)link->data;
-               GPU_free_smoke(smd);
+               GPU_free_smoke_velocity(smd);
        }
        BLI_freelistN(&e_data.smoke_domains);
 }
index 7902c5296aa1c0856c32389032e6bda98e25139c..594a2d6d740819afbe2f2b56cb5166225575766b 100644 (file)
@@ -915,9 +915,10 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres)
                        }
                        /* density only */
                        else {
-                               sds->tex = GPU_texture_create_3D(
-                                       sds->res[0], sds->res[1], sds->res[2],
-                                       GPU_R8, smoke_get_density(sds->fluid), NULL);
+                               sds->tex = GPU_texture_create_nD(
+                                       sds->res[0], sds->res[1], sds->res[2], 3,
+                                       smoke_get_density(sds->fluid),
+                                       GPU_R8, GPU_DATA_FLOAT, 0, true, NULL);
 
                                /* Swizzle the RGBA components to read the Red channel so
                                 * that the shader stay the same for colored and non color
@@ -931,10 +932,11 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres)
                        }
                        sds->tex_flame = (
                                smoke_has_fuel(sds->fluid) ?
-                               GPU_texture_create_3D(
-                                       sds->res[0], sds->res[1], sds->res[2],
-                                       GPU_R8, smoke_get_flame(sds->fluid), NULL) :
-                               NULL);
+                                       GPU_texture_create_nD(
+                                                     sds->res[0], sds->res[1], sds->res[2], 3,
+                                                     smoke_get_flame(sds->fluid),
+                                                     GPU_R8, GPU_DATA_FLOAT, 0, true, NULL) :
+                                       NULL);
                }
                else if (!sds->tex && highres) {
                        /* rgba texture for color + density */
@@ -946,9 +948,10 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres)
                        }
                        /* density only */
                        else {
-                               sds->tex = GPU_texture_create_3D(
-                                       sds->res_wt[0], sds->res_wt[1], sds->res_wt[2],
-                                       GPU_R8, smoke_turbulence_get_density(sds->wt), NULL);
+                               sds->tex = GPU_texture_create_nD(
+                                       sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 3,
+                                       smoke_turbulence_get_density(sds->wt),
+                                       GPU_R8, GPU_DATA_FLOAT, 0, true, NULL);
 
                                /* Swizzle the RGBA components to read the Red channel so
                                 * that the shader stay the same for colored and non color
@@ -962,15 +965,18 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres)
                        }
                        sds->tex_flame = (
                                smoke_turbulence_has_fuel(sds->wt) ?
-                               GPU_texture_create_3D(
-                                       sds->res_wt[0], sds->res_wt[1], sds->res_wt[2],
-                                       GPU_R8, smoke_turbulence_get_flame(sds->wt), NULL) :
-                               NULL);
+                                       GPU_texture_create_nD(
+                                               sds->res[0], sds->res[1], sds->res[2], 3,
+                                               smoke_turbulence_get_flame(sds->wt),
+                                               GPU_R8, GPU_DATA_FLOAT, 0, true, NULL) :
+                                       NULL);
                }
 
-               sds->tex_shadow = GPU_texture_create_3D(
-                       sds->res[0], sds->res[1], sds->res[2],
-                       GPU_R8, sds->shadow, NULL);
+               sds->tex_shadow = GPU_texture_create_nD(
+                                     sds->res[0], sds->res[1], sds->res[2], 3,
+                                     sds->shadow,
+                                     GPU_R8, GPU_DATA_FLOAT, 0, true, NULL);
+
        }
 #else // WITH_SMOKE
        (void)highres;