#VRML_SIM R2022b utf8
# license: Copyright Cyberbotics Ltd. Licensed for use only with Webots.
# license url: https://cyberbotics.com/webots_assets_license
# Configurable standard flat floor.
# template language: javascript

EXTERNPROTO "webots://projects/appearances/protos/Parquetry.proto"

PROTO Floor [
  field SFVec3f    translation     0 0 0
  field SFRotation rotation        0 0 1 0
  field SFString   name            "floor"
  field SFString   contactMaterial "default"                       # Is `Solid.contactMaterial`.
  field SFVec2f    size            10 10                           # Defines the size of the floor.
  field SFVec2f    tileSize        0.5 0.5                         # Defines the size of texture used for the floor.
  field SFNode     appearance      Parquetry { type "chequered" }  # Defines the appearance of the floor.

  # Deprecated in R2019a
  hiddenField MFString   texture  "webots://projects/default/worlds/textures/checkered_parquetry.jpg"
]
{
  %<
    let size = fields.size.value;
    if (size.x <= 0.0 || size.y <= 0.0) {
      size = fields.size.defaultValue;
      console.error('\'size\' must contain positive values. Value reset to (' + size.x + ', ' + size.y + ').');
    }

    let texture = fields.texture.value[0];
    if (texture !== fields.texture.defaultValue[0])
      console.error('The \'texture\' field is deprecated, using the new \'appearance\' field instead.');

    let tileSize = fields.tileSize.value;
    if (tileSize.x <= 0 || tileSize.y <= 0) { // avoid a zero division and negative values
      tileSize = fields.tileSize.defaultValue;
      console.error('\'size\' must contain positive values. Value reset to (' + tileSize.x + ', ' + tileSize.y + ').');
    }
    let textureScale = {x: size.x / tileSize.x, y: size.y / tileSize.y};
  >%
  Solid {
    translation IS translation
    rotation IS rotation
    children [
      Shape {
        appearance IS appearance
        geometry IndexedFaceSet {
          coord Coordinate {
            point [
              %<= -(size.x * 0.5) >% %<= -(size.y * 0.5) >% 0
              %<= size.x * 0.5 >% %<= -(size.y * 0.5) >% 0
              %<= -(size.x * 0.5) >% %<= size.y * 0.5 >% 0
              %<= size.x * 0.5 >% %<= size.y * 0.5 >% 0
            ]
          }
          texCoord TextureCoordinate {
            point [
              0 0
              %<= textureScale.x >% 0
              0 %<= textureScale.y >%
              %<= textureScale.x >% %<= textureScale.y >%
            ]
          }
          coordIndex [
            2 0 1 3 -1
          ]
          texCoordIndex [
            2 0 1 3 -1
          ]
        }
      }
    ]
    name IS name
    model "floor"
    boundingObject Plane {
      size IS size
    }
    contactMaterial IS contactMaterial
    locked TRUE
  }
}