Repository: godotengine/godot
File: editor/plugins/node_3d_editor_plugin.cpp (C++)

Code before:
			mesh_instance->set_surface_override_material(closest_surface, spatial_editor->get_preview_material());
		}

		return true;
	}

	GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(target_inst);
	if (geometry_instance && spatial_editor->get_preview_material() != geometry_instance->get_material_override()) {
		spatial_editor->set_preview_reset_material(geometry_instance->get_material_override());
		geometry_instance->set_material_override(spatial_editor->get_preview_material());
		return true;
	}

	return false;
}

void Node3DEditorViewport::_reset_preview_material() const {
	ObjectID last_target = spatial_editor->get_preview_material_target();
	if (last_target.is_null()) {
		return;
	}
	Object *last_target_inst = ObjectDB::get_instance(last_target);

	MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(last_target_inst);
	GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(last_target_inst);
	if (spatial_editor->get_preview_material_surface() != -1 && mesh_instance) {
		mesh_instance->set_surface_override_material(spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_reset_material());
		spatial_editor->set_preview_material_surface(-1);
	} else if (geometry_instance) {
		geometry_instance->set_material_override(spatial_editor->get_preview_reset_material());
	}
}

void Node3DEditorViewport::_remove_preview_material() {
	preview_material_label->hide();
	preview_material_label_desc->hide();

	spatial_editor->set_preview_material(Ref<Material>());
	spatial_editor->set_preview_reset_material(Ref<Material>());
	spatial_editor->set_preview_material_target(ObjectID());
	spatial_editor->set_preview_material_surface(-1);
}

bool Node3DEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
	if (p_desired_node->get_scene_file_path() == p_target_scene_path) {
		return true;
	}

	int childCount = p_desired_node->get_child_count();
	for (int i = 0; i < childCount; i++) {
		Node *child = p_desired_node->get_child(i);

Review comment (suggestion):
```suggestion
	if (mesh_instance && spatial_editor->get_preview_material_surface() != -1) {
```
Minor optimization.

Code after:
		Vector3 xform_pos = ai.xform(world_pos);

		for (int surface = 0; surface < surface_count; surface++) {
			Ref<TriangleMesh> surface_mesh = mesh->generate_surface_triangle_mesh(surface);

			Vector3 rpos, rnorm;
			if (surface_mesh->intersect_ray(xform_pos, xform_ray, rpos, rnorm)) {
				Vector3 hitpos = gt.xform(rpos);

				const real_t dist = world_pos.distance_to(hitpos);

				if (dist < 0) {
					continue;
				}

				if (dist < closest_dist) {
					closest_surface = surface;
					closest_dist = dist;
				}
			}
		}

		if (closest_surface == -1) {
			return false;
		}

		if (spatial_editor->get_preview_material() != mesh_instance->get_surface_override_material(closest_surface)) {
			spatial_editor->set_preview_material_surface(closest_surface);
			spatial_editor->set_preview_reset_material(mesh_instance->get_surface_override_material(closest_surface));
			mesh_instance->set_surface_override_material(closest_surface, spatial_editor->get_preview_material());
		}

		return true;
	}

	GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(target_inst);
	if (geometry_instance && spatial_editor->get_preview_material() != geometry_instance->get_material_override()) {
		spatial_editor->set_preview_reset_material(geometry_instance->get_material_override());
		geometry_instance->set_material_override(spatial_editor->get_preview_material());
		return true;
	}

	return false;
}

void Node3DEditorViewport::_reset_preview_material() const {
	ObjectID last_target = spatial_editor->get_preview_material_target();
	if (last_target.is_null()) {
		return;
	}
	Object *last_target_inst = ObjectDB::get_instance(last_target);