/*
const int colortex0Format = RGBA16;
const int colortex1Format = RGBA16;
const int colortex2Format = RGBA16F;
const int colortex3Format = RGBA16;
const int colortex4Format = RGBA16F;
const int colortex5Format = RGBA16;
const bool colortex0MipmapEnabled = true;

*/

uint convertValueTo16Bits(float value) {
  return uint(round(clamp(value, 0.0, 1.0) * 65535.0));
}

float unconvertValueTo16Bits(uint value) {
  return value * (1.0 / 65535.0);
}

uint convertValueTo15Bits(float value) {
  return uint(round(clamp(value, 0.0, 1.0) * 32767.0));
}

float unconvertValueTo15Bits(uint value) {
  return value * (1.0 / 32767.0);
}

uint convertValueTo8Bits(float value) {
  return uint(round(clamp(value, 0.0, 1.0) * 255.0));
}

float unconvertValueTo8Bits(uint value) {
  return value * (1.0 / 255.0);
}

uint convertValueTo4Bits(float value) {
  return uint(round(clamp(value, 0.0, 1.0) * 15.0));
}

float unconvertValueTo4Bits(uint value) {
  return value * (1.0 / 15.0);
}

uint convertValueTo2Bits(float value) {
  return uint(round(clamp(value, 0.0, 1.0) * 4.0));
}

uint uintConvertTo01Range(int value) {
  return uint(round(value * 0.5 + 0.5));
}

float convertTo01Range(float value) {
  return value * 0.5 + 0.5;
}

vec2 convertTo01Range(vec2 value) {
  return value * 0.5 + 0.5;
}

vec3 convertTo01Range(vec3 value) {
  return value * 0.5 + 0.5;
}

vec4 convertTo01Range(vec4 value) {
  return value * 0.5 + 0.5;
}

float unConvertTo01Range(float value)
{
  return value * 2.0 - 1.0;
}

vec2 unConvertTo01Range(vec2 value)
{
  return value * 2.0 - 1.0;
}

vec3 unConvertTo01Range(vec3 value)
{
  return value * 2.0 - 1.0;
}

vec4 unConvertTo01Range(vec4 value)
{
  return value * 2.0 - 1.0;
}

uint packNormalData(vec3 normalData) {
  int signOfNormals = int(sign(normalData.b));

  normalData.rg = convertTo01Range(normalData.rg);

  uint packedNormals;
  packedNormals |= convertValueTo16Bits(normalData.r);
  packedNormals |= convertValueTo15Bits(normalData.g) << 16;
  packedNormals |= uintConvertTo01Range(signOfNormals) << 31;

  return packedNormals;
}

vec3 unpackNormalData(uint packedNormals) {
  vec3 unpackedNormals;
  unpackedNormals.r = unconvertValueTo16Bits((packedNormals) & 65535u);
  unpackedNormals.g = unconvertValueTo15Bits((packedNormals >> 16) & 32767u);
  unpackedNormals.b = float((packedNormals >> 31) & ((1u << 1u) - 1u));

  unpackedNormals.b = sqrt(1.0-dot(unpackedNormals.rg, unpackedNormals.rg));


  return unConvertTo01Range(unpackedNormals);
}