| 238 | } |
| 239 | |
| 240 | void EditorInterface::make_scene_preview(const String &p_path, Node *p_scene, int p_preview_size) { |
| 241 | if (!Engine::get_singleton()->is_editor_hint() || !DisplayServer::get_singleton()->window_can_draw()) { |
| 242 | return; |
| 243 | } |
| 244 | ERR_FAIL_COND_MSG(p_path.is_empty(), "Path is empty, cannot generate preview."); |
| 245 | ERR_FAIL_NULL_MSG(p_scene, "The provided scene is null, cannot generate preview."); |
| 246 | ERR_FAIL_COND_MSG(p_scene->is_inside_tree(), "The scene must not be inside the tree."); |
| 247 | ERR_FAIL_NULL_MSG(EditorNode::get_singleton(), "EditorNode doesn't exist."); |
| 248 | |
| 249 | SubViewport *sub_viewport_node = memnew(SubViewport); |
| 250 | AABB scene_aabb; |
| 251 | scene_aabb = _calculate_aabb_for_scene(p_scene, scene_aabb); |
| 252 | |
| 253 | sub_viewport_node->set_update_mode(SubViewport::UPDATE_ALWAYS); |
| 254 | sub_viewport_node->set_size(Vector2i(p_preview_size, p_preview_size)); |
| 255 | sub_viewport_node->set_transparent_background(false); |
| 256 | Ref<World3D> world; |
| 257 | world.instantiate(); |
| 258 | sub_viewport_node->set_world_3d(world); |
| 259 | |
| 260 | EditorNode::get_singleton()->add_child(sub_viewport_node); |
| 261 | Ref<Environment> env; |
| 262 | env.instantiate(); |
| 263 | env->set_background(Environment::BG_CLEAR_COLOR); |
| 264 | |
| 265 | Ref<CameraAttributesPractical> camera_attributes; |
| 266 | camera_attributes.instantiate(); |
| 267 | |
| 268 | Node3D *root = memnew(Node3D); |
| 269 | root->set_name("Root"); |
| 270 | sub_viewport_node->add_child(root); |
| 271 | |
| 272 | Camera3D *camera = memnew(Camera3D); |
| 273 | camera->set_environment(env); |
| 274 | camera->set_attributes(camera_attributes); |
| 275 | camera->set_name("Camera3D"); |
| 276 | root->add_child(camera); |
| 277 | camera->set_current(true); |
| 278 | |
| 279 | camera->set_position(Vector3(0.0, 0.0, 3.0)); |
| 280 | |
| 281 | DirectionalLight3D *light = memnew(DirectionalLight3D); |
| 282 | light->set_name("Light"); |
| 283 | DirectionalLight3D *light2 = memnew(DirectionalLight3D); |
| 284 | light2->set_name("Light2"); |
| 285 | light2->set_color(Color(0.7, 0.7, 0.7, 1.0)); |
| 286 | |
| 287 | root->add_child(light); |
| 288 | root->add_child(light2); |
| 289 | |
| 290 | sub_viewport_node->add_child(p_scene); |
| 291 | |
| 292 | // Calculate the camera and lighting position based on the size of the scene. |
| 293 | Vector3 center = scene_aabb.get_center(); |
| 294 | float camera_size = scene_aabb.get_longest_axis_size(); |
| 295 | |
| 296 | const float cam_rot_x = -Math::PI / 4; |
| 297 | const float cam_rot_y = -Math::PI / 4; |
no test coverage detected