[Bf-blender-cvs] [e32c22d3a47] greasepencil-object: Add Lock to Focal Plane to Blur VFX

Antonioya noreply at git.blender.org
Tue Apr 3 19:31:54 CEST 2018


Commit: e32c22d3a47da2e690f2b0616cbeed130db4c460
Author: Antonioya
Date:   Tue Apr 3 19:31:13 2018 +0200
Branches: greasepencil-object
https://developer.blender.org/rBe32c22d3a47da2e690f2b0616cbeed130db4c460

Add Lock to Focal Plane to Blur VFX

This optn works only in camera view and defines the blur factor depending of location of the object in depth of field. If the object is on a location inside near and far  depth of filed values, the blur is disabled.

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

M	release/scripts/startup/bl_ui/properties_data_modifier.py
M	source/blender/draw/engines/gpencil/gpencil_depth_of_field.c
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/draw/engines/gpencil/gpencil_vfx.c
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/makesrna/intern/rna_modifier.c
M	source/blender/modifiers/intern/MOD_gpencilblur.c

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

diff --git a/release/scripts/startup/bl_ui/properties_data_modifier.py b/release/scripts/startup/bl_ui/properties_data_modifier.py
index c4fe835de4c..d61b9802501 100644
--- a/release/scripts/startup/bl_ui/properties_data_modifier.py
+++ b/release/scripts/startup/bl_ui/properties_data_modifier.py
@@ -1879,6 +1879,12 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
         col.separator()
         col.prop(md, "samples", text="Samples")
 
+        col.separator()
+        col.prop(md, "use_dof_mode")
+        if md.use_dof_mode:
+            col.prop(md, "coc")
+
+
     def GP_WAVE(self, layout, ob, md):
         row = layout.row(align=True)
         row.prop(md, "orientation", expand=True)
diff --git a/source/blender/draw/engines/gpencil/gpencil_depth_of_field.c b/source/blender/draw/engines/gpencil/gpencil_depth_of_field.c
index c0c8f96d011..a34fb9031fe 100644
--- a/source/blender/draw/engines/gpencil/gpencil_depth_of_field.c
+++ b/source/blender/draw/engines/gpencil/gpencil_depth_of_field.c
@@ -53,6 +53,36 @@ static void gpencil_create_shader_depth_of_field(GPENCIL_e_data *e_data)
 	                                          datatoc_gpencil_dof_frag_glsl, "#define STEP_RESOLVE\n");
 }
 
+/* helper to get near and far depth of filed values */
+void GPENCIL_dof_nearfar(Object *camera, float coc, float nearfar[2])
+{
+	if (camera == NULL) {
+		return;
+	}
+
+	const DRWContextState *draw_ctx = DRW_context_state_get();
+	Scene *scene = draw_ctx->scene;
+	Camera *cam = (Camera *)camera->data;
+
+	float fstop = cam->gpu_dof.fstop;
+	float focus_dist = BKE_camera_object_dof_distance(camera);
+	float focal_len = cam->lens;
+
+	/* this is factor that converts to the scene scale. focal length and sensor are expressed in mm
+	* unit.scale_length is how many meters per blender unit we have. We want to convert to blender units though
+	* because the shader reads coordinates in world space, which is in blender units.
+	* Note however that focus_distance is already in blender units and shall not be scaled here (see T48157). */
+	float scale = (scene->unit.system) ? scene->unit.scale_length : 1.0f;
+	float scale_camera = 0.001f / scale;
+	/* we want radius here for the aperture number  */
+	float aperture_scaled = 0.5f * scale_camera * focal_len / fstop;
+	float focal_len_scaled = scale_camera * focal_len;
+
+	float hyperfocal = (focal_len_scaled * focal_len_scaled) / (aperture_scaled * coc);
+	nearfar[0] = (hyperfocal * focus_dist) / (hyperfocal + focal_len);
+	nearfar[1] = (hyperfocal * focus_dist) / (hyperfocal - focal_len);
+}
+
 /* init depth of field effect */
 int GPENCIL_depth_of_field_init(DrawEngineType *draw_engine_gpencil_type, GPENCIL_e_data *e_data, GPENCIL_Data *vedata, Object *camera)
 {
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index ac1fdb8c779..1cae05abc8c 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -1157,6 +1157,8 @@ static void GPENCIL_render_to_image(void *vedata, RenderEngine *engine, struct R
 
 	/* depth of field */
 	Object *camera = DEG_get_evaluated_object(draw_ctx->depsgraph, RE_GetCamera(engine->re));
+	GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
+	stl->storage->camera = camera; /* save current camera */
 	GPENCIL_depth_of_field_init(&draw_engine_gpencil_type, &e_data, vedata, camera);
 
 	GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index dea47745822..4be9ccdadf6 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -191,6 +191,7 @@ typedef struct GPENCIL_Storage {
 	float dof_bokeh[4];
 	float dof_layer_select[2];
 	int dof_target_size[2];
+	Object *camera; /* camera pointer for render mode */
 	struct GPUTexture *unf_source_buffer;   /* pointer copy */
 } GPENCIL_Storage;
 
@@ -389,5 +390,6 @@ bool gpencil_object_use_vfx(struct Object *ob);
 int GPENCIL_depth_of_field_init(struct DrawEngineType *draw_engine_gpencil_type, struct GPENCIL_e_data *e_data, struct GPENCIL_Data *vedata, struct Object *camera);
 void GPENCIL_depth_of_field_cache_init(struct GPENCIL_e_data *e_data, struct GPENCIL_Data *vedata);
 void GPENCIL_depth_of_field_draw(struct GPENCIL_e_data *e_data, struct GPENCIL_Data *vedata);
+void GPENCIL_dof_nearfar(struct Object *camera, float coc, float nearfar[2]);
 
 #endif /* __GPENCIL_ENGINE_H__ */
diff --git a/source/blender/draw/engines/gpencil/gpencil_vfx.c b/source/blender/draw/engines/gpencil/gpencil_vfx.c
index 8bb93a7c82a..7c36d62b0ed 100644
--- a/source/blender/draw/engines/gpencil/gpencil_vfx.c
+++ b/source/blender/draw/engines/gpencil/gpencil_vfx.c
@@ -32,6 +32,7 @@
 #include "DNA_gpencil_types.h"
 #include "DNA_modifier_types.h"
 #include "DNA_screen_types.h"
+#include "DNA_view3d_types.h"
 
 #include "ED_view3d.h"
 #include "ED_gpencil.h"
@@ -153,12 +154,58 @@ static void DRW_gpencil_vfx_blur(
 
 	GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
 	GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
+	const DRWContextState *draw_ctx = DRW_context_state_get();
+	View3D *v3d = draw_ctx->v3d;
+	RegionView3D *rv3d = draw_ctx->rv3d;
 	DRWShadingGroup *vfx_shgrp;
 	const float *viewport_size = DRW_viewport_size_get();
+
 	stl->vfx[ob_idx].vfx_blur.radius[0] = mmd->radius[0];
 	stl->vfx[ob_idx].vfx_blur.radius[1] = mmd->radius[1] * (viewport_size[1] / viewport_size[0]);
 	stl->vfx[ob_idx].vfx_blur.samples = mmd->samples;
 
+	/* init weight */
+	if (mmd->flag & GP_BLUR_DOF_MODE) {
+		/* viewport and opengl render */
+		Object *camera = NULL;
+		if (rv3d) {
+			if (rv3d->persp == RV3D_CAMOB) {
+				camera = v3d->camera;
+			}
+		}
+		else {
+			camera = stl->storage->camera;
+		}
+		
+		if (camera) {
+			float nearfar[2];
+			GPENCIL_dof_nearfar(camera, mmd->coc, nearfar);
+			float zdepth = stl->g_data->gp_object_cache[ob_idx].zdepth;
+			/* the object is on focus area */
+			if ((zdepth >= nearfar[0]) && (zdepth <= nearfar[1])) {
+				stl->vfx[ob_idx].vfx_blur.radius[0] = 0;
+				stl->vfx[ob_idx].vfx_blur.radius[1] = 0;
+			}
+			else {
+				float f;
+				if (zdepth < nearfar[0]) {
+					f = nearfar[0] - zdepth;
+				}
+				else {
+					f = zdepth - nearfar[1];
+				}
+				stl->vfx[ob_idx].vfx_blur.radius[0] = f;
+				stl->vfx[ob_idx].vfx_blur.radius[1] = f;
+				CLAMP2(&stl->vfx[ob_idx].vfx_blur.radius[0], 0, mmd->radius[0]);
+			}
+		}
+		else {
+			/* if not camera view, the blur is disabled */
+			stl->vfx[ob_idx].vfx_blur.radius[0] = 0;
+			stl->vfx[ob_idx].vfx_blur.radius[1] = 0;
+		}
+	}
+
 	struct Gwn_Batch *vfxquad = DRW_cache_fullscreen_quad_get();
 
 	vfx_shgrp = DRW_shgroup_create(e_data->gpencil_vfx_blur_sh, psl->vfx_blur_pass);
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index ec7fd6d8b71..a6b0bef4a06 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -1925,9 +1925,15 @@ typedef struct GpencilBlurModifierData {
 	ModifierData modifier;
 	int radius[2];
 	int flag;                    /* flags */
-	int samples;
+	int samples;                 /* number of samples */
+	float coc;                   /* circle of confusion */
+	char pad[4];
 } GpencilBlurModifierData;
 
+typedef enum eGpencilBlur_Flag {
+	GP_BLUR_DOF_MODE = (1 << 0)
+} eGpencilBlur_Flag;
+
 typedef struct GpencilWaveModifierData {
 	ModifierData modifier;
 	float amplitude;
diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c
index d77a84eebdc..6fe67acb4f7 100644
--- a/source/blender/makesrna/intern/rna_modifier.c
+++ b/source/blender/makesrna/intern/rna_modifier.c
@@ -5854,8 +5854,21 @@ static void rna_def_modifier_gpencilblur(BlenderRNA *brna)
 	RNA_def_property_int_sdna(prop, NULL, "samples");
 	RNA_def_property_range(prop, 0, 32);
 	RNA_def_property_ui_range(prop, 0, 32, 2, -1);
+	RNA_def_property_int_default(prop, 4);
 	RNA_def_property_ui_text(prop, "Samples", "Number of Blur Samples (zero, disable blur)");
 	RNA_def_property_update(prop, 0, "rna_Modifier_update");
+
+	prop = RNA_def_property(srna, "coc", PROP_FLOAT, PROP_NONE);
+	RNA_def_property_float_sdna(prop, NULL, "coc");
+	RNA_def_property_range(prop, 0.001f, 1.0f);
+	RNA_def_property_float_default(prop, 0.025f);
+	RNA_def_property_ui_text(prop, "Precision", "Define circle of confusion for depth of field");
+	RNA_def_property_update(prop, 0, "rna_Modifier_update");
+
+	prop = RNA_def_property(srna, "use_dof_mode", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "flag", GP_BLUR_DOF_MODE);
+	RNA_def_property_ui_text(prop, "Lock Focal Plane", "Blur using focal plane distance as factor to simulate depth of field effect (only in camera view)");
+	RNA_def_property_update(prop, 0, "rna_Modifier_update");
 }
 
 static void rna_def_modifier_gpencilwave(BlenderRNA *brna)
diff --git a/source/blender/modifiers/intern/MOD_gpencilblur.c b/source/blender/modifiers/intern/MOD_gpencilblur.c
index ddfc443c2cd..bf3bcd5ee86 100644
--- a/source/blender/modifiers/intern/MOD_gpencilblur.c
+++ b/source/blender/modifiers/intern/MOD_gpencilblur.c
@@ -43,6 +43,7 @@ static void initData(ModifierData *md)
 	GpencilBlurModifierData *gpmd = (GpencilBlurModifierData *)md;
 	ARRAY_SET_ITEMS(gpmd->radius, 1, 1);
 	gpmd->samples = 4;
+	gpmd->coc = 0.025f;
 }
 
 ModifierTypeInfo modifierType_GpencilBlur = {



More information about the Bf-blender-cvs mailing list