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

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

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

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

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

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

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

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

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

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

Java

Config config = session.getConfig();

// Check whether the user's device supports the Depth API.
boolean isDepthSupported = session.isDepthModeSupported(Config.DepthMode.AUTOMATIC);
if (isDepthSupported) {
  config.setDepthMode(Config.DepthMode.AUTOMATIC);
}
session.configure(config);

Kotlin

val config = session.config

// Check whether the user's device supports the Depth API.
val isDepthSupported = session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)
if (isDepthSupported) {
  config.depthMode = Config.DepthMode.AUTOMATIC
}
session.configure(config)

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

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

Java

// Retrieve the depth image for the current frame, if available.
Image depthImage = null;
try {
  depthImage = frame.acquireDepthImage16Bits();
  // Use the depth image here.
} catch (NotYetAvailableException e) {
  // This 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.
} finally {
  if (depthImage != null) {
    depthImage.close();
  }
}

Kotlin

// Retrieve the depth image for the current frame, if available.
try {
  frame.acquireDepthImage16Bits().use { depthImage ->
    // Use the depth image here.
  }
} catch (e: NotYetAvailableException) {
  // This 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.
}

वापस की गई इमेज, रॉ इमेज बफ़र उपलब्ध कराती है, जिसे रेंडर किए गए हर ऑब्जेक्ट के जीपीयू पर इस्तेमाल के लिए, फ़्रैगमेंट शेडर में भेजा जा सकता है. यह OPENGL_NORMALIZED_DEVICE_COORDINATES में ओरिएंटेशन में है. इसे Frame.transformCoordinates2d() को कॉल करके 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);

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

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

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

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

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

गहराई वाली इमेज से दूरी निकालें

डेप्थ एपीआई का इस्तेमाल वर्चुअल ऑब्जेक्ट को शामिल करने या डेप्थ डेटा को विज़ुअलाइज़ करने के अलावा, अन्य कामों के लिए करने के लिए, डेप्थ इमेज से जानकारी निकालें.

Java

/** Obtain the depth in millimeters for depthImage at coordinates (x, y). */
public int getMillimetersDepth(Image depthImage, int x, int y) {
  // The depth image has a single plane, which stores depth for each
  // pixel as 16-bit unsigned integers.
  Image.Plane plane = depthImage.getPlanes()[0];
  int byteIndex = x * plane.getPixelStride() + y * plane.getRowStride();
  ByteBuffer buffer = plane.getBuffer().order(ByteOrder.nativeOrder());
  return Short.toUnsignedInt(buffer.getShort(byteIndex));
}

Kotlin

/** Obtain the depth in millimeters for [depthImage] at coordinates ([x], [y]). */
fun getMillimetersDepth(depthImage: Image, x: Int, y: Int): UInt {
  // The depth image has a single plane, which stores depth for each
  // pixel as 16-bit unsigned integers.
  val plane = depthImage.planes[0]
  val byteIndex = x * plane.pixelStride + y * plane.rowStride
  val buffer = plane.buffer.order(ByteOrder.nativeOrder())
  val depthSample = buffer.getShort(byteIndex)
  return depthSample.toUInt()
}

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

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

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

Java

float[] cpuCoordinates = new float[] {cpuCoordinateX, cpuCoordinateY};
float[] textureCoordinates = new float[2];
frame.transformCoordinates2d(
    Coordinates2d.IMAGE_PIXELS,
    cpuCoordinates,
    Coordinates2d.TEXTURE_NORMALIZED,
    textureCoordinates);
if (textureCoordinates[0] < 0 || textureCoordinates[1] < 0) {
  // There are no valid depth coordinates, because the coordinates in the CPU image are in the
  // cropped area of the depth image.
  return null;
}
return new Pair<>(
    (int) (textureCoordinates[0] * depthImage.getWidth()),
    (int) (textureCoordinates[1] * depthImage.getHeight()));

Kotlin

val cpuCoordinates = floatArrayOf(cpuCoordinateX.toFloat(), cpuCoordinateY.toFloat())
val textureCoordinates = FloatArray(2)
frame.transformCoordinates2d(
  Coordinates2d.IMAGE_PIXELS,
  cpuCoordinates,
  Coordinates2d.TEXTURE_NORMALIZED,
  textureCoordinates,
)
if (textureCoordinates[0] < 0 || textureCoordinates[1] < 0) {
  // There are no valid depth coordinates, because the coordinates in the CPU image are in the
  // cropped area of the depth image.
  return null
}
return (textureCoordinates[0] * depthImage.width).toInt() to
  (textureCoordinates[1] * depthImage.height).toInt()

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

Java

float[] textureCoordinates =
    new float[] {
      (float) depthCoordinateX / (float) depthImage.getWidth(),
      (float) depthCoordinateY / (float) depthImage.getHeight()
    };
float[] cpuCoordinates = new float[2];
frame.transformCoordinates2d(
    Coordinates2d.TEXTURE_NORMALIZED,
    textureCoordinates,
    Coordinates2d.IMAGE_PIXELS,
    cpuCoordinates);
return new Pair<>((int) cpuCoordinates[0], (int) cpuCoordinates[1]);

Kotlin

val textureCoordinates =
  floatArrayOf(
    depthCoordinatesX.toFloat() / depthImage.width.toFloat(),
    depthCoordinatesY.toFloat() / depthImage.height.toFloat(),
  )
val cpuCoordinates = FloatArray(2)
frame.transformCoordinates2d(
  Coordinates2d.TEXTURE_NORMALIZED,
  textureCoordinates,
  Coordinates2d.IMAGE_PIXELS,
  cpuCoordinates,
)
return cpuCoordinates[0].toInt() to cpuCoordinates[1].toInt()

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

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

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

Java

// Create a hit test using the Depth API.
List<HitResult> hitResultList = frame.hitTest(tap);
for (HitResult hit : hitResultList) {
  Trackable trackable = hit.getTrackable();
  if (trackable instanceof Plane
      || trackable instanceof Point
      || trackable instanceof DepthPoint) {
    useHitResult(hit);
    break;
  }
}

Kotlin

// Create a hit test using the Depth API.
val hitResult =
  frame
    .hitTest(tap)
    .filter {
      val trackable = it.trackable
      trackable is Plane || trackable is Point || trackable is DepthPoint
    }
    .firstOrNull()
useHitResult(hitResult)

आगे क्या होगा

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