diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index f8406e979c..5cc55704df 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -35,6 +35,7 @@ A new header is inserted each time a *tag* is created.
light's intensity in candela.
- Fixed an issue where some `ShadowOptions` were not being respected when passed to
`LightManager::Builder`.
+- Added a Depth of Field post-processing effect
## v1.5.2
diff --git a/android/filament-android/src/main/cpp/View.cpp b/android/filament-android/src/main/cpp/View.cpp
index d67c934622..f57807f9cb 100644
--- a/android/filament-android/src/main/cpp/View.cpp
+++ b/android/filament-android/src/main/cpp/View.cpp
@@ -252,3 +252,10 @@ Java_com_google_android_filament_View_nSetBlendMode(JNIEnv *, jclass , jlong nat
View* view = (View*) nativeView;
view->setBlendMode((View::BlendMode)blendMode);
}
+
+extern "C" JNIEXPORT void JNICALL
+Java_com_google_android_filament_View_nSetDepthOfFieldOptions(JNIEnv *, jclass ,
+ jlong nativeView, jfloat focusDistance, jfloat blurScale, jboolean enabled) {
+ View* view = (View*) nativeView;
+ view->setDepthOfFieldOptions({ .focusDistance = focusDistance, .blurScale = blurScale, .enabled = (bool)enabled });
+}
diff --git a/android/filament-android/src/main/java/com/google/android/filament/View.java b/android/filament-android/src/main/java/com/google/android/filament/View.java
index be1f7ca734..a1aa3df253 100644
--- a/android/filament-android/src/main/java/com/google/android/filament/View.java
+++ b/android/filament-android/src/main/java/com/google/android/filament/View.java
@@ -69,6 +69,7 @@ public class View {
private FogOptions mFogOptions;
private RenderTarget mRenderTarget;
private BlendMode mBlendMode;
+ private DepthOfFieldOptions mDepthOfFieldOptions;
/**
* Generic Quality Level
@@ -310,6 +311,23 @@ public class View {
public boolean enabled = false;
}
+ /**
+ * Options to control Depth of Field (DoF) effect in the scene
+ *
+ * @see View#setDepthOfFieldOptions
+ */
+ public static class DepthOfFieldOptions {
+
+ /** focus distance in world units */
+ public float focusDistance = 10.0f;
+
+ /** scale factor controlling the amount of blur (values other than 1.0 are not physically correct)*/
+ public float blurScale = 1.0f;
+
+ /** enable or disable Depth of field effect */
+ public boolean enabled = false;
+ };
+
/**
* Structure used to set the color precision for the rendering of a View.
*
@@ -977,6 +995,32 @@ public class View {
}
+ /**
+ * Sets Depth of Field options.
+ *
+ * @param options Options for depth of field effect.
+ * @see #getDepthOfFieldOptions
+ */
+ public void setDepthOfFieldOptions(@NonNull DepthOfFieldOptions options) {
+ mDepthOfFieldOptions = options;
+ nSetDepthOfFieldOptions(getNativeObject(), options.focusDistance, options.blurScale, options.enabled);
+ }
+
+ /**
+ * Gets the Depth of Field options
+ *
+ * @return Depth of Field options currently set.
+ * @see #setDepthOfFieldOptions
+ */
+ @NonNull
+ public DepthOfFieldOptions getDepthOfFieldOptions() {
+ if (mDepthOfFieldOptions == null) {
+ mDepthOfFieldOptions = new DepthOfFieldOptions();
+ }
+ return mDepthOfFieldOptions;
+ }
+
+
public long getNativeObject() {
if (mNativeObject == 0) {
throw new IllegalStateException("Calling method on destroyed View");
@@ -1016,4 +1060,5 @@ public class View {
private static native void nSetBloomOptions(long nativeView, long dirtNativeObject, float dirtStrength, float strength, int resolution, float anamorphism, int levels, int blendMode, boolean threshold, boolean enabled);
private static native void nSetFogOptions(long nativeView, float distance, float maximumOpacity, float height, float heightFalloff, float v, float v1, float v2, float density, float inScatteringStart, float inScatteringSize, boolean fogColorFromIbl, boolean enabled);
private static native void nSetBlendMode(long nativeView, int blendMode);
+ private static native void nSetDepthOfFieldOptions(long nativeView, float focusDistance, float blurScale, boolean enabled);
}
diff --git a/filament/CMakeLists.txt b/filament/CMakeLists.txt
index 380ae0af78..813555f5c5 100644
--- a/filament/CMakeLists.txt
+++ b/filament/CMakeLists.txt
@@ -143,6 +143,8 @@ set(PRIVATE_HDRS
set(MATERIAL_SRCS
src/materials/defaultMaterial.mat
+ src/materials/dof.mat
+ src/materials/dofBlur.mat
src/materials/blitLow.mat
src/materials/blitMedium.mat
src/materials/blitHigh.mat
@@ -226,6 +228,18 @@ add_custom_command(
APPEND
)
+add_custom_command(
+ OUTPUT "${MATERIAL_DIR}/dof.filamat"
+ DEPENDS src/materials/dofUtils.fs
+ APPEND
+)
+
+add_custom_command(
+ OUTPUT "${MATERIAL_DIR}/dofBlur.filamat"
+ DEPENDS src/materials/dofUtils.fs
+ APPEND
+)
+
add_custom_command(
OUTPUT ${RESGEN_OUTPUTS}
COMMAND resgen ${RESGEN_FLAGS} ${MATERIAL_BINS}
diff --git a/filament/include/filament/View.h b/filament/include/filament/View.h
index b6c7ffc9d9..dd9bec1b54 100644
--- a/filament/include/filament/View.h
+++ b/filament/include/filament/View.h
@@ -165,6 +165,15 @@ public:
bool enabled = false; //!< enable or disable fog
};
+ /**
+ * Options to control Depth of Field (DoF) effect in the scene
+ */
+ struct DepthOfFieldOptions {
+ float focusDistance = 10.0f; //!< focus distance in world units
+ float blurScale = 1.0f; //!< a scale factor for the amount of blur
+ bool enabled = false; //!< enable or disable Depth of field effect
+ };
+
/**
* Structure used to set the precision of the color buffer and related quality settings.
*
@@ -485,6 +494,13 @@ public:
*/
void setFogOptions(FogOptions options) noexcept;
+ /**
+ * Enables or disables Depth of Field. Disabled by default.
+ *
+ * @param options options
+ */
+ void setDepthOfFieldOptions(DepthOfFieldOptions options) noexcept;
+
/**
* Queries the bloom options.
*
diff --git a/filament/src/Camera.cpp b/filament/src/Camera.cpp
index 572f470770..c0a0a9e69f 100644
--- a/filament/src/Camera.cpp
+++ b/filament/src/Camera.cpp
@@ -25,6 +25,7 @@
#include
#include