/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
 *
 * SPDX-License-Identifier: Apache-2.0 */

#include "stdcycles.h"

shader node_tangent(string attr_name = "geom:tangent",
                    string direction_type = "radial",
                    string axis = "z",
                    output normal Tangent = normalize(dPdu))
{
  vector T = vector(0.0, 0.0, 0.0);

  if (direction_type == "uv_map") {
    getattribute(attr_name, T);
  }
  else if (direction_type == "radial") {
    point generated;

    if (!getattribute("geom:generated", generated))
      generated = P;

    if (axis == "x")
      T = vector(0.0, -(generated[2] - 0.5), (generated[1] - 0.5));
    else if (axis == "y")
      T = vector(-(generated[2] - 0.5), 0.0, (generated[0] - 0.5));
    else
      T = vector(-(generated[1] - 0.5), (generated[0] - 0.5), 0.0);
  }

  T = transform("object", "world", T);
  Tangent = cross(N, normalize(cross(T, N)));
}
