Skip to content

Commit

Permalink
made Texture.pixels return an Array2D of Colors. made GridEditor use …
Browse files Browse the repository at this point in the history
…Array2D.
  • Loading branch information
petteramland committed Nov 26, 2024
1 parent 276f4a3 commit 6e9efcc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
15 changes: 8 additions & 7 deletions ursina/prefabs/grid_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,11 @@ def set_texture(self, texture, render=True, clear_undo_stack=True):
self.canvas.texture = texture
self.w, self.h = int(texture.width), int(texture.height)
self.canvas.scale_x = self.canvas.scale_y * self.w / self.h
self.grid = [[texture.get_pixel(x,y) for y in range(texture.height)] for x in range(texture.width)]

# pixels = texture.pixels
self.grid = Array2D(width=texture.width, height=texture.height)
for (x,y), _ in enumerate_2d(self.grid):
self.grid[x][y] = texture.get_pixel(x,y)
self.canvas.texture.filtering = None

self.gizmo_parent.scale = Vec2(1/self.w, 1/self.h)
Expand All @@ -521,7 +525,6 @@ def set_texture(self, texture, render=True, clear_undo_stack=True):
self.record_undo()
self.undo_index = 0


if render:
self.render()

Expand All @@ -536,9 +539,8 @@ def draw(self, x, y):


def render(self):
for y in range(self.h):
for x in range(self.w):
self.canvas.texture.set_pixel(x, y, self.grid[x][y])
for (x,y), value in enumerate_2d(self.grid):
self.canvas.texture.set_pixel(x, y, value)

self.canvas.texture.apply()

Expand Down Expand Up @@ -571,8 +573,7 @@ def texture(self, value):
'''
from PIL import Image
t = Texture(Image.new(mode='RGBA', size=(32,32), color=(0,0,0,1)))
editor = PixelEditor(parent=scene, texture=load_texture('brick'), scale=10)
# editor.set_texture(Texture('/home/rain/MechanicalHeart/Levels/Up.png'))
editor = PixelEditor(parent=scene, texture=load_texture('test_tileset'), scale=10)
camera.orthographic = True
camera.fov = 15
EditorCamera(rotation_speed=0)
Expand Down
21 changes: 12 additions & 9 deletions ursina/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ursina.vec2 import Vec2
from ursina import color
from ursina.ursinamath import clamp
from ursina.array_tools import Array2D


class Texture():
Expand Down Expand Up @@ -87,13 +88,14 @@ def height(self):
def pixels(self):
from numpy import asarray, flip
from PIL import Image
from ursina.array_tools import rotate_2d_list

if self._cached_image:
return asarray(self._cached_image)
return Array2D(data=asarray(self._cached_image))

pixels = asarray(Image.open(self.path))
pixels = flip(pixels, axis=0)
return pixels
pixels = rotate_2d_list(pixels)
return Array2D(data=pixels)


@property
Expand Down Expand Up @@ -183,12 +185,6 @@ def __repr__(self):
return self.name


def __del__(self):
# self._texture.releaseAll()
del self._cached_image



if __name__ == '__main__':
from ursina import *
from ursina import texture_importer
Expand Down Expand Up @@ -248,6 +244,13 @@ def input(key):
e.texture = 'brick'
# e.texture.set_pixels([e for e in e.texture.get_pixels()])
e.texture.apply()

tex = Texture.new((512,512), (255,0,0))

pixels = e.texture.pixels
new_grid = Array2D(width=pixels.width, height=pixels.height)
for (x,y), value in enumerate_2d(pixels):
new_grid[x][y] = int(color.rgba32(*value).v > .5)

print(new_grid)
app.run()
Binary file modified ursina/textures/test_tileset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6e9efcc

Please sign in to comment.