Ejemplo n.º 1
0
def packIslands(islandList):
    if USER_FILL_HOLES:
        # XXX  Window.DrawProgressBar(0.1, 'Merging Islands (Ctrl: skip merge)...')
        mergeUvIslands(islandList)  # Modify in place

    # Now we have UV islands, we need to pack them.

    # Make a synchronized list with the islands
    # so we can box pack the islands.
    packBoxes = []

    # Keep a list of X/Y offset so we can save time by writing the
    # uv's and packed data in one pass.
    islandOffsetList = []

    islandIdx = 0

    while islandIdx < len(islandList):
        minx, miny, maxx, maxy = boundsIsland(islandList[islandIdx])

        w, h = maxx - minx, maxy - miny

        if USER_ISLAND_MARGIN:
            minx -= USER_ISLAND_MARGIN * w / 2
            miny -= USER_ISLAND_MARGIN * h / 2
            maxx += USER_ISLAND_MARGIN * w / 2
            maxy += USER_ISLAND_MARGIN * h / 2

            # recalc width and height
            w, h = maxx - minx, maxy - miny

        if w < SMALL_NUM:
            w = SMALL_NUM
        if h < SMALL_NUM:
            h = SMALL_NUM

        """Save the offset to be applied later,
        we could apply to the UVs now and align them to the bottom left hand area
        of the UV coords like the box packer imagines they are
        but, its quicker just to remember their offset and
        apply the packing and offset in 1 pass """
        islandOffsetList.append((minx, miny))

        # Add to boxList. use the island idx for the BOX id.
        packBoxes.append([0, 0, w, h])
        islandIdx += 1

    # Now we have a list of boxes to pack that syncs
    # with the islands.

    # print '\tPacking UV Islands...'
# XXX  Window.DrawProgressBar(0.7, "Packing %i UV Islands..." % len(packBoxes) )

    # time1 = time.time()
    packWidth, packHeight = geometry.box_pack_2d(packBoxes)

    # print 'Box Packing Time:', time.time() - time1

    # if len(packedLs) != len(islandList):
    #    raise ValueError("Packed boxes differs from original length")

    # print '\tWriting Packed Data to faces'
# XXX  Window.DrawProgressBar(0.8, "Writing Packed Data to faces")

    # Sort by ID, so there in sync again
    islandIdx = len(islandList)
    # Having these here avoids divide by 0
    if islandIdx:

        if USER_STRETCH_ASPECT:
            # Maximize to uv area?? Will write a normalize function.
            xfactor = 1.0 / packWidth
            yfactor = 1.0 / packHeight
        else:
            # Keep proportions.
            xfactor = yfactor = 1.0 / max(packWidth, packHeight)

    while islandIdx:
        islandIdx -= 1
        # Write the packed values to the UV's

        xoffset = packBoxes[islandIdx][0] - islandOffsetList[islandIdx][0]
        yoffset = packBoxes[islandIdx][1] - islandOffsetList[islandIdx][1]

        for f in islandList[islandIdx]:  # Offsetting the UV's so they fit in there packed box
            for uv in f.uv:
                uv.x = (uv.x + xoffset) * xfactor
                uv.y = (uv.y + yoffset) * yfactor
def packIslands(islandList):
    if USER_FILL_HOLES:
#XXX		Window.DrawProgressBar(0.1, 'Merging Islands (Ctrl: skip merge)...')
        mergeUvIslands(islandList) # Modify in place


    # Now we have UV islands, we need to pack them.

    # Make a synchronized list with the islands
    # so we can box pack the islands.
    packBoxes = []

    # Keep a list of X/Y offset so we can save time by writing the
    # uv's and packed data in one pass.
    islandOffsetList = []

    islandIdx = 0

    while islandIdx < len(islandList):
        minx, miny, maxx, maxy = boundsIsland(islandList[islandIdx])

        w, h = maxx-minx, maxy-miny

        if USER_ISLAND_MARGIN:
            minx -= USER_ISLAND_MARGIN# *w
            miny -= USER_ISLAND_MARGIN# *h
            maxx += USER_ISLAND_MARGIN# *w
            maxy += USER_ISLAND_MARGIN# *h

            # recalc width and height
            w, h = maxx-minx, maxy-miny

        if w < 0.00001 or h < 0.00001:
            del islandList[islandIdx]
            islandIdx -=1
            continue

        """Save the offset to be applied later,
        we could apply to the UVs now and allign them to the bottom left hand area
        of the UV coords like the box packer imagines they are
        but, its quicker just to remember their offset and
        apply the packing and offset in 1 pass """
        islandOffsetList.append((minx, miny))

        # Add to boxList. use the island idx for the BOX id.
        packBoxes.append([0, 0, w, h])
        islandIdx+=1

    # Now we have a list of boxes to pack that syncs
    # with the islands.

    #print '\tPacking UV Islands...'
#XXX	Window.DrawProgressBar(0.7, "Packing %i UV Islands..." % len(packBoxes) )

    # time1 = time.time()
    packWidth, packHeight = geometry.box_pack_2d(packBoxes)

    # print 'Box Packing Time:', time.time() - time1

    #if len(pa	ckedLs) != len(islandList):
    #	raise "Error packed boxes differs from original length"

    #print '\tWriting Packed Data to faces'
#XXX	Window.DrawProgressBar(0.8, "Writing Packed Data to faces")

    # Sort by ID, so there in sync again
    islandIdx = len(islandList)
    # Having these here avoids divide by 0
    if islandIdx:

        if USER_STRETCH_ASPECT:
            # Maximize to uv area?? Will write a normalize function.
            xfactor = 1.0 / packWidth
            yfactor = 1.0 / packHeight
        else:
            # Keep proportions.
            xfactor = yfactor = 1.0 / max(packWidth, packHeight)

    while islandIdx:
        islandIdx -=1
        # Write the packed values to the UV's

        xoffset = packBoxes[islandIdx][0] - islandOffsetList[islandIdx][0]
        yoffset = packBoxes[islandIdx][1] - islandOffsetList[islandIdx][1]

        for f in islandList[islandIdx]: # Offsetting the UV's so they fit in there packed box
            for uv in f.uv:
                uv.x= (uv.x+xoffset) * xfactor
                uv.y= (uv.y+yoffset) * yfactor