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
  7. from direct.task import Task
  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.        
  19.         self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
  20.        
  21.     def spinCameraTask(self, task):
  22.         angleDegrees = task.time * 60.0
  23.         angleRadians = angleDegrees * (pi / 180.0)
  24.         self.camera.setPos(20 * sin(angleRadians), -20.0 * cos(angleRadians), 3)
  25.         self.camera.setHpr(angleDegrees, 0, 0)
  26.         return Task.cont
  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)
  48.                     tex = loader.loadTexture('dirt.png')
  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':
  117.             vertices.addData3f(p0)
  118.             vertices.addData3f(p1)
  119.             vertices.addData3f(p2)
  120.             vertices.addData3f(p3)
  121.            
  122.             normals.addData3f(n_down)
  123.             normals.addData3f(n_down)
  124.             normals.addData3f(n_down)
  125.             normals.addData3f(n_down)
  126.         elif cube_side == 'TOP':
  127.             vertices.addData3f(p7)
  128.             vertices.addData3f(p6)
  129.             vertices.addData3f(p5)
  130.             vertices.addData3f(p4)
  131.            
  132.             normals.addData3f(n_up)
  133.             normals.addData3f(n_up)
  134.             normals.addData3f(n_up)
  135.             normals.addData3f(n_up)
  136.         elif cube_side == 'LEFT':
  137.             vertices.addData3f(p7)
  138.             vertices.addData3f(p4)
  139.             vertices.addData3f(p0)
  140.             vertices.addData3f(p3)
  141.            
  142.             normals.addData3f(n_left)
  143.             normals.addData3f(n_left)
  144.             normals.addData3f(n_left)
  145.             normals.addData3f(n_left)
  146.         elif cube_side == 'RIGHT':
  147.             vertices.addData3f(p5)
  148.             vertices.addData3f(p6)
  149.             vertices.addData3f(p2)
  150.             vertices.addData3f(p1)
  151.            
  152.             normals.addData3f(n_right)
  153.             normals.addData3f(n_right)
  154.             normals.addData3f(n_right)
  155.             normals.addData3f(n_right)
  156.         elif cube_side == 'FRONT':
  157.             vertices.addData3f(p4)
  158.             vertices.addData3f(p5)
  159.             vertices.addData3f(p1)
  160.             vertices.addData3f(p0)
  161.            
  162.             normals.addData3f(n_forward)
  163.             normals.addData3f(n_forward)
  164.             normals.addData3f(n_forward)
  165.             normals.addData3f(n_forward)
  166.         elif cube_side == 'BACK':
  167.             vertices.addData3f(p6)
  168.             vertices.addData3f(p7)
  169.             vertices.addData3f(p3)
  170.             vertices.addData3f(p2)
  171.            
  172.             normals.addData3f(n_back)
  173.             normals.addData3f(n_back)
  174.             normals.addData3f(n_back)
  175.             normals.addData3f(n_back)
  176.        
  177.         if not empty:
  178.             texcoord.addData2f(uv11)
  179.             texcoord.addData2f(uv01)
  180.             texcoord.addData2f(uv00)
  181.             texcoord.addData2f(uv10)
  182.                        
  183.         primitive.addVertices(3, 1, 0)
  184.         primitive.addVertices(3, 2, 1)
  185.  
  186.         geom.addPrimitive(primitive)
  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)
  194.                 self.node.addGeom(geom)
  195.             return self.node
  196.         try:
  197.             if not chunks[x][y][z+1].is_solid:
  198.                 geom = self.create_cube('FRONT', False)
  199.                 self.node.addGeom(geom)
  200.         except:
  201.               geom = self.create_cube('FRONT', True)
  202.               self.node.addGeom(geom)
  203.         try:
  204.             if not chunks[x][y][z-1].is_solid:
  205.                 geom = self.create_cube('BACK', False)
  206.                 self.node.addGeom(geom)
  207.         except:
  208.             geom = self.create_cube('BACK', True)
  209.             self.node.addGeom(geom)
  210.         try:
  211.             if not chunks[x][y+1][z].is_solid:
  212.                 geom = self.create_cube('TOP', False)
  213.                 self.node.addGeom(geom)
  214.         except:
  215.             geom = self.create_cube('TOP', True)
  216.             self.node.addGeom(geom)
  217.         try:
  218.             if not chunks[x][y-1][z].is_solid:
  219.                 geom = self.create_cube('BOTTOM', False)
  220.                 self.node.addGeom(geom)
  221.         except:
  222.             geom = self.create_cube('BOTTOM', True)
  223.             self.node.addGeom(geom)
  224.         try:
  225.             if not chunks[x+1][y][z].is_solid:
  226.                 geom = self.create_cube('RIGHT', False)
  227.                 self.node.addGeom(geom)
  228.         except:
  229.             geom = self.create_cube('RIGHT', True)
  230.             self.node.addGeom(geom)
  231.         try:
  232.             if not chunks[x-1][y][z].is_solid:
  233.                 geom = self.create_cube('LEFT', False)
  234.                 self.node.addGeom(geom)
  235.         except:
  236.             geom = self.create_cube('LEFT', True)
  237.             self.node.addGeom(geom)
  238.            
  239.         return self.node
  240.  
  241. app = Chunk()
  242. app.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top