# LYGIA Shader Library

## reflection (`lygia`/`lighting`/`reflection`)

This function calculates the reflection vector of a given vector and normal. It also takes into account the roughness of the material. If MATERIAL_ANISOTROPY is defined, it will also take into account the anisotropy direction. If MODEL_VERTEX_TANGENT is defined, it will use the tangentToWorld matrix to calculate the anisotropy direction.

Dependencies:

Use:

``````<vec3> reflection(<vec3> vector, <vec3> normal, <float> roughness);
``````

Check it on Github

``````

#ifndef FNC_REFLECTION
#define FNC_REFLECTION

vec3 reflection(const in vec3 _V, const in vec3 _N, const in float _roughness) {
// Reflect
#ifdef MATERIAL_ANISOTROPY
vec3  anisotropicT = MATERIAL_ANISOTROPY_DIRECTION;
vec3  anisotropicB = MATERIAL_ANISOTROPY_DIRECTION;

#ifdef MODEL_VERTEX_TANGENT
anisotropicT = normalize(v_tangentToWorld * MATERIAL_ANISOTROPY_DIRECTION);
anisotropicB = normalize(cross(v_tangentToWorld[2], anisotropicT));
#endif

vec3  anisotropyDirection = MATERIAL_ANISOTROPY >= 0.0 ? anisotropicB : anisotropicT;
vec3  anisotropicTangent  = cross(anisotropyDirection, _V);
vec3  anisotropicNormal   = cross(anisotropicTangent, anisotropyDirection);
float bendFactor          = abs(MATERIAL_ANISOTROPY) * saturate(5.0 * _roughness);
vec3  bentNormal          = normalize(mix(_N, anisotropicNormal, bendFactor));
return reflect(-_V, bentNormal);
#else

return reflect(-_V, _N);
#endif

}

#endif
``````

Use:

``````<float3> reflection(<float3> vector, <float3> normal, <float> roughness);
``````

Check it on Github

``````
#ifndef FNC_REFLECTION
#define FNC_REFLECTION

float3 reflection(float3 _V, float3 _N, float _roughness) {
// Reflect
#ifdef MATERIAL_ANISOTROPY
float3  anisotropicT = MATERIAL_ANISOTROPY_DIRECTION;
float3  anisotropicB = MATERIAL_ANISOTROPY_DIRECTION;

#ifdef MODEL_VERTEX_TANGENT
anisotropicT = normalize(v_tangentToWorld * MATERIAL_ANISOTROPY_DIRECTION);
anisotropicB = normalize(cross(v_tangentToWorld[2], anisotropicT));
#endif

float3  anisotropyDirection = MATERIAL_ANISOTROPY >= 0.0 ? anisotropicB : anisotropicT;
float3  anisotropicTangent  = cross(anisotropyDirection, _V);
float3  anisotropicNormal   = cross(anisotropicTangent, anisotropyDirection);
float bendFactor            = abs(MATERIAL_ANISOTROPY) * saturate(5.0 * _roughness);
float3  bentNormal          = normalize(lerp(_N, anisotropicNormal, bendFactor));
return reflect(-_V, bentNormal);
#else
return reflect(-_V, _N);
#endif

}

#endif
``````

## Licenses

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

## Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository