lib-vectors.glsl
Public Functions:
computeLocalFrame
getEyeVec
tangentSpaceToWorldSpace
worldSpaceToTangentSpace
Import from library
Copied to your clipboardimport lib-normal.glsl
Which view is shaded.
Copied to your clipboard//: param auto is_2d_viewuniform bool is2DView;
What kind of projection is used.
Copied to your clipboard//: param auto is_perspective_projectionuniform bool is_perspective;
Eye position in world space.
Copied to your clipboard//: param auto world_eye_positionuniform vec3 camera_pos;
Camera orientation in world space.
Copied to your clipboard//: param auto world_camera_directionuniform vec3 camera_dir;//: param auto facinguniform int facing;bool isBackFace() {return facing == -1 || (facing == 0 && !gl_FrontFacing);}
Compute the world space eye vector
Copied to your clipboardvec3 getEyeVec(vec3 position) {return is_perspective ?normalize(camera_pos - position) :-camera_dir;}
Convert a vector from tangent space to world space
Copied to your clipboardvec3 tangentSpaceToWorldSpace(vec3 vecTS, V2F inputs) {return normalize(vecTS.x * inputs.tangent +vecTS.y * inputs.bitangent +vecTS.z * inputs.normal);}
Convert a vector from world space to tangent space
Copied to your clipboardvec3 worldSpaceToTangentSpace(vec3 vecWS, V2F inputs) {// Assume the transformation is orthogonalreturn normalize(vecWS * mat3(inputs.tangent, inputs.bitangent, inputs.normal));}
Local frame of vertex in world space
Copied to your clipboardstruct LocalVectors {vec3 vertexNormal;vec3 tangent, bitangent, normal, eye, bent;};
Compute local frame from custom world space normal and anisotropy angle
Copied to your clipboardLocalVectors computeLocalFrame(V2F inputs, vec3 normal, float anisoAngle) {LocalVectors vectors;vectors.vertexNormal = inputs.normal;vectors.normal = normal;vectors.bent = vec3(0.0);// Flip the normals for back facing polygonsif (isBackFace()) {vectors.vertexNormal = -vectors.vertexNormal;vectors.normal = -vectors.normal;}vectors.eye = is2DView ?vectors.normal : // In 2D view, put view vector along the normalgetEyeVec(inputs.position);// Trick to remove black artifacts// Backface ? place the eye at the opposite - removes black zonesif (dot(vectors.eye, vectors.normal) < 0.0) {vectors.eye = reflect(vectors.eye, vectors.normal);}// Create a local frame for BRDF workvec3 tangent = normalize(inputs.tangent- vectors.normal * dot(inputs.tangent, vectors.normal));vec3 bitangent = normalize(inputs.bitangent- vectors.normal * dot(inputs.bitangent, vectors.normal)- tangent * dot(inputs.bitangent, tangent));float cosAngle = cos(anisoAngle);float sinAngle = sin(anisoAngle);vectors.tangent = cosAngle * tangent - sinAngle * bitangent;vectors.bitangent = cosAngle * bitangent + sinAngle * tangent;return vectors;}
Compute local frame from mesh and document height and normals
Copied to your clipboardLocalVectors computeLocalFrame(V2F inputs) {// Get world space normalvec3 normal = computeWSNormal(inputs.sparse_coord, inputs.tangent, inputs.bitangent, inputs.normal);return computeLocalFrame(inputs, normal, 0.0);}