[Bf-blender-cvs] [a57bc75576e] blender2.8: Eevee: Transparency: Add hide backside option.

Clément Foucault noreply at git.blender.org
Tue Jul 11 12:46:13 CEST 2017


Commit: a57bc75576e780797a60e2240a12f49247ca9505
Author: Clément Foucault
Date:   Mon Jul 10 18:34:33 2017 +0200
Branches: blender2.8
https://developer.blender.org/rBa57bc75576e780797a60e2240a12f49247ca9505

Eevee: Transparency: Add hide backside option.

===================================================================

M	release/scripts/startup/bl_ui/properties_material.py
M	source/blender/draw/engines/eevee/eevee_materials.c
M	source/blender/draw/intern/draw_manager.c
M	source/blender/makesdna/DNA_material_types.h
M	source/blender/makesrna/intern/rna_material.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/properties_material.py b/release/scripts/startup/bl_ui/properties_material.py
index f008254d973..88fc2a42734 100644
--- a/release/scripts/startup/bl_ui/properties_material.py
+++ b/release/scripts/startup/bl_ui/properties_material.py
@@ -1172,6 +1172,9 @@ class EEVEE_MATERIAL_PT_options(MaterialButtonsPanel, Panel):
 
         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")
 
diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c
index 0681780484d..bd6c4b9ccc0 100644
--- a/source/blender/draw/engines/eevee/eevee_materials.c
+++ b/source/blender/draw/engines/eevee/eevee_materials.c
@@ -823,7 +823,7 @@ static void material_opaque(
 
 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;
@@ -866,8 +866,14 @@ static void material_transparent(
 		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:
@@ -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);
+
+	/* 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)
@@ -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,
-					        &gpumat_array[i], &shgrp_array[i]);
+					        &gpumat_array[i], &shgrp_array[i], &shgrp_depth_array[i]);
 					break;
 				default:
 					BLI_assert(0);
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index ef000a4f791..812280319e9 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -1270,8 +1270,10 @@ typedef struct ZSortData {
 
 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];
@@ -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;
-	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)
 {
diff --git a/source/blender/makesdna/DNA_material_types.h b/source/blender/makesdna/DNA_material_types.h
index c7d7ff1e5db..4af8ec70b8d 100644
--- a/source/blender/makesdna/DNA_material_types.h
+++ b/source/blender/makesdna/DNA_material_types.h
@@ -213,7 +213,9 @@ typedef struct Material {
 
 	/* 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!). */
@@ -505,5 +507,10 @@ enum {
 	MA_BM_BLEND,
 };
 
+/* blend_flag */
+enum {
+	MA_BL_HIDE_BACKSIDE =       (1 << 0),
+};
+
 #endif
 
diff --git a/source/blender/makesrna/intern/rna_material.c b/source/blender/makesrna/intern/rna_material.c
index 2625c9cf1f3..1d9302f5d37 100644
--- a/source/blender/makesrna/intern/rna_material.c
+++ b/source/blender/makesrna/intern/rna_material.c
@@ -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");
 
+	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");




More information about the Bf-blender-cvs mailing list