فهرست منبع

-Largely rewrote gridmap to simplify it
-Got editor working again
-Added a current-floor marker on selection

Juan Linietsky 8 سال پیش
والد
کامیت
37da8155a4

+ 8 - 13
editor/plugins/cube_grid_theme_editor_plugin.cpp

@@ -98,7 +98,7 @@ void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library,
 
 
 		p_library->set_item_mesh(id, mesh);
 		p_library->set_item_mesh(id, mesh);
 
 
-		Ref<Shape> collision;
+		Vector<MeshLibrary::ShapeData> collisions;
 
 
 		for (int j = 0; j < mi->get_child_count(); j++) {
 		for (int j = 0; j < mi->get_child_count(); j++) {
 
 
@@ -119,24 +119,19 @@ void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library,
 
 
 				for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
 				for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
 
 
-					collision = sb->shape_owner_get_shape(E->get(), k);
+					Ref<Shape> collision = sb->shape_owner_get_shape(E->get(), k);
 					if (collision.is_valid())
 					if (collision.is_valid())
-						break;
-					/*					TileSet::ShapeData shape_data;
-					shape_data.shape = shape;
-					shape_data.shape_transform = shape_transform;
-					shape_data.one_way_collision = one_way;
-					collisions.push_back(shape_data);*/
+						continue;
+					MeshLibrary::ShapeData shape_data;
+					shape_data.shape = collision;
+					shape_data.local_transform = sb->shape_owner_get_transform(E->get());
+					collisions.push_back(shape_data);
 				}
 				}
-				if (collision.is_valid())
-					break;
 			}
 			}
 		}
 		}
 
 
-		if (!collision.is_null()) {
+		p_library->set_item_shapes(id, collisions);
 
 
-			p_library->set_item_shape(id, collision);
-		}
 		Ref<NavigationMesh> navmesh;
 		Ref<NavigationMesh> navmesh;
 		for (int j = 0; j < mi->get_child_count(); j++) {
 		for (int j = 0; j < mi->get_child_count(); j++) {
 			Node *child2 = mi->get_child(j);
 			Node *child2 = mi->get_child(j);

+ 243 - 267
modules/gridmap/grid_map.cpp

@@ -46,7 +46,13 @@ bool GridMap::_set(const StringName &p_name, const Variant &p_value) {
 
 
 		set_theme(p_value);
 		set_theme(p_value);
 	} else if (name == "cell_size") {
 	} else if (name == "cell_size") {
-		set_cell_size(p_value);
+		if (p_value.get_type() == Variant::INT || p_value.get_type() == Variant::REAL) {
+			//compatibility
+			float cs = p_value;
+			set_cell_size(Vector3(cs, cs, cs));
+		} else {
+			set_cell_size(p_value);
+		}
 	} else if (name == "cell_octant_size") {
 	} else if (name == "cell_octant_size") {
 		set_octant_size(p_value);
 		set_octant_size(p_value);
 	} else if (name == "cell_center_x") {
 	} else if (name == "cell_center_x") {
@@ -176,12 +182,12 @@ Ref<MeshLibrary> GridMap::get_theme() const {
 	return theme;
 	return theme;
 }
 }
 
 
-void GridMap::set_cell_size(float p_size) {
+void GridMap::set_cell_size(const Vector3 &p_size) {
 
 
 	cell_size = p_size;
 	cell_size = p_size;
 	_recreate_octant_data();
 	_recreate_octant_data();
 }
 }
-float GridMap::get_cell_size() const {
+Vector3 GridMap::get_cell_size() const {
 
 
 	return cell_size;
 	return cell_size;
 }
 }
@@ -242,56 +248,29 @@ void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
 	ok.y = p_y / octant_size;
 	ok.y = p_y / octant_size;
 	ok.z = p_z / octant_size;
 	ok.z = p_z / octant_size;
 
 
-	if (cell_map.has(key)) {
-
-		int prev_item = cell_map[key].item;
-
-		OctantKey octantkey = ok;
-
-		ERR_FAIL_COND(!octant_map.has(octantkey));
-		Octant &g = *octant_map[octantkey];
-		ERR_FAIL_COND(!g.items.has(prev_item));
-		ERR_FAIL_COND(!g.items[prev_item].cells.has(key));
-
-		g.items[prev_item].cells.erase(key);
-		if (g.items[prev_item].cells.size() == 0) {
-			VS::get_singleton()->free(g.items[prev_item].multimesh_instance);
-			g.items.erase(prev_item);
-		}
-		if (g.items.empty()) {
-
-			PhysicsServer::get_singleton()->free(g.static_body);
-			if (g.collision_debug.is_valid()) {
-				PhysicsServer::get_singleton()->free(g.collision_debug);
-				PhysicsServer::get_singleton()->free(g.collision_debug_instance);
-			}
-
-			memdelete(&g);
-			octant_map.erase(octantkey);
-		} else {
+	if (p_item < 0) {
+		//erase
+		if (cell_map.has(key)) {
+			OctantKey octantkey = ok;
 
 
+			ERR_FAIL_COND(!octant_map.has(octantkey));
+			Octant &g = *octant_map[octantkey];
+			g.cells.erase(key);
 			g.dirty = true;
 			g.dirty = true;
+			cell_map.erase(key);
+			_queue_octants_dirty();
 		}
 		}
-		cell_map.erase(key);
-
-		_queue_dirty_map();
-	}
-
-	if (p_item < 0)
 		return;
 		return;
+	}
 
 
 	OctantKey octantkey = ok;
 	OctantKey octantkey = ok;
 
 
-	//add later
 	if (!octant_map.has(octantkey)) {
 	if (!octant_map.has(octantkey)) {
-
+		//create octant because it does not exist
 		Octant *g = memnew(Octant);
 		Octant *g = memnew(Octant);
 		g->dirty = true;
 		g->dirty = true;
 		g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
 		g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
 		PhysicsServer::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
 		PhysicsServer::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
-		if (is_inside_world())
-			PhysicsServer::get_singleton()->body_set_space(g->static_body, get_world()->get_space());
-
 		SceneTree *st = SceneTree::get_singleton();
 		SceneTree *st = SceneTree::get_singleton();
 
 
 		if (st && st->is_debugging_collisions_hint()) {
 		if (st && st->is_debugging_collisions_hint()) {
@@ -299,45 +278,26 @@ void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
 			g->collision_debug = VisualServer::get_singleton()->mesh_create();
 			g->collision_debug = VisualServer::get_singleton()->mesh_create();
 			g->collision_debug_instance = VisualServer::get_singleton()->instance_create();
 			g->collision_debug_instance = VisualServer::get_singleton()->instance_create();
 			VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
 			VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
-			if (is_inside_world()) {
-				VisualServer::get_singleton()->instance_set_scenario(g->collision_debug_instance, get_world()->get_scenario());
-				VisualServer::get_singleton()->instance_set_transform(g->collision_debug_instance, get_global_transform());
-			}
 		}
 		}
 
 
 		octant_map[octantkey] = g;
 		octant_map[octantkey] = g;
-	}
-
-	Octant &g = *octant_map[octantkey];
-	if (!g.items.has(p_item)) {
 
 
-		Octant::ItemInstances ii;
-		if (theme.is_valid() && theme->has_item(p_item)) {
-			ii.mesh = theme->get_item_mesh(p_item);
-			ii.shape = theme->get_item_shape(p_item);
-			ii.navmesh = theme->get_item_navmesh(p_item);
+		if (is_inside_world()) {
+			_octant_enter_world(octantkey);
+			_octant_transform(octantkey);
 		}
 		}
-		ii.multimesh = Ref<MultiMesh>(memnew(MultiMesh));
-		ii.multimesh->set_color_format(MultiMesh::COLOR_NONE);
-		ii.multimesh->set_transform_format(MultiMesh::TRANSFORM_3D);
-		ii.multimesh->set_mesh(ii.mesh);
-		ii.multimesh_instance = VS::get_singleton()->instance_create();
-		VS::get_singleton()->instance_set_base(ii.multimesh_instance, ii.multimesh->get_rid());
-		VS::get_singleton()->instance_geometry_set_flag(ii.multimesh_instance, VS::INSTANCE_FLAG_USE_BAKED_LIGHT, true);
-
-		g.items[p_item] = ii;
 	}
 	}
 
 
-	Octant::ItemInstances &ii = g.items[p_item];
-	ii.cells.insert(key);
+	Octant &g = *octant_map[octantkey];
+	g.cells.insert(key);
 	g.dirty = true;
 	g.dirty = true;
+	_queue_octants_dirty();
 
 
-	_queue_dirty_map();
-
-	cell_map[key] = Cell();
-	Cell &c = cell_map[key];
+	Cell c;
 	c.item = p_item;
 	c.item = p_item;
 	c.rot = p_rot;
 	c.rot = p_rot;
+
+	cell_map[key] = c;
 }
 }
 
 
 int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
 int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
@@ -372,115 +332,57 @@ int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
 	return cell_map[key].rot;
 	return cell_map[key].rot;
 }
 }
 
 
-void GridMap::_octant_enter_tree(const OctantKey &p_key) {
-	ERR_FAIL_COND(!octant_map.has(p_key));
-	if (navigation) {
-		Octant &g = *octant_map[p_key];
-
-		Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
-		_octant_clear_navmesh(p_key);
-
-		for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
-			Octant::ItemInstances &ii = E->get();
-
-			for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
-
-				IndexKey ik = F->get();
-				Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
-				ERR_CONTINUE(!C);
-
-				Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
-
-				Transform xform;
-
-				if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
-
-					xform.basis.set_zero();
-
-				} else {
-
-					xform.basis.set_orthogonal_index(C->get().rot);
-				}
-
-				xform.set_origin(cellpos * cell_size + ofs);
-				xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
-				// add the item's navmesh at given xform to GridMap's Navigation ancestor
-				if (ii.navmesh.is_valid()) {
-					int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
-					Octant::NavMesh nm;
-					nm.id = nm_id;
-					nm.xform = xform;
-					g.navmesh_ids[ik] = nm;
-				}
-			}
-		}
-	}
-}
-
-void GridMap::_octant_enter_world(const OctantKey &p_key) {
+void GridMap::_octant_transform(const OctantKey &p_key) {
 
 
 	ERR_FAIL_COND(!octant_map.has(p_key));
 	ERR_FAIL_COND(!octant_map.has(p_key));
 	Octant &g = *octant_map[p_key];
 	Octant &g = *octant_map[p_key];
 	PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
 	PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
-	PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
-	//print_line("BODYPOS: "+get_global_transform());
 
 
 	if (g.collision_debug_instance.is_valid()) {
 	if (g.collision_debug_instance.is_valid()) {
-		VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
 		VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
 		VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
 	}
 	}
-	for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
 
 
-		VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, get_world()->get_scenario());
-		VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
-		//print_line("INSTANCEPOS: "+get_global_transform());
+	for (int i = 0; i < g.multimesh_instances.size(); i++) {
+		VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
 	}
 	}
 }
 }
 
 
-void GridMap::_octant_transform(const OctantKey &p_key) {
-
-	ERR_FAIL_COND(!octant_map.has(p_key));
+bool GridMap::_octant_update(const OctantKey &p_key) {
+	ERR_FAIL_COND_V(!octant_map.has(p_key), false);
 	Octant &g = *octant_map[p_key];
 	Octant &g = *octant_map[p_key];
-	PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
+	if (!g.dirty)
+		return false;
 
 
-	if (g.collision_debug_instance.is_valid()) {
-		VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
-	}
+	//erase body shapes
+	PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
 
 
-	for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
+	//erase body shapes debug
+	if (g.collision_debug.is_valid()) {
 
 
-		VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
-		//print_line("UPDATEPOS: "+get_global_transform());
+		VS::get_singleton()->mesh_clear(g.collision_debug);
 	}
 	}
-}
 
 
-void GridMap::_octant_clear_navmesh(const OctantKey &p_key) {
-	Octant &g = *octant_map[p_key];
+	//erase navigation
 	if (navigation) {
 	if (navigation) {
 		for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
 		for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
-			Octant::NavMesh *nvm = &E->get();
-			if (nvm && nvm->id) {
-				navigation->navmesh_remove(E->get().id);
-			}
+			navigation->navmesh_remove(E->get().id);
 		}
 		}
 		g.navmesh_ids.clear();
 		g.navmesh_ids.clear();
 	}
 	}
-}
-
-void GridMap::_octant_update(const OctantKey &p_key) {
-	ERR_FAIL_COND(!octant_map.has(p_key));
-	Octant &g = *octant_map[p_key];
-	if (!g.dirty)
-		return;
 
 
-	Ref<Mesh> mesh;
+	//erase multimeshes
 
 
-	_octant_clear_navmesh(p_key);
-	PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
+	for (int i = 0; i < g.multimesh_instances.size(); i++) {
 
 
-	if (g.collision_debug.is_valid()) {
+		VS::get_singleton()->free(g.multimesh_instances[i].instance);
+		VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
+	}
+	g.multimesh_instances.clear();
 
 
-		VS::get_singleton()->mesh_clear(g.collision_debug);
+	if (g.cells.size() == 0) {
+		//octant no longer needed
+		_octant_clean_up(p_key);
+		return true;
 	}
 	}
 
 
 	PoolVector<Vector3> col_debug;
 	PoolVector<Vector3> col_debug;
@@ -490,80 +392,111 @@ void GridMap::_octant_update(const OctantKey &p_key) {
 	 * set item's multimesh's instance count to number of cells which have this item
 	 * set item's multimesh's instance count to number of cells which have this item
 	 * and set said multimesh bounding box to one containing all cells which have this item
 	 * and set said multimesh bounding box to one containing all cells which have this item
 	 */
 	 */
-	for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
 
 
-		Octant::ItemInstances &ii = E->get();
+	Map<int, List<Pair<Transform, IndexKey> > > multimesh_items;
 
 
-		ii.multimesh->set_instance_count(ii.cells.size());
+	print_line("updating octant " + itos(p_key.x) + ", " + itos(p_key.y) + ", " + itos(p_key.z) + " cells: " + itos(g.cells.size()));
 
 
-		Rect3 aabb;
-		Rect3 mesh_aabb = ii.mesh.is_null() ? Rect3() : ii.mesh->get_aabb();
+	for (Set<IndexKey>::Element *E = g.cells.front(); E; E = E->next()) {
 
 
-		Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
+		ERR_CONTINUE(!cell_map.has(E->get()));
+		const Cell &c = cell_map[E->get()];
+
+		if (!theme.is_valid() || !theme->has_item(c.item))
+			continue;
 
 
 		//print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
 		//print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
-		int idx = 0;
-		// foreach cell containing this item type
-		for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
-			IndexKey ik = F->get();
-			Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
-			ERR_CONTINUE(!C);
 
 
-			Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
+		Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z);
+		Vector3 ofs(cell_size.x * 0.5 * int(center_x), cell_size.y * 0.5 * int(center_y), cell_size.z * 0.5 * int(center_z));
 
 
-			Transform xform;
+		Transform xform;
 
 
-			if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
+		if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
 
 
-				xform.basis.set_zero();
+		} else {
+		}
 
 
-			} else {
+		xform.basis.set_orthogonal_index(c.rot);
+		xform.set_origin(cellpos * cell_size + ofs);
+		xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
 
 
-				xform.basis.set_orthogonal_index(C->get().rot);
+		if (theme->get_item_mesh(c.item).is_valid()) {
+			if (!multimesh_items.has(c.item)) {
+				multimesh_items[c.item] = List<Pair<Transform, IndexKey> >();
 			}
 			}
 
 
-			xform.set_origin(cellpos * cell_size + ofs);
-			xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
+			Pair<Transform, IndexKey> p;
+			p.first = xform;
+			p.second = E->get();
+			multimesh_items[c.item].push_back(p);
+		}
 
 
-			ii.multimesh->set_instance_transform(idx, xform);
-			//ii.multimesh->set_instance_transform(idx,Transform()	);
-			//ii.multimesh->set_instance_color(idx,Color(1,1,1,1));
-			//print_line("MMINST: "+xform);
+		Vector<MeshLibrary::ShapeData> shapes = theme->get_item_shapes(c.item);
+		// add the item's shape at given xform to octant's static_body
+		for (int i = 0; i < shapes.size(); i++) {
+			// add the item's shape
+			if (!shapes[i].shape.is_valid())
+				continue;
+			PhysicsServer::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
+			if (g.collision_debug.is_valid()) {
+				shapes[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
+			}
 
 
-			if (idx == 0) {
+			//print_line("PHIS x: "+xform);
+		}
 
 
-				aabb = xform.xform(mesh_aabb);
-			} else {
+		// add the item's navmesh at given xform to GridMap's Navigation ancestor
+		Ref<NavigationMesh> navmesh = theme->get_item_navmesh(c.item);
+		if (navmesh.is_valid()) {
+			Octant::NavMesh nm;
+			nm.xform = xform;
 
 
-				aabb.merge_with(xform.xform(mesh_aabb));
+			if (navigation) {
+				nm.id = navigation->navmesh_create(navmesh, xform, this);
+			} else {
+				nm.id = -1;
 			}
 			}
+			g.navmesh_ids[E->get()] = nm;
+		}
+	}
 
 
-			// add the item's shape at given xform to octant's static_body
-			if (ii.shape.is_valid()) {
-				// add the item's shape
-				PhysicsServer::get_singleton()->body_add_shape(g.static_body, ii.shape->get_rid(), xform);
-				if (g.collision_debug.is_valid()) {
-					ii.shape->add_vertices_to_array(col_debug, xform);
-				}
+	//update multimeshes
+	for (Map<int, List<Pair<Transform, IndexKey> > >::Element *E = multimesh_items.front(); E; E = E->next()) {
+		print_line("multimesh item " + itos(E->key()) + " transforms " + itos(E->get().size()));
+		Octant::MultimeshInstance mmi;
 
 
-				//print_line("PHIS x: "+xform);
-			}
+		RID mm = VS::get_singleton()->multimesh_create();
+		VS::get_singleton()->multimesh_allocate(mm, E->get().size(), VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
+		VS::get_singleton()->multimesh_set_mesh(mm, theme->get_item_mesh(E->key())->get_rid());
 
 
-			// add the item's navmesh at given xform to GridMap's Navigation ancestor
-			if (navigation) {
-				if (ii.navmesh.is_valid()) {
-					int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
-					Octant::NavMesh nm;
-					nm.id = nm_id;
-					nm.xform = xform;
-					g.navmesh_ids[ik] = nm;
-				}
-			}
+		int idx = 0;
+		for (List<Pair<Transform, IndexKey> >::Element *F = E->get().front(); F; F = F->next()) {
+			VS::get_singleton()->multimesh_instance_set_transform(mm, idx, F->get().first);
+#ifdef TOOLS_ENABLED
+
+			Octant::MultimeshInstance::Item it;
+			it.index = idx;
+			it.transform = F->get().first;
+			it.key = F->get().second;
+			mmi.items.push_back(it);
+#endif
 
 
 			idx++;
 			idx++;
 		}
 		}
 
 
-		//ii.multimesh->set_aabb(aabb);
+		RID instance = VS::get_singleton()->instance_create();
+		VS::get_singleton()->instance_set_base(instance, mm);
+
+		if (is_inside_tree()) {
+			VS::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
+			VS::get_singleton()->instance_set_transform(instance, get_global_transform());
+		}
+
+		mmi.multimesh = mm;
+		mmi.instance = instance;
+
+		g.multimesh_instances.push_back(mmi);
 	}
 	}
 
 
 	if (col_debug.size()) {
 	if (col_debug.size()) {
@@ -580,6 +513,39 @@ void GridMap::_octant_update(const OctantKey &p_key) {
 	}
 	}
 
 
 	g.dirty = false;
 	g.dirty = false;
+
+	return false;
+}
+
+void GridMap::_octant_enter_world(const OctantKey &p_key) {
+
+	ERR_FAIL_COND(!octant_map.has(p_key));
+	Octant &g = *octant_map[p_key];
+	PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
+	PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
+	//print_line("BODYPOS: "+get_global_transform());
+
+	if (g.collision_debug_instance.is_valid()) {
+		VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
+		VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
+	}
+
+	for (int i = 0; i < g.multimesh_instances.size(); i++) {
+		VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world()->get_scenario());
+		VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
+	}
+
+	if (navigation && theme.is_valid()) {
+		for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
+
+			if (cell_map.has(F->key()) && F->get().id < 0) {
+				Ref<NavigationMesh> nm = theme->get_item_navmesh(cell_map[F->key()].item);
+				if (nm.is_valid()) {
+					F->get().id = navigation->navmesh_create(nm, F->get().xform, this);
+				}
+			}
+		}
+	}
 }
 }
 
 
 void GridMap::_octant_exit_world(const OctantKey &p_key) {
 void GridMap::_octant_exit_world(const OctantKey &p_key) {
@@ -594,30 +560,73 @@ void GridMap::_octant_exit_world(const OctantKey &p_key) {
 		VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
 		VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
 	}
 	}
 
 
-	for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
+	for (int i = 0; i < g.multimesh_instances.size(); i++) {
+		VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
+	}
+
+	if (navigation) {
+		for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
 
 
-		VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, RID());
-		//VS::get_singleton()->instance_set_transform(E->get().multimesh_instance,get_global_transform());
+			if (F->get().id >= 0) {
+				navigation->navmesh_remove(F->get().id);
+				F->get().id = -1;
+			}
+		}
 	}
 	}
 }
 }
 
 
+void GridMap::_octant_clean_up(const OctantKey &p_key) {
+
+	ERR_FAIL_COND(!octant_map.has(p_key));
+	Octant &g = *octant_map[p_key];
+
+	if (g.collision_debug.is_valid())
+		VS::get_singleton()->free(g.collision_debug);
+	if (g.collision_debug_instance.is_valid())
+		VS::get_singleton()->free(g.collision_debug_instance);
+
+	PhysicsServer::get_singleton()->free(g.static_body);
+
+	//erase navigation
+	if (navigation) {
+		for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
+			navigation->navmesh_remove(E->get().id);
+		}
+		g.navmesh_ids.clear();
+	}
+
+	//erase multimeshes
+
+	for (int i = 0; i < g.multimesh_instances.size(); i++) {
+
+		VS::get_singleton()->free(g.multimesh_instances[i].instance);
+		VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
+	}
+	g.multimesh_instances.clear();
+}
+
 void GridMap::_notification(int p_what) {
 void GridMap::_notification(int p_what) {
 
 
 	switch (p_what) {
 	switch (p_what) {
 
 
 		case NOTIFICATION_ENTER_WORLD: {
 		case NOTIFICATION_ENTER_WORLD: {
 
 
-			for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
-				//IndexKey ik;
-				//ik.key = E->key().indexkey;
-				_octant_enter_world(E->key());
-				_octant_update(E->key());
-			}
+			Spatial *c = this;
+			while (c) {
+				navigation = Object::cast_to<Navigation>(c);
+				if (navigation) {
+					break;
+				}
 
 
-			awaiting_update = false;
+				c = Object::cast_to<Spatial>(c->get_parent());
+			}
 
 
 			last_transform = get_global_transform();
 			last_transform = get_global_transform();
 
 
+			for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
+				_octant_enter_world(E->key());
+			}
+
 		} break;
 		} break;
 		case NOTIFICATION_TRANSFORM_CHANGED: {
 		case NOTIFICATION_TRANSFORM_CHANGED: {
 
 
@@ -638,56 +647,23 @@ void GridMap::_notification(int p_what) {
 				_octant_exit_world(E->key());
 				_octant_exit_world(E->key());
 			}
 			}
 
 
-			//_queue_dirty_map(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
-			//_update_dirty_map_callback();
-			//_update_area_instances();
-
-		} break;
-		case NOTIFICATION_ENTER_TREE: {
-
-			Spatial *c = this;
-			while (c) {
-				navigation = Object::cast_to<Navigation>(c);
-				if (navigation) {
-					break;
-				}
-
-				c = Object::cast_to<Spatial>(c->get_parent());
-			}
-
-			if (navigation) {
-				for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
-					if (navigation) {
-						_octant_enter_tree(E->key());
-					}
-				}
-			}
-
-			_queue_dirty_map();
-		} break;
-		case NOTIFICATION_EXIT_TREE: {
-			for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
-				if (navigation) {
-					_octant_clear_navmesh(E->key());
-				}
-			}
-
 			navigation = NULL;
 			navigation = NULL;
 
 
+			//_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
+			//_update_octants_callback();
+			//_update_area_instances();
+
 		} break;
 		} break;
 	}
 	}
 }
 }
 
 
-void GridMap::_queue_dirty_map() {
+void GridMap::_queue_octants_dirty() {
 
 
 	if (awaiting_update)
 	if (awaiting_update)
 		return;
 		return;
 
 
-	if (is_inside_world()) {
-
-		MessageQueue::get_singleton()->push_call(this, "_update_dirty_map_callback");
-		awaiting_update = true;
-	}
+	MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
+	awaiting_update = true;
 }
 }
 
 
 void GridMap::_recreate_octant_data() {
 void GridMap::_recreate_octant_data() {
@@ -706,17 +682,7 @@ void GridMap::_clear_internal() {
 		if (is_inside_world())
 		if (is_inside_world())
 			_octant_exit_world(E->key());
 			_octant_exit_world(E->key());
 
 
-		for (Map<int, Octant::ItemInstances>::Element *F = E->get()->items.front(); F; F = F->next()) {
-
-			VS::get_singleton()->free(F->get().multimesh_instance);
-		}
-
-		if (E->get()->collision_debug.is_valid())
-			VS::get_singleton()->free(E->get()->collision_debug);
-		if (E->get()->collision_debug_instance.is_valid())
-			VS::get_singleton()->free(E->get()->collision_debug_instance);
-
-		PhysicsServer::get_singleton()->free(E->get()->static_body);
+		_octant_clean_up(E->key());
 		memdelete(E->get());
 		memdelete(E->get());
 	}
 	}
 
 
@@ -734,13 +700,23 @@ void GridMap::resource_changed(const RES &p_res) {
 	_recreate_octant_data();
 	_recreate_octant_data();
 }
 }
 
 
-void GridMap::_update_dirty_map_callback() {
+void GridMap::_update_octants_callback() {
 
 
 	if (!awaiting_update)
 	if (!awaiting_update)
 		return;
 		return;
 
 
+	List<OctantKey> to_delete;
 	for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
 	for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
-		_octant_update(E->key());
+
+		if (_octant_update(E->key())) {
+			to_delete.push_back(E->key());
+		}
+	}
+
+	while (to_delete.front()) {
+		memdelete(octant_map[to_delete.front()->get()]);
+		octant_map.erase(to_delete.front()->get());
+		to_delete.pop_back();
 	}
 	}
 
 
 	awaiting_update = false;
 	awaiting_update = false;
@@ -762,7 +738,7 @@ void GridMap::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
 	ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
 
 
 	//ClassDB::bind_method(D_METHOD("_recreate_octants"),&GridMap::_recreate_octants);
 	//ClassDB::bind_method(D_METHOD("_recreate_octants"),&GridMap::_recreate_octants);
-	ClassDB::bind_method(D_METHOD("_update_dirty_map_callback"), &GridMap::_update_dirty_map_callback);
+	ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
 	ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
 	ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
 
 
 	ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
 	ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
@@ -800,13 +776,13 @@ void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::
 		g->dirty = true;
 		g->dirty = true;
 	}
 	}
 	awaiting_update = true;
 	awaiting_update = true;
-	_update_dirty_map_callback();
+	_update_octants_callback();
 }
 }
 
 
 void GridMap::set_cell_scale(float p_scale) {
 void GridMap::set_cell_scale(float p_scale) {
 
 
 	cell_scale = p_scale;
 	cell_scale = p_scale;
-	_queue_dirty_map();
+	_queue_octants_dirty();
 }
 }
 
 
 float GridMap::get_cell_scale() const {
 float GridMap::get_cell_scale() const {
@@ -819,7 +795,7 @@ Array GridMap::get_meshes() {
 	if (theme.is_null())
 	if (theme.is_null())
 		return Array();
 		return Array();
 
 
-	Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
+	Vector3 ofs(cell_size.x * 0.5 * int(center_x), cell_size.y * 0.5 * int(center_y), cell_size.z * 0.5 * int(center_z));
 	Array meshes;
 	Array meshes;
 
 
 	for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
 	for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
@@ -851,7 +827,7 @@ Array GridMap::get_meshes() {
 
 
 GridMap::GridMap() {
 GridMap::GridMap() {
 
 
-	cell_size = 2;
+	cell_size = Vector3(2, 2, 2);
 	octant_size = 4;
 	octant_size = 4;
 	awaiting_update = false;
 	awaiting_update = false;
 	_in_tree = false;
 	_in_tree = false;

+ 19 - 16
modules/gridmap/grid_map.h

@@ -96,21 +96,25 @@ class GridMap : public Spatial {
 			Transform xform;
 			Transform xform;
 		};
 		};
 
 
-		struct ItemInstances {
-			Set<IndexKey> cells;
-			Ref<Mesh> mesh;
-			Ref<Shape> shape;
-			Ref<MultiMesh> multimesh;
-			RID multimesh_instance;
-			Ref<NavigationMesh> navmesh;
+		struct MultimeshInstance {
+			RID instance;
+			RID multimesh;
+			struct Item {
+				int index;
+				Transform transform;
+				IndexKey key;
+			};
+
+			Vector<Item> items; //tools only, for changing visibility
 		};
 		};
 
 
+		Vector<MultimeshInstance> multimesh_instances;
+		Set<IndexKey> cells;
 		RID collision_debug;
 		RID collision_debug;
 		RID collision_debug_instance;
 		RID collision_debug_instance;
 
 
 		bool dirty;
 		bool dirty;
 		RID static_body;
 		RID static_body;
-		Map<int, ItemInstances> items;
 		Map<IndexKey, NavMesh> navmesh_ids;
 		Map<IndexKey, NavMesh> navmesh_ids;
 	};
 	};
 
 
@@ -137,7 +141,7 @@ class GridMap : public Spatial {
 	Transform last_transform;
 	Transform last_transform;
 
 
 	bool _in_tree;
 	bool _in_tree;
-	float cell_size;
+	Vector3 cell_size;
 	int octant_size;
 	int octant_size;
 	bool center_x, center_y, center_z;
 	bool center_x, center_y, center_z;
 	float cell_scale;
 	float cell_scale;
@@ -169,15 +173,14 @@ class GridMap : public Spatial {
 	}
 	}
 
 
 	void _octant_enter_world(const OctantKey &p_key);
 	void _octant_enter_world(const OctantKey &p_key);
-	void _octant_enter_tree(const OctantKey &p_key);
 	void _octant_exit_world(const OctantKey &p_key);
 	void _octant_exit_world(const OctantKey &p_key);
-	void _octant_update(const OctantKey &p_key);
+	bool _octant_update(const OctantKey &p_key);
+	void _octant_clean_up(const OctantKey &p_key);
 	void _octant_transform(const OctantKey &p_key);
 	void _octant_transform(const OctantKey &p_key);
-	void _octant_clear_navmesh(const GridMap::OctantKey &);
 	bool awaiting_update;
 	bool awaiting_update;
 
 
-	void _queue_dirty_map();
-	void _update_dirty_map_callback();
+	void _queue_octants_dirty();
+	void _update_octants_callback();
 
 
 	void resource_changed(const RES &p_res);
 	void resource_changed(const RES &p_res);
 
 
@@ -199,8 +202,8 @@ public:
 	void set_theme(const Ref<MeshLibrary> &p_theme);
 	void set_theme(const Ref<MeshLibrary> &p_theme);
 	Ref<MeshLibrary> get_theme() const;
 	Ref<MeshLibrary> get_theme() const;
 
 
-	void set_cell_size(float p_size);
-	float get_cell_size() const;
+	void set_cell_size(const Vector3 &p_size);
+	Vector3 get_cell_size() const;
 
 
 	void set_octant_size(int p_size);
 	void set_octant_size(int p_size);
 	int get_octant_size() const;
 	int get_octant_size() const;

+ 87 - 12
modules/gridmap/grid_map_editor_plugin.cpp

@@ -217,12 +217,15 @@ void GridMapEditor::_update_cursor_transform() {
 }
 }
 
 
 void GridMapEditor::_update_selection_transform() {
 void GridMapEditor::_update_selection_transform() {
+	Transform xf_zero;
+	xf_zero.basis.set_zero();
 
 
 	if (!selection.active) {
 	if (!selection.active) {
 
 
-		Transform xf;
-		xf.basis.set_zero();
-		VisualServer::get_singleton()->instance_set_transform(selection_instance, xf);
+		VisualServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
+		for (int i = 0; i < 3; i++) {
+			VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
+		}
 		return;
 		return;
 	}
 	}
 
 
@@ -231,6 +234,27 @@ void GridMapEditor::_update_selection_transform() {
 	xf.origin = selection.begin * node->get_cell_size();
 	xf.origin = selection.begin * node->get_cell_size();
 
 
 	VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
 	VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
+
+	for (int i = 0; i < 3; i++) {
+		if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
+			VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
+		} else {
+
+			Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
+			scale[edit_axis] = 1.0;
+			Vector3 pos = selection.begin;
+			pos[edit_axis] = edit_floor[edit_axis];
+
+			scale *= node->get_cell_size();
+			pos *= node->get_cell_size();
+
+			Transform xf;
+			xf.basis.scale(scale);
+			xf.origin = pos;
+
+			VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf);
+		}
+	}
 }
 }
 
 
 void GridMapEditor::_validate_selection() {
 void GridMapEditor::_validate_selection() {
@@ -273,7 +297,7 @@ bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, boo
 
 
 	Plane p;
 	Plane p;
 	p.normal[edit_axis] = 1.0;
 	p.normal[edit_axis] = 1.0;
-	p.d = edit_floor[edit_axis] * node->get_cell_size();
+	p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
 
 
 	Vector3 inters;
 	Vector3 inters;
 	if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
 	if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
@@ -289,7 +313,7 @@ bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, boo
 	}
 	}
 
 
 	int cell[3];
 	int cell[3];
-	float cell_size[3] = { node->get_cell_size(), node->get_cell_size(), node->get_cell_size() };
+	float cell_size[3] = { node->get_cell_size().x, node->get_cell_size().y, node->get_cell_size().z };
 
 
 	last_mouseover = Vector3(-1, -1, -1);
 	last_mouseover = Vector3(-1, -1, -1);
 
 
@@ -299,7 +323,7 @@ bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, boo
 			cell[i] = edit_floor[i];
 			cell[i] = edit_floor[i];
 		else {
 		else {
 
 
-			cell[i] = inters[i] / node->get_cell_size();
+			cell[i] = inters[i] / node->get_cell_size()[i];
 			if (inters[i] < 0)
 			if (inters[i] < 0)
 				cell[i] -= 1; //compensate negative
 				cell[i] -= 1; //compensate negative
 			grid_ofs[i] = cell[i] * cell_size[i];
 			grid_ofs[i] = cell[i] * cell_size[i];
@@ -717,6 +741,7 @@ void GridMapEditor::edit(GridMap *p_gridmap) {
 		//update grids
 		//update grids
 		indicator_mat.instance();
 		indicator_mat.instance();
 		indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
 		indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
+		indicator_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
 		indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
 		indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
 		indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
 		indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
 		indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
 		indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
@@ -724,7 +749,7 @@ void GridMapEditor::edit(GridMap *p_gridmap) {
 		Vector<Vector3> grid_points[3];
 		Vector<Vector3> grid_points[3];
 		Vector<Color> grid_colors[3];
 		Vector<Color> grid_colors[3];
 
 
-		float cell_size[3] = { p_gridmap->get_cell_size(), p_gridmap->get_cell_size(), p_gridmap->get_cell_size() };
+		float cell_size[3] = { p_gridmap->get_cell_size().x, p_gridmap->get_cell_size().y, p_gridmap->get_cell_size().z };
 
 
 		for (int i = 0; i < 3; i++) {
 		for (int i = 0; i < 3; i++) {
 
 
@@ -788,7 +813,7 @@ void GridMapEditor::update_grid() {
 
 
 	//VS *vs = VS::get_singleton();
 	//VS *vs = VS::get_singleton();
 
 
-	grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size();
+	grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
 
 
 	edit_grid_xform.origin = grid_ofs;
 	edit_grid_xform.origin = grid_ofs;
 	edit_grid_xform.basis = Basis();
 	edit_grid_xform.basis = Basis();
@@ -811,6 +836,7 @@ void GridMapEditor::_notification(int p_what) {
 
 
 			grid[i] = VS::get_singleton()->mesh_create();
 			grid[i] = VS::get_singleton()->mesh_create();
 			grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
 			grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
+			selection_level_instance[i] = VisualServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world()->get_scenario());
 		}
 		}
 
 
 		selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
 		selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
@@ -827,6 +853,7 @@ void GridMapEditor::_notification(int p_what) {
 			VS::get_singleton()->free(grid[i]);
 			VS::get_singleton()->free(grid[i]);
 			grid_instance[i] = RID();
 			grid_instance[i] = RID();
 			grid[i] = RID();
 			grid[i] = RID();
+			VisualServer::get_singleton()->free(selection_level_instance[i]);
 		}
 		}
 
 
 		VisualServer::get_singleton()->free(selection_instance);
 		VisualServer::get_singleton()->free(selection_instance);
@@ -858,7 +885,7 @@ void GridMapEditor::_notification(int p_what) {
 
 
 			Plane p;
 			Plane p;
 			p.normal[edit_axis] = 1.0;
 			p.normal[edit_axis] = 1.0;
-			p.d = edit_floor[edit_axis] * node->get_cell_size();
+			p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
 			p = node->get_transform().xform(p); // plane to snap
 			p = node->get_transform().xform(p); // plane to snap
 
 
 			SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
 			SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
@@ -906,6 +933,7 @@ void GridMapEditor::_floor_changed(float p_value) {
 	node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
 	node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
 	update_grid();
 	update_grid();
 	_update_clip();
 	_update_clip();
+	_update_selection_transform();
 }
 }
 
 
 void GridMapEditor::_bind_methods() {
 void GridMapEditor::_bind_methods() {
@@ -1047,6 +1075,7 @@ GridMapEditor::GridMapEditor(EditorNode *p_editor) {
 
 
 		PoolVector<Vector3> lines;
 		PoolVector<Vector3> lines;
 		PoolVector<Vector3> triangles;
 		PoolVector<Vector3> triangles;
+		PoolVector<Vector3> square[3];
 
 
 		for (int i = 0; i < 6; i++) {
 		for (int i = 0; i < 6; i++) {
 
 
@@ -1086,12 +1115,41 @@ GridMapEditor::GridMapEditor(EditorNode *p_editor) {
 			lines.push_back(b);
 			lines.push_back(b);
 		}
 		}
 
 
+		for (int i = 0; i < 3; i++) {
+			Vector3 points[4];
+			for (int j = 0; j < 4; j++) {
+
+				static const bool orderx[4] = { 0, 1, 1, 0 };
+				static const bool ordery[4] = { 0, 0, 1, 1 };
+
+				Vector3 sp;
+				if (orderx[j]) {
+					sp[(i + 1) % 3] = 1.0;
+				}
+				if (ordery[j]) {
+					sp[(i + 2) % 3] = 1.0;
+				}
+
+				points[j] = sp;
+			}
+
+			for (int j = 0; j < 4; j++) {
+
+				Vector3 ofs;
+				ofs[i] += 0.01;
+				square[i].push_back(points[j] - ofs);
+				square[i].push_back(points[(j + 1) % 4] - ofs);
+				square[i].push_back(points[j] + ofs);
+				square[i].push_back(points[(j + 1) % 4] + ofs);
+			}
+		}
+
 		Array d;
 		Array d;
 		d.resize(VS::ARRAY_MAX);
 		d.resize(VS::ARRAY_MAX);
 
 
 		inner_mat.instance();
 		inner_mat.instance();
-		inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
-		inner_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
+		inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
+		//inner_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
 		inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
 		inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
 		inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
 		inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
 
 
@@ -1100,12 +1158,19 @@ GridMapEditor::GridMapEditor(EditorNode *p_editor) {
 		VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
 		VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
 
 
 		outer_mat.instance();
 		outer_mat.instance();
-		outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
+		outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
 		outer_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
 		outer_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
 		outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
 		outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
 		outer_mat->set_line_width(3.0);
 		outer_mat->set_line_width(3.0);
 		outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
 		outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
 
 
+		selection_floor_mat.instance();
+		selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
+		selection_floor_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
+		selection_floor_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
+		selection_floor_mat->set_line_width(3.0);
+		//selection_floor_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
+
 		d[VS::ARRAY_VERTEX] = lines;
 		d[VS::ARRAY_VERTEX] = lines;
 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
 		VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
 		VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
@@ -1117,6 +1182,13 @@ GridMapEditor::GridMapEditor(EditorNode *p_editor) {
 		d[VS::ARRAY_VERTEX] = lines;
 		d[VS::ARRAY_VERTEX] = lines;
 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_LINES, d);
 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_LINES, d);
 		VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat->get_rid());
 		VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat->get_rid());
+
+		for (int i = 0; i < 3; i++) {
+			d[VS::ARRAY_VERTEX] = square[i];
+			selection_level_mesh[i] = VS::get_singleton()->mesh_create();
+			VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], VS::PRIMITIVE_LINES, d);
+			VisualServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
+		}
 	}
 	}
 
 
 	selection.active = false;
 	selection.active = false;
@@ -1133,6 +1205,9 @@ GridMapEditor::~GridMapEditor() {
 			VisualServer::get_singleton()->free(grid_instance[i]);
 			VisualServer::get_singleton()->free(grid_instance[i]);
 		if (cursor_instance.is_valid())
 		if (cursor_instance.is_valid())
 			VisualServer::get_singleton()->free(cursor_instance);
 			VisualServer::get_singleton()->free(cursor_instance);
+		if (selection_level_instance[i].is_valid()) {
+			VisualServer::get_singleton()->free(selection_level_instance[i]);
+		}
 	}
 	}
 
 
 	VisualServer::get_singleton()->free(selection_mesh);
 	VisualServer::get_singleton()->free(selection_mesh);

+ 4 - 1
modules/gridmap/grid_map_editor_plugin.h

@@ -109,12 +109,15 @@ class GridMapEditor : public VBoxContainer {
 	RID cursor_instance;
 	RID cursor_instance;
 	RID selection_mesh;
 	RID selection_mesh;
 	RID selection_instance;
 	RID selection_instance;
+	RID selection_level_mesh[3];
+	RID selection_level_instance[3];
 	RID duplicate_mesh;
 	RID duplicate_mesh;
 	RID duplicate_instance;
 	RID duplicate_instance;
 
 
 	Ref<SpatialMaterial> indicator_mat;
 	Ref<SpatialMaterial> indicator_mat;
 	Ref<SpatialMaterial> inner_mat;
 	Ref<SpatialMaterial> inner_mat;
 	Ref<SpatialMaterial> outer_mat;
 	Ref<SpatialMaterial> outer_mat;
+	Ref<SpatialMaterial> selection_floor_mat;
 
 
 	bool updating;
 	bool updating;
 
 
@@ -223,7 +226,7 @@ class GridMapEditorPlugin : public EditorPlugin {
 	EditorNode *editor;
 	EditorNode *editor;
 
 
 public:
 public:
-	virtual bool forward_spatial_input_event(Camera *p_camera, const Ref<InputEvent> &p_event) { return gridmap_editor->forward_spatial_input_event(p_camera, p_event); }
+	virtual bool forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event) { return gridmap_editor->forward_spatial_input_event(p_camera, p_event); }
 	virtual String get_name() const { return "GridMap"; }
 	virtual String get_name() const { return "GridMap"; }
 	bool has_main_screen() const { return false; }
 	bool has_main_screen() const { return false; }
 	virtual void edit(Object *p_object);
 	virtual void edit(Object *p_object);

+ 48 - 13
scene/resources/mesh_library.cpp

@@ -43,9 +43,15 @@ bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
 			set_item_name(idx, p_value);
 			set_item_name(idx, p_value);
 		else if (what == "mesh")
 		else if (what == "mesh")
 			set_item_mesh(idx, p_value);
 			set_item_mesh(idx, p_value);
-		else if (what == "shape")
-			set_item_shape(idx, p_value);
-		else if (what == "preview")
+		else if (what == "shape") {
+			Vector<ShapeData> shapes;
+			ShapeData sd;
+			sd.shape = p_value;
+			shapes.push_back(sd);
+			set_item_shapes(idx, shapes);
+		} else if (what == "shapes") {
+			_set_item_shapes(idx, p_value);
+		} else if (what == "preview")
 			set_item_preview(idx, p_value);
 			set_item_preview(idx, p_value);
 		else if (what == "navmesh")
 		else if (what == "navmesh")
 			set_item_navmesh(idx, p_value);
 			set_item_navmesh(idx, p_value);
@@ -69,8 +75,8 @@ bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
 		r_ret = get_item_name(idx);
 		r_ret = get_item_name(idx);
 	else if (what == "mesh")
 	else if (what == "mesh")
 		r_ret = get_item_mesh(idx);
 		r_ret = get_item_mesh(idx);
-	else if (what == "shape")
-		r_ret = get_item_shape(idx);
+	else if (what == "shapes")
+		r_ret = _get_item_shapes(idx);
 	else if (what == "navmesh")
 	else if (what == "navmesh")
 		r_ret = get_item_navmesh(idx);
 		r_ret = get_item_navmesh(idx);
 	else if (what == "preview")
 	else if (what == "preview")
@@ -88,7 +94,7 @@ void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
 		String name = "item/" + itos(E->key()) + "/";
 		String name = "item/" + itos(E->key()) + "/";
 		p_list->push_back(PropertyInfo(Variant::STRING, name + "name"));
 		p_list->push_back(PropertyInfo(Variant::STRING, name + "name"));
 		p_list->push_back(PropertyInfo(Variant::OBJECT, name + "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
 		p_list->push_back(PropertyInfo(Variant::OBJECT, name + "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
-		p_list->push_back(PropertyInfo(Variant::OBJECT, name + "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape"));
+		p_list->push_back(PropertyInfo(Variant::ARRAY, name + "shapes"));
 		p_list->push_back(PropertyInfo(Variant::OBJECT, name + "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
 		p_list->push_back(PropertyInfo(Variant::OBJECT, name + "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
 		p_list->push_back(PropertyInfo(Variant::OBJECT, name + "preview", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_HELPER));
 		p_list->push_back(PropertyInfo(Variant::OBJECT, name + "preview", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_HELPER));
 	}
 	}
@@ -118,10 +124,10 @@ void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
 	_change_notify();
 	_change_notify();
 }
 }
 
 
-void MeshLibrary::set_item_shape(int p_item, const Ref<Shape> &p_shape) {
+void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
 
 
 	ERR_FAIL_COND(!item_map.has(p_item));
 	ERR_FAIL_COND(!item_map.has(p_item));
-	item_map[p_item].shape = p_shape;
+	item_map[p_item].shapes = p_shapes;
 	_change_notify();
 	_change_notify();
 	notify_change_to_owners();
 	notify_change_to_owners();
 	emit_changed();
 	emit_changed();
@@ -156,10 +162,10 @@ Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
 	return item_map[p_item].mesh;
 	return item_map[p_item].mesh;
 }
 }
 
 
-Ref<Shape> MeshLibrary::get_item_shape(int p_item) const {
+Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
 
 
-	ERR_FAIL_COND_V(!item_map.has(p_item), Ref<Shape>());
-	return item_map[p_item].shape;
+	ERR_FAIL_COND_V(!item_map.has(p_item), Vector<ShapeData>());
+	return item_map[p_item].shapes;
 }
 }
 
 
 Ref<NavigationMesh> MeshLibrary::get_item_navmesh(int p_item) const {
 Ref<NavigationMesh> MeshLibrary::get_item_navmesh(int p_item) const {
@@ -226,17 +232,46 @@ int MeshLibrary::get_last_unused_item_id() const {
 		return item_map.back()->key() + 1;
 		return item_map.back()->key() + 1;
 }
 }
 
 
+void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
+
+	ERR_FAIL_COND(p_shapes.size() & 1);
+	Vector<ShapeData> shapes;
+	for (int i = 0; i < p_shapes.size(); i += 2) {
+		ShapeData sd;
+		sd.shape = p_shapes[i + 0];
+		sd.local_transform = p_shapes[i + 1];
+
+		if (sd.shape.is_valid()) {
+			shapes.push_back(sd);
+		}
+	}
+
+	set_item_shapes(p_item, shapes);
+}
+
+Array MeshLibrary::_get_item_shapes(int p_item) const {
+
+	Vector<ShapeData> shapes = get_item_shapes(p_item);
+	Array ret;
+	for (int i = 0; i < shapes.size(); i++) {
+		ret.push_back(shapes[i].shape);
+		ret.push_back(shapes[i].local_transform);
+	}
+
+	return ret;
+}
+
 void MeshLibrary::_bind_methods() {
 void MeshLibrary::_bind_methods() {
 
 
 	ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
 	ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
 	ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
 	ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
 	ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
 	ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
 	ClassDB::bind_method(D_METHOD("set_item_navmesh", "id", "navmesh"), &MeshLibrary::set_item_navmesh);
 	ClassDB::bind_method(D_METHOD("set_item_navmesh", "id", "navmesh"), &MeshLibrary::set_item_navmesh);
-	ClassDB::bind_method(D_METHOD("set_item_shape", "id", "shape"), &MeshLibrary::set_item_shape);
+	ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
 	ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
 	ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
 	ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
 	ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
 	ClassDB::bind_method(D_METHOD("get_item_navmesh", "id"), &MeshLibrary::get_item_navmesh);
 	ClassDB::bind_method(D_METHOD("get_item_navmesh", "id"), &MeshLibrary::get_item_navmesh);
-	ClassDB::bind_method(D_METHOD("get_item_shape", "id"), &MeshLibrary::get_item_shape);
+	ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
 	ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
 	ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
 	ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
 	ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
 	ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
 	ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);

+ 11 - 3
scene/resources/mesh_library.h

@@ -41,16 +41,24 @@ class MeshLibrary : public Resource {
 	GDCLASS(MeshLibrary, Resource);
 	GDCLASS(MeshLibrary, Resource);
 	RES_BASE_EXTENSION("meshlib");
 	RES_BASE_EXTENSION("meshlib");
 
 
+public:
+	struct ShapeData {
+		Ref<Shape> shape;
+		Transform local_transform;
+	};
 	struct Item {
 	struct Item {
 		String name;
 		String name;
 		Ref<Mesh> mesh;
 		Ref<Mesh> mesh;
-		Ref<Shape> shape;
+		Vector<ShapeData> shapes;
 		Ref<Texture> preview;
 		Ref<Texture> preview;
 		Ref<NavigationMesh> navmesh;
 		Ref<NavigationMesh> navmesh;
 	};
 	};
 
 
 	Map<int, Item> item_map;
 	Map<int, Item> item_map;
 
 
+	void _set_item_shapes(int p_item, const Array &p_shapes);
+	Array _get_item_shapes(int p_item) const;
+
 protected:
 protected:
 	bool _set(const StringName &p_name, const Variant &p_value);
 	bool _set(const StringName &p_name, const Variant &p_value);
 	bool _get(const StringName &p_name, Variant &r_ret) const;
 	bool _get(const StringName &p_name, Variant &r_ret) const;
@@ -63,12 +71,12 @@ public:
 	void set_item_name(int p_item, const String &p_name);
 	void set_item_name(int p_item, const String &p_name);
 	void set_item_mesh(int p_item, const Ref<Mesh> &p_mesh);
 	void set_item_mesh(int p_item, const Ref<Mesh> &p_mesh);
 	void set_item_navmesh(int p_item, const Ref<NavigationMesh> &p_navmesh);
 	void set_item_navmesh(int p_item, const Ref<NavigationMesh> &p_navmesh);
-	void set_item_shape(int p_item, const Ref<Shape> &p_shape);
+	void set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes);
 	void set_item_preview(int p_item, const Ref<Texture> &p_preview);
 	void set_item_preview(int p_item, const Ref<Texture> &p_preview);
 	String get_item_name(int p_item) const;
 	String get_item_name(int p_item) const;
 	Ref<Mesh> get_item_mesh(int p_item) const;
 	Ref<Mesh> get_item_mesh(int p_item) const;
 	Ref<NavigationMesh> get_item_navmesh(int p_item) const;
 	Ref<NavigationMesh> get_item_navmesh(int p_item) const;
-	Ref<Shape> get_item_shape(int p_item) const;
+	Vector<ShapeData> get_item_shapes(int p_item) const;
 	Ref<Texture> get_item_preview(int p_item) const;
 	Ref<Texture> get_item_preview(int p_item) const;
 
 
 	void remove_item(int p_item);
 	void remove_item(int p_item);