Untitled

 avatar
unknown
plain_text
16 days ago
3.0 kB
14
Indexable
shader TriangularSectors_Outside(
    vector MidPoint1 = vector(0.5, 0.5, 0.0),  // First midpoint
    vector MidPoint2 = vector(0.5, 0.5, 0.0),  // Second midpoint
    vector MidPoint3 = vector(0.5, 0.7, 0.0),  // Third midpoint
    vector OceanPosition = vector(0.0, 0.0, 0.0),  // UV space origin
    vector OceanParameters = vector(1.0, 1.0, 1.0),  // UV space scaling
    color Color1 = color(1.0, 0.0, 0.0),  // Color for Sector 1
    color Color2 = color(0.0, 1.0, 0.0),  // Color for Sector 2
    color Color3 = color(0.0, 0.0, 1.0),  // Color for Sector 3
    color ColorInside = color(0.5, 0.5, 0.5),  // Color for Inside triangle
    output color Result = color(0.0, 0.0, 0.0)  // Output color
)
{
    // Step 1: Normalize midpoints relative to the UV space (0-1 range)
    vector normMid1 = (MidPoint1 - OceanPosition) / OceanParameters + vector(0.5, 0.5, 0.0);
    vector normMid2 = (MidPoint2 - OceanPosition) / OceanParameters + vector(0.5, 0.5, 0.0);
    vector normMid3 = (MidPoint3 - OceanPosition) / OceanParameters + vector(0.5, 0.5, 0.0);

    // Step 2: Get the current UV coordinates (u, v)
    vector uvPos = vector(u, v, 0.0);  // Assuming `u` and `v` are your texture coordinates

    // Step 3: Calculate barycentric coordinates to check if point is inside the triangle
    float denom = (normMid2[1] - normMid3[1]) * (normMid1[0] - normMid3[0]) +
                  (normMid3[0] - normMid2[0]) * (normMid1[1] - normMid3[1]);

    float w1 = ((normMid2[1] - normMid3[1]) * (uvPos[0] - normMid3[0]) +
                (normMid3[0] - normMid2[0]) * (uvPos[1] - normMid3[1])) / denom;

    float w2 = ((normMid3[1] - normMid1[1]) * (uvPos[0] - normMid3[0]) +
                (normMid1[0] - normMid3[0]) * (uvPos[1] - normMid3[1])) / denom;

    float w3 = 1.0 - w1 - w2;

    // Step 4: If point is inside the triangle, assign ColorInside
    if (w1 >= 0.0 && w2 >= 0.0 && w3 >= 0.0) {
        Result = ColorInside;
    }
    else {
        // Step 5: For points outside, assign sectors based on angle from centroid
        vector centroid = (normMid1 + normMid2 + normMid3) / 3.0;  // Triangle centroid

        vector delta = uvPos - centroid;
        float angle = atan2(delta[1], delta[0]);  // Calculate angle in radians

        // Normalize the angle to the range [0, 2*pi]
        if (angle < 0.0) {
            angle += 2.0 * 3.14159;
        }

        // Step 6: Divide the outside space into 3 sectors based on angle
        float sector = angle / (2.0 * 3.14159) * 3.0;  // Divide circle into 3 sectors
        int sectorIndex = int(floor(sector));  // Determine sector index (0, 1, 2)

        // Assign colors to the sectors
        if (sectorIndex == 0) {
            Result = Color1;  // First outside sector
        }
        else if (sectorIndex == 1) {
            Result = Color2;  // Second outside sector
        }
        else {
            Result = Color3;  // Third outside sector
        }
    }
}
Leave a Comment