Advertisement
Guest User

cube

a guest
Apr 5th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. extends MeshInstance
  2.  
  3. var array_quad_vertices = [];
  4. var array_quad_indices = [];
  5.  
  6. var dictionary_check_quad_vertices = {};
  7.  
  8. const CUBE_SIZE = 0.5;
  9.  
  10.  
  11. func _ready():
  12.     make_cube();
  13.  
  14.  
  15. func make_cube():
  16.     array_quad_vertices = [];
  17.     array_quad_indices = [];
  18.     dictionary_check_quad_vertices = {};
  19.    
  20.     var result_mesh = Mesh.new();
  21.     var surface_tool = SurfaceTool.new();
  22.    
  23.     surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES);
  24.    
  25.     var vert_north_topright = Vector3(-CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
  26.     var vert_north_topleft = Vector3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
  27.     var vert_north_bottomleft = Vector3(CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE);
  28.     var vert_north_bottomright = Vector3(-CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE);
  29.    
  30.     var vert_south_topright = Vector3(-CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE);
  31.     var vert_south_topleft = Vector3(CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE);
  32.     var vert_south_bottomleft = Vector3(CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE);
  33.     var vert_south_bottomright = Vector3(-CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE);
  34.    
  35.    
  36.     # Make the six quads for needed to make a box!
  37.     # ============================================
  38.     # IMPORTANT: You have to input the points in the going either clockwise, or counter clockwise
  39.     # or the add_quad function will not work!
  40.    
  41.     add_quad(vert_south_topright, vert_south_topleft, vert_south_bottomleft, vert_south_bottomright);
  42.     add_quad(vert_north_topright, vert_north_bottomright, vert_north_bottomleft, vert_north_topleft);
  43.    
  44.     add_quad(vert_north_bottomleft, vert_north_bottomright, vert_south_bottomright, vert_south_bottomleft);
  45.     add_quad(vert_north_topleft, vert_south_topleft, vert_south_topright, vert_north_topright);
  46.    
  47.     add_quad(vert_north_topright, vert_south_topright, vert_south_bottomright, vert_north_bottomright);
  48.     add_quad(vert_north_topleft, vert_north_bottomleft, vert_south_bottomleft, vert_south_topleft);
  49.     # ============================================
  50.    
  51.     for vertex in array_quad_vertices:
  52.         surface_tool.add_vertex(vertex);
  53.     for index in array_quad_indices:
  54.         surface_tool.add_index(index);
  55.    
  56.     surface_tool.generate_normals();
  57.    
  58.     result_mesh = surface_tool.commit();
  59.     self.mesh = result_mesh;
  60.  
  61.  
  62. func add_quad(point_1, point_2, point_3, point_4):
  63.    
  64.     var vertex_index_one = -1;
  65.     var vertex_index_two = -1;
  66.     var vertex_index_three = -1;
  67.     var vertex_index_four = -1;
  68.    
  69.     vertex_index_one = _add_or_get_vertex_from_array(point_1);
  70.     vertex_index_two = _add_or_get_vertex_from_array(point_2);
  71.     vertex_index_three = _add_or_get_vertex_from_array(point_3);
  72.     vertex_index_four = _add_or_get_vertex_from_array(point_4);
  73.    
  74.     array_quad_indices.append(vertex_index_one)
  75.     array_quad_indices.append(vertex_index_two)
  76.     array_quad_indices.append(vertex_index_three)
  77.    
  78.     array_quad_indices.append(vertex_index_one)
  79.     array_quad_indices.append(vertex_index_three)
  80.     array_quad_indices.append(vertex_index_four)
  81.  
  82.  
  83. func _add_or_get_vertex_from_array(vertex):
  84.     if dictionary_check_quad_vertices.has(vertex) == true:
  85.         return dictionary_check_quad_vertices[vertex];
  86.    
  87.     else:
  88.         array_quad_vertices.append(vertex);
  89.        
  90.         dictionary_check_quad_vertices[vertex] = array_quad_vertices.size()-1;
  91.         return array_quad_vertices.size()-1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement