ให้แสงวัตถุเสมือนจริงในฉากอย่างสมจริง

ดูวิธีใช้การประมาณแสง ในแอปของคุณเอง

ข้อกำหนดเบื้องต้น

โปรดทำความเข้าใจแนวคิดพื้นฐานของ AR และวิธีกำหนดค่าเซสชัน ARCore ก่อนดำเนินการต่อ

กำหนดค่า API หนึ่งครั้งต่อเซสชันด้วยโหมดที่เหมาะสม

กําหนดค่าการประมาณแสง 1 ครั้งต่อเซสชันสําหรับโหมดที่ต้องการใช้

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

การรับประกันการประหยัดพลังงานด้วย Environmental HDR API

การอนุรักษ์พลังงานเป็นหลักการที่ว่าแสงที่สะท้อนจากพื้นผิวจะไม่มีวันสว่างกว่าก่อนที่จะกระทบพื้นผิว กฎนี้จะ บังคับใช้ในการเรนเดอร์ตามหลักการทางกายภาพ แต่โดยปกติแล้วจะละเว้นจากไปป์ไลน์การเรนเดอร์ เดิมที่ใช้ในวิดีโอเกมและแอปบนอุปกรณ์เคลื่อนที่

หากคุณใช้ไปป์ไลน์การเรนเดอร์ตามหลักฟิสิกส์ที่มีการประมาณแสง HDR สำหรับสภาพแวดล้อม เพียงตรวจสอบว่าใช้วัสดุตามหลักฟิสิกส์ในออบเจ็กต์เสมือน

อย่างไรก็ตาม หากไม่ได้ใช้ไปป์ไลน์ที่อิงตามฟิสิกส์ คุณจะมีตัวเลือก 2 อย่างดังนี้

  • โซลูชันที่เหมาะที่สุดสำหรับปัญหานี้คือการย้ายข้อมูลไปยังไปป์ไลน์ที่อิงตามหลักฟิสิกส์

  • แต่หากทำไม่ได้ วิธีแก้ปัญหาที่ดีคือการคูณค่าอัลบีโดจากวัสดุที่ไม่ใช่แบบอิงตามหลักฟิสิกส์ด้วยปัจจัยการอนุรักษ์พลังงาน ซึ่งจะช่วยให้มั่นใจได้ว่าอย่างน้อยโมเดลการแรเงา BRDF จะแปลงเป็นโมเดลตามหลักฟิสิกส์ได้ BRDF แต่ละรายการมีปัจจัยที่แตกต่างกัน เช่น สำหรับการสะท้อนแบบกระจายจะเป็น 1/Pi