• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Mar 2nd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from math import pi, sin, cos
2.
3. import random
4.
5. import sys
6. from direct.showbase.ShowBase import ShowBase
8. from panda3d.core import *
9.
10. class Chunk(ShowBase):
11.     def __init__(self):
12.         ShowBase.__init__(self)
13.         self.disableMouse()
14.
15.         self.node = GeomNode('Chunk')
16.
17.         geo_node = self.create_chunk(4)
18.
20.
22.         angleDegrees = task.time * 60.0
23.         angleRadians = angleDegrees * (pi / 180.0)
25.         self.camera.setHpr(angleDegrees, 0, 0)
27.
28.     def list_3D(self, dim):
29.         return [[[None for _ in range(dim[2])] for _ in range(dim[1])] for _ in range(dim[0])]
30.
31.     def create_chunk(self, world_size):
32.         chunk_data = self.list_3D([world_size, world_size, world_size])
33.         for i in range(world_size):
34.             for j in range(world_size):
35.                 for k in range(world_size):
36.                     b = Block()
37.                     r = random.randint(0, 100)
38.                     if r < 25:
39.                         b.create_block('AIR', (i, j, k))
40.                     else:
41.                         b.create_block('DIRT', (i, j, k))
42.                     chunk_data[i][j][k] = b
43.         for i in range(world_size):
44.             for j in range(world_size):
45.                 for k in range(world_size):
46.                     geo_node = chunk_data[i][j][k].draw_cube(chunk_data, i, j, k)
47.                     card = self.render.attachNewNode(geo_node)
49.                     card.setTexture(tex)
50.
51. class Block:
52.     def __init__(self):
53.         self.types = ['GRASS', 'DIRT', 'STONE', 'AIR']
54.         self.sides = ['BOTTOM', 'TOP', 'LEFT', 'RIGHT', 'FRONT', 'BACK']
55.
56.         self.block_type = None
57.         self.block_position = None
58.         self.is_solid = None
59.
60.         self.node = GeomNode('Cube')
61.
62.     def create_block(self, block_type, block_position):
63.         self.block_type = block_type
64.         self.block_position = block_position
65.
66.         if self.block_type == 'AIR':
67.             self.is_solid = False
68.         else:
69.             self.is_solid = True
70.
71.     def create_cube(self, cube_side, empty):
72.         format = GeomVertexFormat.getV3n3t2()
73.
74.         n_forward = (0, 0, 1)
75.         n_back = (0, 0, -1)
76.         n_up = (0, 1, 0)
77.         n_down = (0, -1, 0)
78.         n_right = (1, 0, 0)
79.         n_left = (-1, 0, 0)
80.
81.         p0 = (-0.5, -0.5, 0.5)
82.         p1 = (0.5, -0.5,  0.5)
83.         p2 = (0.5, -0.5, -0.5)
84.         p3 = (-0.5, -0.5, -0.5)
85.         p4 = (-0.5, 0.5, 0.5)
86.         p5 = (0.5, 0.5, 0.5)
87.         p6 = (0.5, 0.5, -0.5)
88.         p7 = (-0.5, 0.5, -0.5)
89.
90.         p0 = tuple(l * r for l, r in zip(p0, self.block_position))
91.         p1 = tuple(l * r for l, r in zip(p1, self.block_position))
92.         p2 = tuple(l * r for l, r in zip(p2, self.block_position))
93.         p3 = tuple(l * r for l, r in zip(p3, self.block_position))
94.         p4 = tuple(l * r for l, r in zip(p4, self.block_position))
95.         p5 = tuple(l * r for l, r in zip(p5, self.block_position))
96.         p6 = tuple(l * r for l, r in zip(p6, self.block_position))
97.         p7 = tuple(l * r for l, r in zip(p7, self.block_position))
98.
99.         uv00 = (0, 0)
100.         uv10 = (1, 0)
101.         uv01 = (0, 1)
102.         uv11 = (1, 1)
103.
104.         vertexData = GeomVertexData('Quad', format, Geom.UHStatic)
105.
106.         vertexData.setNumRows(24)
107.
108.         vertices = GeomVertexWriter(vertexData, 'vertex')
109.         normals = GeomVertexWriter(vertexData, 'normal')
110.         texcoord = GeomVertexWriter(vertexData, 'texcoord')
111.
112.         geom = Geom(vertexData)
113.
114.         primitive = GeomTriangles(Geom.UHStatic)
115.
116.         if cube_side == 'BOTTOM':
121.
126.         elif cube_side == 'TOP':
131.
136.         elif cube_side == 'LEFT':
141.
146.         elif cube_side == 'RIGHT':
151.
156.         elif cube_side == 'FRONT':
161.
166.         elif cube_side == 'BACK':
171.
176.
177.         if not empty:
182.
185.
187.
188.         return geom
189.
190.     def draw_cube(self, chunks, x, y, z):
191.         if chunks[x][y][z].block_type == 'AIR':
192.             for side in self.sides:
193.                 geom = self.create_cube(side, True)
195.             return self.node
196.         try:
197.             if not chunks[x][y][z+1].is_solid:
198.                 geom = self.create_cube('FRONT', False)
200.         except:
201.               geom = self.create_cube('FRONT', True)
203.         try:
204.             if not chunks[x][y][z-1].is_solid:
205.                 geom = self.create_cube('BACK', False)
207.         except:
208.             geom = self.create_cube('BACK', True)
210.         try:
211.             if not chunks[x][y+1][z].is_solid:
212.                 geom = self.create_cube('TOP', False)
214.         except:
215.             geom = self.create_cube('TOP', True)
217.         try:
218.             if not chunks[x][y-1][z].is_solid:
219.                 geom = self.create_cube('BOTTOM', False)
221.         except:
222.             geom = self.create_cube('BOTTOM', True)
224.         try:
225.             if not chunks[x+1][y][z].is_solid:
226.                 geom = self.create_cube('RIGHT', False)
228.         except:
229.             geom = self.create_cube('RIGHT', True)
231.         try:
232.             if not chunks[x-1][y][z].is_solid:
233.                 geom = self.create_cube('LEFT', False)