Eevee: Transparency: Add hide backside option.
authorClément Foucault <foucault.clem@gmail.com>
Mon, 10 Jul 2017 16:34:33 +0000 (18:34 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Tue, 11 Jul 2017 10:39:35 +0000 (12:39 +0200)
release/scripts/startup/bl_ui/properties_material.py
source/blender/draw/engines/eevee/eevee_materials.c
source/blender/draw/intern/draw_manager.c
source/blender/makesdna/DNA_material_types.h
source/blender/makesrna/intern/rna_material.c

index f008254d9733c02d56ab28d07027e5a58afc8422..88fc2a427344bfd49593e7376584f701e4e423d5 100644 (file)
@@ -1172,6 +1172,9 @@ class EEVEE_MATERIAL_PT_options(MaterialButtonsPanel, Panel):
 
         layout.prop(mat, "blend_method")
 
 
         layout.prop(mat, "blend_method")
 
+        if mat.blend_method not in {"CLIP", "HASHED"}:
+            layout.prop(mat, "blend_hide_backside")
+
         if mat.blend_method == "CLIP":
             layout.prop(mat, "alpha_threshold")
 
         if mat.blend_method == "CLIP":
             layout.prop(mat, "alpha_threshold")
 
index 0681780484d77d6d9e9b4ba89b33f0214de43248..bd6c4b9ccc09387c008ccfbe5c5717c308e12b9f 100644 (file)
@@ -823,7 +823,7 @@ static void material_opaque(
 
 static void material_transparent(
         Material *ma, EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata,
 
 static void material_transparent(
         Material *ma, EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata,
-        bool do_cull, bool use_flat_nor, struct GPUMaterial **gpumat, struct DRWShadingGroup **shgrp)
+        bool do_cull, bool use_flat_nor, struct GPUMaterial **gpumat, struct DRWShadingGroup **shgrp, struct DRWShadingGroup **shgrp_depth)
 {
        const DRWContextState *draw_ctx = DRW_context_state_get();
        Scene *scene = draw_ctx->scene;
 {
        const DRWContextState *draw_ctx = DRW_context_state_get();
        Scene *scene = draw_ctx->scene;
@@ -866,8 +866,14 @@ static void material_transparent(
                DRW_shgroup_uniform_float(*shgrp, "roughness", rough_p, 1);
        }
 
                DRW_shgroup_uniform_float(*shgrp, "roughness", rough_p, 1);
        }
 
-       DRWState cur_state = (do_cull) ? DRW_STATE_CULL_BACK : 0;
-       DRWState all_state = DRW_STATE_CULL_BACK | DRW_STATE_BLEND | DRW_STATE_ADDITIVE | DRW_STATE_MULTIPLY;
+       const bool use_prepass = ((ma->blend_flag & MA_BL_HIDE_BACKSIDE) != 0);
+
+       DRWState all_state = DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_CULL_BACK | DRW_STATE_DEPTH_LESS | DRW_STATE_DEPTH_EQUAL |
+                            DRW_STATE_BLEND | DRW_STATE_ADDITIVE | DRW_STATE_MULTIPLY;
+
+       DRWState cur_state = DRW_STATE_WRITE_COLOR;
+       cur_state |= (use_prepass) ? DRW_STATE_DEPTH_EQUAL : DRW_STATE_DEPTH_LESS;
+       cur_state |= (do_cull) ? DRW_STATE_CULL_BACK : 0;
 
        switch (ma->blend_method) {
                case MA_BM_ADD:
 
        switch (ma->blend_method) {
                case MA_BM_ADD:
@@ -887,6 +893,17 @@ static void material_transparent(
        /* Disable other blend modes and use the one we want. */
        DRW_shgroup_state_disable(*shgrp, all_state);
        DRW_shgroup_state_enable(*shgrp, cur_state);
        /* Disable other blend modes and use the one we want. */
        DRW_shgroup_state_disable(*shgrp, all_state);
        DRW_shgroup_state_enable(*shgrp, cur_state);
+
+       /* Depth prepass */
+       if (use_prepass) {
+               *shgrp_depth = DRW_shgroup_create(e_data.default_prepass_clip_sh, psl->transparent_pass);
+
+               cur_state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
+               cur_state |= (do_cull) ? DRW_STATE_CULL_BACK : 0;
+
+               DRW_shgroup_state_disable(*shgrp_depth, all_state);
+               DRW_shgroup_state_enable(*shgrp_depth, cur_state);
+       }
 }
 
 void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sldata, Object *ob)
 }
 
 void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sldata, Object *ob)
@@ -945,7 +962,7 @@ void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sl
                                case MA_BM_MULTIPLY:
                                case MA_BM_BLEND:
                                        material_transparent(ma, sldata, vedata, do_cull, use_flat_nor,
                                case MA_BM_MULTIPLY:
                                case MA_BM_BLEND:
                                        material_transparent(ma, sldata, vedata, do_cull, use_flat_nor,
-                                               &gpumat_array[i], &shgrp_array[i]);
+                                               &gpumat_array[i], &shgrp_array[i], &shgrp_depth_array[i]);
                                        break;
                                default:
                                        BLI_assert(0);
                                        break;
                                default:
                                        BLI_assert(0);
index ef000a4f79106ee96ee2a8d8a4056d79f8774c57..812280319e998b366abc2c00550d1450a63bce91 100644 (file)
@@ -1270,8 +1270,10 @@ typedef struct ZSortData {
 
 static int pass_shgroup_dist_sort(void *thunk, const void *a, const void *b)
 {
 
 static int pass_shgroup_dist_sort(void *thunk, const void *a, const void *b)
 {
-       const DRWCall *call_a = (DRWCall *)((const DRWShadingGroup *)a)->calls.first;
-       const DRWCall *call_b = (DRWCall *)((const DRWShadingGroup *)b)->calls.first;
+       const DRWShadingGroup *shgrp_a = (const DRWShadingGroup *)a;
+       const DRWShadingGroup *shgrp_b = (const DRWShadingGroup *)b;
+       const DRWCall *call_a = (DRWCall *)(shgrp_a)->calls.first;
+       const DRWCall *call_b = (DRWCall *)(shgrp_b)->calls.first;
        const ZSortData *zsortdata = (ZSortData *)thunk;
 
        float tmp[3];
        const ZSortData *zsortdata = (ZSortData *)thunk;
 
        float tmp[3];
@@ -1282,12 +1284,21 @@ static int pass_shgroup_dist_sort(void *thunk, const void *a, const void *b)
 
        if      (a_sq < b_sq) return  1;
        else if (a_sq > b_sq) return -1;
 
        if      (a_sq < b_sq) return  1;
        else if (a_sq > b_sq) return -1;
-       else                  return  0;
+       else {
+               /* If there is a depth prepass put it before */
+               if ((shgrp_a->state_extra & DRW_STATE_WRITE_DEPTH) != 0) {
+                       return -1;
+               }
+               else if ((shgrp_b->state_extra & DRW_STATE_WRITE_DEPTH) != 0) {
+                       return  1;
+               }
+               else return  0;
+       }
 }
 
 /**
 }
 
 /**
- * Sort Shading groups by decreasing Z between
- * the first call object center and a given world space point.
+ * Sort Shading groups by decreasing Z of their first draw call.
+ * This is usefull for order dependant effect such as transparency.
  **/
 void DRW_pass_sort_shgroup_z(DRWPass *pass)
 {
  **/
 void DRW_pass_sort_shgroup_z(DRWPass *pass)
 {
index c7d7ff1e5dbab84bb07995f9741a8606cb9c6e65..4af8ec70b8d605e23ea501959e23c477b703e483 100644 (file)
@@ -213,7 +213,9 @@ typedef struct Material {
 
        /* Transparency */
        float alpha_threshold;
 
        /* Transparency */
        float alpha_threshold;
-       char blend_method, pad6[3];
+       char blend_method;
+       char blend_flag;
+       char pad6[2];
 
        /* image to use for image/uv space, also bake target
         * (not to be used shading/rendering pipeline, this is editor featyure only!). */
 
        /* image to use for image/uv space, also bake target
         * (not to be used shading/rendering pipeline, this is editor featyure only!). */
@@ -505,5 +507,10 @@ enum {
        MA_BM_BLEND,
 };
 
        MA_BM_BLEND,
 };
 
+/* blend_flag */
+enum {
+       MA_BL_HIDE_BACKSIDE =       (1 << 0),
+};
+
 #endif
 
 #endif
 
index 2625c9cf1f3989e2c2a049994ee7fab8651c93f2..1d9302f5d37040dd3cc13ba0440cab3abaca950b 100644 (file)
@@ -1849,6 +1849,12 @@ void RNA_def_material(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Clip Threshold", "A pixel is rendered only if its alpha value is above this threshold");
        RNA_def_property_update(prop, 0, "rna_Material_update");
 
        RNA_def_property_ui_text(prop, "Clip Threshold", "A pixel is rendered only if its alpha value is above this threshold");
        RNA_def_property_update(prop, 0, "rna_Material_update");
 
+       prop = RNA_def_property(srna, "blend_hide_backside", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "blend_flag", MA_BL_HIDE_BACKSIDE);
+       RNA_def_property_ui_text(prop, "Hide Backside" , "Limit transparency to a single layer "
+                                                        "(avoids transparency sorting problems)");
+       RNA_def_property_update(prop, 0, "rna_Material_update");
+
        /* For Preview Render */
        prop = RNA_def_property(srna, "preview_render_type", PROP_ENUM, PROP_NONE);
        RNA_def_property_enum_sdna(prop, NULL, "pr_type");
        /* For Preview Render */
        prop = RNA_def_property(srna, "preview_render_type", PROP_ENUM, PROP_NONE);
        RNA_def_property_enum_sdna(prop, NULL, "pr_type");