From c11e23a18ff52db8c4f1dfe76c46fd26b92f71ef Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Thu, 14 Oct 2010 13:57:48 -0400 Subject: [PATCH] added semi-correct lighting for half-blocks --- chunk.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/chunk.py b/chunk.py index 9cb5be2..67571f2 100644 --- a/chunk.py +++ b/chunk.py @@ -328,7 +328,7 @@ class ChunkRenderer(object): # Nighttime return 1.0 - pow(0.8, 15 - max(blocklight, skylight - 11)) - def get_lighting_coefficient(self, x, y, z): + def get_lighting_coefficient(self, x, y, z, norecurse=False): """Calculates the lighting coefficient for the given coordinate, using default lighting and peeking into neighboring chunks, if needed. A lighting coefficient of 1.0 @@ -337,10 +337,7 @@ class ChunkRenderer(object): Returns a tuple (coefficient, occluded), where occluded is True if the given coordinate is filled with a solid block, and therefore the returned coefficient is just the default.""" - - # fill it in with the default first (full skylight) - coefficient = self.calculate_darkness(15, 0) - + # placeholders for later data arrays, coordinates blocks = None skylight = None @@ -373,13 +370,36 @@ class ChunkRenderer(object): local_x >= 0 and local_x < 16 and local_y >= 0 and local_y < 16 and local_z >= 0 and local_z < 128): # we have no useful info, return default - return (coefficient, False) + return (self.calculate_darkness(15, 0), False) + + # special handling for half-blocks + # (don't recurse more than once!) + if blocks[local_x, local_y, local_z] == 44 and not norecurse: + # average gathering variables + averagegather = 0.0 + averagecount = 0 + + # how bright we need before we consider a side "lit" + threshold = self.calculate_darkness(0, 0) + # iterate through all the sides of the block + sides = [(x-1, y, z), (x+1, y, z), (x, y, z-1), (x, y, z+1), (x, y-1, z), (x, y+1, z)] + + for side in sides: + val, occ = self.get_lighting_coefficient(*side, norecurse=True) + if (not occ) and (val < threshold): + averagegather += val + averagecount += 1 + + # if at least one side was lit, return the average + if averagecount > 0: + return (averagegather / averagecount, False) # calculate the return... occluded = not (blocks[local_x, local_y, local_z] in transparent_blocks) - - # only calculate the coefficient if we're not occluded - if not occluded: + # only calculate the non-default coefficient if we're not occluded + if occluded: + coefficient = self.calculate_darkness(15, 0) + else: coefficient = self.calculate_darkness(skylight[local_x, local_y, local_z], blocklight[local_x, local_y, local_z]) # only say we're occluded if the point is in the CURRENT