अपने Android NDK ऐप्लिकेशन में 'गहराई' का इस्तेमाल करें

डेप्थ एपीआई, डिवाइस के कैमरे को किसी सीन में मौजूद असल ऑब्जेक्ट के साइज़ और आकार को समझने में मदद करता है. यह डेप्थ इमेज या डेप्थ मैप बनाने के लिए कैमरे का इस्तेमाल करता है. इससे आपके ऐप्लिकेशन में एआर (ऑगमेंटेड रिएलिटी) की हकीकत दिखती है. डेप्थ इमेज से मिली जानकारी का इस्तेमाल करके, वर्चुअल ऑब्जेक्ट को असल दुनिया की चीज़ों के सामने या पीछे सही तरीके से दिखाया जा सकता है. इससे, इमर्सिव और असली लगने वाले उपयोगकर्ता अनुभव मिलते हैं.

गहराई की जानकारी का हिसाब गति से लगाया जाता है और इसे हार्डवेयर डेप्थ सेंसर से मिली जानकारी के साथ जोड़ा जा सकता है. अगर उपलब्ध हो, तो फ़्लाइट के समय (ToF) सेंसर जैसी जानकारी भी इसका इस्तेमाल किया जाता है. डेप्थ एपीआई पर काम करने के लिए, डिवाइस को ToF सेंसर की ज़रूरत नहीं होती.

ज़रूरी शर्तें

आगे बढ़ने से पहले, पक्का करें कि आपने बुनियादी एआर (ऑगमेंटेड रिएलिटी) सिद्धांत और ARCore सेशन को कॉन्फ़िगर करने का तरीका समझ लिया है.

उन डिवाइसों का ऐक्सेस प्रतिबंधित करें जो गहराई से चलने वाले डिवाइसों पर काम करते हैं

अगर आपके ऐप्लिकेशन को डेप्थ एपीआई की ज़रूरत होती है, तो एआर (एआर) अनुभव का एक मुख्य हिस्सा डेप्थ पर निर्भर होता है या ऐप्लिकेशन के उन हिस्सों के लिए कोई बेहतर विकल्प नहीं होता जो डेप्थ का इस्तेमाल करते हैं, तो आपके पास Google Play Store में डेप्थ एपीआई की सुविधा वाले डिवाइस पर अपना ऐप्लिकेशन डिस्ट्रिब्यूशन को सीमित करने का विकल्प है. इसके लिए, एआर (ऑगमेंटेड रिएलिटी) में बताए गए AndroidManifest.xml बदलावों के साथ-साथ, AndroidManifest.xml कोर की इस लाइन को चालू करें

<uses-feature android:name="com.google.ar.core.depth" />

गहराई की सुविधा चालू करें

नए ARCore सेशन में, देखें कि उपयोगकर्ता के डिवाइस पर 'डेंसिटी' सुविधा काम करती है या नहीं. प्रोसेस करने में कम समय लगता है, इसलिए ARCore के साथ काम करने वाले सभी डिवाइस depth API के साथ काम नहीं करते हैं. संसाधनों को सेव करने के लिए, ARCore पर डेप्थ डिफ़ॉल्ट रूप से बंद रहती है. अपने ऐप्लिकेशन में डेप्थ एपीआई का इस्तेमाल करने के लिए, डेप्थ मोड चालू करें.

// Check whether the user's device supports the Depth API.
int32_t is_depth_supported = 0;
ArSession_isDepthModeSupported(ar_session, AR_DEPTH_MODE_AUTOMATIC,
                               &is_depth_supported);

// Configure the session for AR_DEPTH_MODE_AUTOMATIC.
ArConfig* ar_config = NULL;
ArConfig_create(ar_session, &ar_config);
if (is_depth_supported) {
  ArConfig_setDepthMode(ar_session, ar_config, AR_DEPTH_MODE_AUTOMATIC);
}
CHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS);
ArConfig_destroy(ar_config);

ज़्यादा जानकारी वाली इमेज पाएं

मौजूदा फ़्रेम की डेप्थ इमेज देखने के लिए, ArFrame_acquireDepthImage16Bits() को कॉल करें.

// Retrieve the depth image for the current frame, if available.
ArImage* depth_image = NULL;
// If a depth image is available, use it here.
if (ArFrame_acquireDepthImage16Bits(ar_session, ar_frame, &depth_image) !=
    AR_SUCCESS) {
  // No depth image received for this frame.
  // This normally means that depth data is not available yet.
  // Depth data will not be available if there are no tracked
  // feature points. This can happen when there is no motion, or when the
  // camera loses its ability to track objects in the surrounding
  // environment.
  return;
}

वापस की गई इमेज, रॉ इमेज बफ़र उपलब्ध कराती है, जिसे रेंडर किए गए हर ऑब्जेक्ट के जीपीयू पर इस्तेमाल के लिए, फ़्रैगमेंट शेडर में भेजा जा सकता है. यह AR_COORDINATES_2D_OPENGL_NORMALIZED_DEVICE_COORDINATES में ओरिएंटेशन में है. इसे ArFrame_transformCoordinates2d() को कॉल करके AR_COORDINATES_2D_TEXTURE_NORMALIZED में बदला जा सकता है. ऑब्जेक्ट शेडर में डेप्थ इमेज को ऐक्सेस करने के बाद, इन डेप्थ मेज़रमेंट को सीधे ऑक्लूज़न हैंडलिंग के लिए ऐक्सेस किया जा सकता है.

डेप्थ वैल्यू को समझना

देखी गई वास्तविक दुनिया की ज्यामिति पर A और 2D पॉइंट a दिया गया बिंदु, जो गहराई वाली इमेज में एक ही बिंदु को दर्शाता है, a में डेप्थ API से दिया गया मान मुख्य धुरी पर CA की लंबाई के बराबर होता है. इसे कैमरा ऑरिजिन C के मुकाबले, A का z-कोऑर्डिनेट भी कहा जा सकता है. डेप्थ एपीआई के साथ काम करते समय, यह समझना ज़रूरी है कि गहराई के मान किरण CA की लंबाई के नहीं, बल्कि उसका प्रोजेक्शन है.

शेडर में गहराई का इस्तेमाल करें

मौजूदा फ़्रेम के लिए गहराई की जानकारी पार्स करें

स्क्रीन की मौजूदा पोज़िशन की गहराई की जानकारी ऐक्सेस करने के लिए, फ़्रैगमेंट शेडर में DepthGetMillimeters() और DepthGetVisibility() हेल्पर फ़ंक्शन का इस्तेमाल करें. इसके बाद, रेंडर किए गए ऑब्जेक्ट के हिस्सों को चुनिंदा तरीके से छिपाने के लिए, इस जानकारी का इस्तेमाल करें.

// Use DepthGetMillimeters() and DepthGetVisibility() to parse the depth image
// for a given pixel, and compare against the depth of the object to render.
float DepthGetMillimeters(in sampler2D depth_texture, in vec2 depth_uv) {
  // Depth is packed into the red and green components of its texture.
  // The texture is a normalized format, storing millimeters.
  vec3 packedDepthAndVisibility = texture2D(depth_texture, depth_uv).xyz;
  return dot(packedDepthAndVisibility.xy, vec2(255.0, 256.0 * 255.0));
}

// Return a value representing how visible or occluded a pixel is relative
// to the depth image. The range is 0.0 (not visible) to 1.0 (completely
// visible).
float DepthGetVisibility(in sampler2D depth_texture, in vec2 depth_uv,
                         in float asset_depth_mm) {
  float depth_mm = DepthGetMillimeters(depth_texture, depth_uv);

  // Instead of a hard Z-buffer test, allow the asset to fade into the
  // background along a 2 * kDepthTolerancePerMm * asset_depth_mm
  // range centered on the background depth.
  const float kDepthTolerancePerMm = 0.015f;
  float visibility_occlusion = clamp(0.5 * (depth_mm - asset_depth_mm) /
    (kDepthTolerancePerMm * asset_depth_mm) + 0.5, 0.0, 1.0);

 // Use visibility_depth_near to set the minimum depth value. If using
 // this value for occlusion, avoid setting it too close to zero. A depth value
 // of zero signifies that there is no depth data to be found.
  float visibility_depth_near = 1.0 - InverseLerp(
      depth_mm, /*min_depth_mm=*/150.0, /*max_depth_mm=*/200.0);

  // Use visibility_depth_far to set the maximum depth value. If the depth
  // value is too high (outside the range specified by visibility_depth_far),
  // the virtual object may get inaccurately occluded at further distances
  // due to too much noise.
  float visibility_depth_far = InverseLerp(
      depth_mm, /*min_depth_mm=*/7500.0, /*max_depth_mm=*/8000.0);

  const float kOcclusionAlpha = 0.0f;
  float visibility =
      max(max(visibility_occlusion, kOcclusionAlpha),
          max(visibility_depth_near, visibility_depth_far));

  return visibility;
}

वर्चुअल ऑब्जेक्ट को शामिल करें

फ़्रैगमेंट शेडर के शरीर में वर्चुअल ऑब्जेक्ट छिपाएं. ऑब्जेक्ट के ऐल्फ़ा चैनल को उसकी गहराई के आधार पर अपडेट करें. इससे एक रोका हुआ ऑब्जेक्ट रेंडर हो जाएगा.

// Occlude virtual objects by updating the object’s alpha channel based on its depth.
const float kMetersToMillimeters = 1000.0;

float asset_depth_mm = v_ViewPosition.z * kMetersToMillimeters * -1.;

// Compute the texture coordinates to sample from the depth image.
vec2 depth_uvs = (u_DepthUvTransform * vec3(v_ScreenSpacePosition.xy, 1)).xy;

gl_FragColor.a *= DepthGetVisibility(u_DepthTexture, depth_uvs, asset_depth_mm);

दो-पास रेंडरिंग या हर ऑब्जेक्ट, फ़ॉरवर्ड-पास रेंडरिंग का इस्तेमाल करके ओक्लूज़न को रेंडर किया जा सकता है. हर तरीका कितने काम का है, यह इस बात पर निर्भर करता है कि सीन कितना मुश्किल है और कौनसा ऐप्लिकेशन खास तौर पर इस्तेमाल किया जा रहा है.

हर ऑब्जेक्ट, फ़ॉरवर्ड-पास रेंडरिंग

हर ऑब्जेक्ट के हिसाब से, फ़ॉरवर्ड-पास रेंडरिंग से यह पता चलता है कि ऑब्जेक्ट के हर पिक्सल को उसके मटीरियल शेडर में शामिल किया गया है या नहीं. अगर पिक्सल नहीं दिखते हैं, तो आम तौर पर उन्हें ऐल्फ़ा ब्लेंडिंग की मदद से क्लिप किया जाता है. इस वजह से, उपयोगकर्ता के डिवाइस पर उन्हें दिखने लगता है.

दो-पास रेंडरिंग

दो-पास रेंडरिंग का इस्तेमाल करके, पहला पास, सभी वर्चुअल कॉन्टेंट को इंटरमीडियरी बफ़र में रेंडर करता है. दूसरा पास, असल दुनिया की गहराई और वर्चुअल सीन की गहराई के अंतर के आधार पर, वर्चुअल सीन को बैकग्राउंड के साथ ब्लेंड करता है. इस तरीके के लिए, किसी खास ऑब्जेक्ट के शेडर काम की ज़रूरत नहीं होती. साथ ही, फ़ॉरवर्ड-पास तरीके की तुलना में, यह आम तौर पर एक जैसे दिखने वाले नतीजे देता है.

कैमरे की इमेज और डेप्थ इमेज के बीच निर्देशांक बदलना

ArFrame_acquireCameraImage() का इस्तेमाल करके मिली इमेज का आसपेक्ट रेशियो, डेप्थ इमेज से अलग हो सकता है. इस मामले में, डेप्थ इमेज किसी कैमरे की इमेज का हिस्सा होती है और कैमरे की इमेज में मौजूद सभी पिक्सल के लिए, क्वालिटी का सही अनुमान नहीं लगाया जाता.

सीपीयू इमेज पर निर्देशांकों के लिए, डेप्थ इमेज के कोऑर्डिनेट पाने के लिए:

const float cpu_image_coordinates[] = {(float)cpu_coordinate_x,
                                 (float)cpu_coordinate_y};
float texture_coordinates[2];
ArFrame_transformCoordinates2d(
    ar_session, ar_frame, AR_COORDINATES_2D_IMAGE_PIXELS, 1,
    cpu_image_coordinates, AR_COORDINATES_2D_TEXTURE_NORMALIZED,
    texture_coordinates);
if (texture_coordinates[0] < 0 || texture_coordinates[1] < 0) {
  // There are no valid depth coordinates, because the coordinates in the CPU
  // image are in the cropped area of the depth image.
} else {
  int depth_image_width = 0;
  ArImage_getWidth(ar_session, depth_image, &depth_image_width);
  int depth_image_height = 0;
  ArImage_getHeight(ar_session, depth_image, &depth_image_height);

  int depth_coordinate_x = (int)(texture_coordinates[0] * depth_image_width);
  int depth_coordinate_y = (int)(texture_coordinates[1] * depth_image_height);
}

डेप्थ इमेज निर्देशांक के लिए सीपीयू इमेज कोऑर्डिनेट पाने के लिए:

int depth_image_width = 0;
ArImage_getWidth(ar_session, depth_image, &depth_image_width);
int depth_image_height = 0;
ArImage_getHeight(ar_session, depth_image, &depth_image_height);

float texture_coordinates[] = {
    (float)depth_coordinate_x / (float)depth_image_width,
    (float)depth_coordinate_y / (float)depth_image_height};
float cpu_image_coordinates[2];
ArFrame_transformCoordinates2d(
    ar_session, ar_frame, AR_COORDINATES_2D_TEXTURE_NORMALIZED, 1,
    texture_coordinates, AR_COORDINATES_2D_IMAGE_PIXELS,
    cpu_image_coordinates);

int cpu_image_coordinate_x = (int)cpu_image_coordinates[0];
int cpu_image_coordinate_y = (int)cpu_image_coordinates[1];

डेप्थ हिट टेस्ट

हिट टेस्ट की मदद से उपयोगकर्ता, सीन में किसी असल जगह पर चीज़ें डाल सकते हैं. पहले, हिट की जांच सिर्फ़ खोजे गए हवाई जहाज़ों पर की जा सकती थी. साथ ही, जगहों को बड़े, सपाट प्लैटफ़ॉर्म पर सीमित किया जाता था, जैसे कि हरे रंग के Android डिवाइसों से दिखने वाले नतीजे. हिट के ज़्यादा सटीक नतीजे देने के लिए, डेप्थ हिट टेस्ट, आसान और रॉ गहराई की जानकारी का फ़ायदा लेते हैं. ऐसा नॉन-प्लानर और कम टेक्सचर वाले प्लैटफ़ॉर्म पर भी होता है. यह लाल रंग के Androids से दिखाया जाता है.

गहराई से चालू किए गए हिट टेस्ट का इस्तेमाल करने के लिए, ArFrame_hitTest() को कॉल करें और रिटर्न सूची में AR_TRACKABLE_DEPTH_POINT देखें.

// Create a hit test using the Depth API.
ArHitResultList* hit_result_list = NULL;
ArHitResultList_create(ar_session, &hit_result_list);
ArFrame_hitTest(ar_session, ar_frame, hit_x, hit_y, hit_result_list);

int32_t hit_result_list_size = 0;
ArHitResultList_getSize(ar_session, hit_result_list, &hit_result_list_size);

ArHitResult* ar_hit_result = NULL;
for (int32_t i = 0; i < hit_result_list_size; ++i) {
  ArHitResult* ar_hit = NULL;
  ArHitResult_create(ar_session, &ar_hit);
  ArHitResultList_getItem(ar_session, hit_result_list, i, ar_hit);

  ArTrackable* ar_trackable = NULL;
  ArHitResult_acquireTrackable(ar_session, ar_hit, &ar_trackable);
  ArTrackableType ar_trackable_type = AR_TRACKABLE_NOT_VALID;
  ArTrackable_getType(ar_session, ar_trackable, &ar_trackable_type);
  // Creates an anchor if a plane or an oriented point was hit.
  if (AR_TRACKABLE_DEPTH_POINT == ar_trackable_type) {
    // Do something with the hit result.
  }
  ArTrackable_release(ar_trackable);
  ArHitResult_destroy(ar_hit);
}

ArHitResultList_destroy(hit_result_list);

आगे क्या होगा

  • रॉ डेप्थ एपीआई की मदद से, ज़्यादा सटीक सेंसिंग की सुविधा चालू करें.
  • ARCore डेप्थ लैब देखें, जिसमें डेप्थ डेटा को ऐक्सेस करने के अलग-अलग तरीके बताए गए हैं.