add to my game a SkyBox class, to make a skybox using 6 images: import numpy as np…
- User
add to my game a SkyBox class, to make a skybox using 6 images:
import numpy as np import pygame
class BasicMeshMaterial: def init(self, color, obj): self.color = color # Color in RGB format, e.g., (255, 0, 0) for red self.object = obj
def draw_material(self, screen, screen_width, screen_height, fov=256, viewer_distance=5): # Rotate and project vertices vertices = self.object.rotate(self.object.angle_x, self.object.angle_y, self.object.angle_z) projected_vertices = [self.object.project(v, screen_width, screen_height, fov, viewer_distance) for v in vertices] # Exclude vertices that can't be projected if all(pv is not None for pv in projected_vertices): self.draw_faces(screen, projected_vertices) def draw_faces(self, screen, projected_vertices): faces = self.object.get_faces() # Get faces from object for face in faces: pygame.draw.polygon(screen, self.color, [projected_vertices[i] for i in face])class OutlineMaterial: def init(self, color, obj, line_width=1): self.color = color self.object = obj self.line_width = line_width
def draw_material(self, screen, screen_width, screen_height, fov=256, viewer_distance=5): # Rotate and project vertices vertices = self.object.rotate(self.object.angle_x, self.object.angle_y, self.object.angle_z) projected_vertices = [self.object.project(v, screen_width, screen_height, fov, viewer_distance) for v in vertices] # Exclude vertices that can't be projected if all(pv is not None for pv in projected_vertices): self.draw_edges(screen, projected_vertices) def draw_edges(self, screen, projected_vertices): edges = self.object.get_edges() # Get edges from object for edge in edges: pygame.draw.line(screen, self.color, projected_vertices[edge[0]], projected_vertices[edge[1]], self.line_width)class Cube: def init(self, x, y, z, width, height, depth, material=None): self.position = np.array([x, y, z]) self.width = width self.height = height self.depth = depth self.angle_x = 0 self.angle_y = 0 self.angle_z = 0 self.material = material if material else BasicMeshMaterial((255, 255, 255), self) # default white material
def setPosition(self, new_position): self.position = np.array(new_position) def getVertices(self): x, y, z = self.position return [ [x, y, z], [x + self.width, y, z], [x + self.width, y + self.height, z], [x, y + self.height, z], [x, y, z + self.depth], [x + self.width, y, z + self.depth], [x + self.width, y + self.height, z + self.depth], [x, y + self.height, z + self.depth] ] def get_faces(self): return [ (0, 1, 2, 3), # Front (4, 5, 6, 7), # Back (0, 3, 7, 4), # Left (1, 2, 6, 5), # Right (3, 2, 6, 7), # Top (0, 1, 5, 4) # Bottom ] def get_edges(self): return [ (0, 1), (1, 2), (2, 3), (3, 0), # Front edges (4, 5), (5, 6), (6, 7), (7, 4), # Back edges (0, 4), (1, 5), (2, 6), (3, 7) # Connecting edges ] def rotate(self, angle_x, angle_y, angle_z): cos_x, sin_x = np.cos(angle_x), np.sin(angle_x) cos_y, sin_y = np.cos(angle_y), np.sin(angle_y) cos_z, sin_z = np.cos(angle_z), np.sin(angle_z) rotation_x = np.array([ [1, 0, 0], [0, cos_x, -sin_x], [0, sin_x, cos_x] ]) rotation_y = np.array([ [cos_y, 0, sin_y], [0, 1, 0], [-sin_y, 0, cos_y] ]) rotation_z = np.array([ [cos_z, -sin_z, 0], [sin_z, cos_z, 0], [0, 0, 1] ]) rotation_matrix = rotation_x @ rotation_y @ rotation_z rotated_vertices = [] for vertex in self.getVertices(): rotated_vertex = np.dot(rotation_matrix, np.array(vertex) - self.position) + self.position rotated_vertices.append(rotated_vertex) return rotated_vertices def project(self, vertex, screen_width, screen_height, fov, viewer_distance, epsilon=0.1): z = vertex[2] + epsilon if z <= 0: return None # Ignore vertices "behind the camera" factor = fov / z x = vertex[0] * factor + screen_width / 2 y = -vertex[1] * factor + screen_height / 2 return (int(x), int(y)) def draw(self, screen, screen_width, screen_height, fov=256, viewer_distance=5): self.material.draw_material(screen, screen_width, screen_height, fov, viewer_distance) - Assistant
- User
why skybox doesn't show?:
import numpy as np import pygame from engine3d.shapes import * from engine3d.scene import *
time = pygame.time.Clock()
pygame.init() screen_width, screen_height = 800, 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("3D Engine")
cubeMatrial = BasicMeshMaterial((255,0,0), None) piramydMaterial = OutlineMaterial((0,0,255), None, line_width=2) sphereMaterial = BasicMeshMaterial((0,255,0), None)
kostka = Cube(0, 0, 10, 3, 5, 3, material=cubeMatrial) piramida = Pyramid(-5, 5, 10, 3, 5, 3, material=piramydMaterial) kula = Sphere(5,5,10, 3, 36, 18, sphereMaterial)
- Assistant
If the skybox isn't displaying properly, there might be several reasons contributing to this issue. Here are some steps to troubleshoot and address these potential problems:
-
: Ensure that the file paths to your skybox images are correct. If your working directory is different from where the images are stored, you may need to provide the absolute paths or adjust your current working directory.
-
- User
dalej nic
- Assistant
- User
- Assistant
- User
PS E:\Mariusz\Programowanie\JavaScript\Engine 3D> python -u "e:\Mariusz\Programowanie\JavaScript\Engine 3D\main.py" pygame 2.5.2 (SDL 2.28.3, Python 3.12.1) Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "e:\Mariusz\Programowanie\JavaScript\Engine 3D\main.py", line 71, in scena.render(screen, screen_width, screen_height) File "e:\Mariusz\Programowanie\JavaScript\Engine 3D\engine3d\scene.py", line 26, in render self.objects.sort(key=lambda obj: obj.position[2], reverse=True) File "e:\Mariusz\Programowanie\JavaScript\Engine 3D\engine3d\scene.py", line 26, in self.objects.sort(key=lambda obj: obj.position[2], reverse=True) ^^^^^^^^^^^^ AttributeError: 'SkyBox' object has no attribute 'position'
- Assistant
- User
ale to jest mój kod sceny i uwzględnij to i błąd z klasą SkyBox i zrób tak by to działało proprawnie:
import pygame
class Scene: def init(self): self.objects = [] self.sky_color = (0, 0, 0)
def add(self, obj): """Adds an object to the scene.""" self.objects.append(obj) def remove(self, obj): """Removes an object from the scene if it exists.""" if obj in self.objects: self.objects.remove(obj) def set_sky_color(self, color: tuple): """Sets the sky color.""" self.sky_color = color def render(self, screen: pygame.Surface, screen_width: int, screen_height: int, fov: int = 256, viewer_distance: float = 5.0): """Renders the scene onto a pygame screen.""" screen.fill(self.sky_color) # Sort objects by their Z position for proper rendering order (farthest to nearest) self.objects.sort(key=lambda obj: obj.position[2], reverse=True) # Debug: Print sorted order for obj in self.objects: print(f"Object at Z: {obj.position[2]}") # This helps verify sorting order for obj in self.objects: if hasattr(obj, 'draw'): obj.draw(screen, screen_width, screen_height, fov, viewer_distance) else: raise AttributeError(f"Object {obj} does not have a 'draw' method") - Assistant
- User
okej ale w klasie skybox usuń size i dodaj by skybox ustawiał się sam bazując o szerokośc i wysokość okna, oraz zrób by było widać inne ściany skybox bo każdej stronie oprócz obserwatorea (tej jednej której nie widać)
- Assistant
- User
ale ciągle widać tylko fragment jednej ściany skybox NAPRAW TO
- Assistant
- User
teraz widać tylko jeden kwadrat TOP skyboxu zamiast 5 !!
- Assistant