#VRML_SIM R2022b utf8
# license: Copyright Cyberbotics Ltd. Licensed for use only with Webots.
# license url: https://cyberbotics.com/webots_assets_license
# TexturedBackground provides a set of background textures, and applies them to the background skybox and to the reflections on the 3D objects.
# It is designed to be used together with the TexturedBackgroundLight PROTO.
#
# Supported values for the `texture` field:
#
# - dusk
# - empty_office
# - entrance_hall
# - factory
# - mars
# - noon_building_overcast
# - noon_cloudy_countryside
# - noon_park_empty
# - mountains
# - music_hall
# - stadium
# - stadium_dry
#
# Deprecated backgrounds:
#
# - dawn_cloudy_empty
# - morning_cloudy_empty
# - noon_cloudy_empty
# - noon_cloudy_mountains
# - noon_stormy_empty
# - noon_sunny_empty
# - noon_sunny_garden
# - twilight_cloudy_empty
# template language: javascript

PROTO TexturedBackground [
  field SFString{"dawn_cloudy_empty", "dusk", "empty_office", "entrance_hall", "factory", "mars", "morning_cloudy_empty", "mountains", "music_hall", "noon_building_overcast", "noon_cloudy_countryside", "noon_cloudy_empty", "noon_cloudy_mountains", "noon_park_empty", "noon_stormy_empty", "noon_sunny_empty", "noon_sunny_garden", "stadium", "stadium_dry", "twilight_cloudy_empty"}
                 texture      "mountains"  # Defines the texture of the background.
  field SFFloat  luminosity   1            # Is `Background.luminosity`.
  field SFBool   skybox       TRUE         # Specifies if the `texture` field is used to define the skybox shown in the scene background.
  field MFColor  skyColor     [0 0 0]      # Defines the background color in case the `skybox` field is `FALSE`.
]
{
  %<
    const hdrBackgrounds = [
      'dusk',
      'empty_office',
      'entrance_hall',
      'factory',
      'mars',
      'noon_building_overcast',
      'noon_cloudy_countryside',
      'noon_park_empty',
      'mountains',
      'music_hall',
      'stadium',
      'stadium_dry'
    ];

    const texture = fields.texture.value;
    let topTexture;
    if (texture === 'stadium_dry')
      topTexture = 'stadium';
    else
      topTexture = texture;

    let isHDR = false;
    for (let i = 0; i < hdrBackgrounds.length; ++i) {
      if (texture === hdrBackgrounds[i]){
        isHDR = true;
        break;
      }
    }

    const path = 'webots://projects/default/worlds/textures/cubic';

    if (!isHDR) {
      let availableHdr = ''
      for (let i = 0; i < hdrBackgrounds.length; ++i) {
        if (i === hdrBackgrounds.length - 1)
          availableHdr += '"' + hdrBackgrounds[i] + '".\n';
        else
          availableHdr += '"' + hdrBackgrounds[i] + '", ';
      }
      console.error('Non-HDR backgrounds are deprecated. Please select one of the following \'TexturedBackground.texture\': ' + availableHdr);
    }
  >%
  Background {
    skyColor IS skyColor
    %< if (fields.skybox.value) { >%
      backUrl [
        %<= '"' + path + '/' + texture + '_back.jpg' + '"' >%
      ]
      bottomUrl [
        %<= '"' + path + '/' + texture + '_bottom.jpg' + '"' >%
      ]
      frontUrl [
        %<= '"' + path + '/' + texture + '_front.jpg' + '"' >%
      ]
      leftUrl [
        %<= '"' + path + '/' + texture + '_left.jpg' + '"' >%
      ]
      rightUrl [
        %<= '"' + path + '/' + texture + '_right.jpg' + '"' >%
      ]
      topUrl [
        %<= '"' + path + '/' + topTexture + '_top.jpg' + '"' >%
      ]
    %< } >%
    %< if (isHDR) { >%
      backIrradianceUrl [
        %<= '"' + path + '/' + texture + '_back.hdr"' >%
      ]
      bottomIrradianceUrl [
        %<= '"' + path + '/' + texture + '_bottom.hdr"' >%
      ]
      frontIrradianceUrl [
        %<= '"' + path + '/' + texture + '_front.hdr"' >%
      ]
      leftIrradianceUrl [
        %<= '"' + path + '/' + texture + '_left.hdr"' >%
      ]
      rightIrradianceUrl [
        %<= '"' + path + '/' + texture + '_right.hdr"' >%
      ]
      topIrradianceUrl [
        %<= '"' + path + '/' + topTexture + '_top.hdr"' >%
      ]
    %< } >%
    luminosity IS luminosity
  }
}