Compute variant rotation matrix only once for a variant, instead of for each element face

This commit is contained in:
David Vierra 2015-03-03 05:48:02 -10:00
parent d4aa2893f6
commit 9901135218

View File

@ -213,8 +213,9 @@ cdef class BlockModels(object):
b = blockColor & 0xff b = blockColor & 0xff
blockColor = r, g, b blockColor = r, g, b
variantMatrix = variantRotation(variantXrot, variantYrot, variantZrot)
for element in allElements: for element in allElements:
quads = self.buildBoxQuads(element, nameAndState, textureVars, variantXrot, variantYrot, variantZrot, blockColor) quads = self.buildBoxQuads(element, nameAndState, textureVars, variantXrot, variantYrot, variantZrot, variantMatrix, blockColor)
allQuads.extend(quads) allQuads.extend(quads)
@ -227,7 +228,7 @@ cdef class BlockModels(object):
raise raise
def buildBoxQuads(self, element, nameAndState, textureVars, variantXrot, variantYrot, variantZrot, blockColor): def buildBoxQuads(self, element, nameAndState, textureVars, variantXrot, variantYrot, variantZrot, variantMatrix, blockColor):
quads = [] quads = []
shade = element.get("shade", True) shade = element.get("shade", True)
fromPoint = Vector(*element["from"]) fromPoint = Vector(*element["from"])
@ -235,6 +236,7 @@ cdef class BlockModels(object):
fromPoint /= 16. fromPoint /= 16.
toPoint /= 16. toPoint /= 16.
box = FloatBox(fromPoint, maximum=toPoint) box = FloatBox(fromPoint, maximum=toPoint)
for face, info in element["faces"].iteritems(): for face, info in element["faces"].iteritems():
face = facesByCardinal[face] face = facesByCardinal[face]
texture = info["texture"] texture = info["texture"]
@ -268,7 +270,7 @@ cdef class BlockModels(object):
quads.append((box, face, quads.append((box, face,
texture, uv, cullface, texture, uv, cullface,
shade, element.get("rotation"), info.get("rotation"), shade, element.get("rotation"), info.get("rotation"),
variantXrot, variantYrot, variantZrot, tintcolor)) variantXrot, variantYrot, variantZrot, variantMatrix, tintcolor))
return quads return quads
@ -301,7 +303,7 @@ cdef class BlockModels(object):
modelQuads.quads = <ModelQuad *>malloc(modelQuads.count * sizeof(ModelQuad)) modelQuads.quads = <ModelQuad *>malloc(modelQuads.count * sizeof(ModelQuad))
for i, (box, quadface, texture, uv, cullface, shade, rotation, textureRotation, for i, (box, quadface, texture, uv, cullface, shade, rotation, textureRotation,
variantXrot, variantYrot, variantZrot, tintcolor) in enumerate(allQuads): variantXrot, variantYrot, variantZrot, variantMatrix, tintcolor) in enumerate(allQuads):
l, t, w, h = textureAtlas.texCoordsByName[texture] l, t, w, h = textureAtlas.texCoordsByName[texture]
u1, v1, u2, v2 = uv u1, v1, u2, v2 = uv
@ -334,7 +336,7 @@ cdef class BlockModels(object):
if variantYrot: if variantYrot:
cullface = rotateFace(cullface, 1, variantYrot) cullface = rotateFace(cullface, 1, variantYrot)
rotateVertices(rotation, variantXrot, variantYrot, variantZrot, xyzuvstc) rotateVertices(rotation, variantMatrix, xyzuvstc)
rgba = xyzuvstc.view('uint8')[:, 28:] rgba = xyzuvstc.view('uint8')[:, 28:]
if shade: if shade:
@ -463,7 +465,8 @@ def rotateFace(face, axis, degrees):
return rots[idx] return rots[idx]
def rotateVertices(rotation, variantXrot, variantYrot, variantZrot, xyzuvstc): cdef rotateVertices(rotation, numpy.ndarray variantMatrix, numpy.ndarray[ndim=2,dtype=float] xyzuvstc):
if rotation is not None: if rotation is not None:
origin = rotation["origin"] origin = rotation["origin"]
axis = rotation["axis"] axis = rotation["axis"]
@ -478,8 +481,17 @@ def rotateVertices(rotation, variantXrot, variantYrot, variantZrot, xyzuvstc):
xyzuvstc[:, :3] = (matrix[:3, :3] * xyz).transpose() xyzuvstc[:, :3] = (matrix[:3, :3] * xyz).transpose()
xyzuvstc[:, :3] += origin xyzuvstc[:, :3] += origin
rotate = variantXrot or variantYrot or variantZrot if variantMatrix is not None:
if rotate: xyzuvstc[:, :3] -= 0.5
xyz = xyzuvstc[:, :3].transpose()
xyzuvstc[:, :3] = (variantMatrix[:3, :3] * xyz).transpose()
xyzuvstc[:, :3] += 0.5
cdef variantRotation(variantXrot, variantYrot, variantZrot):
if variantXrot or variantYrot or variantZrot:
matrix = numpy.matrix(numpy.identity(4)) matrix = numpy.matrix(numpy.identity(4))
if variantYrot: if variantYrot:
matrix *= npRotate("y", -variantYrot) matrix *= npRotate("y", -variantYrot)
@ -487,10 +499,7 @@ def rotateVertices(rotation, variantXrot, variantYrot, variantZrot, xyzuvstc):
matrix *= npRotate("x", -variantXrot) matrix *= npRotate("x", -variantXrot)
if variantZrot: if variantZrot:
matrix *= npRotate("z", -variantZrot) matrix *= npRotate("z", -variantZrot)
xyzuvstc[:, :3] -= 0.5, 0.5, 0.5 return matrix
xyz = xyzuvstc[:, :3].transpose()
xyzuvstc[:, :3] = (matrix[:3, :3] * xyz).transpose()
xyzuvstc[:, :3] += 0.5, 0.5, 0.5
def npRotate(axis, angle, rescale=False): def npRotate(axis, angle, rescale=False):
# ( xx(1-c)+c xy(1-c)-zs xz(1-c)+ys 0 ) # ( xx(1-c)+c xy(1-c)-zs xz(1-c)+ys 0 )