diff --git a/android/filament-android/src/main/cpp/View.cpp b/android/filament-android/src/main/cpp/View.cpp index ac04c5326ea..f1eae2c4de6 100644 --- a/android/filament-android/src/main/cpp/View.cpp +++ b/android/filament-android/src/main/cpp/View.cpp @@ -222,6 +222,20 @@ Java_com_google_android_filament_View_nIsFrontFaceWindingInverted(JNIEnv*, return static_cast(view->isFrontFaceWindingInverted()); } +extern "C" JNIEXPORT void JNICALL +Java_com_google_android_filament_View_nSetTransparentPickingEnabled(JNIEnv*, + jclass, jlong nativeView, jboolean enabled) { + View* view = (View*) nativeView; + view->setTransparentPickingEnabled(enabled); +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_google_android_filament_View_nIsTransparentPickingEnabled(JNIEnv*, + jclass, jlong nativeView) { + View* view = (View*) nativeView; + return static_cast(view->isTransparentPickingEnabled()); +} + extern "C" JNIEXPORT void JNICALL Java_com_google_android_filament_View_nSetAmbientOcclusion(JNIEnv*, jclass, jlong nativeView, jint ordinal) { View* view = (View*) nativeView; 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 f300d07f885..0bcd1299b7b 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 @@ -745,6 +745,33 @@ public void setFrontFaceWindingInverted(boolean inverted) { nSetFrontFaceWindingInverted(getNativeObject(), inverted); } + /** + * Returns true if transparent picking is enabled. + * + * @see #setTransparentPickingEnabled + */ + public boolean isTransparentPickingEnabled() { + return nIsTransparentPickingEnabled(getNativeObject()); + } + + /** + * Enables or disables transparent picking. Disabled by default. + * + * When transparent picking is enabled, View::pick() will pick from both + * transparent and opaque renderables. When disabled, View::pick() will only + * pick from opaque renderables. + * + *

+ * Transparent picking will create an extra pass for rendering depth + * from both transparent and opaque renderables. + *

+ * + * @param enabled true enables transparent picking, false disables it. + */ + public void setTransparentPickingEnabled(boolean enabled) { + nSetTransparentPickingEnabled(getNativeObject(), enabled); + } + /** * Sets options relative to dynamic lighting for this view. * @@ -1281,6 +1308,8 @@ void clearNativeObject() { private static native boolean nIsPostProcessingEnabled(long nativeView); private static native void nSetFrontFaceWindingInverted(long nativeView, boolean inverted); private static native boolean nIsFrontFaceWindingInverted(long nativeView); + private static native void nSetTransparentPickingEnabled(long nativeView, boolean enabled); + private static native boolean nIsTransparentPickingEnabled(long nativeView); private static native void nSetAmbientOcclusion(long nativeView, int ordinal); private static native int nGetAmbientOcclusion(long nativeView); private static native void nSetAmbientOcclusionOptions(long nativeView, float radius, float bias, float power, float resolution, float intensity, float bilateralThreshold, int quality, int lowPassFilter, int upsampling, boolean enabled, boolean bentNormals, float minHorizonAngleRad); diff --git a/filament/include/filament/View.h b/filament/include/filament/View.h index 139dcac7f40..5c258e5107c 100644 --- a/filament/include/filament/View.h +++ b/filament/include/filament/View.h @@ -668,6 +668,26 @@ class UTILS_PUBLIC View : public FilamentAPI { */ bool isFrontFaceWindingInverted() const noexcept; + /** + * Enables or disables transparent picking. Disabled by default. + * + * When transparent picking is enabled, View::pick() will pick from both + * transparent and opaque renderables. When disabled, View::pick() will only + * pick from opaque renderables. + * + * @param enabled true enables transparent picking, false disables it. + * + * @note Transparent picking will create an extra pass for rendering depth + * from both transparent and opaque renderables. + */ + void setTransparentPickingEnabled(bool enabled) noexcept; + + /** + * Returns true if transparent picking is enabled. + * See setTransparentPickingEnabled() for more information. + */ + bool isTransparentPickingEnabled() const noexcept; + /** * Enables use of the stencil buffer. * diff --git a/filament/src/View.cpp b/filament/src/View.cpp index 0d5d3aa61ca..7bd4f718ad5 100644 --- a/filament/src/View.cpp +++ b/filament/src/View.cpp @@ -185,6 +185,14 @@ bool View::isFrontFaceWindingInverted() const noexcept { return downcast(this)->isFrontFaceWindingInverted(); } +void View::setTransparentPickingEnabled(bool enabled) noexcept { + downcast(this)->setTransparentPickingEnabled(enabled); +} + +bool View::isTransparentPickingEnabled() const noexcept { + return downcast(this)->isTransparentPickingEnabled(); +} + void View::setDynamicLightingOptions(float zLightNear, float zLightFar) noexcept { downcast(this)->setDynamicLightingOptions(zLightNear, zLightFar); } diff --git a/filament/src/details/Engine.cpp b/filament/src/details/Engine.cpp index e62a94d0307..a6c5f2eead8 100644 --- a/filament/src/details/Engine.cpp +++ b/filament/src/details/Engine.cpp @@ -835,8 +835,9 @@ FMaterialInstance* FEngine::createMaterialInstance(const FMaterial* material, return p; } -FMaterialInstance* FEngine::createMaterialInstance(const FMaterial* material) noexcept { - FMaterialInstance* p = mHeapAllocator.make(*this, material); +FMaterialInstance* FEngine::createMaterialInstance(const FMaterial* material, + const char* name) noexcept { + FMaterialInstance* p = mHeapAllocator.make(*this, material, name); if (UTILS_UNLIKELY(p)) { // should never happen auto pos = mMaterialInstances.emplace(material, "MaterialInstance"); pos.first->second.insert(p); diff --git a/filament/src/details/Engine.h b/filament/src/details/Engine.h index 331f934d993..272bafd4db9 100644 --- a/filament/src/details/Engine.h +++ b/filament/src/details/Engine.h @@ -303,7 +303,8 @@ class FEngine : public Engine { FMaterialInstance* createMaterialInstance(const FMaterial* material, const FMaterialInstance* other, const char* name) noexcept; - FMaterialInstance* createMaterialInstance(const FMaterial* material) noexcept; + FMaterialInstance* createMaterialInstance(const FMaterial* material, + const char* name) noexcept; FScene* createScene() noexcept; FView* createView() noexcept; diff --git a/filament/src/details/Material.cpp b/filament/src/details/Material.cpp index d60087c4bd4..90f65b64013 100644 --- a/filament/src/details/Material.cpp +++ b/filament/src/details/Material.cpp @@ -462,13 +462,13 @@ FMaterialInstance* FMaterial::createInstance(const char* name) const noexcept { return FMaterialInstance::duplicate(mDefaultMaterialInstance, name); } else { // but if we don't, just create an instance with all the default parameters - return mEngine.createMaterialInstance(this); + return mEngine.createMaterialInstance(this, name); } } FMaterialInstance* FMaterial::getDefaultInstance() noexcept { if (UTILS_UNLIKELY(!mDefaultMaterialInstance)) { - mDefaultMaterialInstance = mEngine.createMaterialInstance(this); + mDefaultMaterialInstance = mEngine.createMaterialInstance(this, mName.c_str()); mDefaultMaterialInstance->setDefaultInstance(true); } return mDefaultMaterialInstance; diff --git a/filament/src/details/MaterialInstance.cpp b/filament/src/details/MaterialInstance.cpp index afc02418aa4..4bbf44eddfa 100644 --- a/filament/src/details/MaterialInstance.cpp +++ b/filament/src/details/MaterialInstance.cpp @@ -56,7 +56,8 @@ namespace filament { using namespace backend; -FMaterialInstance::FMaterialInstance(FEngine& engine, FMaterial const* material) noexcept +FMaterialInstance::FMaterialInstance(FEngine& engine, FMaterial const* material, + const char* name) noexcept : mMaterial(material), mDescriptorSet(material->getDescriptorSetLayout()), mCulling(CullingMode::BACK), @@ -66,7 +67,8 @@ FMaterialInstance::FMaterialInstance(FEngine& engine, FMaterial const* material) mHasScissor(false), mIsDoubleSided(false), mIsDefaultInstance(false), - mTransparencyMode(TransparencyMode::DEFAULT) { + mTransparencyMode(TransparencyMode::DEFAULT), + mName(name ? CString(name) : material->getName()) { FEngine::DriverApi& driver = engine.getDriverApi(); diff --git a/filament/src/details/MaterialInstance.h b/filament/src/details/MaterialInstance.h index b31c0a65273..0a43831e12a 100644 --- a/filament/src/details/MaterialInstance.h +++ b/filament/src/details/MaterialInstance.h @@ -53,7 +53,8 @@ class FTexture; class FMaterialInstance : public MaterialInstance { public: - FMaterialInstance(FEngine& engine, FMaterial const* material) noexcept; + FMaterialInstance(FEngine& engine, FMaterial const* material, + const char* name) noexcept; FMaterialInstance(FEngine& engine, FMaterialInstance const* other, const char* name); FMaterialInstance(const FMaterialInstance& rhs) = delete; FMaterialInstance& operator=(const FMaterialInstance& rhs) = delete; diff --git a/web/filament-js/filament.d.ts b/web/filament-js/filament.d.ts index 17d874c3528..05b48da1360 100644 --- a/web/filament-js/filament.d.ts +++ b/web/filament-js/filament.d.ts @@ -514,6 +514,8 @@ export class View { public setAntiAliasing(antialiasing: View$AntiAliasing): void; public setStencilBufferEnabled(enabled: boolean): void; public isStencilBufferEnabled(): boolean; + public setTransparentPickingEnabled(enabled: boolean): void; + public isTransparentPickingEnabled(): boolean; } export class TransformManager { diff --git a/web/filament-js/jsbindings.cpp b/web/filament-js/jsbindings.cpp index 7dcdb7c1838..c45c50b35b1 100644 --- a/web/filament-js/jsbindings.cpp +++ b/web/filament-js/jsbindings.cpp @@ -680,6 +680,8 @@ class_("View") .function("setRenderTarget", EMBIND_LAMBDA(void, (View* self, RenderTarget* renderTarget), { self->setRenderTarget(renderTarget); }), allow_raw_pointers()) + .function("setTransparentPickingEnabled", &View::setTransparentPickingEnabled) + .function("isTransparentPickingEnabled", &View::isTransparentPickingEnabled) .function("setStencilBufferEnabled", &View::setStencilBufferEnabled) .function("isStencilBufferEnabled", &View::isStencilBufferEnabled) .function("setMaterialGlobal", &View::setMaterialGlobal)