אובייקטים וירטואליים מציאותיים בסצנה

רוצים לדעת איך משתמשים בהערכת תאורה באפליקציות שלכם?

דרישות מוקדמות

לפני שממשיכים, חשוב לוודא שאתם מבינים את המושגים הבסיסיים של AR ואת האופן שבו מגדירים סשן של ARCore.

הגדרת ה-API פעם אחת בכל סשן, באמצעות המצב המתאים

צריך להגדיר את הערכת התאורה פעם אחת בכל סשן, במצב שבו רוצים להשתמש.

Java

// 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);

Kotlin

// 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 הסביבתיים שבהם רוצים להשתמש.

Java

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.
  }
}

Kotlin

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, קודם כדאי להימנע מהקצאה של תיקון צבע בכל פריים באמצעות שימוש חוזר בהקצאה משותפת.

Java

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

Kotlin

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

בודקים את הערכת התאורה לכל תמונה, ואז משיגים את הרכיבים של עוצמת הסביבה שרוצים להשתמש בהם.

Java

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);
}

Kotlin

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)
}

הקפדה על שימור אנרגיה באמצעות ממשקי HDR API

שימור אנרגיה הוא העיקרון שאור שמשתקף ממשטח לעולם לא יהיה חזק יותר מאשר לפני שהתנגש בפני השטח. הכלל הזה נאכף ברינדור שמבוסס על פיזי, אבל בדרך כלל הוא מושמט מצינורות עיבוד הנתונים מדור קודם במשחקי וידאו ובאפליקציות לנייד.

אם אתם משתמשים בצינור עיבוד נתונים פיזי עם הערכת תאורה סביבתית של HDR, פשוט צריך לוודא שנעשה שימוש בחומרים מבוססי-פיזי באובייקטים הווירטואליים.

עם זאת, אם אתם לא משתמשים בצינור עיבוד נתונים פיזי, יש לכם שתי אפשרויות:

  • הפתרון האידיאלי ביותר לכך הוא לעבור לצינור עיבוד נתונים פיזי.

  • עם זאת, אם זה לא אפשרי, כדאי להכפיל את ערך האלבדו מחומר לא פיזי בגורם שימור אנרגיה. כך לפחות יהיה אפשר להמיר את מודל ההצללה של BRDF לפורמט פיזי. לכל BRDF יש גורם אחר -- לדוגמה, בהשתקפות פיזור החישוב הוא 1/Pi.