نور واقعی اشیاء مجازی در یک صحنه

یاد بگیرید که چگونه از تخمین روشنایی در برنامه‌های خودتان استفاده کنید.

پیش‌نیازها

قبل از ادامه، مطمئن شوید که مفاهیم اساسی AR و نحوه پیکربندی یک جلسه ARCore را درک کرده‌اید.

API را یک بار در هر جلسه با حالت مناسب پیکربندی کنید

تخمین روشنایی را یک بار در هر جلسه برای حالتی که می‌خواهید استفاده کنید، پیکربندی کنید.

جاوا

// Configure the session with the Lighting Estimation API in ENVIRONMENTAL_HDR mode.
Config config = session.getConfig();
config.setLightEstimationMode(LightEstimationMode.ENVIRONMENTAL_HDR);
session.configure(config);

// Configure the session with the Lighting Estimation API in AMBIENT_INTENSITY mode.
Config config = session.getConfig();
config.setLightEstimationMode(LightEstimationMode.AMBIENT_INTENSITY);
session.configure(config);

// Configure the session with the Lighting Estimation API turned off.
Config config = session.getConfig();
config.setLightEstimationMode(LightEstimationMode.DISABLED);
session.configure(config);

کاتلین

// Configure the session with the Lighting Estimation API in ENVIRONMENTAL_HDR mode.
Config config = session.config
config.lightEstimationMode = LightEstimationMode.ENVIRONMENTAL_HDR
session.configure(config)

// Configure the session with the Lighting Estimation API in AMBIENT_INTENSITY mode.
Config config = session.config
config.lightEstimationMode = LightEstimationMode.AMBIENT_INTENSITY
session.configure(config)

// Configure the session with the Lighting Estimation API turned off.
Config config = session.config
config.lightEstimationMode = LightEstimationMode.DISABLED
session.configure(config)

حالت ENVIRONMENTAL_HDR را پیکربندی کنید

برای پیکربندی حالت ENVIRONMENTAL_HDR ، تخمین نور برای هر فریم را دریافت کنید، سپس اجزای نورپردازی HDR محیطی مورد نظر خود را دریافت کنید.

جاوا

void update() {
  // Get the current frame.
  Frame frame = session.update();

  // Get the light estimate for the current frame.
  LightEstimate lightEstimate = frame.getLightEstimate();

  // Get intensity and direction of the main directional light from the current light estimate.
  float[] intensity = lightEstimate.getEnvironmentalHdrMainLightIntensity(); // note - currently only out param.
  float[] direction = lightEstimate.getEnvironmentalHdrMainLightDirection();
  app.setDirectionalLightValues(intensity, direction); // app-specific code.

  // Get ambient lighting as spherical harmonics coefficients.
  float[] harmonics = lightEstimate.getEnvironmentalHdrAmbientSphericalHarmonics();
  app.setAmbientSphericalHarmonicsLightValues(harmonics); // app-specific code.

  // Get HDR environmental lighting as a cubemap in linear color space.
  Image[] lightmaps = lightEstimate.acquireEnvironmentalHdrCubeMap();
  for (int i = 0; i < lightmaps.length /*should be 6*/; ++i) {
    app.uploadToTexture(i, lightmaps[i]);  // app-specific code.
  }
}

کاتلین

fun update() {
  // Get the current frame.
  val frame = session.update()

  // Get the light estimate for the current frame.
  val lightEstimate = frame.lightEstimate

  // Get intensity and direction of the main directional light from the current light estimate.
  val intensity = lightEstimate.environmentalHdrMainLightIntensity
  val direction = lightEstimate.environmentalHdrMainLightDirection
  app.setDirectionalLightValues(intensity, direction) // app-specific code.

  // Get ambient lighting as spherical harmonics coefficients.
  val harmonics = lightEstimate.environmentalHdrAmbientSphericalHarmonics
  app.ambientSphericalHarmonicsLightValues = harmonics // app-specific code.

  // Get HDR environmental lighting as a cubemap in linear color space.
  val lightMaps = lightEstimate.acquireEnvironmentalHdrCubeMap();
  for ((index, lightMap) in lightMaps.withIndex()) { // 6 maps total.
    app.uploadToTexture(index, lightMap); // app-specific code.
  }
}

حالت AMBIENT_INTENSITY را پیکربندی کنید

اگر قصد دارید از مؤلفه تصحیح رنگ حالت AMBIENT_INTENSITY استفاده کنید، ابتدا با استفاده مجدد از یک تخصیص مشترک، از تخصیص تصحیح رنگ در هر فریم جلوگیری کنید.

جاوا

 // Avoid allocation on every frame.
float[] colorCorrection = new float[4];

کاتلین

val colorCorrection = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f)

تخمین نور برای هر فریم را دریافت کنید، و سپس مؤلفه‌های شدت محیطی که می‌خواهید استفاده کنید را دریافت کنید.

جاوا

void update() {
  // Get the current frame.
  Frame frame = session.update();

  // Get the light estimate for the current frame.
  LightEstimate lightEstimate = frame.getLightEstimate();

  // Get the pixel intensity of AMBIENT_INTENSITY mode.
  float pixelIntensity = lightEstimate.getPixelIntensity();

  // Read the pixel color correction of AMBIENT_INTENSITY mode into colorCorrection.
  lightEstimate.getColorCorrection(colorCorrection, 0);
}

کاتلین

fun update() {
    // Get the current frame.
  val frame = session.update()

  // Get the light estimate for the current frame.
  val lightEstimate = frame.lightEstimate

  // Get the pixel intensity of AMBIENT_INTENSITY mode.
  val pixelIntensity = lightEstimate.pixelIntensity

  // Read the pixel color correction of AMBIENT_INTENSITY mode into colorCorrection.
  lightEstimate.getColorCorrection(colorCorrection, 0)
}

تضمین صرفه‌جویی در مصرف انرژی با APIهای HDR محیطی

پایستگی انرژی اصلی است که می‌گوید نور منعکس‌شده از یک سطح هرگز شدیدتر از قبل از برخورد به سطح نخواهد بود. این قانون در رندرینگ مبتنی بر فیزیک اعمال می‌شود، اما معمولاً در خطوط لوله رندرینگ قدیمی مورد استفاده در بازی‌های ویدیویی و برنامه‌های تلفن همراه حذف می‌شود.

اگر از یک خط لوله رندر مبتنی بر فیزیک با تخمین نور HDR محیطی استفاده می‌کنید، کافیست مطمئن شوید که از مواد مبتنی بر فیزیک در اشیاء مجازی خود استفاده می‌کنید.

با این حال، اگر از خط لوله فیزیکی استفاده نمی‌کنید، چند گزینه دارید:

  • ایده‌آل‌ترین راه‌حل برای این مشکل، مهاجرت به یک خط لوله فیزیکی است.

  • با این حال، اگر این امکان وجود ندارد، یک راه حل خوب ضرب کردن مقدار آلبدو از یک ماده غیر فیزیکی در ضریب پایستگی انرژی است. این کار می‌تواند حداقل تضمین کند که مدل سایه‌زنی BRDF می‌تواند به مدل فیزیکی تبدیل شود. هر BRDF ضریب متفاوتی دارد -- برای مثال، برای یک بازتاب پراکنده، 1/Pi است.