Skip to content

Instantly share code, notes, and snippets.

@pydsigner
Created October 23, 2012 01:58
Show Gist options
  • Save pydsigner/3936237 to your computer and use it in GitHub Desktop.
Save pydsigner/3936237 to your computer and use it in GitHub Desktop.
Surface Merger
#import timeit
class Surface(object):
"""
The surface class, which contains material information, how many
indices it uses, and its starting offset in the index buffer.
"""
def __init__(self, material, icount, start):
self.material = material
self.icount = icount
self.start = start
def merge():
# A sample index buffer.
indices = [0, 1, 3, 1, 2, 0, 2, 3, 0, 1, 2, 3]
# Create three surfaces to perform merging on.
# Surfaces 1 & 3 both use material 1, and thus
# their indices should be aligned together in
# the final buffer.
# Surface 2 uses material 2, so its indices
# should follow 1 & 3's.
# Material 1, a Triangle (3 indices), beginning at index 0
s1 = Surface(1, 3, 0)
# 6 indices = a quad (I dunno why)
s2 = Surface(2, 6, 3)
s3 = Surface(1, 3, 9)
# All of the surfaces to process
surfaces = [s1, s2, s3]
# Sort the surfaces by material
surfaces.sort(key=lambda s: s.material)
# Re-arrange the index buffer in the same way that we did the surfaces
aligned = []
for surf in surfaces:
aligned += indices[surf.start:surf.start + surf.icount]
# Original index
print "Unaligned indices: ", repr(indices)
# Aligned index
print "Aligned indices: ", repr(aligned)
if __name__ == '__main__':
merge()
#print timeit.timeit(merge)
# 7.91372203827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment