Skip to main content
added 193 characters in body
Source Link
Panda Pajama
  • 13.5k
  • 4
  • 47
  • 79

I don't completely understand the problem you're having, but this is a sketch of an extrusion algorithm

Supposing polygon is an array of 3D points in counter-clockwise order (I guess they don't have to be convex, or even coplanar), and direction is a 3D vector determining the direction in which you want to extrude your polygon, this is what I came up with (in no particular language)

function extrude(polygon, direction)
    triangles = []
    -- Top cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0] + direction
        p1 = polygon[p + 1] + direction
        p2 = polygon[p + 2] + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Bottom cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0]
        p1 = polygon[p + 2] -- pointing down now...
        p2 = polygon[p + 1]
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Sides
    for p = 0 to count(polygon) do
        p0 = polygon[p]
        p1 = polygon[(p + 1) mod count(polygon)]
        p2 = p0 + direction
        p3 = p1 + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
        triangles.push(new triangle(p0, p2, p3, normal))
    end
    
    return triangles
end

The normals created by this snippet point outwards for each face. You could also average them with the normal of the adjacent face to create a softer look for polygons with lots of faces...

Would something like this work for you?

I don't completely understand the problem you're having, but this is a sketch of an extrusion algorithm

Supposing polygon is an array of 3D points in counter-clockwise order (I guess they don't have to be convex, or even coplanar), and direction is a 3D vector determining the direction in which you want to extrude your polygon, this is what I came up with (in no particular language)

function extrude(polygon, direction)
    triangles = []
    -- Top cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0] + direction
        p1 = polygon[p + 1] + direction
        p2 = polygon[p + 2] + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Bottom cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0]
        p1 = polygon[p + 2] -- pointing down now...
        p2 = polygon[p + 1]
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Sides
    for p = 0 to count(polygon) do
        p0 = polygon[p]
        p1 = polygon[(p + 1) mod count(polygon)]
        p2 = p0 + direction
        p3 = p1 + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
        triangles.push(new triangle(p0, p2, p3, normal))
    end
    
    return triangles
end

Would something like this work for you?

I don't completely understand the problem you're having, but this is a sketch of an extrusion algorithm

Supposing polygon is an array of 3D points in counter-clockwise order (I guess they don't have to be convex, or even coplanar), and direction is a 3D vector determining the direction in which you want to extrude your polygon, this is what I came up with (in no particular language)

function extrude(polygon, direction)
    triangles = []
    -- Top cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0] + direction
        p1 = polygon[p + 1] + direction
        p2 = polygon[p + 2] + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Bottom cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0]
        p1 = polygon[p + 2] -- pointing down now...
        p2 = polygon[p + 1]
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Sides
    for p = 0 to count(polygon) do
        p0 = polygon[p]
        p1 = polygon[(p + 1) mod count(polygon)]
        p2 = p0 + direction
        p3 = p1 + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
        triangles.push(new triangle(p0, p2, p3, normal))
    end
    
    return triangles
end

The normals created by this snippet point outwards for each face. You could also average them with the normal of the adjacent face to create a softer look for polygons with lots of faces...

Would something like this work for you?

Source Link
Panda Pajama
  • 13.5k
  • 4
  • 47
  • 79

I don't completely understand the problem you're having, but this is a sketch of an extrusion algorithm

Supposing polygon is an array of 3D points in counter-clockwise order (I guess they don't have to be convex, or even coplanar), and direction is a 3D vector determining the direction in which you want to extrude your polygon, this is what I came up with (in no particular language)

function extrude(polygon, direction)
    triangles = []
    -- Top cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0] + direction
        p1 = polygon[p + 1] + direction
        p2 = polygon[p + 2] + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Bottom cap
    for p = 0 to count(polygon) - 2 do
        p0 = polygon[0]
        p1 = polygon[p + 2] -- pointing down now...
        p2 = polygon[p + 1]
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
    end
    
    -- Sides
    for p = 0 to count(polygon) do
        p0 = polygon[p]
        p1 = polygon[(p + 1) mod count(polygon)]
        p2 = p0 + direction
        p3 = p1 + direction
        normal = normalize(cross(p1 - p0, p2 - p0))
        triangles.push(new triangle(p0, p1, p2, normal))
        triangles.push(new triangle(p0, p2, p3, normal))
    end
    
    return triangles
end

Would something like this work for you?