-- glslfx version 0.1

//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
//    names, trademarks, service marks, or product names of the Licensor
//    and its affiliates, except as required to comply with Section 4(c) of
//    the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//

--- This is what an import might look like.
--- #import $TOOLS/hdSt/shaders/edgeId.glslfx

--- --------------------------------------------------------------------------
-- glsl EdgeId.Fragment.Common

// Determines whether a fragment is on or close to an unhidden triangle edge.
const float edgePickRadius = 2; // in viewport pixels
const float edgePickParametricRadius = 0.02; // in parametric space

// For smooth looking edges, use an exponential falloff for the opacity.
// Note: We use a slower falloff fn than in meshWire.glslfx to make the
// selected edges stand out.
float SelectedEdgeFalloff(float d) {
    return exp2(-1 * d * d);
}

--- --------------------------------------------------------------------------
-- glsl EdgeId.Fragment.TriangleSurface

FORWARD_DECL(vec3 GetEdgeDistanceTriangle());

vec3 ComputeBaseFaceEdgeDistanceTriangle()
{
    return GetEdgeDistanceTriangle();
}

--- --------------------------------------------------------------------------
-- glsl EdgeId.Fragment.TriangleLines

vec3 ComputeBaseFaceEdgeDistanceTriangle()
{
    // Use parametric distance since we can't use surface derivatives for lines
    vec3 param = GetEdgeParamTriangle();
    bvec3 nearEdge = lessThan(param, vec3(edgePickParametricRadius));
    return vec3(edgePickRadius) * vec3(not(nearEdge));
}

--- --------------------------------------------------------------------------
-- glsl EdgeId.Fragment.TriangleParam

float GetSelectedEdgeOpacity()
{
    float closestEdgeDistance = 0.0;

    // The sequence of comparisons should match GetPrimitiveEdgeId()
    vec3 dist = ComputeBaseFaceEdgeDistanceTriangle();
    if (dist.x < edgePickRadius) {
        closestEdgeDistance = dist.x;
    } else if (dist.y < edgePickRadius) {
        closestEdgeDistance = dist.y;
    } else if (dist.z < edgePickRadius) {
        closestEdgeDistance = dist.z;
    } else {
        return 0;
    }

    return SelectedEdgeFalloff(closestEdgeDistance);
}

// called from hdx/renderPass.glslfx and selection.glslfx
int GetPrimitiveEdgeId()
{
    //              2 (0,1,0)
    //              ^
    //         e2 /   \ e1
    //           /     \'
    // (0,0,1)  0 ----- 1 (1,0,0)
    //              e0

    int edgeId = -1;

    vec3 dist = ComputeBaseFaceEdgeDistanceTriangle();
    if (dist.x < edgePickRadius) {
        edgeId = 0;
    } else if (dist.y < edgePickRadius) {
        edgeId = 1;
    } else if (dist.z < edgePickRadius) {
        edgeId = 2;
    } else {
        return -1; // Not on a mesh edge
    }

    const int edgeFlag = GetEdgeFlag();
    const int meshEdgeIndex = HdGetScalar_edgeIndices();

    // Translate face-edge to authored mesh-edge
    if (edgeFlag == 0) {
        edgeId = meshEdgeIndex + edgeId; // regular triangle face.
    } else if ((bool(edgeFlag & 1) && edgeId == 2) ||
               (bool(edgeFlag & 2) && edgeId == 0)) {
        edgeId = -1; // Not an authored mesh edge
    } else {
        edgeId = meshEdgeIndex + edgeId - 1;
    }
    return edgeId;
}

--- --------------------------------------------------------------------------
-- glsl EdgeId.Fragment.QuadSurface

vec4 ComputeBaseFaceEdgeDistanceQuad()
{
    return GetEdgeDistanceQuad();
}

--- --------------------------------------------------------------------------
-- glsl EdgeId.Fragment.QuadLines

vec4 ComputeBaseFaceEdgeDistanceQuad()
{
    // Use parametric distance since we can't use surface derivatives for lines
    vec4 param = GetEdgeParamQuad();
    bvec4 nearEdge = lessThan(param, vec4(edgePickParametricRadius));
    return vec4(edgePickRadius) * vec4(not(nearEdge));
}

--- --------------------------------------------------------------------------
-- glsl EdgeId.Fragment.QuadParam

float GetSelectedEdgeOpacity()
{
    float closestEdgeDistance = 0.0;

    // The sequence of comparisons should match GetPrimitiveEdgeId()
    vec4 dist = ComputeBaseFaceEdgeDistanceQuad();
    if (dist.x < edgePickRadius) {
        closestEdgeDistance = dist.x;
    } else if (dist.y < edgePickRadius) {
        closestEdgeDistance = dist.y;
    } else if (dist.z < edgePickRadius) {
        closestEdgeDistance = dist.z;
    } else if (dist.w < edgePickRadius) {
        closestEdgeDistance = dist.w;
    } else {
        return 0;
    }

    return SelectedEdgeFalloff(closestEdgeDistance);
}

// called from hdx/renderPass.glslfx and selection.glslfx
int GetPrimitiveEdgeId()
{
    //               e2
    //     (0,1) 3 ------ 2 (1,1)
    //           |        |
    //       e3  |        |  e1
    //           |        |
    //     (0,0) 0 ------ 1 (1,0)
    //               e0

    int edgeId = -1;

    vec4 dist = ComputeBaseFaceEdgeDistanceQuad();
    if (dist.x < edgePickRadius) {
        edgeId = 0;
    } else if (dist.y < edgePickRadius) {
        edgeId = 1;
    } else if (dist.z < edgePickRadius) {
        edgeId = 2;
    } else if (dist.w < edgePickRadius) {
        edgeId = 3;
    } else {
        return -1; // Not on a mesh edge
    }

    const int edgeFlag = GetEdgeFlag();
    const ivec2 meshEdgeIndices = HdGet_edgeIndices();

    // Translate face-edge to authored mesh-edge
    if (edgeFlag == 0) {
        edgeId = meshEdgeIndices[0] + edgeId; // regular quad face
    } else if (edgeId == 0) {
        edgeId = meshEdgeIndices[0];
    } else if (edgeId == 3) {
        edgeId = meshEdgeIndices[1];
    } else {
        edgeId = -1; // Not an authored mesh edge
    }
    return edgeId;
}
