Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shader to render blocks #100

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
pyglet = "*"
cython = "*"

[requires]
python_version = "3.7"
69 changes: 69 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 51 additions & 12 deletions blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Future imports

from math import floor
from math import floor, ceil, sqrt

# Python packages
# Nothing for now...
Expand Down Expand Up @@ -41,15 +41,15 @@ def get_texture_coordinates(x, y, tileset_size=G.TILESET_SIZE):
#To enable, extract a texture pack's blocks folder to resources/texturepacks/textures/blocks/
#For MC 1.5 Texture Packs
class TextureGroupIndividual(pyglet.graphics.Group):
def __init__(self, names, height=1.0, width=1.0):
def __init__(self, names, height=1.0, width=1.0, background_color=None):
super(TextureGroupIndividual, self).__init__()
atlas = None
# self.texture = atlas.texture
self.texture_data = []
i=0
texture_pack = G.texture_pack_list.selected_texture_pack

for name in names:

if not name in BLOCK_TEXTURE_DIR:
BLOCK_TEXTURE_DIR[name] = texture_pack.load_texture(['textures', 'blocks', name + '.png'])

Expand All @@ -58,14 +58,25 @@ def __init__(self, names, height=1.0, width=1.0):

texture_size = BLOCK_TEXTURE_DIR[name].width

# remove background color for crack textures
if background_color is not None:
data = bytearray(BLOCK_TEXTURE_DIR[name].get_image_data().get_data('RGBA', texture_size * 4))
for i in range(len(data)):
if data [i] == background_color:
data[i] = 0
BLOCK_TEXTURE_DIR[name].get_image_data().set_data('RGBA', texture_size * 4, bytes(data))

if atlas == None:
atlas = TextureAtlas(texture_size * len(names), texture_size)
# prevent pyglet from using rectangle texture
atlas = TextureAtlas(texture_size * ceil(sqrt(len(names))), texture_size * ceil(sqrt(len(names))))
self.texture = atlas.texture

subtex = atlas.add(BLOCK_TEXTURE_DIR[name].get_region(0,0,texture_size,texture_size))

for val in subtex.tex_coords:
i += 1
if i % 3 != 0: self.texture_data.append(val) #tex_coords has a z component we don't utilize
if i % 3 != 0: self.texture_data.append(val) # tex_coords has a z component we don't utilize

if atlas == None:
atlas = TextureAtlas(1, 1)
self.texture = atlas.texture
Expand Down Expand Up @@ -96,13 +107,17 @@ def __init__(self, names, height=1.0, width=1.0):

def set_state(self):
if self.texture:
glActiveTexture(GL_TEXTURE0)
glBindTexture(self.texture.target, self.texture.id)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glEnable(self.texture.target)

def unset_state(self):
if self.texture:
glActiveTexture(GL_TEXTURE0)
glDisable(self.texture.target)


Expand Down Expand Up @@ -326,6 +341,30 @@ def get_vertices(self, x, y, z):
)
return vertices

def get_normal(self):
if self.vertex_mode == G.VERTEX_GRID or self.vertex_mode == G.VERTEX_CROSS:
return None

normal = ()

if len(self.top_texture) > 0 or len(self.side_texture) == 0:
normal += (
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0 # top
)
if len(self.bottom_texture) > 0 or len(self.side_texture) == 0:
normal += (
0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0 # bottom
)

normal += (
-1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, # left
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, # right
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, # front
0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, # back
)

return normal

def get_metadata(self):
if self.sub_id_as_metadata:
return self.id.sub
Expand All @@ -347,10 +386,10 @@ def update_texture(self):

if self.group:
self.texture_data = self.group.texture_data

if self.top_texture == ():
return

if self.front_texture is None:
self.front_texture = self.side_texture
if not self.texture_data:
Expand Down Expand Up @@ -412,7 +451,7 @@ class StoneBlock(HardBlock):
def __init__(self):
super(StoneBlock, self).__init__()
self.drop_id = BlockID(CobbleBlock.id)

colorizer = BlockColorizer('stonecolor.png')

def get_color(self, temperature, humidity):
Expand Down Expand Up @@ -1401,7 +1440,7 @@ def fertilize(self):
return False
G.CLIENT.update_tile_entity(self.entity.position, make_nbt_from_dict({'action': 'fertilize'}))
return True

def update_tile_entity(self, value):
nbt = extract_nbt(value)
# client side
Expand Down Expand Up @@ -1738,7 +1777,7 @@ def __init__(self):
texture_name = []
for i in range(self.crack_level):
texture_name.append('destroy_' + str(i))
self.group = TextureGroupIndividual(texture_name)
self.group = TextureGroupIndividual(texture_name, background_color=0x7f)

for i in range(self.crack_level):
self.texture_data.append(self.group.texture_data[i * 8:(i + 1) * 8] * 6)
Expand Down
Loading